package com.jeeplus.finance.ccpm.controller;


import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jeeplus.finance.ccpm.service.ccpm.FlowRequest;
import com.jeeplus.finance.ccpm.vo.RuralProjectRecordsVo;
import com.jeeplus.finance.ccpm.vo.WorkClientBankVo;
import com.jeeplus.finance.ccpm.vo.WorkClientInfoVo;
import com.jeeplus.finance.ccpm.vo.WorkContractInfoVo;
import com.jeeplus.finance.contractRegistration.domain.ContractFile;
import com.jeeplus.finance.contractRegistration.domain.ContractFilePaper;
import com.jeeplus.finance.contractRegistration.domain.ContractInfo;
import com.jeeplus.finance.contractRegistration.mapper.ContractFileMapper;
import com.jeeplus.finance.contractRegistration.mapper.ContractFilePaperMapper;
import com.jeeplus.finance.contractRegistration.mapper.ContractInfoMapper;
import com.jeeplus.finance.projectRecords.domain.CwProjectRecords;
import com.jeeplus.finance.projectRecords.mapper.CwProjectRecordsMapper;
import com.jeeplus.finance.projectRecords.service.dto.CwProjectRecordsDTO;
import com.jeeplus.finance.projectReport.domain.*;
import com.jeeplus.finance.projectReport.mapper.*;
import com.jeeplus.finance.utils.RestTemplateService;
import com.jeeplus.finance.utils.RestUtil;
import com.jeeplus.finance.workClientInfo.domain.*;
import com.jeeplus.finance.workClientInfo.mapper.CwWorkClientBaseMapper;
import com.jeeplus.finance.workClientInfo.mapper.CwWorkClientBillingMapper;
import com.jeeplus.finance.workClientInfo.mapper.CwWorkClientTypeMapper;

import com.jeeplus.sys.domain.Area;
import com.jeeplus.sys.feign.IAreaApi;


import com.jeeplus.sys.feign.IDictApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.lang.reflect.Type;
import java.util.*;

@Component
public class CcpmList  {
    @Autowired
    private FlowRequest flowRequest;

    @Autowired
    private RestTemplateService restTemplateService;

    @Autowired
    private CwProjectRecordsMapper cwProjectRecordsMapper;

    @Autowired
    private CwProjectReviewNewMapper cwProjectReviewNewMapper;

    @Autowired
    private CwProjectReportMapper cwProjectReportMapper;

    @Autowired
    private CwProjectInfoMapper cwProjectInfoMapper;

    @Autowired
    private CwProjectReportSignAndIssueMapper cwProjectReportSignAndIssueMapper;

    @Autowired
    private CwProjectReportOnlineArchivingMapper cwProjectReportOnlineArchivingMapper;

    @Autowired
    private CwProjectReportPaperArchivingMapper cwProjectReportPaperArchivingMapper;

    @Autowired
    private ContractInfoMapper contractInfoMapper;

    @Autowired
    private CwWorkClientBaseMapper cwWorkClientBaseMapper;

    @Autowired
    private CwWorkClientBillingMapper cwWorkClientBillingMapper;

    @Autowired
    private CwWorkClientTypeMapper cwWorkClientTypeMapper;

    @Autowired
    private ContractFilePaperMapper contractFilePaperMapper;




    @PostConstruct
    public void initMethod() throws Exception {
        getProjectList();
        getWorkContract();
        getAllClient();
    }

