package com.cool.store.dao.tp;

import com.alibaba.excel.util.CollectionUtils;
import com.cool.store.constants.CommonConstants;
import com.cool.store.entity.tp.TpAuditRecordDO;
import com.cool.store.enums.ErrorCodeEnum;
import com.cool.store.enums.OperationLogAuditEnum;
import com.cool.store.exception.ServiceException;
import com.cool.store.mapper.tp.TpAuditRecordMapper;
import com.cool.store.response.AuditInfoResponse;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.*;

/**
 * <p>
 * 十二分制-审批记录DAO
 * </p>
 *
 * @author wangff
 * @since 2025/11/6
 */
@Repository
@RequiredArgsConstructor
public class TpAuditRecordDAO {
    private final TpAuditRecordMapper tpAuditRecordMapper;

    /**
     * 新增提交记录
     *
     * @param applyId   申请单id
     * @param applyType 单据类型，0加分单 1警告书 2处罚书
     * @param userId    申请人id
     * @param userName  申请人名称
     */
    public void addSubmitRecord(Long applyId, Integer applyType, String userId, String userName) {
        Date now = new Date();
        TpAuditRecordDO recordDO = TpAuditRecordDO.builder()
                .applyType(applyType)
                .applyId(applyId)
                .recordType(1)
                .auditStatus(1)
                .handlerUserId(userId)
                .handlerUserName(userName)
                .receiveTaskTime(now)
                .finishTaskTime(now)
                .build();
        tpAuditRecordMapper.insertSelective(recordDO);
    }

    /**
     * 新增审批记录
     *
     * @param applyId   申请单id
     * @param applyType 单据类型，0加分单 1警告书 2处罚书
     */
    public void addApproveRecord(Long applyId, Integer applyType) {
        Date now = new Date();
        TpAuditRecordDO recordDO = TpAuditRecordDO.builder()
                .applyType(applyType)
                .applyId(applyId)
                .recordType(2)
                .auditStatus(0)
                .receiveTaskTime(now)
                .finishTaskTime(now)
                .build();
        tpAuditRecordMapper.insertSelective(recordDO);
    }

    /**
     * 首次发起流程
     *
     * @param applyId   申请单id
     * @param applyType 单据类型，0加分单 1警告书 2处罚书
     * @param userId    申请人id
     * @param userName  申请人名称
     */
    public void firstAddRecord(Long applyId, Integer applyType, String userId, String userName) {
        TpAuditRecordDO auditRecordDO = getPendingRecordByApplyId(applyId, applyType);
        if (Objects.isNull(auditRecordDO)) {
            addSubmitRecord(applyId, applyType, userId, userName);
            addApproveRecord(applyId, applyType);
        }
    }

    /**
     * 根据申请单id查询待审批记录
     *
     * @param applyId   申请单id
     * @param applyType 单据类型，0加分单 1警告书 2处罚书
     * @return 审批记录
     */
    public TpAuditRecordDO getPendingRecordByApplyId(Long applyId, Integer applyType) {
        TpAuditRecordDO recordDO = TpAuditRecordDO.builder()
                .applyId(applyId)
                .applyType(applyType)
                .auditStatus(0)
                .build();
        return tpAuditRecordMapper.selectOne(recordDO);
    }

    /**
     * 根据申请单id审批待处理的审批记录
     *
     * @param applyId     申请单id
     * @param applyType   单据类型，0加分单 1警告书 2处罚书
     * @param userId      审批人id
     * @param userName    审批人名称
     * @param auditStatus 审批状态
     * @param remark      备注
     * @param auditTime   审批时间
     */
    public void auditPendingRecordByApplyId(Long applyId, Integer applyType, String userId, String userName, Integer auditStatus, String remark, Date auditTime) {
        TpAuditRecordDO auditRecordDO = getPendingRecordByApplyId(applyId, applyType);
        if (Objects.isNull(auditRecordDO)) {
            throw new ServiceException(ErrorCodeEnum.TP_NOT_EXIST_PENDING_AUDIT);
        }
        auditRecordDO.setAuditStatus(auditStatus);
        auditRecordDO.setRemark(remark);
        auditRecordDO.setHandlerUserId(userId);
        auditRecordDO.setHandlerUserName(userName);
        auditRecordDO.setFinishTaskTime(auditTime);
        updateKeySelective(auditRecordDO);
    }

    /**
     * 编辑
     */
    public void updateKeySelective(TpAuditRecordDO recordDO) {
        tpAuditRecordMapper.updateByPrimaryKeySelective(recordDO);
    }

    /**
     * 审批记录
     * @param applyId 申请单id
     * @return 审批记录列表
     */
    public List<TpAuditRecordDO> getAuditRecordList(Long applyId) {
        Example example = new Example(TpAuditRecordDO.class);
        example.createCriteria()
                .andEqualTo("applyId", applyId)
                .andEqualTo("deleted", 0);
        example.setOrderByClause("create_time ASC");
        return tpAuditRecordMapper.selectByExample(example);
    }

    /**
     * 根据申请单id删除
     */
    public void deleteByApplyIds(List<Long> applyIds) {
        if (CollectionUtils.isEmpty(applyIds)) {
            return;
        }
        Example example = new Example(TpAuditRecordDO.class);
        example.createCriteria().andIn("applyId", applyIds).andEqualTo("deleted", 0);
        tpAuditRecordMapper.updateByExampleSelective(TpAuditRecordDO.builder().deleted(1).build(), example);
    }
}
