package com.sunxung.factoring.service.fundmanagement.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunxung.factoring.component.util.NumberFormatUtil;
import com.sunxung.factoring.dict.impl.FileModuleDict;
import com.sunxung.factoring.entity.fundmanagement.FinancialCustomerExpenditureContractHisInfo;
import com.sunxung.factoring.entity.fundmanagement.FinancialCustomerExpenditureHisInfo;
import com.sunxung.factoring.entity.fundmanagement.vo.DownstreamDetailVo;
import com.sunxung.factoring.entity.fundmanagement.vo.FinancialCustomerExpenditureDetailVo;
import com.sunxung.factoring.entity.fundmanagement.vo.UpstreamDetailVo;
import com.sunxung.factoring.entity.sys.FileStorage;
import com.sunxung.factoring.mapper.fundmanagement.FinancialCustomerExpenditureHisInfoMapper;
import com.sunxung.factoring.service.fundmanagement.FinancialCustomerExpenditureContractHisInfoService;
import com.sunxung.factoring.service.fundmanagement.FinancialCustomerExpenditureHisInfoService;
import com.sunxung.factoring.service.sys.ISysAttachmentRefService;
import com.sunxung.factoring.service.sys.dto.AttachmentDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @Description :
 * @Author : L.Jia
 * @Date: 2023-02-22 18:03
 */
