SysLogController.java 10.3 KB
Newer Older
liqin's avatar
liqin committed
1
package cn.chnmuseum.party.web.controller;
liqin's avatar
liqin committed
2

liqin's avatar
liqin committed
3 4 5 6 7 8 9 10 11 12 13 14
import cn.chnmuseum.party.common.log.OperType;
import cn.chnmuseum.party.common.util.DateUtil80;
import cn.chnmuseum.party.common.util.NetWorkUtil;
import cn.chnmuseum.party.common.vo.GenericPageParam;
import cn.chnmuseum.party.model.RunLog;
import cn.chnmuseum.party.model.SysLog;
import cn.chnmuseum.party.model.TOperationLog;
import cn.chnmuseum.party.model.TUser;
import cn.chnmuseum.party.service.TOperationLogService;
import cn.chnmuseum.party.service.impl.RunLogServiceImpl;
import cn.chnmuseum.party.service.impl.SysLogServiceImpl;
import cn.chnmuseum.party.web.controller.base.BaseController;
wzp's avatar
wzp committed
15
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
liqin's avatar
liqin committed
16
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
wzp's avatar
wzp committed
17
import io.swagger.annotations.Api;
liqin's avatar
liqin committed
18 19 20
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
wzp's avatar
wzp committed
21
import org.apache.commons.lang3.StringUtils;
wzp's avatar
wzp committed
22
import org.apache.pdfbox.pdmodel.interactive.digitalsignature.PDSeedValueMDP;
wzp's avatar
wzp committed
23
import org.apache.shiro.authz.annotation.RequiresAuthentication;
liqin's avatar
liqin committed
24 25 26 27 28 29 30 31 32 33
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
wzp's avatar
wzp committed
34
import java.time.LocalDate;
wzp's avatar
wzp committed
35 36 37
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Map;
liqin's avatar
liqin committed
38 39 40

@RestController
@RequestMapping("/sysLog")
wzp's avatar
wzp committed
41
@Api(tags = {"日志管理接口"})
liqin's avatar
liqin committed
42 43 44 45 46
public class SysLogController extends BaseController {

    @Resource
    private SysLogServiceImpl sysLogService;

47 48 49
    @Resource
    private RunLogServiceImpl runLogService;

wzp's avatar
wzp committed
50 51 52
    @Resource
    private TOperationLogService operationLogService;

liqin's avatar
liqin committed
53 54 55 56 57
    /**
     * 插入系统日志表
     */
    @ApiOperation(value = "插入系统日志", notes = "插入系统日志")
    @PostMapping(value = "/insertSysLog")
wzp's avatar
wzp committed
58
    public Boolean insertSysLog(String operationContent, TUser user) {
liqin's avatar
liqin committed
59 60
        SysLog sysLog = new SysLog();
        //日志时间
liqin's avatar
liqin committed
61
        sysLog.setOperationTime(DateUtil80.getDateTimeOfTimestamp(System.currentTimeMillis()));
liqin's avatar
liqin committed
62 63 64 65 66
        //获取登录IP并插入
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributes.getRequest();
        String operationIp = NetWorkUtil.getLoggableAddress(request);
        sysLog.setOperationIp(operationIp);
wzp's avatar
wzp committed
67
        if (user.getUserName() == null) {
liqin's avatar
liqin committed
68 69 70
            //操作者用户名
            sysLog.setOperator(this.getUserName());
        } else {
wzp's avatar
wzp committed
71
            sysLog.setOperator(user.getUserName());
liqin's avatar
liqin committed
72
        }
wzp's avatar
wzp committed
73
        if (user.getRoleList().contains("1")) {
74
            sysLog.setType(1);
wzp's avatar
wzp committed
75
        } else {
76 77
            sysLog.setType(2);
        }
liqin's avatar
liqin committed
78 79
        //日志内容
        sysLog.setOperationContent(operationContent);
80 81
        sysLog.setOperationObject("登录管理");
        sysLog.setOperationType("登录");
liqin's avatar
liqin committed
82 83 84 85 86 87 88 89
        Boolean ret = this.sysLogService.save(sysLog);
        return ret;
    }

