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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.domain.bo.PsApproverRecordBo;
import com.pusong.business.domain.vo.PsApproverRecordVo;
import com.pusong.business.domain.vo.PsCompanyInfoVo;
import com.pusong.business.domain.vo.PsContractInfoVo;
import com.pusong.business.domain.vo.PsContractPayVo;
import com.pusong.business.enums.*;
import com.pusong.business.mapper.PsApproverRecordMapper;
import com.pusong.business.mapper.PsTaskAppointMapper;
import com.pusong.business.mapper.PsTaskMainMapper;
import com.pusong.business.service.IPsCompanyInfoService;
import com.pusong.business.service.IPsContractInfoService;
import com.pusong.common.core.utils.DateUtils;
import com.pusong.common.core.utils.StringUtils;
import com.pusong.common.mybatis.core.page.PageQuery;
import jakarta.annotation.Resource;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.List;

/**
 * 免费工单审批
 */
@RequiredArgsConstructor
@Service
@Slf4j
public class FreeApproverServiceImpl extends ApproverAbstractServiceImpl {
    @Override
    public String approverType() {
        return ApproverTypeEnum.FREE.getCode();
    }
    private final PsTaskMainMapper taskMainMapper;
    private final PsTaskAppointMapper taskAppointMapper;
    private final IPsContractInfoService contractInfoService;
    private final IPsCompanyInfoService companyInfoService;
    @Resource
    private PsApproverRecordMapper recordMapper;
    /**
     * 1 审批成功
     * @param psApproverRecord 神瀑类
     */
    @Override
    public void success(PsApproverRecord psApproverRecord) {
//        PsContractInfoVo vo = contractInfoService.queryContractByCode(psApproverRecord.getBusinessId());
        PsCompanyInfoVo psCompanyInfoVo = companyInfoService.queryById(Long.valueOf(psApproverRecord.getBusinessId()));

        PsTaskMain psTaskMain = new PsTaskMain();
        psTaskMain.setCompanyId(psCompanyInfoVo.getId());
        psTaskMain.setCustomId(psCompanyInfoVo.getCustomId());
//        psTaskMain.setContractCode(vo.getContractCode());
        psTaskMain.setTaskType(TaskTypeEnum.FREE.getCode());
        psTaskMain.setTaskStatus(TaskStatusEnum.INIT.getCode());
        psTaskMain.setServiceCompanyId(psCompanyInfoVo.getId());
        psTaskMain.setBusinessType(psApproverRecord.getUpdateData().split("@@")[0]);
        psTaskMain.setBusinessProject(psApproverRecord.getUpdateData().split("@@")[1]);
        taskMainMapper.insert(psTaskMain);
//        if(StringUtils.equals(vo.getSrcContractCode(), ContractStatusEnum.SUCCESS.getCode())){
//            PsContractInfo info = new PsContractInfo();
//            info.setContractStatus(ContractStatusEnum.EXECUTION.getCode());
//            info.setContractCode(vo.getContractCode());
//            contractInfoService.updateByCode(info);
//        }

    }

    /**
     * 审批拒绝
     * @param psApproverRecord
     */
    @Override
    public void fail(PsApproverRecord psApproverRecord) {

    }

    /**
     * 查询列表
     */
    @Override
    public Page<PsApproverRecordVo> queryList(PsApproverRecordBo bo, PageQuery pageQuery){
        QueryWrapper<PsApproverRecord> lqw = Wrappers.<PsApproverRecord>query().eq("app.business_type", approverType())
                .eq(bo.getId()!=null,"app.id",bo.getId())
                .eq("app.del_flag","0")
                .eq(StringUtils.isNotBlank(bo.getApproverStatus()),"app.approver_status", bo.getApproverStatus())
                .like(StringUtils.isNotBlank(bo.getCustomerName()),"cus.custom_name", bo.getCustomerName())
                .like(StringUtils.isNotBlank(bo.getCompanyName()),"com.company_name", bo.getCompanyName())
                //销售经理姓名
                .exists(StringUtils.isNotBlank(bo.getCustomManagerName()),"select 1 from sys_user su where su.user_id = info.custom_manager and su.nick_name like '%"+bo.getCustomManagerName()+"%'")
                .orderByAsc("app.approver_status").orderByDesc("app.apply_date");
        Page<PsApproverRecordVo> res = recordMapper.selectPageFreeApproverList(pageQuery.build(), lqw);
        if(CollectionUtils.isNotEmpty(res.getRecords())){
            res.getRecords().forEach(item->{
                if (item.getContractAmount() != null)
                    item.setResidualMoney(item.getContractAmount().subtract(item.getPayMoney()));
                //装填回款周期
                List<PsContractPayVo> payList = queryListByContractCode(item.getContractCode(),"1", PayStatusEnum.SUCCESS);
                if(CollectionUtils.isNotEmpty(payList)){
                    item.setPeriod(DateUtils.calWorkDate(payList.get(0).getPayDate(),payList.get(payList.size()-1).getPayDate()));
                }

                //装填服务公司信息
                PsCompanyInfoVo psCompanyInfoVo = companyInfoService.queryById(Long.valueOf(item.getBusinessId()));
                if (psCompanyInfoVo != null){
                    item.setServicePsCompanyInfoVo(Arrays.asList(psCompanyInfoVo));
                }
            });
        }
        return res;
    }

}
