package com.jeeplus.finance.ccpm.controller;

import cn.hutool.extra.spring.SpringUtil;
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.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.ContractFilePaper;
import com.jeeplus.finance.contractRegistration.domain.ContractInfo;
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.projectReport.domain.*;
import com.jeeplus.finance.projectReport.mapper.*;
import com.jeeplus.finance.workClientInfo.domain.CwWorkClientBase;
import com.jeeplus.finance.workClientInfo.domain.CwWorkClientBilling;
import com.jeeplus.finance.workClientInfo.domain.CwWorkClientType;
import com.jeeplus.finance.workClientInfo.mapper.CwWorkClientBaseMapper;
import com.jeeplus.finance.workClientInfo.mapper.CwWorkClientBillingMapper;
import com.jeeplus.finance.workClientInfo.mapper.CwWorkClientTypeMapper;
import com.jeeplus.finance.workClientInfo.service.dto.CwWorkClientBillingDTO;
import com.jeeplus.sys.domain.Area;
import com.jeeplus.sys.feign.IAreaApi;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@RestController
@RequestMapping(value = "/ccpmData/data")
public class receiveData {

    @Autowired
    private FlowRequest flowRequest;

    @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;

    //保存客户信息
    @PostMapping("/saveClient")
    public void saveClient(@RequestBody WorkClientInfoVo workClientInfoVo) {
        if (ObjectUtils.isNotEmpty(workClientInfoVo)) {
            //先进行删除
            cwWorkClientBaseMapper.deleteClientById(workClientInfoVo.getId());
            //客户基本信息
            CwWorkClientBase clientBase = new CwWorkClientBase();
            clientBase.setId(workClientInfoVo.getId());
            clientBase.setName(workClientInfoVo.getName());//客户名称
            clientBase.setIsUscCode(workClientInfoVo.getHasUscc() + "");//是否有统一社会信用代码
            clientBase.setUscCode(workClientInfoVo.getUscCode());//统一社会信用代码
            clientBase.setAddress(workClientInfoVo.getRegisterAddress());//地址
            clientBase.setMobile(workClientInfoVo.getTelephone()); //电话
            clientBase.setIndustry(workClientInfoVo.getCompanyIndustry()); //所属行业
            clientBase.setType(workClientInfoVo.getClientType()); //客户类型
            clientBase.setStatus("5");
            clientBase.setTenantId("ccpm");
            cwWorkClientBaseMapper.insert(clientBase);
            //修改客户开户行信息
            if (CollectionUtils.isNotEmpty(workClientInfoVo.getWorkClientBankList())) {
                List<CwWorkClientBillingDTO> client_id = cwWorkClientBillingMapper.getBillingListByClientId(new QueryWrapper<CwWorkClientBilling>().eq("client_id", workClientInfoVo.getId()));
                for (CwWorkClientBillingDTO cwWorkClientBillingDTO : client_id) {
                    cwWorkClientBillingMapper.deleteClientById(cwWorkClientBillingDTO.getId());
                }
                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);

                }
            }
            List<CwWorkClientType> clientTypes = cwWorkClientTypeMapper.selectList(new QueryWrapper<CwWorkClientType>().eq("client_id", workClientInfoVo.getId()));
            for (CwWorkClientType clientType : clientTypes) {
                cwWorkClientTypeMapper.deleteClientById(clientType.getId());
            }
            //修改客户类型信息
            CwWorkClientType cwWorkClientType = new CwWorkClientType();
            cwWorkClientType.setIsListed("0");
            cwWorkClientType.setTenantId("ccpm");
            cwWorkClientType.setClientId(workClientInfoVo.getId());
            cwWorkClientTypeMapper.insert(cwWorkClientType);
        }
    }

    //保存合同及归档信息
    @PostMapping(value = "saveContractInfo")
    public void saveContractInfo(@RequestBody WorkContractInfoVo workContractInfoVo){
        if (ObjectUtils.isNotEmpty(workContractInfoVo)){
            //先进行删除
            contractInfoMapper.deleteInfoById(workContractInfoVo.getId());
            //保存合同基本信息
            ContractInfo workContractInfo = new ContractInfo();
            workContractInfo.setId(workContractInfoVo.getId());//id
            workContractInfo.setCreateTime(new Date());
            workContractInfo.setContractName(workContractInfoVo.getName());//合同名称
            workContractInfo.setContractNo(workContractInfoVo.getContractNum());//合同编号
            workContractInfo.setClientContacts(workContractInfoVo.getClient().getId());//客户id
            workContractInfo.setStatus(workContractInfoVo.getContractState());//合同状态
            workContractInfo.setTenantId("ccpm");
            //合同金额类别
            if (StringUtils.isNotBlank(workContractInfoVo.getContractInformation())){
                if ("0".equals(workContractInfoVo.getContractInformation())){
                    workContractInfo.setContractAmountType("1");//金额合同
                }else if ("1".equals(workContractInfoVo.getContractInformation())){
                    workContractInfo.setContractAmountType("2");//费率合同
                }
            }
            contractInfoMapper.insert(workContractInfo);
            //先删除
            contractFilePaperMapper.deleteInfoByContractId(workContractInfoVo.getId());
            //保存合同归档信息
            ContractFilePaper workContractFile = new ContractFilePaper();
            workContractFile.setContractInfoId(workContractInfoVo.getId());
            workContractInfo.setFiledNo(workContractInfoVo.getRecordNum());//案卷号
            workContractFile.setFiledPaperType(workContractInfoVo.getContrractRecordStatus());
            workContractFile.setTenantId("ccpm");
            contractFilePaperMapper.insert(workContractFile);
        }

    }

    //保存项目基本信息
    @PostMapping(value = "saveProjectInfo")
    public void saveProjectInfo(@RequestBody RuralProjectRecordsVo ruralProjectRecordsVo){
        if (ObjectUtils.isNotEmpty(ruralProjectRecordsVo)){
            //先进行删除
            cwProjectRecordsMapper.deleteInfoById(ruralProjectRecordsVo.getId());

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

    //保存质量复核信息
    @PostMapping(value = "saveProjectReview")
    public void saveProjectReview(@RequestBody RuralProjectRecordsVo recordsVo){
        //先进行删除复核信息
        cwProjectReviewNewMapper.deleteInfoById(recordsVo.getPrdId());
        //根据报告id删除报告编号信息
        List<CwProjectReportData> reportData = cwProjectReportMapper.getByProjectId(recordsVo.getId());
        for (CwProjectReportData reportDatum : reportData) {
            cwProjectInfoMapper.deleteByReportId(reportDatum.getId());
        }
        //根据项目id删除报告信息
        cwProjectReportMapper.deleteByProjectId(recordsVo.getId());
        //复核
        CwProjectReviewNew cwProjectReviewNew = new CwProjectReviewNew();
        //报告
        CwProjectReport report = new CwProjectReport();
        CwProjectInfoData cwProjectInfoData = new CwProjectInfoData();

        if (StringUtils.isNotBlank(recordsVo.getPrdId())) {
            //保存质量复核及报告信息
            cwProjectReviewNew.setId(recordsVo.getPrdId());
            cwProjectReviewNew.setProjectId(recordsVo.getId());
            cwProjectReviewNew.setReviewStatus(recordsVo.getProjectReportStatus().toString());
            cwProjectReviewNew.setTenantId("ccpm");
            cwProjectReviewNewMapper.insert(cwProjectReviewNew);

            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);
        }
    }


    //保存报告签发信息
    @PostMapping(value = "saveProjectReportTwo")
    public void saveProjectReportTwo(@RequestBody RuralProjectRecordsVo recordsVo){
        //先进行删除
        cwProjectReportSignAndIssueMapper.deleteInfoById(recordsVo.getPrdtId());
        //保存报告签发信息
        CwProjectReportSignAndIssue cwProjectReportSignAndIssue = new CwProjectReportSignAndIssue();
        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);
    }

    //保存电子归档信息
    @PostMapping(value = "saveProjectReportOnline")
    public  void saveProjectReportOnline(@RequestBody RuralProjectRecordsVo recordsVo){
        //先进行删除
        cwProjectReportOnlineArchivingMapper.deleteInfoById(recordsVo.getPrrId());

        //根据项目id查询报告信息
        List<CwProjectReportData> reportData = cwProjectReportMapper.getByProjectId(recordsVo.getId());
        if (CollectionUtils.isNotEmpty(reportData)){
            for (CwProjectReportData reportDatum : reportData) {
                CwProjectReportOnlineArchiving cwProjectReportOnlineArchiving = new CwProjectReportOnlineArchiving();
                cwProjectReportOnlineArchiving.setId(recordsVo.getPrrId());
                cwProjectReportOnlineArchiving.setReportId(reportDatum.getId());
                if (ObjectUtils.isNotEmpty(recordsVo.getProjectReportRecordStatus())){
                    cwProjectReportOnlineArchiving.setStatus(recordsVo.getProjectReportRecordStatus().toString());
                }else {
                    cwProjectReportOnlineArchiving.setStatus("");
                }
                cwProjectReportOnlineArchiving.setTenantId("ccpm");
                cwProjectReportOnlineArchivingMapper.insert(cwProjectReportOnlineArchiving);
             }
        }
    }

    //保存纸质归档信息
    @PostMapping(value = "saveProjectReportPaper")
    public  void saveProjectReportPaper(@RequestBody RuralProjectRecordsVo recordsVo){
        //先进行删除
        cwProjectReportPaperArchivingMapper.deleteInfoById(recordsVo.getPrrdId());

        //根据项目id查询报告信息
        List<CwProjectReportData> reportData = cwProjectReportMapper.getByProjectId(recordsVo.getId());
        if (CollectionUtils.isNotEmpty(reportData)){
            for (CwProjectReportData reportDatum : reportData) {
                CwProjectReportPaperArchiving cwProjectReportPaperArchiving = new CwProjectReportPaperArchiving();
                cwProjectReportPaperArchiving.setId(recordsVo.getPrrdId());
                cwProjectReportPaperArchiving.setReportId(reportDatum.getId());
                if (ObjectUtils.isNotEmpty(recordsVo.getDownProjectReportRecordStatus())){
                    cwProjectReportPaperArchiving.setStatus(recordsVo.getDownProjectReportRecordStatus().toString());
                }else{
                    cwProjectReportPaperArchiving.setStatus("");
                }
                cwProjectReportPaperArchiving.setTenantId("ccpm");
                cwProjectReportPaperArchivingMapper.insert(cwProjectReportPaperArchiving);
            }
        }
    }



}
