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

import cn.hutool.core.bean.BeanUtil;
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.ledger.LedgerAccountBasicInfo;
import com.sunxung.factoring.entity.refundsManagement.*;
import com.sunxung.factoring.entity.refundsManagement.vo.LedgerDetailsVo;
import com.sunxung.factoring.entity.refundsManagement.vo.RefundManagementVo;
import com.sunxung.factoring.entity.sys.Dictionary;
import com.sunxung.factoring.entity.underlying.sop.PaymentCollectionPlanFactoring;
import com.sunxung.factoring.mapper.refundsManagement.RefundsApplyDetailsMapper;
import com.sunxung.factoring.service.ledger.LedgerAccountBasicInfoService;
import com.sunxung.factoring.service.refundsManagement.*;
import com.sunxung.factoring.service.sys.DictionaryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

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

/**
 * <p>
 * 退款管理-申请内容表 服务实现类
 * </p>
 *
 * @author yafan
 * @since 2024-03-04
 */
@Service("refund_applyService")
public class RefundsApplyDetailsServiceImpl extends ServiceImpl<RefundsApplyDetailsMapper, RefundsApplyDetails> implements IRefundsApplyDetailsService {

    @Autowired
    private IRefundManagementHisService refundManagementHisService;
    @Autowired
    private IRefundManagementService refundManagementService;
    @Autowired
    private IRefundDetailsHisService refundDetailsHisService;
    @Autowired
    private LedgerAccountBasicInfoService ledgerAccountBasicInfoService;
    @Autowired
    private DictionaryService dictionaryService;
    @Autowired
    private IRefundManagementPayCollPlanFactoringRefHisService refundManagementPayCollPlanFactoringRefHisService;
    @Autowired
    private IRefundsApplyDetailsHisService refundsApplyDetailsHisService;
    @Autowired
    private IRefundsApplyAccountInfoHisService refundsApplyAccountInfoHisService;


