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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunxung.factoring.dict.impl.FileModuleDict;
import com.sunxung.factoring.entity.acceptance.AcceptanceBasicInfo;
import com.sunxung.factoring.entity.acceptance.AcceptanceGoodsInfo;
import com.sunxung.factoring.entity.businessmanagement.BusinessBasicInfo;
import com.sunxung.factoring.entity.project.ProjectInformation;
import com.sunxung.factoring.entity.settlement.SettlementBasicInfo;
import com.sunxung.factoring.entity.settlement.SettlementHisGoodsInfo;
import com.sunxung.factoring.entity.settlement.SettlementHisInfo;
import com.sunxung.factoring.entity.settlement.SettlementHisRepaymentPlan;
import com.sunxung.factoring.entity.supplier.SupplierInfo;
import com.sunxung.factoring.entity.sys.FileStorage;
import com.sunxung.factoring.mapper.settlement.SettlementBasicInfoMapper;
import com.sunxung.factoring.service.acceptancesheet.AcceptanceBasicInfoService;
import com.sunxung.factoring.service.acceptancesheet.AcceptanceGoodsInfoService;
import com.sunxung.factoring.service.businessmanagement.BusinessBasicInfoService;
import com.sunxung.factoring.service.customerinformation.CustomerService;
import com.sunxung.factoring.service.entprise.IEnterpriseService;
import com.sunxung.factoring.service.project.IProjectInformationService;
import com.sunxung.factoring.service.settlement.SettlementBasicInfoFlowServcice;
import com.sunxung.factoring.service.settlement.SettlementHisGoodsInfoService;
import com.sunxung.factoring.service.settlement.SettlementHisInfoService;
import com.sunxung.factoring.service.settlement.SettlementHisRepaymentPlanService;
import com.sunxung.factoring.service.supplier.ISupplierInfoService;
import com.sunxung.factoring.service.sys.ISysAttachmentRefService;
import com.sunxung.factoring.service.sys.dto.AttachmentDto;
import com.sunxung.factoring.service.tradingPlatform.PlatformService;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author liutao
 * @date 2021.10.08
 */
@Service("bill_createService")
public class SettlementBasicInfoFlowServiceImpl extends ServiceImpl<SettlementBasicInfoMapper, SettlementBasicInfo> implements SettlementBasicInfoFlowServcice {

    @Autowired
    private CustomerService customerService;

    @Autowired
    private PlatformService platformService;

    @Autowired
    private BusinessBasicInfoService businessBasicInfoService;

    @Autowired
    private SettlementHisGoodsInfoService settlementHisGoodsInfoService;

    @Autowired
    private SettlementHisRepaymentPlanService settlementHisRepaymentPlanService;

    @Autowired
    private SettlementHisInfoService settlementHisInfoService;

    @Autowired
    private ISysAttachmentRefService sysAttachmentRefService;

    @Autowired
    private AcceptanceGoodsInfoService acceptanceGoodsInfoService;

    @Autowired
    private AcceptanceBasicInfoService acceptanceBasicInfoService;
    @Autowired
    private IEnterpriseService enterpriseService;

    @Resource(name = "SupplierInfoService")
    private ISupplierInfoService iSupplierInfoService;

    @Autowired
    @Resource(name = "projectInformationService")
    private IProjectInformationService projectInformationService;