    /**
     * 查询系统日志
     */
    @ApiOperation(value = "查询系统日志", notes = "查询系统日志")
wzp's avatar
wzp committed
90 91 92 93 94 95 96 97 98
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "_index", value = "分页起始偏移量", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "_size", value = "返回条数", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "nameOrCode", value = "名称或编码", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "startDate", value = "创建时间-开始", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "endDate", value = "创建时间-结束", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "type", value = "日志类型", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "operationType", value = "操作类型", paramType = "query", dataType = "String")
    })
liqin's avatar
liqin committed
99
    @GetMapping(value = "/querySysLogList")
wzp's avatar
wzp committed
100
    @RequiresAuthentication  //@RequiresPermissions("/sysLog/querySysLogList")
wzp's avatar
wzp committed
101
    public Map<String, Object> querySysLogList(GenericPageParam genericPageParam) {
liqin's avatar
liqin committed
102
        try {
wzp's avatar
wzp committed
103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
            LambdaQueryWrapper<SysLog> queryWrapper = new LambdaQueryWrapper<>();
            // 对名称或编码模糊查询
            if (StringUtils.isNotBlank(genericPageParam.getNameOrCode())) {
                queryWrapper.like(SysLog::getOperator, genericPageParam.getNameOrCode());
            }
            if (StringUtils.isNotBlank(genericPageParam.getType())) {
                queryWrapper.eq(SysLog::getType, genericPageParam.getType());
            }
            if (StringUtils.isNotBlank(genericPageParam.getOperationType())) {
                queryWrapper.eq(SysLog::getOperationType,genericPageParam.getOperationType());
            }
            // 根据创建时间区间检索
            if (genericPageParam.getStartDate() != null && genericPageParam.getEndDate() != null) {
                queryWrapper.ge(SysLog::getOperationTime, genericPageParam.getStartDate().atTime(0, 0, 0))
                        .le(SysLog::getOperationTime, genericPageParam.getEndDate().atTime(23, 59, 59));
            }
            // 设置排序规则
            queryWrapper.orderByDesc(SysLog::getOperationTime);
            Page<SysLog> page = sysLogService.page(getPage(), queryWrapper);
wzp's avatar
wzp committed
122
            return getResult(page);
liqin's avatar
liqin committed
123 124 125
        } catch (Exception e) {
            logger.error("查询系统日志列表出错!", e);
        }
wzp's avatar
wzp committed
126
        return getFailResult();
liqin's avatar
liqin committed
127 128
    }

129 130 131
    /**
     * 插入机顶盒日志表
     */
wzp's avatar
wzp committed
132
    @ApiOperation(value = "插入机顶盒日志表", notes = "插入机顶盒日志表")
133 134
    @PostMapping(value = "/insertRunLog")
    public Boolean insertRunLog(RunLog runLog) {
wzp's avatar
wzp committed
135 136 137 138 139 140 141
        boolean b = false;
        try {
            b = runLogService.insertRunLog(runLog);
            return b;
        } catch (Exception e) {
            e.printStackTrace();
        }
142 143 144
        return b;
    }

wzp's avatar
wzp committed
145 146 147 148 149 150 151
    /**
     * 插入运维日志表
     */
    @ApiOperation(value = "插入运维日志表", notes = "插入运维日志表")
    @PostMapping(value = "/insertOperationLog")
    public Boolean insertOperationLog(TOperationLog tOperationLog) {
        tOperationLog.setCreateTime(LocalDateTime.now());
wzp's avatar
wzp committed
152 153 154 155 156 157 158
        boolean b = false;
        try {
            b = operationLogService.save(tOperationLog);
            return b;
        } catch (Exception e) {
            e.printStackTrace();
        }
wzp's avatar
wzp committed
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
        return b;
    }

    /**
     * 查询运维日志
     */
    @ApiOperation(value = "查询运维日志", notes = "查询运维日志")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "_index", value = "分页起始偏移量", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "_size", value = "返回条数", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "userName", value = "运维账号", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "startDate", value = "创建时间-开始", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "endDate", value = "创建时间-结束", paramType = "query", dataType = "String")
    })
    @GetMapping(value = "/OperationLog")
