package com.engine.salary.util;


import com.engine.hrmelog.entity.dto.LoggerContext;
import com.engine.hrmelog.util.LoggerTemplate;
import com.engine.salary.enums.OperateTypeEnum;
import weaver.hrm.User;

/**
 * 操作日志工具类
 * <p>Copyright: Copyright (c) 2023</p>
 * <p>Company: 泛微软件</p>
 *
 * @author qiantao
 * @version 1.0
 **/
public class SalaryLoggerUtil {

    /**
     * 记录单个对象新增操作日志
     * @param loggerTemplate
     * @param targetId
     * @param targetName
     * @param operateTypeName
     * @param operatedesc
     * @param newValues
     * @param user
     */
    public static void recordAddSingleLog(LoggerTemplate loggerTemplate, Long targetId, String targetName, String operateTypeName, String operatedesc, Object newValues, User user) {
        recoreSingleLog(loggerTemplate, targetId, targetName, OperateTypeEnum.ADD.getValue(), operateTypeName, operatedesc, null, newValues, user);
    }

    /**
     * 记录单个对象修改操作日志
     * @param loggerTemplate
     * @param targetId
     * @param targetName
     * @param operateTypeName
     * @param operatedesc
     * @param oldValues
     * @param newValues
     * @param user
     */
    public static void recordUpdateSingleLog(LoggerTemplate loggerTemplate, Long targetId, String targetName, String operateTypeName, String operatedesc, Object oldValues, Object newValues, User user) {
        recoreSingleLog(loggerTemplate, targetId, targetName, OperateTypeEnum.UPDATE.getValue(), operateTypeName, operatedesc, oldValues, newValues, user);
    }

    /**
     * 记录单个对象删除操作日志
     * @param loggerTemplate
     * @param targetId
     * @param targetName
     * @param operateTypeName
     * @param operatedesc
     * @param oldValues
     * @param user
     */
    public static void recordDeleteSingleLog(LoggerTemplate loggerTemplate, Long targetId, String targetName, String operateTypeName, String operatedesc, Object oldValues, User user) {
        recoreSingleLog(loggerTemplate, targetId, targetName, OperateTypeEnum.DELETE.getValue(), operateTypeName, operatedesc, oldValues, null, user);
    }

    /**
     * 记录单个对象导出操作日志
     * @param loggerTemplate
     * @param targetId
     * @param targetName
     * @param operateTypeName
     * @param operatedesc
     * @param user
     */
    public static void recordExportSingleLog(LoggerTemplate loggerTemplate, Long targetId, String targetName, String operateTypeName, String operatedesc, User user) {
        recoreSingleLog(loggerTemplate, targetId, targetName, OperateTypeEnum.EXCEL_EXPORT.getValue(), operateTypeName, operatedesc, null, null, user);
    }

    /**
     * 记录单个对象日志
     * @param loggerTemplate
     * @param targetId
     * @param targetName
     * @param operateType
     * @param operateTypeName
     * @param operatedesc
     * @param oldValues
     * @param newValues
     */
    private static void recoreSingleLog(LoggerTemplate loggerTemplate, Long targetId, String targetName, String operateType, String operateTypeName, String operatedesc, Object oldValues, Object newValues, User user) {
        LoggerContext loggerContext = new LoggerContext();
        loggerContext.setUser(user);
        loggerContext.setTargetId(String.valueOf(targetId));
        loggerContext.setTargetName(targetName);
        loggerContext.setOperateType(operateType);
        loggerContext.setOperateTypeName(operateTypeName);
        loggerContext.setOperatedesc(operatedesc);
        loggerContext.setOldValues(oldValues);
        loggerContext.setNewValues(newValues);
        loggerTemplate.write(loggerContext);
    }
}
