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

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
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.bo.*;
import com.pusong.business.domain.vo.PsApproverRecordVo;
import com.pusong.business.domain.vo.PsCompanyInfoVo;
import com.pusong.business.domain.vo.PsContractBusinessVo;
import com.pusong.business.domain.vo.PsContractInfoVo;
import com.pusong.business.enums.ApproverTypeEnum;
import com.pusong.business.enums.CommonStatusEnum;
import com.pusong.business.mapper.PsApproverRecordMapper;
import com.pusong.business.service.IPsContractBusinessService;
import com.pusong.business.service.IPsContractInfoService;
import com.pusong.common.core.utils.MapstructUtils;
import com.pusong.common.core.utils.StringUtils;
import com.pusong.common.mybatis.core.page.PageQuery;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * 电子签章审批
 */
@RequiredArgsConstructor
@Service
@Slf4j
public class SignApproverServiceImpl extends ApproverAbstractServiceImpl {
    @Override
    public String approverType() {
        return ApproverTypeEnum.SIGN.getCode();
    }
    private final IPsContractBusinessService businessService;
    private final IPsContractInfoService contractInfoService;


    /**
     * 1 审批成功生成签章合同 修改签章状态
     * @param psApproverRecord 神瀑类
     */
    @Override
    public void success(PsApproverRecord psApproverRecord) {
        //1查询源数据
        PsContractInfoVo vo = contractInfoService.queryContractByCode(psApproverRecord.getContractCode());
        //合同信息bo
        PsContractInfoBo bo = new PsContractInfoBo();
        //合同信息
        PsContractInfo info = new PsContractInfo();
        BeanUtils.copyProperties(vo, info);
        PsCompanyInfoBo companyInfo = new PsCompanyInfoBo();
        BeanUtils.copyProperties(vo.getPsCompanyInfoVo(), companyInfo);
        bo.setCompanyInfoBo(companyInfo);
        List<PsCompanyInfoBo> serviceCompanyInfoList = new ArrayList<>();
        bo.setServiceCompanyInfoList(serviceCompanyInfoList);

        for (PsCompanyInfoVo psCompanyInfoVo : vo.getPsCompanySerivceVo()) {
            List<PsContractBusinessBo> businessBos = new ArrayList<>();
            for (PsContractBusinessVo item : psCompanyInfoVo.getBusinessVoList()) {
                List<PsContractBusinessDetailBo> detailBos = new ArrayList<>();
                item.getDetailVoList().forEach(it->{
                    PsContractBusinessDetailBo detailBo = new PsContractBusinessDetailBo();
                    BeanUtils.copyProperties(it, detailBo);
                    detailBos.add(detailBo);
                });
                PsContractBusinessBo businessBo = new PsContractBusinessBo();
                BeanUtils.copyProperties(item, businessBo);
                businessBo.setDetailBos(detailBos);
                businessBos.add(businessBo);
                if ("1".equals(item.getIsProxy())){
                    companyInfo.setIsProxy("1");
                }
            }

            PsCompanyInfoBo psCompanyInfoBo = new PsCompanyInfoBo();
            BeanUtils.copyProperties(psCompanyInfoVo, psCompanyInfoBo);
            psCompanyInfoBo.setBusinessList(businessBos);
            serviceCompanyInfoList.add(psCompanyInfoBo);
        }

        info.setSignStatus(CommonStatusEnum.SUCCESS.getCode());
        contractInfoService.updateByCode(info);
        //生成合同
        contractInfoService.makePdf(info, bo,true);
    }

    //修改签章状态
    @Override
    public void fail(PsApproverRecord psApproverRecord) {
        PsContractInfo info = new PsContractInfo();
        info.setContractCode(psApproverRecord.getContractCode());
        info.setSignStatus(CommonStatusEnum.FAIL.getCode());
        contractInfoService.updateByCode(info);
    }




}
