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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunxung.factoring.component.util.CollectionUtil;
import com.sunxung.factoring.dict.impl.FileModuleDict;
import com.sunxung.factoring.entity.acceptance.*;
import com.sunxung.factoring.entity.entprise.Enterprise;
import com.sunxung.factoring.entity.project.ProjectInformation;
import com.sunxung.factoring.entity.project.vo.ProjectInformationVo;
import com.sunxung.factoring.entity.sys.FileStorageDO;
import com.sunxung.factoring.mapper.acceptancesheet.AcceptanceBasicInfoMapper;
import com.sunxung.factoring.service.acceptancesheet.*;
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.project.impl.ProjectInformationServiceImpl;
import com.sunxung.factoring.service.sys.FileService;
import com.sunxung.factoring.service.sys.ISysAttachmentRefService;
import com.sunxung.factoring.service.sys.dto.AttachmentDto;
import com.sunxung.factoring.service.tradingPlatform.PlatformService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Service(value = "acceptance_sheetService")
public class AcceptanceSheetCreatFlowServiceImpl extends ServiceImpl<AcceptanceBasicInfoMapper, AcceptanceBasicInfo> implements AcceptanceSheetCreatFlowService {

    @Autowired
    private IEnterpriseService enterpriseService;

    @Autowired
    private AcceptanceHisInfoService acceptanceHisInfoService;

    @Autowired
    private AcceptanceHisGoodsInfoService acceptanceHisGoodsInfoService;

    @Autowired
    private ISysAttachmentRefService sysAttachmentRefService;

    @Autowired
    private PlatformService platformService;

    @Autowired
    private IAcceptancePersonInfoHistoryService acceptancePersonInfoHistoryService;

    @Autowired
    private IAcceptanceSellBillRefHisService acceptanceSellBillRefHisService;

    @Autowired
    private FileService fileService;

    @Autowired
    private IProjectInformationService projectInformationService;

    @Override
    public AcceptanceHisInfo getView(Long acceptanceHisInfoId) {
        AcceptanceHisInfo acceptanceHisInfo = acceptanceHisInfoService.getById(acceptanceHisInfoId);
        //添加验收单货物信息
        List<AcceptanceHisGoodsInfo> hisGoodsInfos = acceptanceHisGoodsInfoService
                .list(new QueryWrapper<AcceptanceHisGoodsInfo>().eq("acceptance_his_info_id", acceptanceHisInfo.getId()));
        if (CollectionUtil.isNotEmpty(hisGoodsInfos)) {
            hisGoodsInfos.stream().forEach(
                    u -> u.setSalesPriceStr(u.getSalesPrice() != null ? u.getSalesPrice().toString() : "")
            );
        }
        acceptanceHisInfo.setAcceptanceBusinessGoods(hisGoodsInfos);
        //添加验收单附件信息
        List<FileStorageDO> fileStorageDOS = fileService
                .findByBusinessId(acceptanceHisInfoId, FileModuleDict.ChildEnum.ACCEPTANCE_HIS_FILE.getCode());
        acceptanceHisInfo.setFileStorage(fileStorageDOS);

        //添加补充说明附件
        List<FileStorageDO> supplementFiles = fileService
                .findByBusinessId(acceptanceHisInfoId, FileModuleDict.ChildEnum.ACCEPTANCE_HIS_SUPPLEMENT_FILE.getCode());
        acceptanceHisInfo.setSupplementFiles(supplementFiles);

        //添加供应商信息
        acceptanceHisInfo.setSupplier(enterpriseService.getById(acceptanceHisInfo.getcSupplierInfoId()));
        //添加核心企业信息
        acceptanceHisInfo.setCustomer(enterpriseService.getById(acceptanceHisInfo.getcProjectCoreEnterpriseId()));
        //添加贸易平台信息
        acceptanceHisInfo.setPlatform(platformService.getById(acceptanceHisInfo.getTradingPlatformId()));
        //通过 业务单号 查询 项目信息
        ProjectInformation one = projectInformationService.lambdaQuery().eq(
            ProjectInformation::getBusinessNumber, acceptanceHisInfo.getBusinessNumber()).one();
        acceptanceHisInfo.setBusinessBasicInfo(one);


        //添加人员信息
        List<AcceptancePersonInfoHistory> acceptancePersonInfoHistories = acceptancePersonInfoHistoryService.lambdaQuery().eq(AcceptancePersonInfoHistory::getAcceptanceBasicInfoId, acceptanceHisInfoId).list();
        acceptanceHisInfo.setAcceptancePersonInfoHistories(acceptancePersonInfoHistories);
        List<String> numbers = new ArrayList<>();
        if(acceptanceHisInfo.getSteelWaiter().equals(1)){
            List<AcceptanceSellBillRefHis> list = acceptanceSellBillRefHisService.lambdaQuery().eq(AcceptanceSellBillRefHis::getAcceptanceHisInfoId, acceptanceHisInfoId).list();
            numbers = list.stream().map(r -> r.getSellBillNumber()).collect(Collectors.toList());
        }
        acceptanceHisInfo.setSellBillNumber(numbers);

        //验收单管理-验收单历史文件
        List<FileStorageDO> files1 = fileService.findByBusinessId(acceptanceHisInfo.getId(),
            FileModuleDict.ChildEnum.ACCEPTANCE_HIS_FILE.getCode());
        //验收单管理-补充说明历史文件
        List<FileStorageDO> files2 = fileService.findByBusinessId(acceptanceHisInfo.getId(),
            FileModuleDict.ChildEnum.ACCEPTANCE_HIS_SUPPLEMENT_FILE.getCode());
        //验收单管理-生成的上游验收单历史文件
        List<FileStorageDO> files3 = fileService.findByBusinessId(acceptanceHisInfo.getId(),
            FileModuleDict.ChildEnum.ACCEPTANCE_HIS_UPSTREAM_FILE.getCode());

        acceptanceHisInfo.setFileStorage(files1);
        acceptanceHisInfo.setSupplementFiles(files2);
        acceptanceHisInfo.setUpstreamFiles(files3);

        return acceptanceHisInfo;
    }
}
