OperationLogService.java
5.29 KB
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
package cn.com.glsx.stoassistant.service;
import cn.com.glsx.framework.core.beans.rpc.RpcPagination;
import cn.com.glsx.framework.core.beans.rpc.RpcResponse;
import cn.com.glsx.framework.core.exception.RpcServiceException;
import cn.com.glsx.scrm.equity.dto.card.EquityCardListExcelVO;
import cn.com.glsx.scrm.system.dto.SystemOperationLogDTO;
import cn.com.glsx.scrm.system.model.SystemOperationLog;
import cn.com.glsx.scrm.system.model.wechatapplets.LoginUserInfoResponse;
import cn.com.glsx.scrm.system.remote.SystemLoginLogRemote;
import cn.com.glsx.stoassistant.request.OperationLogAddRequest;
import cn.com.glsx.stoassistant.utils.EasyExcelUtil;
import cn.com.glsx.stoassistant.utils.IpUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.Environment;
import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.stream.Collectors;
/**
* @author huangch
* @version V1.0
* @Title: OperationLogService
* @Description:
* @Company: Didihu.com.cn
* @Date 2023/5/10 15:45
*/
@Service
public class OperationLogService {
@Autowired
private SystemLoginLogRemote systemLoginLogRemote;
@Autowired
private Environment env;
public boolean addOperationLog(OperationLogAddRequest request, LoginUserInfoResponse loginUser){
HttpServletRequest httpServletRequest = (HttpServletRequest)request.getRopRequestContext().getRawRequestObject();
String ip = IpUtils.getIpAddr(httpServletRequest);
SystemOperationLog systemOperationLog = new SystemOperationLog();
systemOperationLog.setLoginIp(ip);
systemOperationLog.setMerchantId(loginUser.getMerchantId());
systemOperationLog.setMerchantName(loginUser.getMerchantName());
systemOperationLog.setOperateModule(request.getOperateModule());
systemOperationLog.setOperateMethod(request.getOperateMethod());
systemOperationLog.setLoginName(loginUser.getPhone());
systemOperationLog.setUserType(loginUser.getUserType());
systemOperationLog.setCreatedDate(request.getOperateTime());
systemLoginLogRemote.insertOperationLog(systemOperationLog);
return true;
}
/**
* 导出门店助手使用情况excel
* @param :
* @return java.lang.String
* @author huangch
* @date 2023/5/10 18:28
*/
public String exportModuleReport(){
//获取配置文件中需要统计的模块
String property = env.getProperty("stoassistant.operation.report.modules", String.class, "");
List<String> modules = Arrays.asList(property.split(","));
List<String> moduleList = modules.stream().map(s -> s.split("-")[0]).collect(Collectors.toList());
List<String> methodList = modules.stream().map(s -> s.split("-")[1]).collect(Collectors.toList());
//查询这些模块的所有数据
RpcPagination<SystemOperationLogDTO> pagination = new RpcPagination<>();
pagination.setPageNum(1);
pagination.setPageSize(100);
SystemOperationLogDTO condition = new SystemOperationLogDTO();
condition.setModuleList(moduleList);
condition.setMethodList(methodList);
pagination.setCondition(condition);
List<SystemOperationLogDTO> operationList = new ArrayList<>();
//查询数据
while(true){
RpcResponse<RpcPagination<SystemOperationLogDTO>> rpcResponse = systemLoginLogRemote.queryOperationLogList(pagination);
if(rpcResponse.isFailed()){
throw new RpcServiceException(rpcResponse.getMessage());
}
RpcPagination<SystemOperationLogDTO> page = rpcResponse.getResult();
operationList.addAll(page.getList());
//判断是是否最后一页
if(page.getList().size() < pagination.getPageSize()){
break;
}
pagination.setPageNum(pagination.getPageNum()+1);
}
//计算
Map<String, Map<String, Long>> resultMap = new HashMap<>();
Map<String, String> merchantNameMap = new HashMap<>();
operationList.forEach(operationLog -> {
String key = operationLog.getMerchantId()+"";
Map<String, Long> userMap = resultMap.getOrDefault(key, new HashMap<>());
Long count = userMap.getOrDefault(operationLog.getOperateMethod(), 0L);
count ++;
userMap.put(operationLog.getOperateMethod(), count);
resultMap.put(key, userMap);
merchantNameMap.put(key, operationLog.getMerchantName());
});
List<Map<String, Object>> excelList = new ArrayList<>();
for(Map.Entry<String, Map<String, Long>> entry: resultMap.entrySet()){
String key = entry.getKey();
Map<String, Long> userMap = entry.getValue();
Map<String, Object> excelMap = new HashMap<>();
excelMap.put("商户号", key);
excelMap.put("商户名称", merchantNameMap.get(key));
for(String method : methodList){
excelMap.put(method, userMap.getOrDefault(method, 0L));
}
excelList.add(excelMap);
}
methodList.add(0, "商户号");
methodList.add(1, "商户名称");
return EasyExcelUtil.exportExcel(excelList, methodList, "门店助手使用情况");
}
}