package com.cool.store.dao.closeup;

import com.cool.store.entity.closeup.CloseUpAuditRecordDO;
import com.cool.store.enums.close.CloseStoreAuditStatusEnum;
import com.cool.store.enums.close.RecordTypeEnum;
import com.cool.store.mapper.closeup.CloseUpAuditRecordMapper;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.Collections;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * <p>
 * 歇业开业审批记录DAO
 * </p>
 *
 * @author wangff
 * @since 2026/4/8
 */
@Repository
@RequiredArgsConstructor
public class CloseUpAuditRecordDAO {
    private final CloseUpAuditRecordMapper closeUpAuditRecordMapper;

    /**
     * 提交新增记录
     */
    public void addSubmitRecord(Long applyId, Integer applyType, String userId, String userName) {
        CloseUpAuditRecordDO recordDO = CloseUpAuditRecordDO.builder()
                .applyId(applyId)
                .applyType(applyType)
                .recordType(RecordTypeEnum.CLOSE_STORE_RECORD_TYPE_10.getRecordType())
                .actionRemark("")
                .finishTaskTime(new Date())
                .receiveTaskTime(new Date())
                .handlerUserId(userId)
                .handlerUserName(userName)
                .auditStatus(CloseStoreAuditStatusEnum.PASS.getStatus())
                .handlerUserIds(getUserIds(Collections.singletonList(userId)))
                .build();
        closeUpAuditRecordMapper.insertSelective(recordDO);
    }

    /**
     * 提交审批记录
     */
    public void addApproveRecord(Long applyId, Integer applyType, List<String> userIdList) {
        CloseUpAuditRecordDO recordDO = CloseUpAuditRecordDO.builder()
                .applyId(applyId)
                .applyType(applyType)
                .recordType(RecordTypeEnum.CLOSE_STORE_RECORD_TYPE_20.getRecordType())
                .receiveTaskTime(new Date())
                .handlerUserIds(getUserIds(userIdList))
                .auditStatus(CloseStoreAuditStatusEnum.PENDING.getStatus())
                .build();
        closeUpAuditRecordMapper.insertSelective(recordDO);
    }

    /**
     * 首次发起流程 添加审批记录
     */
    public void addRecord(Long applyId, Integer applyType, String userId, String userName, List<String> userIdList) {
        //新增提交审批
        addSubmitRecord(applyId, applyType, userId, userName);
        //新增审批记录
        addApproveRecord(applyId, applyType, userIdList);
    }

    private String getUserIds(List<String> userIds) {
        String userIdStr = userIds.stream()
                .filter(StringUtils::isNotBlank)
                .distinct()
                .collect(Collectors.joining(","));
        return "," + userIdStr + ",";
    }

    public CloseUpAuditRecordDO getById(Long id) {
        CloseUpAuditRecordDO recordDO = closeUpAuditRecordMapper.selectByPrimaryKey(id);
        if (Objects.nonNull(recordDO) && recordDO.getDeleted().equals(0)) {
            return recordDO;
        }
        return null;
    }

    public CloseUpAuditRecordDO getOngoingRecordByApplyId(Long applyId) {
        Example example = new Example(CloseUpAuditRecordDO.class);
        example.createCriteria()
                .andEqualTo("applyId", applyId)
                .andEqualTo("auditStatus", CloseStoreAuditStatusEnum.PENDING.getStatus());
        example.setOrderByClause("create_time desc limit 1");
        return closeUpAuditRecordMapper.selectOneByExample(example);
    }

    public CloseUpAuditRecordDO getLatestRecordByStatus(Long applyId, Integer auditStatus) {
        Example example = new Example(CloseUpAuditRecordDO.class);
        example.createCriteria()
                .andEqualTo("applyId", applyId)
                .andEqualTo("auditStatus", auditStatus);
        example.setOrderByClause("create_time desc limit 1");
        return closeUpAuditRecordMapper.selectOneByExample(example);
    }

    public void updateByPrimaryKeySelective(CloseUpAuditRecordDO recordDO) {
        closeUpAuditRecordMapper.updateByPrimaryKeySelective(recordDO);
    }

    public List<CloseUpAuditRecordDO> getList(String userId, Integer applyType, Integer auditStatus) {
        Example example = new Example(CloseUpAuditRecordDO.class);
        example.createCriteria()
                .andLike("handlerUserIds", "%," + userId + ",%")
                .andEqualTo("applyType", applyType)
                .andEqualTo("auditStatus", auditStatus);
        example.setOrderByClause("create_time desc");
        return closeUpAuditRecordMapper.selectByExample(example);
    }

    /**
     * 根据申请id查询审批记录
     * @param applyId 申请id
     * @return 审批记录列表
     */
    public List<CloseUpAuditRecordDO> getListByApplyId(Long applyId) {
        Example example = new Example(CloseUpAuditRecordDO.class);
        example.createCriteria()
                .andEqualTo("applyId", applyId)
                .andEqualTo("deleted", 0);
        example.setOrderByClause("id ASC");
        return closeUpAuditRecordMapper.selectByExample(example);
    }
}
