SysLogController.java 9.7 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.shiro.authz.annotation.RequiresAuthentication;
liqin's avatar
liqin committed
23 24 25 26 27 28 29 30 31 32
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
33 34 35
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Map;
liqin's avatar
liqin committed
36 37 38

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

    @Resource
    private SysLogServiceImpl sysLogService;

45 46 47
    @Resource
    private RunLogServiceImpl runLogService;

wzp's avatar
wzp committed
48 49 50
    @Resource
    private TOperationLogService operationLogService;

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

    /**
     * 查询系统日志
     */
    @ApiOperation(value = "查询系统日志", notes = "查询系统日志")
wzp's avatar
wzp committed
88 89 90 91 92 93 94 95 96
    @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
97
    @GetMapping(value = "/querySysLogList")
wzp's avatar
wzp committed
98
    @RequiresAuthentication  //@RequiresPermissions("/sysLog/querySysLogList")
wzp's avatar
wzp committed
99
    public Map<String, Object> querySysLogList(GenericPageParam genericPageParam) {
liqin's avatar
liqin committed
100
        try {
wzp's avatar
wzp committed
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119
            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
120
            return getResult(page);
liqin's avatar
liqin committed
121 122 123
        } catch (Exception e) {
            logger.error("查询系统日志列表出错!", e);
        }
wzp's avatar
wzp committed
124
        return getFailResult();
liqin's avatar
liqin committed
125 126
    }

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

wzp's avatar
wzp committed
143 144 145 146 147 148 149
    /**
     * 插入运维日志表
     */
    @ApiOperation(value = "插入运维日志表", notes = "插入运维日志表")
    @PostMapping(value = "/insertOperationLog")
    public Boolean insertOperationLog(TOperationLog tOperationLog) {
        tOperationLog.setCreateTime(LocalDateTime.now());
wzp's avatar
wzp committed
150 151 152 153 154 155 156
        boolean b = false;
        try {
            b = operationLogService.save(tOperationLog);
            return b;
        } catch (Exception e) {
            e.printStackTrace();
        }
wzp's avatar
wzp committed
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
        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
172
    @RequiresAuthentication  //@RequiresPermissions("/sysLog/OperationLog")
wzp's avatar
wzp committed
173
    public Map<String, Object> OperationLog(TOperationLog operationLog) {
wzp's avatar
wzp committed
174 175
        try {
            Page<TOperationLog> page = operationLogService.pageList(getPage(), operationLog);
wzp's avatar
wzp committed
176
            return getResult(page);
wzp's avatar
wzp committed
177 178 179
        } catch (Exception e) {
            logger.error("查询系统日志列表出错!", e);
        }
wzp's avatar
wzp committed
180
        return getFailResult();
wzp's avatar
wzp committed
181 182 183 184 185 186 187 188 189 190 191 192 193 194
    }

    /**
     * 查询机顶盒日志
     */
    @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
195
    @RequiresAuthentication  //@RequiresPermissions("/sysLog/runLogList")
wzp's avatar
wzp committed
196
    public Map<String, Object> runLogList(RunLog runLog) {
wzp's avatar
wzp committed
197 198
        try {
            Page<RunLog> page = runLogService.pageList(getPage(), runLog);
wzp's avatar
wzp committed
199
            return getResult(page);
wzp's avatar
wzp committed
200 201 202
        } catch (Exception e) {
            logger.error("查询系统日志列表出错!", e);
        }
wzp's avatar
wzp committed
203
        return getFailResult();
wzp's avatar
wzp committed
204 205 206 207 208 209 210 211 212
    }


    /**
     * 返回日志操作类型
     * @param
     * @return
     */
    @PostMapping("/getOperationType")
wzp's avatar
wzp committed
213
    @RequiresAuthentication  //@RequiresPermissions("/sysLog/getOperationType")
wzp's avatar
wzp committed
214 215 216 217 218 219 220 221 222 223
    @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
224 225
}