    /*8
    获取项目信息
     */
    public void getProjectList() throws Exception {
        //ccpm系统中的项目信息
        Object projectList = flowRequest.getProjectList();
        System.out.println(projectList);
        if(ObjectUtils.isNotEmpty(projectList)){
            List<RuralProjectRecordsVo> ruralProjectRecordsVos = JSONArray.parseArray(projectList.toString(), RuralProjectRecordsVo.class);
            //删除之前添加的数据
            cwProjectRecordsMapper.deleteByTenantId();
            cwProjectReviewNewMapper.deleteByTenantId();
            cwProjectReportMapper.deleteByTenantId();
            cwProjectInfoMapper.deleteByTenantId();
            cwProjectReportSignAndIssueMapper.deleteByTenantId();
            cwProjectReportOnlineArchivingMapper.deleteByTenantId();
            cwProjectReportPaperArchivingMapper.deleteByTenantId();
            //将获取到的项目信息通过遍历的方式插入到数据表中
            if (CollectionUtils.isNotEmpty(ruralProjectRecordsVos)){
                for (RuralProjectRecordsVo recordsVo : ruralProjectRecordsVos) {
                    //项目
                    CwProjectRecords cwProjectRecords = new CwProjectRecords();
                    //复核
                    CwProjectReviewNew cwProjectReviewNew = new CwProjectReviewNew();
                    //报告
                    CwProjectReport report = new CwProjectReport();
                    CwProjectInfoData cwProjectInfoData = new CwProjectInfoData();
                    //报告签发
                    CwProjectReportSignAndIssue cwProjectReportSignAndIssue = new CwProjectReportSignAndIssue();
                    //电子归档
                    CwProjectReportOnlineArchiving cwProjectReportOnlineArchiving = new CwProjectReportOnlineArchiving();
                    //纸质归档
                    CwProjectReportPaperArchiving cwProjectReportPaperArchiving = new CwProjectReportPaperArchiving();

                    //项目
                    if (StringUtils.isNotBlank(recordsVo.getId())){
                        cwProjectRecords.setId(recordsVo.getId());//项目id
                        cwProjectRecords.setProjectNumber(recordsVo.getProjectId());//项目编号
                        cwProjectRecords.setContractId(recordsVo.getWorkContractInfo().getId());
                        cwProjectRecords.setProjectName(recordsVo.getProjectName());
                        cwProjectRecords.setProjectType(recordsVo.getProjectType());
                        cwProjectRecords.setStatus(recordsVo.getProjectStatus()+"");
                        cwProjectRecords.setTenantId("ccpm");
                        //获取项目所在地
                        List<Area> areaDTO = SpringUtil.getBean(IAreaApi.class).getAreaDTOByName(recordsVo.getArea().getName());
                        if (areaDTO.size()>1){
                            //当出现两个相同的最后一级区域的时候去查询他们的上一级区域,然后根据上一级id进行添加
                            List<Area> parentArea = SpringUtil.getBean(IAreaApi.class).getAreaDTOByName(recordsVo.getCity());
                            for (Area area : areaDTO) {
                                for (Area parArea : parentArea) {
                                    if (area.getParentId().equals(parArea.getId())){
                                        cwProjectRecords.setAreaId(area.getId());
                                    }

                                }
                            }
                        }else {
                            if (ObjectUtils.isNotEmpty(areaDTO)){
                                for (Area area : areaDTO) {
                                    cwProjectRecords.setAreaId(area.getId());

                                }
                            }
                        }
                        cwProjectRecordsMapper.insert(cwProjectRecords);
                    }
                    //质量复核
                    if (StringUtils.isNotBlank(recordsVo.getPrdId())){
                        //复核信息
                        cwProjectReviewNew.setId(recordsVo.getPrdId());
                        cwProjectReviewNew.setProjectId(recordsVo.getId());
                        cwProjectReviewNew.setReviewStatus(recordsVo.getProjectReportStatus().toString());
                        cwProjectReviewNew.setTenantId("ccpm");
                        cwProjectReviewNewMapper.insert(cwProjectReviewNew);
                    }
                    //报告
                    if (StringUtils.isNotBlank(recordsVo.getPrdId())){
                        String id = UUID.randomUUID().toString().replace("-", "");
                        //报告信息
                        report.setId(id);
                        report.setProjectId(recordsVo.getId());
                        report.setTenantId("ccpm");
                        cwProjectReportMapper.insert(report);

                        //报告编号
                        String id1 = UUID.randomUUID().toString().replace("-", "");
                        cwProjectInfoData.setId(id1);
                        cwProjectInfoData.setReportId(id);
                        cwProjectInfoData.setReportNo(recordsVo.getProjectReportNumber());
                        cwProjectInfoData.setTenantId("ccpm");
                        cwProjectInfoMapper.insert(cwProjectInfoData);
                    }
                    //报告签发
                    if (StringUtils.isNotBlank(recordsVo.getPrdtId())){
                        cwProjectReportSignAndIssue.setId(recordsVo.getPrdtId());
                        cwProjectReportSignAndIssue.setProjectId(recordsVo.getId());
                        if (ObjectUtils.isNotEmpty(recordsVo.getProjectReportStatusTwo())){
                            cwProjectReportSignAndIssue.setStatus(recordsVo.getProjectReportStatusTwo().toString());
                        }else {
                            cwProjectReportSignAndIssue.setStatus("");

                        }
                        cwProjectReportSignAndIssue.setTenantId("ccpm");
                        cwProjectReportSignAndIssueMapper.insert(cwProjectReportSignAndIssue);
                    }

                    //电子归档
                    if (StringUtils.isNotBlank(recordsVo.getPrrId())){
                        cwProjectReportOnlineArchiving.setId(recordsVo.getPrrId());
                        cwProjectReportOnlineArchiving.setReportId(report.getId());
                        if (ObjectUtils.isNotEmpty(recordsVo.getProjectReportRecordStatus())){
                            cwProjectReportOnlineArchiving.setStatus(recordsVo.getProjectReportRecordStatus().toString());
                        }else {
                            cwProjectReportOnlineArchiving.setStatus("");
                        }
                        cwProjectReportOnlineArchiving.setTenantId("ccpm");
                        cwProjectReportOnlineArchivingMapper.insert(cwProjectReportOnlineArchiving);
                    }
                    //纸质归档
                    if (StringUtils.isNotBlank(recordsVo.getPrrdId())){
                        //纸质归档
                        cwProjectReportPaperArchiving.setId(recordsVo.getPrrdId());
                        cwProjectReportPaperArchiving.setReportId(report.getId());
                        if (ObjectUtils.isNotEmpty(recordsVo.getDownProjectReportRecordStatus())){
                            cwProjectReportPaperArchiving.setStatus(recordsVo.getDownProjectReportRecordStatus().toString());
                        }else{
                            cwProjectReportPaperArchiving.setStatus("");
                        }
                        cwProjectReportPaperArchiving.setTenantId("ccpm");
                        cwProjectReportPaperArchivingMapper.insert(cwProjectReportPaperArchiving);
                    }
                }
            }
        }


    }


