lyh
2025-01-13 137d008bd9b7d932160436a3a560b24512f6d1db
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
package org.jeecg.modules.dnc.service.impl;
 
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.common.util.IpUtils;
import org.jeecg.common.util.SpringContextUtils;
import org.jeecg.modules.dnc.entity.NcLogInfo;
import org.jeecg.modules.dnc.exception.ExceptionCast;
import org.jeecg.modules.dnc.mapper.NcLogInfoMapper;
import org.jeecg.modules.dnc.response.CommonCode;
import org.jeecg.modules.dnc.response.QueryPageResponseResult;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysDictService;
import org.jeecg.modules.system.service.ISysUserService;
import org.jeecgframework.poi.excel.def.NormalExcelConstants;
import org.jeecgframework.poi.excel.entity.ExportParams;
import org.jeecgframework.poi.excel.view.JeecgEntityExcelView;
import org.jeecg.modules.dnc.request.NcLogInfoRequest;
import org.jeecg.modules.dnc.service.INcLogInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.servlet.ModelAndView;
 
import javax.servlet.http.HttpServletRequest;
import java.util.List;
 
/**
 * @author clown
 * * @date 2023/12/4
 */
@Service
public class NcLogInfoServiceImpl extends ServiceImpl<NcLogInfoMapper, NcLogInfo> implements INcLogInfoService {
 
    @Autowired
    private ISysUserService userService;
 
    @Autowired
    private ISysDictService iSysDictService;
 
    @Override
    public QueryPageResponseResult<NcLogInfo> findByPageList(int page, int size, NcLogInfoRequest requestParams) {
        if(page < 1 || size < 1) {
            ExceptionCast.cast(CommonCode.INVALID_PAGE);
        }
        IPage<NcLogInfo> pageData = new Page<>(page, size);
        QueryWrapper<NcLogInfo> queryWrapper=new QueryWrapper<>();
        if(requestParams != null) {
            queryWrapper.eq(StrUtil.isNotEmpty(requestParams.getUsername()),"user_mame",requestParams.getOperateType());
            if(requestParams.getOperateType() != null) {
                queryWrapper.eq("operate_type", requestParams.getOperateType());
            }
            queryWrapper.like(StrUtil.isNotEmpty(requestParams.getLogContent()),"log_content",requestParams.getLogContent());
            queryWrapper.ge(StrUtil.isNotEmpty(requestParams.getStartTime()),"create_time",requestParams.getStartTime());
            queryWrapper.le(StrUtil.isNotEmpty(requestParams.getEndTime()),"create_time",requestParams.getEndTime());
        }
        queryWrapper.orderByDesc("create_time");
        IPage<NcLogInfo> userIPage = this.page(pageData, queryWrapper);
        /*if (userIPage != null && userIPage.getRecords() != null && !userIPage.getRecords().isEmpty()) {
            for (NcLogInfo nc : userIPage.getRecords()) {
                User user = userService.getById(nc.getCreateUser());
                if (user != null) {
                    nc.setNickname(user.getNickname());
                    nc.setUsername(user.getUsername());
                }
            }
        }*/
        return new QueryPageResponseResult<>(CommonCode.SUCCESS, userIPage);
    }
 
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean saveLogNcInfos(NcLogInfo logInfo) {
        HttpServletRequest request = SpringContextUtils.getHttpServletRequest();
        //设置IP地址
        logInfo.setIp(IpUtils.getIpAddr(request));
        super.save(logInfo);
        SysUser user = userService.getById(logInfo.getCreateUser());
        if (user != null) {
            logInfo.setNickName(user.getRealname());
            logInfo.setUserName(user.getUsername());
        }
        super.saveOrUpdate(logInfo);
        return true;
    }
 
    @Override
    @Transactional(rollbackFor = {Exception.class})
    public boolean saveLogInfo(String moduleInfo, Integer operateType, String logContent) {
        HttpServletRequest request = SpringContextUtils.getHttpServletRequest();
        NcLogInfo logInfo = new NcLogInfo();
        logInfo.setModuleInfo(moduleInfo);
        logInfo.setOperateType(operateType);
        logInfo.setLogContent(logContent);
        //设置IP地址
        logInfo.setIp(IpUtils.getIpAddr(request));
        super.save(logInfo);
        SysUser user = userService.getById(logInfo.getCreateUser());
        if (user != null) {
            logInfo.setNickName(user.getRealname());
            logInfo.setUserName(user.getUsername());
        }
        super.saveOrUpdate(logInfo);
        return true;
    }
 
    /**
     * 导出查询List
     * @param requestParams
     * @return
     */
    @Override
    @Transactional
    public ModelAndView exportLogList(NcLogInfoRequest requestParams){
        QueryWrapper<NcLogInfo> queryWrapper=new QueryWrapper<>();
        if(requestParams != null) {
            queryWrapper.eq(StrUtil.isNotEmpty(requestParams.getUsername()),"user_mame",requestParams.getOperateType());
            if(requestParams.getOperateType() != null) {
                queryWrapper.eq("operate_type", requestParams.getOperateType());
            }
            queryWrapper.like(StrUtil.isNotEmpty(requestParams.getLogContent()),"log_content",requestParams.getLogContent());
            queryWrapper.ge(StrUtil.isNotEmpty(requestParams.getStartTime()),"create_time",requestParams.getStartTime());
            queryWrapper.le(StrUtil.isNotEmpty(requestParams.getEndTime()),"create_time",requestParams.getEndTime());
        }
        queryWrapper.orderByDesc("create_time");
        List<NcLogInfo> ncLogInfoList=this.list(queryWrapper);
        ncLogInfoList.forEach(item->{
            //翻译用户名、操作类型
            item.setOperateTypeStr(iSysDictService.queryDictTextByKey("OPERATE_TYPE",item.getOperateType().toString()));
            item.setCreateUser(userService.getById(item.getCreateUser()).getRealname());
        });
        LoginUser user = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String userId = user.getId();
        ModelAndView mv = new ModelAndView(new JeecgEntityExcelView());
        mv.addObject(NormalExcelConstants.FILE_NAME, "DNC日志管理");
        mv.addObject(NormalExcelConstants.CLASS, NcLogInfo.class);
        ExportParams exportParams = new ExportParams("DNC日志管理" + "报表", "导出人:" + userService.getById(userId).getRealname(), "DNC日志管理");
        mv.addObject(NormalExcelConstants.PARAMS, exportParams);
        mv.addObject(NormalExcelConstants.DATA_LIST, ncLogInfoList);
        return mv;
    }
 
}