package com.fjhx.sale.service.statement.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.fjhx.common.entity.currency.po.CurrencyRate;
import com.fjhx.common.service.currency.CurrencyRateService;
import com.fjhx.customer.service.customer.CustomerService;
import com.fjhx.purchase.entity.purchase.po.Purchase;
import com.fjhx.purchase.entity.purchase.po.PurchaseDetail;
import com.fjhx.purchase.service.purchase.PurchaseDetailService;
import com.fjhx.purchase.service.purchase.PurchaseService;
import com.fjhx.sale.entity.claim.po.Claim;
import com.fjhx.sale.entity.claim.po.ClaimContract;
import com.fjhx.sale.entity.contract.bo.ContractCurrencyRate;
import com.fjhx.sale.entity.contract.po.Contract;
import com.fjhx.sale.entity.statement.dto.ProfitSettlementDto;
import com.fjhx.sale.entity.statement.vo.ProfitSettlementVo;
import com.fjhx.sale.service.claim.ClaimContractService;
import com.fjhx.sale.service.claim.ClaimService;
import com.fjhx.sale.service.contract.ContractService;
import com.fjhx.sale.service.statement.SaleStatementService;
import com.ruoyi.common.core.domain.BaseIdPo;
import com.ruoyi.system.utils.UserUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SaleStatementServiceImpl implements SaleStatementService {

    @Autowired
    private ContractService contractService;

    @Autowired
    private CustomerService customerService;

    @Autowired
    private CurrencyRateService currencyRateService;

    @Autowired
    private ClaimContractService claimContractService;

    @Autowired
    private ClaimService claimService;

    @Autowired
    private PurchaseDetailService purchaseDetailService;

    @Autowired
    private PurchaseService purchaseService;

    @Override
    public Page<ProfitSettlementVo> getProfitSettlement(ProfitSettlementDto dto) {

        Page<Contract> contractPage = getContractPage(dto);
        List<Contract> records = contractPage.getRecords();
        if (records.size() == 0) {
            return new Page<>();
        }

        // 获取默认税率
        Map<String, BigDecimal> defaultRateMap = getDefaultRateMap();

        // 获取税率明细
        Map<Long, Map<String, BigDecimal>> detailsRateMap = getDetailsRateMap(records, defaultRateMap);

        // 转换为vo
        List<ProfitSettlementVo> list = createResultList(records, detailsRateMap);

        // 赋值业务员名称
        UserUtil.assignmentNickName(list, ProfitSettlementVo::getUserId, ProfitSettlementVo::setUserName);

        // 赋值客户名称
        customerService.attributeAssign(list, ProfitSettlementVo::getCustomerId, (item, customer) -> {
            item.setCustomerName(customer.getName());
        });

        // 赋值合同到账
        setContractArrival(list, detailsRateMap);

        // 赋值采购合同金额和支付货款
        setPurchaseAmountAndPayForGoods(list, detailsRateMap);

        // 计算
        calculation(list);

        // 转page
        return toPage(contractPage, list);
    }

    /**
     * 获取单条合同汇率
     */
    private Map<Long, Map<String, BigDecimal>> getDetailsRateMap(
            List<Contract> records, Map<String, BigDecimal> defaultRateMap) {

        HashMap<Long, Map<String, BigDecimal>> result = new HashMap<>();

        for (Contract record : records) {

            Map<String, BigDecimal> map;

            // 明细数据税率
            String currencyRateJson = record.getCurrencyRateJson();
            if (!StrUtil.isBlank(currencyRateJson)) {
                List<ContractCurrencyRate> contractCurrencyRates = JSON.parseArray(currencyRateJson, ContractCurrencyRate.class);

                map = contractCurrencyRates.stream()
                        .collect(Collectors.toMap(ContractCurrencyRate::getType, ContractCurrencyRate::getRate));

                defaultRateMap.forEach(map::putIfAbsent);

            } else {
                map = new HashMap<>(defaultRateMap);
            }

            result.put(record.getId(), map);
        }

        return result;
    }

    /**
     * 获取合同分页
     */
    private Page<Contract> getContractPage(ProfitSettlementDto dto) {

        Page<Contract> page = contractService.page(dto.getPage(), q -> q
                .eq(ObjectUtil.isNotEmpty(dto.getSettlementStatus()), Contract::getSettlementStatus, dto.getSettlementStatus())
                .eq(ObjectUtil.isNotEmpty(dto.getUserId()), Contract::getCreateUser, dto.getUserId())
        );

        return page;
    }

    /**
     * 创建结果集list
     */
    private List<ProfitSettlementVo> createResultList(
            List<Contract> records, Map<Long, Map<String, BigDecimal>> detailsRateMap) {

        return records.stream().map(item -> {
            Map<String, BigDecimal> ietmRateMap = detailsRateMap.get(item.getId());

            ProfitSettlementVo profitSettlementVo = new ProfitSettlementVo();
            profitSettlementVo.setContractId(item.getId());
            profitSettlementVo.setContractCode(item.getCode());
            profitSettlementVo.setCustomerId(item.getBuyCorporationId());
            profitSettlementVo.setUserId(item.getCreateUser());
            profitSettlementVo.setContractAmount(item.getAmount()
                    .multiply(ietmRateMap.getOrDefault(item.getCurrency(), BigDecimal.ONE)));
            profitSettlementVo.setCurrencyRateJson(item.getCurrencyRateJson());
            profitSettlementVo.setSettlementStatus(item.getSettlementStatus());

            return profitSettlementVo;
        }).collect(Collectors.toList());

    }

    /**
     * 获取默认汇率
     */
    private Map<String, BigDecimal> getDefaultRateMap() {
        List<CurrencyRate> list = currencyRateService.list();
        return list.stream().collect(Collectors.toMap(CurrencyRate::getType, CurrencyRate::getRate));
    }

    /**
     * 赋值合同到账金额
     */
    private void setContractArrival(List<ProfitSettlementVo> list, Map<Long, Map<String, BigDecimal>> detailsRateMap) {

        // 合同id
        List<Long> contractIdList = list.stream().map(ProfitSettlementVo::getContractId).collect(Collectors.toList());

        // 根据合同id获取到账认领明细列表
        List<ClaimContract> claimContractList = claimContractService.list(q ->
                q.in(ClaimContract::getContractId, contractIdList));

        if (claimContractList.size() == 0) {
            return;
        }

        // 到账认领明细id列表
        List<Long> claimIdList = claimContractList.stream()
                .map(ClaimContract::getClaimId).distinct().collect(Collectors.toList());

        // 到账认领列表
        List<Claim> claimList = claimService.listByIds(claimIdList);

        // 到账认领id 币种 map
        Map<Long, String> claimIdCurrencyMap = claimList.stream()
                .collect(Collectors.toMap(BaseIdPo::getId, Claim::getCurrency));

        // 合同id 到账认领明细列表 map
        Map<Long, List<ClaimContract>> contractIdClaimContractListMap =
                claimContractList.stream().collect(Collectors.groupingBy(ClaimContract::getContractId));

        for (ProfitSettlementVo profitSettlementVo : list) {

            // 合同id
            Long contractId = profitSettlementVo.getContractId();

            // 到账认领
            List<ClaimContract> itemClaimContractList = contractIdClaimContractListMap.getOrDefault(contractId, new ArrayList<>());

            // 汇率币种map
            Map<String, BigDecimal> map = detailsRateMap.get(contractId);

            BigDecimal contractArrival = itemClaimContractList.stream().map(claimContract -> {

                // 根据到账认领id获取币种
                String currency = claimIdCurrencyMap.get(claimContract.getClaimId());

                // 根据币种获取汇率
                BigDecimal rate = map.getOrDefault(currency, BigDecimal.ONE);

                // 获取认领金额
                BigDecimal money = claimContract.getMoney();

                // 返回兑人民币后金额
                return rate.multiply(money);

            }).reduce(BigDecimal.ZERO, BigDecimal::add);

            profitSettlementVo.setContractArrival(contractArrival);

        }

    }

    /**
     * 赋值采购合同金额和支付货款
     */
    private void setPurchaseAmountAndPayForGoods(
            List<ProfitSettlementVo> list, Map<Long, Map<String, BigDecimal>> detailsRateMap) {

        // 合同id
        List<Long> contractIdList = list.stream().map(ProfitSettlementVo::getContractId).collect(Collectors.toList());

        List<PurchaseDetail> purchaseDetailList = purchaseDetailService.list(q -> q.in(PurchaseDetail::getDataResourceId, contractIdList));
        if (purchaseDetailList.size() == 0) {
            list.forEach(item -> item.setPurchaseAmount(BigDecimal.ZERO));
            return;
        }

        List<Long> purchaseIdList = purchaseDetailList.stream().map(PurchaseDetail::getPurchaseId).collect(Collectors.toList());
        List<Purchase> purchaseList = purchaseService.listByIds(purchaseIdList);

        // 采购id 币种 map
        Map<Long, String> purchaseIdCurrencyMap = purchaseList.stream()
                .collect(Collectors.toMap(BaseIdPo::getId, Purchase::getCurrency));

        // 合同id 申购单列表 map
        Map<Long, List<PurchaseDetail>> contractIdPurchaseDetailListMap = purchaseDetailList.stream()
                .collect(Collectors.groupingBy(PurchaseDetail::getDataResourceId));

        for (ProfitSettlementVo profitSettlementVo : list) {

            // 合同id
            Long contractId = profitSettlementVo.getContractId();

            // 申购单
            List<PurchaseDetail> itemPurchaseDetailList = contractIdPurchaseDetailListMap.getOrDefault(contractId, new ArrayList<>());

            // 汇率币种map
            Map<String, BigDecimal> map = detailsRateMap.getOrDefault(contractId, new HashMap<>());

            BigDecimal purchaseAmount = itemPurchaseDetailList.stream().map(purchaseDetail -> {

                // 根据申购单id获取币种
                String currency = purchaseIdCurrencyMap.get(purchaseDetail.getPurchaseId());

                // 根据币种获取汇率
                BigDecimal rate = map.getOrDefault(currency, BigDecimal.ONE);

                // 获取申购金额
                BigDecimal money = purchaseDetail.getAmount();

                // 返回兑人民币后金额
                return rate.multiply(money);

            }).reduce(BigDecimal.ZERO, BigDecimal::add);

            profitSettlementVo.setPurchaseAmount(purchaseAmount);

        }

    }

    /**
     * 计算
     */
    private void calculation(List<ProfitSettlementVo> list) {

        for (ProfitSettlementVo profitSettlementVo : list) {
            BigDecimal totalIncome = profitSettlementVo.getOtherIncome().add(profitSettlementVo.getContractArrival());
            profitSettlementVo.setTotalIncome(totalIncome);

            BigDecimal totalExpenses = profitSettlementVo.getOtherExpenses().add(profitSettlementVo.getPayForGoods());
            profitSettlementVo.setTotalExpenses(totalExpenses);

            profitSettlementVo.setGrossProfit(totalIncome.subtract(totalExpenses));

            if (totalIncome.equals(BigDecimal.ZERO)) {
                profitSettlementVo.setGrossProfitMargin(BigDecimal.ZERO);
            } else {
                profitSettlementVo.setGrossProfitMargin(totalExpenses.divide(totalIncome, RoundingMode.HALF_UP));
            }

        }

    }

    /**
     * 转page
     */
    private Page<ProfitSettlementVo> toPage(Page<Contract> contractPage, List<ProfitSettlementVo> list) {
        Page<ProfitSettlementVo> page = new Page<>();
        page.setTotal(contractPage.getTotal());
        page.setCurrent(contractPage.getCurrent());
        page.setPages(contractPage.getPages());
        page.setSize(contractPage.getSize());
        page.setRecords(list);

        return page;
    }


}
