package com.sundot.airport.check.handler;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.sundot.airport.check.domain.CheckCorrection;
import com.sundot.airport.check.domain.CheckUser;
import com.sundot.airport.check.service.ICheckCorrectionService;
import com.sundot.airport.common.enums.CheckCorrectionStatusEnum;
import com.sundot.airport.common.utils.DateUtils;
import com.sundot.airport.common.utils.SecurityUtils;
import com.sundot.airport.system.domain.approval.ApprovalCc;
import com.sundot.airport.system.domain.approval.ApprovalInstance;
import com.sundot.airport.system.enums.ProcessStatusEnum;
import com.sundot.airport.system.mapper.approval.ApprovalCcMapper;
import com.sundot.airport.system.service.approval.BusinessApprovalHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.stream.Collectors;


/**
 * 问题整改审批处理
 *
 * @author sundot
 */
@Component
public class CheckCorrectionApprovalHandler implements BusinessApprovalHandler {

    private static final Logger log = LoggerFactory.getLogger(CheckCorrectionApprovalHandler.class);

    @Autowired
    private ICheckCorrectionService checkCorrectionService;

    @Autowired
    private ApprovalCcMapper ccMapper;

    @Override
    public boolean supports(String businessType) {
        return "PERSONAL_CHECK".equals(businessType)
                || "GROUP_CHECK".equals(businessType)
                || "DEPARTMENT_CHECK".equals(businessType)
                || "BRIGADE_CHECK".equals(businessType);
    }

    @Override
    public void handleApproved(ApprovalInstance instance) {
        log.info("问题整改审批 {}", instance);
        if ("COMPLETED".equals(instance.getStatus())) {
            LambdaUpdateWrapper<CheckCorrection> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(CheckCorrection::getId, instance.getBusinessId())
                    .set(CheckCorrection::getProcessStatus, ProcessStatusEnum.ARCHIVED.getCode())
                    .set(CheckCorrection::getProcessStatusDesc, ProcessStatusEnum.ARCHIVED.getInfo())
                    .set(CheckCorrection::getStatus, CheckCorrectionStatusEnum.RECTIFIED.getCode())
                    .set(CheckCorrection::getStatusDesc, CheckCorrectionStatusEnum.RECTIFIED.getDesc())
                    .set(CheckCorrection::getCompletionTime, DateUtils.getNowDate());
            checkCorrectionService.update(updateWrapper);
            CheckCorrection checkCorrection = checkCorrectionService.selectCheckCorrectionById(instance.getBusinessId());
            if (ObjUtil.isNull(checkCorrection)) {
                return;
            }
            List<CheckUser> checkUserList = checkCorrection.getCheckProjectItemList().stream().filter(item -> CollUtil.isNotEmpty(item.getCheckUserList())).flatMap(item -> item.getCheckUserList().stream()).distinct().collect(Collectors.toList());
            checkUserList.forEach(item -> {
                ApprovalCc cc = new ApprovalCc();
                cc.setInstanceId(instance.getId());
                cc.setCcUserId(item.getUserId());
                cc.setCcUserName(item.getUserName());
                cc.setCcTime(DateUtils.getNowDate());
                cc.setIsRead("0"); // 未读
                cc.setCreateBy(SecurityUtils.getUsername());
                cc.setCreateTime(DateUtils.getNowDate());
                ccMapper.insertApprovalCc(cc);
            });
        } else {
            LambdaUpdateWrapper<CheckCorrection> updateWrapper = new LambdaUpdateWrapper<>();
            updateWrapper.eq(CheckCorrection::getId, instance.getBusinessId())
                    .set(CheckCorrection::getProcessStatus, ProcessStatusEnum.AUDITING.getCode())
                    .set(CheckCorrection::getProcessStatusDesc, ProcessStatusEnum.AUDITING.getInfo())
                    .set(CheckCorrection::getStatus, CheckCorrectionStatusEnum.UNDER_RECTIFICATION.getCode())
                    .set(CheckCorrection::getStatusDesc, CheckCorrectionStatusEnum.UNDER_RECTIFICATION.getDesc());
            checkCorrectionService.update(updateWrapper);
        }
    }

    @Override
    public void handleRejected(ApprovalInstance instance) {
        log.info("问题整改审批驳回 {}", instance);
    }

    @Override
    public void handleCancelled(ApprovalInstance instance) {
        log.info("问题整改审批取消{}", instance);
        checkCorrectionService.lambdaUpdate().set(CheckCorrection::getProcessStatus, ProcessStatusEnum.TERMINATED.getCode()).eq(CheckCorrection::getId, instance.getBusinessId());
    }

}