    /**
     * 获取合同信息
     */
    public void getWorkContract(){
        //ccpm系统中的合同信息
        Object contractList = flowRequest.getWorkContractList();
        System.out.println(contractList);
        if (ObjectUtils.isNotEmpty(contractList)){
            List<WorkContractInfoVo> workContractInfoVos = JSONArray.parseArray(contractList.toString(), WorkContractInfoVo.class);
            //删除之前添加的合同数据
            contractInfoMapper.deleteByTenantId();
            contractFilePaperMapper.deleteByTenantId();
            //新增数据
            if (CollectionUtils.isNotEmpty(workContractInfoVos)){
                for (WorkContractInfoVo contractInfoVo : workContractInfoVos) {
                    ContractInfo workContractInfo = new ContractInfo();
                    workContractInfo.setId(contractInfoVo.getId());//id
                    workContractInfo.setCreateTime(new Date());
                    workContractInfo.setContractName(contractInfoVo.getName());//合同名称
                    workContractInfo.setContractNo(contractInfoVo.getContractNum());//合同编号
                    workContractInfo.setClientContacts(contractInfoVo.getClient().getId());//客户id
                    workContractInfo.setStatus(contractInfoVo.getContractState());//合同状态
                    workContractInfo.setTenantId("ccpm");
                    //合同金额类别
                    if (StringUtils.isNotBlank(contractInfoVo.getContractInformation())){
                        if ("0".equals(contractInfoVo.getContractInformation())){
                            workContractInfo.setContractAmountType("1");//金额合同
                        }else if ("1".equals(contractInfoVo.getContractInformation())){
                            workContractInfo.setContractAmountType("2");//费率合同
                        }
                    }
                    //收费标准
                    if (StringUtils.isNotBlank(contractInfoVo.getChargeCriterion())){
                        String dictValue = SpringUtil.getBean(IDictApi.class).getDictValue(contractInfoVo.getChargeCriterion(), "contract_fee_new", "");
                        workContractInfo.setContractFee(dictValue);
                    }
                    if (StringUtils.isNotBlank(contractInfoVo.getOtherDetails())){
                        workContractInfo.setFees(contractInfoVo.getOtherDetails());
                    }
                    //对方合同编号
                    workContractInfo.setContractOpposite(contractInfoVo.getContractOpposite());
                    contractInfoMapper.insert(workContractInfo);
                    //保存合同归档信息
                    ContractFilePaper workContractFile = new ContractFilePaper();
                    workContractFile.setContractInfoId(contractInfoVo.getId());
                    workContractInfo.setFiledNo(contractInfoVo.getRecordNum());//案卷号
                    workContractFile.setFiledPaperType(contractInfoVo.getContrractRecordStatus());
                    workContractFile.setTenantId("ccpm");
                    contractFilePaperMapper.insert(workContractFile);
                }
            }
        }

    }


