package com.pusong.business.service.approver.impl;

import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.pusong.business.domain.PsApproverRecord;
import com.pusong.business.domain.PsContractInfo;
import com.pusong.business.domain.PsTaskAppoint;
import com.pusong.business.domain.PsTaskMain;
import com.pusong.business.enums.ApproverTypeEnum;
import com.pusong.business.enums.CommonStatusEnum;
import com.pusong.business.enums.ContractStatusEnum;
import com.pusong.business.enums.TaskStatusEnum;
import com.pusong.business.mapper.PsApproverRecordMapper;
import com.pusong.business.mapper.PsTaskAppointMapper;
import com.pusong.business.mapper.PsTaskMainMapper;
import com.pusong.business.service.IPsContractBusinessService;
import com.pusong.business.service.IPsContractInfoService;
import com.pusong.business.service.IPsTaskService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 合同作废审批
 */
@RequiredArgsConstructor
@Service
@Slf4j
public class CancellaApproverServiceImpl extends ApproverAbstractServiceImpl {
    @Override
    public String approverType() {
        return ApproverTypeEnum.CANCELLA.getCode();
    }
    private final IPsContractInfoService contractInfoService;
    private final PsTaskMainMapper taskMainMapper;
    private final PsTaskAppointMapper taskAppointMapper;
    private final PsApproverRecordMapper baseMapper;

    /**
     * 1 审批成功 合同作废成功，合同状态显示为【已作废】
     * @param psApproverRecord 神瀑类
     */
    @Override
    public void success(PsApproverRecord psApproverRecord) {
        PsContractInfo psContractInfo = new PsContractInfo();
        psContractInfo.setContractCode(psApproverRecord.getContractCode());
        psContractInfo.setIsCancel(CommonStatusEnum.SUCCESS.getCode());
        contractInfoService.updateByCode(psContractInfo);
        List<PsTaskMain> list = taskMainMapper.selectList(Wrappers.<PsTaskMain>lambdaUpdate().eq(PsTaskMain::getContractCode, psApproverRecord.getContractCode()));
        List<PsTaskAppoint> lis = taskAppointMapper.selectList(Wrappers.<PsTaskAppoint>lambdaUpdate().eq(PsTaskAppoint::getContractCode, psApproverRecord.getContractCode()));
        //主任务作废
        taskMainMapper.update(Wrappers.<PsTaskMain>lambdaUpdate()
                .set(PsTaskMain::getTaskStatus,TaskStatusEnum.CANCEL.getCode())
                .eq(PsTaskMain::getContractCode, psApproverRecord.getContractCode())
                .ne(PsTaskMain::getTaskStatus,TaskStatusEnum.CANCEL.getCode()));
        //子任务作废
        taskAppointMapper.update(Wrappers.<PsTaskAppoint>lambdaUpdate()
                .set(PsTaskAppoint::getAppointStatus, TaskStatusEnum.AppointStatusEnum.CANCEL.getCode())
                .eq(PsTaskAppoint::getContractCode, psApproverRecord.getContractCode()));
        //审批删除
        if(CollectionUtils.isNotEmpty(list)){
            List<String> ids = new java.util.ArrayList<>(list.stream().map(PsTaskMain::getId).map(Object::toString).toList());
            if(CollectionUtils.isNotEmpty(lis)){ids.addAll(lis.stream().map(PsTaskAppoint::getId).map(Object::toString).toList());}
            baseMapper.update(Wrappers.<PsApproverRecord>lambdaUpdate().set(PsApproverRecord::getDelFlag, "2")
                    .in(PsApproverRecord::getBusinessId,ids));
        }

    }

    /**
     * 审批拒绝 合同的状态变回原状态，并在原状态下方展示【拒绝原因】
     * @param psApproverRecord
     */
    @Override
    public void fail(PsApproverRecord psApproverRecord) {
        PsContractInfo info = new PsContractInfo();
        info.setContractCode(psApproverRecord.getContractCode());
        info.setIsCancel(CommonStatusEnum.FAIL.getCode());
        contractInfoService.updateByCode(info);
    }
}