@Service("financial_customer_expenditure_editService")
public class FinancialCustomerExpenditureHisInfoServiceImpl extends ServiceImpl<FinancialCustomerExpenditureHisInfoMapper, FinancialCustomerExpenditureHisInfo>
        implements FinancialCustomerExpenditureHisInfoService {

    @Autowired
    private FinancialCustomerExpenditureHisInfoMapper financialCustomerExpenditureHisInfoMapper;

    @Autowired
    private ISysAttachmentRefService sysAttachmentRefService;

    @Autowired
    private FinancialCustomerExpenditureContractHisInfoService expenditureContractHisInfoService;

    @Override
    public FinancialCustomerExpenditureDetailVo getView(Long id) {

        FinancialCustomerExpenditureHisInfo expenditureHisInfo = financialCustomerExpenditureHisInfoMapper.selectById(id);

        FinancialCustomerExpenditureDetailVo detailVo = new FinancialCustomerExpenditureDetailVo();
        if (expenditureHisInfo != null) {
            //客户名称
            detailVo.setCustomerName(expenditureHisInfo.getCustomerName());
            //到期日
            if (expenditureHisInfo.getExpireDate() != null) {
                detailVo.setExpireDate(DateUtil.parse(expenditureHisInfo.getExpireDate(), "yyyy-MM-dd"));
            }
            //放款日
            List<Date> dateList = new ArrayList<>();
            if (expenditureHisInfo.getLoanDate() != null) {
                String[] split = expenditureHisInfo.getLoanDate().split(",");
                for (String str : split) {
                    dateList.add(DateUtil.parse(str, "yyyy-MM-dd"));
                }
            }
            detailVo.setLoanDateList(dateList);
            //放款金额
            if (expenditureHisInfo.getLoanAmountTotal() != null) {
                detailVo.setLoanAmountTotal(NumberFormatUtil.convertDecimal(expenditureHisInfo.getLoanAmountTotal(), NumberFormatUtil.THOUSAND_SEPARATOR_TWO_DECIMAL));
            }

            if (expenditureHisInfo != null) {
                List<FinancialCustomerExpenditureContractHisInfo> expenditureContractHisInfoList = expenditureContractHisInfoService.list(new QueryWrapper<FinancialCustomerExpenditureContractHisInfo>()
                        .eq("financial_customer_expenditure_his_info_id", expenditureHisInfo.getId()));
                List<UpstreamDetailVo> upstreamDetailList = new ArrayList<>();
                List<DownstreamDetailVo> downstreamDetailList = new ArrayList<>();

                Boolean upstreamShow = false;
                if (CollectionUtil.isNotEmpty(expenditureContractHisInfoList)) {
                    for (FinancialCustomerExpenditureContractHisInfo contractHisInfo : expenditureContractHisInfoList) {
                        if (contractHisInfo.getUpstreamContractNo() != null && contractHisInfo.getUpstreamContractAmount() != null
                                && contractHisInfo.getUpstreamContractPaymentDays() != null
                                && contractHisInfo.getUpstreamAccountsReceivableAmount() != null) {

                            upstreamShow = true;
                            UpstreamDetailVo upstreamDetailVo = new UpstreamDetailVo();
                            //上游合同编号
                            upstreamDetailVo.setUpstreamContractNo(contractHisInfo.getUpstreamContractNo());
                            //上游合同金额
                            upstreamDetailVo.setUpstreamContractAmount(NumberFormatUtil.convertDecimal(contractHisInfo.getUpstreamContractAmount(), NumberFormatUtil.THOUSAND_SEPARATOR_TWO_DECIMAL));
                            //上游合同账期
                            upstreamDetailVo.setUpstreamContractPaymentDays(contractHisInfo.getUpstreamContractPaymentDays());
                            //上游应收账款金额
                            upstreamDetailVo.setUpstreamAccountsReceivableAmount(NumberFormatUtil.convertDecimal(contractHisInfo.getUpstreamAccountsReceivableAmount(), NumberFormatUtil.THOUSAND_SEPARATOR_TWO_DECIMAL));
                            //上游采购合同
                            List<FileStorage> upstreamPurchFileList = sysAttachmentRefService.getFiles(AttachmentDto.builder().setEntityId(contractHisInfo.getId())
                                    .setChildEnum(FileModuleDict.ChildEnum.FINANCIAL_EXPENDITURE_UPSTREAM_PURCH_HIS_FILE).build());
                            upstreamDetailVo.setUpstreamPurchContractFiles(upstreamPurchFileList);
                            //上游结算单
                            List<FileStorage> upstreamSettleFileList = sysAttachmentRefService.getFiles(AttachmentDto.builder().setEntityId(contractHisInfo.getId())
                                    .setChildEnum(FileModuleDict.ChildEnum.FINANCIAL_EXPENDITURE_UPSTREAM_SETTLEMENT_HIS_FILE).build());
                            upstreamDetailVo.setUpstreamSettlementFiles(upstreamSettleFileList);
                            //上游发票
                            List<FileStorage> upstreamInvoiceFileList = sysAttachmentRefService.getFiles(AttachmentDto.builder().setEntityId(contractHisInfo.getId())
                                    .setChildEnum(FileModuleDict.ChildEnum.FINANCIAL_EXPENDITURE_UPSTREAM_INVOICE_HIS_FILE).build());
                            upstreamDetailVo.setUpstreamInvoiceFiles(upstreamInvoiceFileList);
                            upstreamDetailList.add(upstreamDetailVo);
                        }

                        DownstreamDetailVo downstreamDetailVo = new DownstreamDetailVo();
                        //下游合同编号
                        downstreamDetailVo.setDownstreamContractNo(contractHisInfo.getDownstreamContractNo());
                        //下游合同金额
                        downstreamDetailVo.setDownstreamContractAmount(NumberFormatUtil.convertDecimal(contractHisInfo.getDownstreamContractAmount(), NumberFormatUtil.THOUSAND_SEPARATOR_TWO_DECIMAL));
                        //下游合同账期
                        downstreamDetailVo.setDownstreamContractPaymentDays(contractHisInfo.getDownstreamContractPaymentDays());
                        //下游应收账款金额
                        downstreamDetailVo.setDownstreamAccountsReceivableAmount(NumberFormatUtil.convertDecimal(contractHisInfo.getDownstreamAccountsReceivableAmount(), NumberFormatUtil.THOUSAND_SEPARATOR_TWO_DECIMAL));
                        //下游采购合同
                        List<FileStorage> salesFileList = sysAttachmentRefService.getFiles(AttachmentDto.builder().setEntityId(contractHisInfo.getId())
                                .setChildEnum(FileModuleDict.ChildEnum.FINANCIAL_EXPENDITURE_DOWNSTREAM_SALES_HIS_FILE).build());
                        downstreamDetailVo.setDownstreamSalesContractFiles(salesFileList);
                        //下游结算单
                        List<FileStorage> downstreamSettleFileList = sysAttachmentRefService.getFiles(AttachmentDto.builder().setEntityId(contractHisInfo.getId())
                                .setChildEnum(FileModuleDict.ChildEnum.FINANCIAL_EXPENDITURE_DOWNSTREAM_SETTLEMENT_HIS_FILE).build());
                        downstreamDetailVo.setDownstreamSettlementFiles(downstreamSettleFileList);
                        //下游发票
                        List<FileStorage> downstreamInvoiceFileList = sysAttachmentRefService.getFiles(AttachmentDto.builder().setEntityId(contractHisInfo.getId())
                                .setChildEnum(FileModuleDict.ChildEnum.FINANCIAL_EXPENDITURE_DOWNSTREAM_INVOICE_HIS_FILE).build());
                        downstreamDetailVo.setDownstreamInvoiceFiles(downstreamInvoiceFileList);
                        downstreamDetailList.add(downstreamDetailVo);
                    }
                }
                detailVo.setUpstreamDetailList(upstreamDetailList);
                detailVo.setDownstreamDetailList(downstreamDetailList);
                //附件
                List<FileStorage> attachmentFileList = sysAttachmentRefService.getFiles(AttachmentDto.builder().setEntityId(expenditureHisInfo.getId())
                        .setChildEnum(FileModuleDict.ChildEnum.FINANCIAL_EXPENDITURE_ATTACHMENT_HIS_FILE).build());
                detailVo.setAttachmentFiles(attachmentFileList);
                //其它
                List<FileStorage> otherFileList = sysAttachmentRefService.getFiles(AttachmentDto.builder().setEntityId(expenditureHisInfo.getId())
                        .setChildEnum(FileModuleDict.ChildEnum.FINANCIAL_EXPENDITURE_OTHER_HIS_FILE).build());
                detailVo.setOtherFiles(otherFileList);
                detailVo.setUpstreamShow(upstreamShow);
            }
        }
        return detailVo;
    }
}