    @Override
    public RefundManagementVo getView(Long id) {

        RefundManagementVo refundManagementVo = new RefundManagementVo();
        RefundManagementHis managementHis = refundManagementHisService.getById(id);
        RefundManagement refundManagement = refundManagementService.get(managementHis.getRefundManagementId());
        BeanUtil.copyProperties(refundManagement, refundManagementVo);
        refundManagementVo.setPlatformName(refundManagement.getTradingPlatformName());

        //应退明细
        List<RefundDetailsHis> detailsHisList = refundDetailsHisService
                .lambdaQuery().eq(RefundDetailsHis::getRefundManagementHisId, managementHis.getId()).list();
        if (CollectionUtil.isNotEmpty(detailsHisList)) {
            List<LedgerDetailsVo> ledgerDetailsVos = new ArrayList<>();
            detailsHisList.forEach(detailsHis -> {
                LedgerDetailsVo details = new LedgerDetailsVo();
                BeanUtil.copyProperties(detailsHis, details);
                LedgerAccountBasicInfo ledgerAccountBasicInfo = ledgerAccountBasicInfoService.getDetail(details.getLedgerAccountBasicInfoId());
                details.setLedgerNumber(ledgerAccountBasicInfo.getLedgerNumber());
                details.setLedgerDate(ledgerAccountBasicInfo.getLedgerDate());
                Dictionary amountTypeDic = dictionaryService.getByCode(details.getAmountType());
                details.setAmountTypeDic(amountTypeDic);
                ledgerDetailsVos.add(details);
            });
            List<List<LedgerDetailsVo>> ledgerDetailsVoLists = groupRefundDetails(ledgerDetailsVos);
            refundManagementVo.setLedgerDetails(ledgerDetailsVoLists);
        }

        //收放款计划
        List<RefundManagementPayCollPlanFactoringRefHis> refundPlanHiss = refundManagementPayCollPlanFactoringRefHisService
                .lambdaQuery().eq(RefundManagementPayCollPlanFactoringRefHis::getRefundManagementHisId, id).list();
        List<PaymentCollectionPlanFactoring> planFactoringList = new ArrayList<>();
        if (CollectionUtil.isNotEmpty(refundPlanHiss)) {
            List<PaymentCollectionPlanFactoring> allPlans = refundManagementService.findPlanBySop(refundManagement.getBusinessNumber(),
                    refundManagement.getContractNumber(), refundManagement.getQuotaDisburseBatch(), null);
            if (CollectionUtil.isNotEmpty(allPlans)) {
                allPlans.forEach(plan -> {
                    for (RefundManagementPayCollPlanFactoringRefHis planHiss : refundPlanHiss) {
                        if (plan.getId().equals(planHiss.getPayCollPlanFactoringId())) {
                            plan.setSelected(true);
                            plan.setStatusStr(plan.getStatus().getName());
                            plan.setAmountTypeName(plan.getAmountType().getName());
                            plan.setPaymentCollectionTypeName(ConstantConversionUtil.paymentCollectionMap.get(plan.getPaymentCollectionType()));
                            planFactoringList.add(plan);
                        }
                    }
                });
            }
        }
        refundManagementVo.setPaymentCollectionPlanFactorings(planFactoringList);

        //申请内容
        List<RefundsApplyDetails> refundsApplyDetails = new ArrayList<>();
        List<RefundsApplyDetailsHis> applyDetailsHisList = refundsApplyDetailsHisService
                .lambdaQuery().eq(RefundsApplyDetailsHis::getRefundManagementHisId, id).list();
        if (CollectionUtil.isNotEmpty(applyDetailsHisList)) {
            applyDetailsHisList.forEach(applyDetailsHis -> {
                RefundsApplyDetails applyDetails = new RefundsApplyDetails();
                BeanUtil.copyProperties(applyDetailsHis, applyDetails);
                Dictionary paymentMatterDic = dictionaryService.getByCode(applyDetailsHis.getPaymentMatter());
                applyDetails.setPaymentMatterDic(paymentMatterDic);
                Dictionary projectTypeDic = dictionaryService.getByCode(applyDetailsHis.getProjectType());
                applyDetails.setProjectTypeDic(projectTypeDic);


                //账户信息
                List<RefundsApplyAccountInfo> accountInfos = new ArrayList<>();
                List<RefundsApplyAccountInfoHis> applyAccountInfoHisList = refundsApplyAccountInfoHisService.lambdaQuery()
                        .eq(RefundsApplyAccountInfoHis::getRefundsApplyDetailsHisId, applyDetailsHis.getId()).list();
                if (CollectionUtil.isNotEmpty(applyAccountInfoHisList)) {
                    applyAccountInfoHisList.forEach(accountHis -> {
                        RefundsApplyAccountInfo accountInfo = new RefundsApplyAccountInfo();
                        BeanUtil.copyProperties(accountHis, accountInfo);
                        accountInfos.add(accountInfo);
                    });
                }
                applyDetails.setAccountInfoList(accountInfos);
                refundsApplyDetails.add(applyDetails);
            });
        }
        refundManagementVo.setRefundsApplyDetails(refundsApplyDetails);

        return refundManagementVo;
    }


    /**
     * 应退明细分组
     *
     * @param ledgerDetailsVos
     * @return
     */
    List<List<LedgerDetailsVo>> groupRefundDetails(List<LedgerDetailsVo> ledgerDetailsVos) {
        Map<String, List<LedgerDetailsVo>> listMap = ledgerDetailsVos.stream()
                .collect(Collectors.groupingBy(LedgerDetailsVo::getLedgerNumber));

        List<List<LedgerDetailsVo>> ledgerDetailsVoLists = new ArrayList<>();
        for (Map.Entry<String, List<LedgerDetailsVo>> map : listMap.entrySet()) {
            ledgerDetailsVoLists.add(map.getValue());
        }
        return ledgerDetailsVoLists;
    }


}