    @Override
    public SettlementHisInfo getView(Long billBasicInformationId) {
        //结算单基础信息
        SettlementHisInfo settlementHisInfo = settlementHisInfoService.getById(billBasicInformationId);
        //添加货物信息
        List<SettlementHisGoodsInfo> billGoodsInformations = settlementHisGoodsInfoService.list(
                new QueryWrapper<SettlementHisGoodsInfo>().eq("settlement_his_info_id", settlementHisInfo.getId()));
        for (SettlementHisGoodsInfo settlementHisGoodsInfo : billGoodsInformations) {
            AcceptanceGoodsInfo acceptanceGoodsInfo = acceptanceGoodsInfoService.getById(settlementHisGoodsInfo.getAcceptanceGoodsInfoId());
            AcceptanceBasicInfo acceptanceBasicInfo = acceptanceBasicInfoService.getById(acceptanceGoodsInfo.getAcceptanceBasicInfoId());
            settlementHisGoodsInfo.setGoodsName(acceptanceGoodsInfo.getGoodsName());
            settlementHisGoodsInfo.setGoodsFactory(acceptanceGoodsInfo.getGoodsFactory());
            settlementHisGoodsInfo.setGoodsType(acceptanceGoodsInfo.getGoodsType());
            settlementHisGoodsInfo.setGoodsUnit(acceptanceGoodsInfo.getGoodsUnit());
//            settlementHisGoodsInfo.setGoodsQuantity(acceptanceGoodsInfo.getGoodsQuantity());
            settlementHisGoodsInfo.setGoodsFactory(acceptanceGoodsInfo.getGoodsFactory());
            settlementHisGoodsInfo.setCustomer(customerService.getById(settlementHisInfo.getSupplierId()));
            settlementHisGoodsInfo.setAcceptanceDate(acceptanceGoodsInfo.getAcceptanceDate());
            settlementHisGoodsInfo.setAcceptanceNumber(acceptanceBasicInfo.getAcceptanceNumber());
            settlementHisGoodsInfo.setAcceptanceGoodsNumber(acceptanceGoodsInfo.getAcceptanceGoodsNumber());
        }
        settlementHisInfo.setSettlementGoodsInfos(billGoodsInformations);
        //添加回款计划信息
        settlementHisInfo.setSettlementRepaymentPlans(
                settlementHisRepaymentPlanService.list(new QueryWrapper<SettlementHisRepaymentPlan>().eq("settlement_his_info_id",settlementHisInfo.getId())));
        //平台信息
        settlementHisInfo.setPlatform(platformService.getById(settlementHisInfo.getTradingPlatformId()));
        //核心企业信息
        settlementHisInfo.setCustomer(enterpriseService.getById(settlementHisInfo.getEnterpriseCustomerInfoId()));
        //供应商信息
        SupplierInfo supplierInfo = iSupplierInfoService.get(settlementHisInfo.getSupplierId());
        settlementHisInfo.setSupplier(enterpriseService.getById(supplierInfo.getcEnterpriseId()));
        ProjectInformation projectInformation = projectInformationService.getOne(new QueryWrapper<ProjectInformation>()
                .eq("business_number", settlementHisInfo.getBusinessNumber()));
        settlementHisInfo.setBusinessName(projectInformation.getProjectName());
        //业务信息
//        settlementHisInfo.setBusinessBasicInfo(
//                businessBasicInfoService.getOne(new QueryWrapper<BusinessBasicInfo>().eq("business_number",settlementHisInfo.getBusinessNumber())));
        //下游结算清单附件
        AttachmentDto attachmentDto = AttachmentDto.builder()
                .setEntityId(settlementHisInfo.getId())
                .setChildEnum(FileModuleDict.ChildEnum.SETTLEMENT_HIS_FILE)
                .build();
        settlementHisInfo.setFiles(sysAttachmentRefService.getFiles(attachmentDto));
        //再新增结算单说明附件
        AttachmentDto attachmentDtoAdditional = AttachmentDto.builder()
                .setEntityId(settlementHisInfo.getId())
                .setChildEnum(FileModuleDict.ChildEnum.SETTLEMENT_HIS_SUPPLEMENT_FILE)
                .build();
        settlementHisInfo.setAdditionalFiles(sysAttachmentRefService.getFiles(attachmentDtoAdditional));
        //再新增结算单网价附件
        AttachmentDto attachmentDtoNetPrice = AttachmentDto.builder()
                .setEntityId(settlementHisInfo.getId())
                .setChildEnum(FileModuleDict.ChildEnum.SETTLEMENT_HIS_NET_PRICE_FILE)
                .build();
        settlementHisInfo.setNetPriceFiles(sysAttachmentRefService.getFiles(attachmentDtoNetPrice));
        //上有结算单
        AttachmentDto attachmentDtoAcceptanceFileHis = AttachmentDto.builder()
                .setEntityId(settlementHisInfo.getId())
                .setChildEnum(FileModuleDict.ChildEnum.SETTLEMENT_HIS_UPSTREAM_UNSIGNED_FILE)
                .build();
        settlementHisInfo.setUpstreamUnsignedFiles(sysAttachmentRefService.getFiles(attachmentDtoAcceptanceFileHis));
        return settlementHisInfo;
    }
}
