package com.sunxung.factoring.service.financing.review.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.component.util.ConstantConversionUtil;
import com.sunxung.factoring.entity.financing.review.*;
import com.sunxung.factoring.entity.sys.Dictionary;
import com.sunxung.factoring.entity.sys.FileStorage;
import com.sunxung.factoring.mapper.financing.review.ReviewMortgageBasicInfoHisMapper;
import com.sunxung.factoring.service.financing.review.*;
import com.sunxung.factoring.service.sys.FileStorageService;
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;

/**
 * <p>
 * 额度支用-抵质押基础信息-历史表 服务实现类
 * </p>
 *
 * @author liutao
 * @since 2024-01-09
 */
@Service
public class ReviewMortgageBasicInfoHisServiceImpl extends ServiceImpl<ReviewMortgageBasicInfoHisMapper, ReviewMortgageBasicInfoHis>
        implements IReviewMortgageBasicInfoHisService {

    @Autowired
    private IReviewQuotaMortgageRelHisService reviewQuotaMortgageRelHisService;
    @Autowired
    private IReviewMortgageCollateralInfoHisService reviewMortgageCollateralInfoHisService;
    @Autowired
    private IReviewMortgagePledgeInfoHisService reviewMortgagePledgeInfoHisService;
    @Autowired
    private IReviewGuaranteeFileHisService reviewGuaranteeFileHisService;
    @Autowired
    private FileStorageService fileStorageService;

    @Override
    public List<ReviewMortgageBasicInfoHis> findHistoryByQuotaDisburseId(Long quotaDisburseId) {
        List<ReviewQuotaMortgageRelHis> quotaMortgageRelList = reviewQuotaMortgageRelHisService.list(new QueryWrapper<ReviewQuotaMortgageRelHis>()
                .eq("review_quota_disburse_his_id", quotaDisburseId));

        List<ReviewMortgageBasicInfoHis> mortgageBasicInfoHisList = new ArrayList<>();
        if (quotaMortgageRelList != null && !quotaMortgageRelList.isEmpty()) {
            for (ReviewQuotaMortgageRelHis quotaMortgageRel : quotaMortgageRelList) {
                ReviewMortgageBasicInfoHis mortgageBasicInfoHis = getById(quotaMortgageRel.getReviewMortgageBasicHisId());
                if (mortgageBasicInfoHis != null && mortgageBasicInfoHis.getId() != null) {
                    //获取抵押物信息
                    List<ReviewMortgageCollateralInfoHis> collateralInfoHisList = reviewMortgageCollateralInfoHisService.list(new QueryWrapper<ReviewMortgageCollateralInfoHis>()
                            .eq("review_mortgage_basic_his_id", mortgageBasicInfoHis.getId()));
                    if (CollectionUtil.isNotEmpty(collateralInfoHisList)) {
                        collateralInfoHisList.forEach(reviewMortgageCollateralInfo -> {
                            List<ReviewGuaranteeFileHis> reviewGuaranteeSecondFiles = reviewGuaranteeFileHisService.lambdaQuery().eq(ReviewGuaranteeFileHis::getGuaranteeId, reviewMortgageCollateralInfo.getId())
                                    .eq(ReviewGuaranteeFileHis::getGuaranteeFileType, ConstantConversionUtil.CHATTELMORTGAGE_FILE).list();
                            if (CollectionUtil.isNotEmpty(reviewGuaranteeSecondFiles)) {
                                List<FileStorage> fileStorages = reviewGuaranteeSecondFiles.stream().map(reviewGuaranteeSecondFile -> fileStorageService.get(reviewGuaranteeSecondFile.getFileId())).collect(Collectors.toList());
                                reviewMortgageCollateralInfo.setFileStorageList(fileStorages);
                            }
                        });
                    }
                    mortgageBasicInfoHis.setMortgageCollateralInfoList(collateralInfoHisList);

                    //获取质押物信息
                    List<ReviewMortgagePledgeInfoHis> pledgeInfoList = reviewMortgagePledgeInfoHisService.list(new QueryWrapper<ReviewMortgagePledgeInfoHis>()
                            .eq("review_mortgage_basic_his_id", mortgageBasicInfoHis.getId()));
                    if (CollectionUtil.isNotEmpty(pledgeInfoList)) {
                        pledgeInfoList.forEach(reviewMortgagePledgeInfo -> {
                            List<ReviewGuaranteeFileHis> reviewGuaranteeSecondFiles = reviewGuaranteeFileHisService.lambdaQuery().eq(ReviewGuaranteeFileHis::getGuaranteeId, reviewMortgagePledgeInfo.getId())
                                    .eq(ReviewGuaranteeFileHis::getGuaranteeFileType, ConstantConversionUtil.PLEDGEINFORMATION_FILE).list();
                            if (CollectionUtil.isNotEmpty(reviewGuaranteeSecondFiles)) {
                                List<FileStorage> fileStorages = reviewGuaranteeSecondFiles.stream().map(reviewGuaranteeSecondFile -> fileStorageService.get(reviewGuaranteeSecondFile.getFileId())).collect(Collectors.toList());
                                reviewMortgagePledgeInfo.setFileStorageList(fileStorages);
                            }
                        });
                    }
                    mortgageBasicInfoHis.setMortgagePledgeInfoList(pledgeInfoList);

                    // 填充附件信息
                    List<ReviewGuaranteeFileHis> reviewGuaranteeSecondFiles = reviewGuaranteeFileHisService.lambdaQuery().eq(ReviewGuaranteeFileHis::getGuaranteeId, mortgageBasicInfoHis.getId())
                            .eq(ReviewGuaranteeFileHis::getGuaranteeFileType, ConstantConversionUtil.MORTGAGEINFORMATION_FILE).list();
                    if (CollectionUtil.isNotEmpty(reviewGuaranteeSecondFiles)) {
                        List<FileStorage> fileStorages = reviewGuaranteeSecondFiles.stream().map(reviewGuaranteeSecondFile -> fileStorageService.get(reviewGuaranteeSecondFile.getFileId())).collect(Collectors.toList());
                        mortgageBasicInfoHis.setFileStorageList(fileStorages);
                    }
                }
                mortgageBasicInfoHisList.add(mortgageBasicInfoHis);
            }
        }

        return mortgageBasicInfoHisList;
    }
}