wzp's avatar
wzp committed
174
    @RequiresAuthentication  //@RequiresPermissions("/sysLog/OperationLog")
wzp's avatar
wzp committed
175
    public Map<String, Object> OperationLog(TOperationLog operationLog) {
wzp's avatar
wzp committed
176 177 178 179
        if (null!=operationLog.getStartDate()) {
            operationLog.setStartDate(operationLog.getStartDate().toLocalDate().atTime(0, 0, 0));
            operationLog.setEndDate(operationLog.getEndDate().toLocalDate().atTime(23, 59, 59));
        }
wzp's avatar
wzp committed
180 181
        try {
            Page<TOperationLog> page = operationLogService.pageList(getPage(), operationLog);
wzp's avatar
wzp committed
182
            return getResult(page);
wzp's avatar
wzp committed
183 184 185
        } catch (Exception e) {
            logger.error("查询系统日志列表出错!", e);
        }
wzp's avatar
wzp committed
186
        return getFailResult();
wzp's avatar
wzp committed
187 188 189 190 191 192 193 194 195 196 197 198 199
    }
    /**
     * 查询机顶盒日志
     */
    @ApiOperation(value = "查询机顶盒日志", notes = "查询机顶盒日志")
    @ApiImplicitParams(value = {
            @ApiImplicitParam(name = "_index", value = "分页起始偏移量", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "_size", value = "返回条数", paramType = "query", dataType = "Integer"),
            @ApiImplicitParam(name = "nameOrCode", value = "名称或编码", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "startDate", value = "创建时间-开始", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "endDate", value = "创建时间-结束", paramType = "query", dataType = "String")
    })
    @GetMapping(value = "/runLogList")
wzp's avatar
wzp committed
200
    @RequiresAuthentication  //@RequiresPermissions("/sysLog/runLogList")
wzp's avatar
wzp committed
201
    public Map<String, Object> runLogList(RunLog runLog) {
wzp's avatar
wzp committed
202 203 204 205
        if (null!=runLog.getStartDate()) {
            runLog.setStartDate(runLog.getStartDate().toLocalDate().atTime(0, 0, 0));
            runLog.setEndDate(runLog.getEndDate().toLocalDate().atTime(23, 59, 59));
        }
wzp's avatar
wzp committed
206 207
        try {
            Page<RunLog> page = runLogService.pageList(getPage(), runLog);
wzp's avatar
wzp committed
208
            return getResult(page);
wzp's avatar
wzp committed
209 210 211
        } catch (Exception e) {
            logger.error("查询系统日志列表出错!", e);
        }
wzp's avatar
wzp committed
212
        return getFailResult();
wzp's avatar
wzp committed
213 214 215 216 217 218 219 220 221
    }


    /**
     * 返回日志操作类型
     * @param
     * @return
     */
    @PostMapping("/getOperationType")
wzp's avatar
wzp committed
222
    @RequiresAuthentication  //@RequiresPermissions("/sysLog/getOperationType")
wzp's avatar
wzp committed
223 224 225 226 227 228 229 230 231 232
    @ApiOperation(value = "返回日志操作类型", notes = "返回日志操作类型")
    public Map<String, Object> getTInteractionPageList() {
        OperType[] values = OperType.values();
        ArrayList<String> list = new ArrayList<>();
        for (OperType value : values) {
            list.add(value.getMsg());
        }
        return getResult(list);
    }

liqin's avatar
liqin committed
233 234
}