OperationLogService.java 5.29 KB
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, "门店助手使用情况");
    }

}