package com.sunxung.factoring.service.financing.payment.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sunxung.factoring.component.exception.BusinessException;
import com.sunxung.factoring.component.util.CodeUtil;
import com.sunxung.factoring.entity.financing.payment.*;
import com.sunxung.factoring.entity.sys.FileStorage;
import com.sunxung.factoring.mapper.financing.payment.PaymentApprovalMapper;
import com.sunxung.factoring.service.financing.payment.*;
import com.sunxung.factoring.service.sys.FileStorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 付款审批表 服务实现类
 * </p>
 *
 * @author liutao
 * @since 2024-03-01
 */
@Service("payment_approvalService")
public class PaymentApprovalServiceImpl extends ServiceImpl<PaymentApprovalMapper, PaymentApproval>
        implements IPaymentApprovalService {

    @Autowired
    private IPaymentConditionService paymentConditionService;
    @Autowired
    private IPaymentConditionFileRefService paymentConditionFileRefService;
    @Autowired
    private FileStorageService fileStorageService;
    @Autowired
    private IReceivablesRegisterInfoService receivablesRegisterInfoService;
    @Autowired
    private IReceivablesRegisterFileService receivablesRegisterFileService;

    @Override
    public PaymentApproval getView(Long id) {
        PaymentApproval paymentApproval = getById(id);
        if (paymentApproval == null) {
            throw new BusinessException(CodeUtil.FAIL, "获取不到对应的付款审批信息");
        }

        //应收账款登记信息
        ReceivablesRegisterInfo receivablesRegisterInfo = receivablesRegisterInfoService.getOne(new QueryWrapper<ReceivablesRegisterInfo>()
                .eq("payment_approval_id", paymentApproval.getId()));
        if (receivablesRegisterInfo != null) {
            if (receivablesRegisterInfo.getFileId() != null) {
                FileStorage fileStorage = fileStorageService.get(receivablesRegisterInfo.getFileId());
                receivablesRegisterInfo.setFileStorage(fileStorage);
            }
            List<ReceivablesRegisterFile> receivablesFiles = receivablesRegisterFileService.list(new QueryWrapper<ReceivablesRegisterFile>()
                    .eq("receivables_register_id", receivablesRegisterInfo.getId())
                    .eq("register_type", "receivables"));
            if (CollectionUtil.isNotEmpty(receivablesFiles)) {
                List<FileStorage> fileStorageList = receivablesFiles.stream().map(file -> fileStorageService.get(file.getFileId())).collect(Collectors.toList());
                receivablesRegisterInfo.setReceivablesFiles(fileStorageList);
            }

            List<ReceivablesRegisterFile> mortgageFiles = receivablesRegisterFileService.list(new QueryWrapper<ReceivablesRegisterFile>()
                    .eq("receivables_register_id", receivablesRegisterInfo.getId())
                    .eq("register_type", "mortgage"));
            if (CollectionUtil.isNotEmpty(mortgageFiles)) {
                List<FileStorage> fileStorageList = mortgageFiles.stream().map(file -> fileStorageService.get(file.getFileId())).collect(Collectors.toList());
                receivablesRegisterInfo.setMortgageFiles(fileStorageList);
            }

            List<ReceivablesRegisterFile> ownershipFiles = receivablesRegisterFileService.list(new QueryWrapper<ReceivablesRegisterFile>()
                    .eq("receivables_register_id", receivablesRegisterInfo.getId())
                    .eq("register_type", "ownership"));
            if (CollectionUtil.isNotEmpty(ownershipFiles)) {
                List<FileStorage> fileStorageList = ownershipFiles.stream().map(file -> fileStorageService.get(file.getFileId())).collect(Collectors.toList());
                receivablesRegisterInfo.setOwnershipFiles(fileStorageList);
            }
        }
        paymentApproval.setReceivablesRegisterInfo(receivablesRegisterInfo);

        //付款条件
        List<PaymentCondition> paymentConditionList = paymentConditionService.list(new QueryWrapper<PaymentCondition>()
                .eq("payment_apply_id", paymentApproval.getPaymentApplyId()));
        if (CollectionUtil.isNotEmpty(paymentConditionList)) {
            for (PaymentCondition paymentCondition : paymentConditionList) {

                List<PaymentConditionFileRef> fileRefList = paymentConditionFileRefService.list(new QueryWrapper<PaymentConditionFileRef>()
                        .eq("payment_condition_id", paymentCondition.getId()));
                if (CollectionUtil.isNotEmpty(fileRefList)) {
                    List<FileStorage> fileStorageList = fileRefList.stream().map(fileRef -> fileStorageService.get(fileRef.getFileId())).collect(Collectors.toList());
                    paymentCondition.setExemptionFile(fileStorageList);
                }
            }
        }
        paymentApproval.setPaymentConditionList(paymentConditionList);

        return paymentApproval;
    }
}
