wl
2022-10-17 66a6b31ddc84644dfcae8a341a99796db76e60a7
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
package com.ycl.controller.user;
 
import com.alibaba.excel.EasyExcel;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ycl.annotation.LogSave;
import com.ycl.api.CommonResult;
import com.ycl.dto.log.LogQueryParams;
import com.ycl.entity.user.UmsAdmin;
import com.ycl.entity.user.UmsAdminLoginLog;
import com.ycl.entity.user.vo.UmsAdminLogVO;
import com.ycl.service.user.IAdminLoginLogService;
import com.ycl.service.user.UmsAdminService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.SneakyThrows;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import javax.servlet.http.HttpServletResponse;
import java.net.URLEncoder;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;
 
@RestController
@RequestMapping("/log_manage")
@Api(tags = "日志管理")
public class UmsAdminLogController {
 
    @Autowired
    IAdminLoginLogService iAdminLoginLogService;
    @Autowired
    UmsAdminService umsAdminService;
 
    @GetMapping("/query")
    @ApiOperation(value = "查询")
    @LogSave(operationType = "日志管理", contain = "查询日志信息")
    public CommonResult searchLog(LogQueryParams logQueryParams) {
        Page<UmsAdminLoginLog> umsAdminLoginLogPage = new Page<>();
        umsAdminLoginLogPage.setCurrent(logQueryParams.getCurrent());
        umsAdminLoginLogPage.setSize(logQueryParams.getSize());
        Page<UmsAdminLoginLog> page = iAdminLoginLogService
                .page(umsAdminLoginLogPage, new LambdaQueryWrapper<UmsAdminLoginLog>()
                        .like(StringUtils.isNotBlank(logQueryParams.getContent()), UmsAdminLoginLog::getContain, logQueryParams.getContent())
                        .eq(StringUtils.isNotBlank(logQueryParams.getOperationType()), UmsAdminLoginLog::getOperationType, logQueryParams.getOperationType())
                        .eq(StringUtils.isNotBlank(logQueryParams.getPortEquipment()), UmsAdminLoginLog::getUserAgent, logQueryParams.getPortEquipment())
                        .eq(logQueryParams.getId() != null, UmsAdminLoginLog::getAdminId, logQueryParams.getId())
                        .between(logQueryParams.getStartTime() != null && logQueryParams.getEndTime() != null, UmsAdminLoginLog::getCreateTime, logQueryParams.getStartTime(), logQueryParams.getEndTime())
                        .orderBy(logQueryParams.getSort() != null && logQueryParams.getSort() == 0, true, UmsAdminLoginLog::getCreateTime)
                        .orderBy(logQueryParams.getSort() != null && logQueryParams.getSort() == 1, false, UmsAdminLoginLog::getCreateTime));
        List<UmsAdminLogVO> umsAdminLogVOList = page.getRecords().stream()
                .map(item -> {
                    UmsAdminLogVO umsAdminlogVO = new UmsAdminLogVO();
                    BeanUtils.copyProperties(item, umsAdminlogVO);
                    umsAdminlogVO.setAdminId(umsAdminService
                            .getOne(new LambdaQueryWrapper<UmsAdmin>()
                                    .eq(UmsAdmin::getId, item.getAdminId()))
                            .getUsername());
                    return umsAdminlogVO;
                }).collect(Collectors.toList());
        Page<UmsAdminLogVO> umsAdminLogVOPage = new Page<>();
        BeanUtils.copyProperties(page, umsAdminLogVOPage);
        umsAdminLogVOPage.setRecords(umsAdminLogVOList);
        return CommonResult.success(umsAdminLogVOPage);
    }
 
    @DeleteMapping("/deletion_batch")
    @ApiOperation(value = "批量删除日志")
    @LogSave(operationType = "日志管理", contain = "批量删除日志信息")
    public CommonResult deleteBatch(@RequestParam List<Long> ids) {
        return CommonResult.success(iAdminLoginLogService.removeBatchByIds(ids));
    }
 
    @DeleteMapping("/clear")
    @ApiOperation(value = "清空日志")
    @LogSave(operationType = "日志管理", contain = "清空日志信息")
    public CommonResult clear() {
        return CommonResult.success(iAdminLoginLogService.remove(new LambdaQueryWrapper<UmsAdminLoginLog>()));
    }
 
    @GetMapping("/query_operationType")
    @ApiOperation(value = "查询操作类型")
    public CommonResult queryOperationType() {
        return CommonResult.success(iAdminLoginLogService.list(new LambdaQueryWrapper<UmsAdminLoginLog>().groupBy(UmsAdminLoginLog::getOperationType)));
    }
 
    @PostMapping("/export")
    @ApiOperation(value = "导出")
    @SneakyThrows
    @LogSave(operationType = "日志管理", contain = "导出日志")
    public void export(HttpServletResponse response, LogQueryParams logQueryParams) {
        Page<UmsAdminLoginLog> umsAdminLoginLogPage = new Page<>();
        umsAdminLoginLogPage.setCurrent(logQueryParams.getCurrent());
        umsAdminLoginLogPage.setSize(logQueryParams.getSize());
        StringBuilder fileName = new StringBuilder();
        fileName.append(LocalDateTime.now().getYear())
                .append("-")
                .append(LocalDateTime.now().getMonthValue())
                .append("-")
                .append(LocalDateTime.now().getDayOfMonth())
                .append("日志数据");
        response.setContentType("application/vnd.ms-excel");
        response.setCharacterEncoding("utf-8");
        String file = URLEncoder.encode(fileName.toString(), "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename=" + file + ".xlsx");
        EasyExcel
                .write(response.getOutputStream(), UmsAdminLogVO.class)
                .sheet("数据")
                .doWrite(iAdminLoginLogService
                        .page(umsAdminLoginLogPage, new LambdaQueryWrapper<UmsAdminLoginLog>()
                                .like(StringUtils.isNotBlank(logQueryParams.getContent()), UmsAdminLoginLog::getContain, logQueryParams.getContent())
                                .eq(StringUtils.isNotBlank(logQueryParams.getOperationType()), UmsAdminLoginLog::getOperationType, logQueryParams.getOperationType())
                                .between(logQueryParams.getStartTime() != null && logQueryParams.getEndTime() != null, UmsAdminLoginLog::getCreateTime, logQueryParams.getStartTime(), logQueryParams.getEndTime())
                                .orderBy(logQueryParams.getSort() != null && logQueryParams.getSort() == 0, true, UmsAdminLoginLog::getCreateTime)
                                .orderBy(logQueryParams.getSort() != null && logQueryParams.getSort() == 1, false, UmsAdminLoginLog::getCreateTime))
                        .getRecords()
                        .stream()
                        .map(item -> {
                            UmsAdminLogVO umsAdminlogVO = new UmsAdminLogVO();
                            BeanUtils.copyProperties(item, umsAdminlogVO);
                            umsAdminlogVO.setAdminId(umsAdminService
                                    .getOne(new LambdaQueryWrapper<UmsAdmin>()
                                            .eq(UmsAdmin::getId, item.getAdminId()))
                                    .getUsername());
                            return umsAdminlogVO;
                        }).collect(Collectors.toList()));
    }
}