package com.jwipc.finance.logic.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jwipc.constant.DictCode;
import com.jwipc.finance.logic.BudgetReportLogic;
import com.jwipc.finance.model.dto.BudgetReportDTO;
import com.jwipc.finance.model.query.BudgetReportQuery;
import com.jwipc.finance.service.IInvoicedApplyPostResultService;
import com.jwipc.page.PageUtil;
import com.jwipc.page.PageView;
import com.jwipc.sys.client.DictClient;
import com.jwipc.sys.client.PurchaseGroupClient;
import com.jwipc.sys.client.dto.DictDTO;
import com.jwipc.sys.client.dto.PurchaseGroupDTO;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

/**
 * @Author: swq
 * @Date: 2024/4/7 11:27
 */
@Service
@RequiredArgsConstructor
public class BudgetReportLogicImpl implements BudgetReportLogic {
    private final IInvoicedApplyPostResultService invoicedApplyPostResultService;
    private final DictClient dictClient;
    private final PurchaseGroupClient purchaseGroupClient;
    @Override
    public PageView budgetReportList(BudgetReportQuery query){
        Page<BudgetReportDTO> page = PageUtil.toPage(query);
        List<BudgetReportDTO> list = invoicedApplyPostResultService.findPageByQuery(page, query);
        //付款条件缓存
        List<DictDTO> paymentMethods = dictClient.getDicByType(DictCode.PAYMENT_TERMS.getCode());
        //付款方式缓存
        List<DictDTO> paymentWays = dictClient.getDicByType(DictCode.PAYMENT_WAY.getCode());
        //采购组缓存
        final List<PurchaseGroupDTO> all = purchaseGroupClient.getAll();
        final Map<String, DictDTO> paymentMethodCache = paymentMethods.stream().collect(Collectors.toMap(DictDTO::getCode, a -> a));
        final Map<String, DictDTO> paymentWayCache = paymentWays.stream().collect(Collectors.toMap(DictDTO::getCode, a -> a));
        final Map<String, PurchaseGroupDTO> purchaseGroupCache = all.stream().collect(Collectors.toMap(PurchaseGroupDTO::getPurchaseGroupCode, a -> a));

        for(BudgetReportDTO budgetReportDTO:list ){
            if(budgetReportDTO !=null){
                //付款条件取描述
                if(budgetReportDTO.getPaymentTermCode()==null){
                    budgetReportDTO.setPaymentTermDesc(null);
                }else{
                    budgetReportDTO.setPaymentTermDesc(paymentMethodCache.get(budgetReportDTO.getPaymentTermCode()).getName());
                }
                //付款方式取描述
                if(budgetReportDTO.getPaymentWay()==null){
                    budgetReportDTO.setPaymentWayDesc(null);
                }else{
                    budgetReportDTO.setPaymentWayDesc(paymentWayCache.get(budgetReportDTO.getPaymentWay()).getName());
                }
                //采购组取名称
                if(budgetReportDTO.getPurchaseGroup()==null){
                    budgetReportDTO.setPurchaseGroupName(null);
                }else{
                    budgetReportDTO.setPurchaseGroupName(purchaseGroupCache.get(budgetReportDTO.getPurchaseGroup()).getPurchaseGroupName());
                }
            }

        }

        return PageUtil.getPV(list, page.getTotal());
    }

    @Override
    public List<BudgetReportDTO> listAll(BudgetReportQuery budgetReportQuery) {
        List<BudgetReportDTO> list = invoicedApplyPostResultService.findPageByQuery(null, budgetReportQuery);
        //付款条件缓存
        List<DictDTO> paymentMethods = dictClient.getDicByType(DictCode.PAYMENT_TERMS.getCode());
        //付款方式缓存
        List<DictDTO> paymentWays = dictClient.getDicByType(DictCode.PAYMENT_WAY.getCode());
        //采购组缓存
        final List<PurchaseGroupDTO> all = purchaseGroupClient.getAll();
        final Map<String, DictDTO> paymentMethodCache = paymentMethods.stream().collect(Collectors.toMap(DictDTO::getCode, a -> a));
        final Map<String, DictDTO> paymentWayCache = paymentWays.stream().collect(Collectors.toMap(DictDTO::getCode, a -> a));
        final Map<String, PurchaseGroupDTO> purchaseGroupCache = all.stream().collect(Collectors.toMap(PurchaseGroupDTO::getPurchaseGroupCode, a -> a));

        for(BudgetReportDTO budgetReportDTO:list ){
            if(budgetReportDTO !=null) {
                //付款条件取描述
                if (budgetReportDTO.getPaymentTermCode() == null) {
                    budgetReportDTO.setPaymentTermDesc(null);
                } else {
                    budgetReportDTO.setPaymentTermDesc(paymentMethodCache.get(budgetReportDTO.getPaymentTermCode()).getName());
                }
                //付款方式取描述
                if (budgetReportDTO.getPaymentWay() == null) {
                    budgetReportDTO.setPaymentWayDesc(null);
                } else {
                    budgetReportDTO.setPaymentWayDesc(paymentWayCache.get(budgetReportDTO.getPaymentWay()).getName());
                }
                //采购组取名称
                if (budgetReportDTO.getPurchaseGroup() == null) {
                    budgetReportDTO.setPurchaseGroupName(null);
                } else {
                    budgetReportDTO.setPurchaseGroupName(purchaseGroupCache.get(budgetReportDTO.getPurchaseGroup()).getPurchaseGroupName());
                }
            }
        }
        return list;
    }
}
