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

import com.sunxung.factoring.component.util.CollectionUtil;
import com.sunxung.factoring.component.util.StringUtil;
import com.sunxung.factoring.entity.financing.payment.PaymentApply;
import com.sunxung.factoring.entity.financing.payment.PaymentCondition;
import com.sunxung.factoring.entity.financing.payment.PaymentConditionFileRef;
import com.sunxung.factoring.entity.financing.payment.PaymentInfo;
import com.sunxung.factoring.entity.loanmanagement.ExemptionMatters;
import com.sunxung.factoring.entity.loanmanagement.ExemptionMattersHistory;
import com.sunxung.factoring.entity.loanmanagement.ExemptionMattersRefHistory;
import com.sunxung.factoring.entity.sys.Dictionary;
import com.sunxung.factoring.entity.sys.FileStorage;
import com.sunxung.factoring.mapper.financing.payment.PaymentApplyMapper;
import com.sunxung.factoring.service.financing.payment.IPaymentApplyService;
import com.sunxung.factoring.service.financing.payment.IPaymentConditionFileRefService;
import com.sunxung.factoring.service.financing.payment.IPaymentConditionService;
import com.sunxung.factoring.service.financing.payment.IPaymentInfoService;
import com.sunxung.factoring.service.financing.review.ReviewQuotaDisburseService;
import com.sunxung.factoring.service.loanmanagement.ExemptionMatterAddFlowService;
import com.sunxung.factoring.service.loanmanagement.IExemptionMattersHistoryService;
import com.sunxung.factoring.service.loanmanagement.IExemptionMattersRefHistoryService;
import com.sunxung.factoring.service.loanmanagement.IExemptionMattersService;
import com.sunxung.factoring.service.sys.DictionaryService;
import com.sunxung.factoring.service.sys.FileStorageService;
import com.sunxung.factoring.service.sys.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * @author liutao
 * @date 2023-04-24
 */
@Service("exemption_matter_addService")
public class ExemptionMatterAddFlowServiceImpl implements ExemptionMatterAddFlowService {

    @Autowired
    private IExemptionMattersHistoryService exemptionMattersHistoryService;

    @Autowired
    private IPaymentApplyService paymentApplyService;

    @Autowired
    private ReviewQuotaDisburseService reviewQuotaDisburseService;

    @Autowired
    private IPaymentInfoService paymentInfoService;

    @Autowired
    private PaymentApplyMapper applyMapper;

    @Autowired
    private DictionaryService dictService;

    @Autowired
    private UserService userService;

    @Autowired
    private FileStorageService fileStorageService;

    @Autowired
    private IExemptionMattersRefHistoryService exemptionMattersRefHistoryService;

    @Autowired
    private IExemptionMattersService exemptionMattersService;

    @Autowired
    private DictionaryService dictionaryService;

    @Autowired
    private IPaymentConditionService paymentConditionService;

    @Autowired
    private IPaymentConditionFileRefService paymentConditionFileRefService;


    @Override
    public ExemptionMatters getView(Long id) {
        ExemptionMattersHistory exemptionMattersHistory = exemptionMattersHistoryService.getById(id);
        ExemptionMatters exemptionMatters = exemptionMattersService
                .lambdaQuery().eq(ExemptionMatters::getPaymentApplyId, exemptionMattersHistory.getPaymentApplyId()).one();
        exemptionMatters = exemptionMattersService.get(exemptionMatters.getId());
        PaymentApply paymentApply = paymentApplyService.getById(exemptionMattersHistory.getPaymentApplyId());
        if (null != paymentApply) {
            PaymentInfo paymentInfo = paymentInfoService.getById(paymentApply.getPaymentInfoId());
            List<PaymentCondition> conditions = paymentConditionService
                    .lambdaQuery().eq(PaymentCondition::getPaymentApplyId, paymentApply.getId()).list();
            conditions.forEach(r -> {
                //填充附件
                List<PaymentConditionFileRef> conditionFileRefs = paymentConditionFileRefService
                        .lambdaQuery().eq(PaymentConditionFileRef::getPaymentConditionId, r.getId()).list();
                if (CollectionUtil.isNotEmpty(conditionFileRefs)) {
                    List<FileStorage> fileStorages = conditionFileRefs.stream()
                            .map(conditionFileRef -> fileStorageService.get(conditionFileRef.getFileId())).collect(Collectors.toList());
                    r.setExemptionFile(fileStorages);
                }
                if (StringUtil.isEmpty(r.getSupplement())) {
                    r.setSupplement("");
                }
                List<ExemptionMattersRefHistory> exemptionMattersRefs = exemptionMattersRefHistoryService
                        .lambdaQuery().eq(ExemptionMattersRefHistory::getPaymentConditionId, r.getId())
                        .eq(ExemptionMattersRefHistory::getExemptionMattersId, exemptionMattersHistory.getId()).list();
                if (CollectionUtil.isNotEmpty(exemptionMattersRefs)) {
                    List<FileStorage> fileStorages = exemptionMattersRefs.stream().map(exemptionMattersRef ->
                            fileStorageService.get(exemptionMattersRef.getFileId())).collect(Collectors.toList());
                    r.setFiles(fileStorages);
                }
            });
            //付款条件
            paymentApply.setConditionList(conditions);
            //付款类型
            Dictionary paymentType = dictionaryService.getByCode(paymentApply.getPaymentType());
            paymentApply.setPaymentType(paymentType.getName());
            exemptionMatters.setPaymentApply(paymentApply);
        }
        return exemptionMatters;
    }
}
