ludc
2025-01-16 5203081b68e3a8dc139d1807b2f8774e4a00a82a
Source/plt-web/plt-web-parent/plt-web/src/main/java/com/vci/web/service/impl/LogBasicServiceImpl.java
@@ -1,4 +1,5 @@
package com.vci.web.service.impl;
import com.vci.client.common.objects.UserObject;
import com.vci.corba.common.PLException;
import com.vci.corba.common.data.UserEntityInfo;
@@ -9,18 +10,30 @@
import com.vci.dto.LogInfoDTO;
import com.vci.dto.LogPeriodInfoDTO;
import com.vci.dto.LogQueryCriteriaDTO;
import com.vci.web.service.SmUserQueryServiceI;
import com.vci.starter.poi.bo.WriteExcelData;
import com.vci.starter.poi.bo.WriteExcelOption;
import com.vci.starter.poi.util.ExcelUtil;
import com.vci.starter.web.exception.VciBaseException;
import com.vci.starter.web.pagemodel.BaseResult;
import com.vci.web.service.*;
import com.vci.web.util.*;
import com.vci.starter.web.util.LangBaseUtil;
import com.vci.starter.web.util.LocalFileUtil;
import com.vci.starter.web.util.VciBaseUtil;
import com.vci.web.service.LogBasicServiceI;
import com.vci.starter.web.util.Lcm.Func;
import com.vci.web.util.PlatformClientUtil;
import com.vci.web.util.WebUtil;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.io.File;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.stream.Collectors;
/**
@@ -41,6 +54,10 @@
     */
    @Autowired
    private PlatformClientUtil platformClientUtil;
    @Autowired
    private SmUserQueryServiceI userQueryServiceI;
    public static final String UTF8_BOM="\uFEFF";
    private final String LOG_SAVE_PERIOD = "logSavePeriod";//日志保存期限
