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

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.sunxung.factoring.component.util.CollectionUtil;
import com.sunxung.factoring.component.util.ConstantConversionUtil;
import com.sunxung.factoring.component.util.ConstantUtil;
import com.sunxung.factoring.dict.impl.FileModuleDict;
import com.sunxung.factoring.entity.acceptance.*;
import com.sunxung.factoring.entity.sys.FileStorage;
import com.sunxung.factoring.entity.sys.FileStorageDO;
import com.sunxung.factoring.mapper.acceptancesheet.AcceptanceApprovalMapper;
import com.sunxung.factoring.service.acceptancesheet.*;
import com.sunxung.factoring.service.sys.FileService;
import com.sunxung.factoring.service.sys.FileStorageService;
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.List;
import java.util.stream.Collectors;

/**
 * @author liutao
 * @date 2021.09.27
 */
@Service(value = "legal_reviewService")
public class AcceptanceSheetLegalFlowServiceImpl extends ServiceImpl<AcceptanceApprovalMapper, AcceptanceApproval> implements AcceptanceSheetLegalFlowService {



    @Autowired
    private AcceptanceBasicInfoService acceptanceBasicInfoService;

    @Autowired
    private  AcceptanceHisInfoService acceptanceHisInfoService;

    @Autowired
    private ISysAttachmentRefService sysAttachmentRefService;
    @Autowired
    private IAcceptanceSignConfirmationService acceptanceSignConfirmationService;

    @Autowired
    private IAcceptanceSignConfirmationElectronicInfoService acceptanceSignConfirmationElectronicInfoService;

    @Autowired
    private FileStorageService fileStorageService;

    @Autowired
    private FileService fileService;

    @Override
    public AcceptanceApproval getView(Long acceptanceApprovalId) {

        AcceptanceApproval acceptanceApproval = getById(acceptanceApprovalId);
        AcceptanceBasicInfo detail = acceptanceBasicInfoService.getDetail(
            acceptanceApproval.getAcceptanceBasicInfoId());
//        //判断这次审核 是所有审核中的第几次
//        List<AcceptanceApproval> approvals = this.lambdaQuery()
//            .eq(AcceptanceApproval::getAcceptanceBasicInfoId,
//                acceptanceApproval.getAcceptanceBasicInfoId()).list();
//        int index = 0;
//        for(int i =0;i<approvals.size();i++){
//            if(approvals.get(i).getId().equals(acceptanceApprovalId)){
//                index = i;
//                break;
//            }
//        }
        //通过业务编号查询 这次审核对应的上次 的历史验收单信息
//        List<AcceptanceHisInfo> list = acceptanceHisInfoService.lambdaQuery()
//            .eq(AcceptanceHisInfo::getAcceptanceNumber, detail.getAcceptanceNumber()).list();
//        AcceptanceHisInfo acceptanceHisInfo = Lists.reverse(list).get(index);
        AcceptanceHisInfo acceptanceHisInfo = acceptanceHisInfoService.getById(
            acceptanceApproval.getAcceptanceBasicHisId());
        //
        List<FileStorageDO> file1 = fileService.findByBusinessId(acceptanceHisInfo.getId(),
            FileModuleDict.ChildEnum.ACCEPTANCE_HIS_FILE.getCode());
        List<FileStorageDO> file2 = fileService.findByBusinessId(acceptanceHisInfo.getId(),
            FileModuleDict.ChildEnum.ACCEPTANCE_HIS_SUPPLEMENT_FILE.getCode());
        List<FileStorageDO> file3 = fileService.findByBusinessId(acceptanceHisInfo.getId(),
            FileModuleDict.ChildEnum.ACCEPTANCE_HIS_UPSTREAM_FILE.getCode());
        /**法务审核完成后*/
        List<FileStorage> acceptanceUpstreamFileSign = sysAttachmentRefService.getFiles(AttachmentDto.builder()
            .setChildEnum(FileModuleDict.ChildEnum.ACCEPTANCE_UPSTREAM_FILE_SIGN)
            .setEntityId(detail.getId()).build());

//        List<FileStorage> acceptanceUpstreamFileSign = fileStorageService.findByBelongIdAndBelongType(acceptanceHisInfo.getId().toString(),
//            FileModuleDict.ChildEnum.ACCEPTANCE_UPSTREAM_FILE_SIGN.getCode());


//        AcceptanceApproval fileList = acceptanceBasicInfoService.findFileList(acceptanceApproval.getAcceptanceBasicInfoId());
//        //上游验收单文件已签署
//        AttachmentDto attachmentDto = AttachmentDto.builder()
//                .setEntityId(acceptanceApproval.getAcceptanceBasicInfoId())
//                .setChildEnum(FileModuleDict.ChildEnum.ACCEPTANCE_UPSTREAM_FILE_SIGN)
//                .build();
//        List<FileStorage> acceptanceUpstreamFileSign = sysAttachmentRefService.getFiles(attachmentDto);
        AcceptanceApproval approval = getById(acceptanceApprovalId);
//        if (CollectionUtil.isNotEmpty(acceptanceUpstreamFileSign)){
//            approval.setAcceptanceSignedFile(acceptanceUpstreamFileSign.get(0));
//        }
//        approval.setFile(fileList.getFile());
//        approval.setAdditional(fileList.getAdditional());
//        approval.setFileStorageDOList(detail.getFileStorageDOList());
//        detail.setSupplementFiles(detail.getSupplementFiles());
//        approval.setAcceptanceUpstreamFile(detail.getAcceptanceUpstreamFiles());


        approval.setAcceptanceUpstreamFile(file3);
        approval.setSupplementFiles(file2);
        approval.setFileStorageDOList(file1);
        approval.setSalesContract(detail.getSaleContractFiles());
        approval.setPurchContract(detail.getProcureContractFile());
        approval.setSignConfirmFiles(detail.getSignConfirmFiles());
        approval.setAcceptanceUpstreamFileSign(acceptanceUpstreamFileSign);

        String result;
        switch (approval.getApprovalConclusion()){
            case ConstantConversionUtil.APPROVAL_RESULT_PASS:
                result = ConstantUtil.APPROVAL;
                break;
            case ConstantConversionUtil.APPROVAL_RESULT_BACK:
                result = ConstantUtil.OVERRULE;
                break;
            default:
                result = "";

        }
        approval.setResult(result);
//        //下游验收单
//        AcceptanceSignConfirmation acceptanceSignConfirmation = acceptanceSignConfirmationService.lambdaQuery()
//                .eq(AcceptanceSignConfirmation::getAcceptanceBasicInfoId, acceptanceApproval.getAcceptanceBasicInfoId()).one();
//        if(acceptanceSignConfirmation!=null){
//            approval.setSignConfirm(1);
//            List<AcceptanceSignConfirmationElectronicInfo> acceptanceSignConfirmationElectronicInfos = acceptanceSignConfirmationElectronicInfoService.lambdaQuery()
//                    .eq(AcceptanceSignConfirmationElectronicInfo::getAcceptanceSignConfirmationId, acceptanceSignConfirmation.getId()).list();
//            List<FileStorage> signConfirmFiles = acceptanceSignConfirmationElectronicInfos.stream().filter(r->r.getBackFileId()!=null).map(r ->
//                    fileStorageService.get(r.getBackFileId())
//            ).collect(Collectors.toList());
//            approval.setSignConfirmFiles(signConfirmFiles);
//        }
        return approval;
    }
}