    //获取ccpm中所有的客户信息
    public void getAllClient(){
        Object clientList = flowRequest.getClientList();
        System.out.println(clientList);
        if (ObjectUtils.isNotEmpty(clientList)){
            List<WorkClientInfoVo> workClientInfoVos = JSONArray.parseArray(clientList.toString(), WorkClientInfoVo.class);
            //删除本系统中所有之前新增的ccpm系统中的数据
            cwWorkClientBaseMapper.deleteByTenantId();
            cwWorkClientBillingMapper.deleteByTenantId();
            cwWorkClientTypeMapper.deleteByTenantId();
            for (WorkClientInfoVo workClientInfoVo : workClientInfoVos) {
                //客户基本信息
                CwWorkClientBase cwWorkClientBase = new CwWorkClientBase();
                cwWorkClientBase.setId(workClientInfoVo.getId());
                cwWorkClientBase.setName(workClientInfoVo.getName());//客户名称
                cwWorkClientBase.setIsUscCode(workClientInfoVo.getHasUscc()+"");//是否有统一社会信用代码
                cwWorkClientBase.setUscCode(workClientInfoVo.getUscCode());//统一社会信用代码
                cwWorkClientBase.setAddress(workClientInfoVo.getRegisterAddress());//地址
                cwWorkClientBase.setMobile(workClientInfoVo.getTelephone()); //电话
                cwWorkClientBase.setIndustry(workClientInfoVo.getCompanyIndustry()); //所属行业
                cwWorkClientBase.setType(workClientInfoVo.getClientType()); //客户类型
                cwWorkClientBase.setStatus("5");
                cwWorkClientBase.setTenantId("ccpm");
                cwWorkClientBaseMapper.insert(cwWorkClientBase);
                //修改客户开户行信息
                if (CollectionUtils.isNotEmpty(workClientInfoVo.getWorkClientBankList())){
                    for (WorkClientBankVo workClientBank : workClientInfoVo.getWorkClientBankList()) {
                        CwWorkClientBilling clientBilling = new CwWorkClientBilling();
                        clientBilling.setId(workClientBank.getId());
                        clientBilling.setClientId(workClientBank.getClientId().getId());
                        clientBilling.setAccountHolder(workClientBank.getOurBank());//开户行
                        clientBilling.setAccount(workClientBank.getBankNumber());//银行账号
                        clientBilling.setTenantId("ccpm");
                        cwWorkClientBillingMapper.insert(clientBilling);

                    }
                }
                //修改客户类型信息
                CwWorkClientType cwWorkClientType = new CwWorkClientType();
                cwWorkClientType.setIsListed("0");
                cwWorkClientType.setTenantId("ccpm");
                cwWorkClientType.setClientId(workClientInfoVo.getId());
                cwWorkClientTypeMapper.insert(cwWorkClientType);


            }
        }



    }

}