@@ -131,8 +148,12 @@
     */
    @Override
    public BaseResult getLogListByContion(LogQueryCriteriaDTO queryDto) throws PLException {
        VciBaseUtil.alertNotNull(queryDto,"日志查询参数对象");
        List<String> userNameList = userQueryServiceI.queryUserNameByRoleType(queryDto.getRoleType());
        queryDto.setUserNameList(userNameList);
        String querySql = getSQL(queryDto);
        LogInfo[] logInfos = platformClientUtil.getLogService().getLogListByContion(queryDto.getPageNo(),queryDto.getPageSize(),querySql);
        List<LogInfoDTO> dtos = new ArrayList<>();
        for (LogInfo logInfo : logInfos) {
            LogInfoDTO dto = new LogInfoDTO();
@@ -160,6 +181,7 @@
        result.setTotal(sumLogRows);
        return result;
    }
    /**
     * 操作用户获取
     */
@@ -179,13 +201,73 @@
    }
    /**
     * 导出日志
     * @param dto 导出的文件名
     * @return
     */
    @Override
    public String exportLogs(LogQueryCriteriaDTO dto) throws PLException{
        VciBaseUtil.alertNotNull(dto,"日志查询参数对象");
        List<String> userNameList = userQueryServiceI.queryUserNameByRoleType(dto.getRoleType());
        dto.setUserNameList(userNameList);
        String querySql = getSQL(dto);
        //全查询
        if(dto.getPageSize() == -1){
            long sumLogRows = platformClientUtil.getLogService().getSumLogRows(querySql);
            dto.setPageSize((int) sumLogRows);
        }
        LogInfo[] logList = platformClientUtil.getLogService().getLogListByContion(dto.getPageNo(),dto.getPageSize(),querySql);
        //界面没传名称,使用默认导出名称
        String exportFileName = "日志导出_" + Func.format(new Date(),"yyyy-MM-dd HHmmss.sss");
        //设置列名
        List<String> columns = new ArrayList<>(
                Arrays.asList("用户名", "姓名", "用户ip","模块", "操作", "时间", "操作结果","描述")
        );
        //写excel
        String excelPath = LocalFileUtil.getDefaultTempFolder() + File.separator + exportFileName +  ".xls";
        try {
            new File(excelPath).createNewFile();
        } catch (Throwable e) {
            throw new VciBaseException(LangBaseUtil.getErrorMsg(e), new String[]{excelPath}, e);
        }
        //设置列
        List<WriteExcelData> excelDataList = new ArrayList<>();
        //设置列头
        for (int index = 0; index < columns.size(); index++) {
            excelDataList.add(new WriteExcelData(0,index, columns.get(index)));
        }
        if(Func.isEmpty(logList)){
            excelDataList.add(new WriteExcelData(1,1, "导出的日志列表为空!"));
        }else{
            AtomicInteger i = new AtomicInteger();
            Arrays.stream(logList).forEach(log->{
                excelDataList.add(new WriteExcelData(i.get() +1,0, log.username));
                excelDataList.add(new WriteExcelData(i.get() +1,1, log.truename));
                excelDataList.add(new WriteExcelData(i.get() +1,2, log.userIp));
                excelDataList.add(new WriteExcelData(i.get() +1,3, log.moduleName));
                excelDataList.add(new WriteExcelData(i.get() +1,4, log.type));
                excelDataList.add(new WriteExcelData(i.get() +1,5, log.date));
                excelDataList.add(new WriteExcelData(i.get() +1,6, log.logType));
                excelDataList.add(new WriteExcelData(i.get() +1,7, log.result));
                i.getAndIncrement();
            });
        }
        WriteExcelOption excelOption = new WriteExcelOption(excelDataList);
        ExcelUtil.writeDataToFile(excelPath, excelOption);
        return excelPath;
    }
    /**
     * 获取用户信息
     * @param userNames 用户名称
     * @return 用户信息
     * @throws PLException
     */
    private List<UserObject> getUsersByUserNames(List<String> userNames) throws PLException {
        List<UserObject> userList = new ArrayList<UserObject>();
        List<UserObject> userList = new ArrayList<>();
        for(String userName : userNames){
            UserInfo userInfo = platformClientUtil.getFrameworkService().fetchUserInfoByName(userName);
            UserObject user = new UserObject();
@@ -233,14 +315,15 @@
        }
        return userNames;
    }
    /**
     * 获取查询条件并拼成SQL,只拼where子句后面的SQL
     * @return
     */
    public String getSQL(LogQueryCriteriaDTO dto) throws PLException {
        StringBuffer sql = new StringBuffer("");
//        int period = getPeriod(LOG_SAVE_PERIOD);//获取保存期限,以月为单位
        int period = platformClientUtil.getLogService().getCurPeriod();;//获取保存期限,以月为单位
        // int period = getPeriod(LOG_SAVE_PERIOD);//获取保存期限,以月为单位
        int period = platformClientUtil.getLogService().getCurPeriod();//获取保存期限,以月为单位
        //下面是拼出SQL
        if(period != 0){
            sql.append(" to_date(PLDATE) >= add_months(to_date(sysdate),"+(-period)+")");
@@ -274,6 +357,21 @@
            }
            sql.append("PLLOGTYPE in('" + getLogTypeString(LogType.General) + "')");
        }
        if(Func.isNotEmpty(dto.getUserNameList())){
            if(!sql.toString().equals("")){
                sql.append(" and ");
            }
            if(dto.getUserNameList().size()<=1){
                sql.append(" PLUSER = '").append(dto.getUserNameList().get(0).trim()).append("'");
            }else{
                sql.append(" PLUSER in (");
                String inWhere = dto.getUserNameList().stream()
                        .map(item -> "'" + item + "'")
                        .collect(Collectors.joining(","));
                sql.append(inWhere).append(")");
            }
        }
        if(StringUtils.isNotBlank(dto.getUserName())){//操作用户
            if(!dto.getUserName().trim().equals("")){//如果用户名为空格则不加用户条件
                if(!sql.toString().equals("")){
@@ -320,6 +418,7 @@
        }
        return sql.toString();
    }
    protected String getLogTypeString(LogType logType){
        String res = "";
        if(logType == LogType.Login) {
@@ -387,4 +486,6 @@
        res = true;
        return res;
    }
}