package com.hzs.bonus.bonus.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.hzs.activity.base.IActivityServiceApi;
import com.hzs.common.core.domain.R;
import com.hzs.common.core.enums.EBonusIncomeStatus;
import com.hzs.common.core.enums.EBonusItems;
import com.hzs.common.core.enums.ECalType;
import com.hzs.common.core.utils.ComputeUtil;
import com.hzs.common.domain.activity.recommend.AcRepurCouponsRuleConfig;
import com.hzs.common.domain.bonus.BdBonusDeduct;
import com.hzs.common.domain.member.bonus.CuMemberBonus;
import com.hzs.common.domain.member.bonus.CuMemberBonusDetail;
import com.hzs.common.domain.member.ext.CuMemberSettleExt;
import com.hzs.common.domain.sale.ext.SaOrderExt;
import com.hzs.system.config.dto.BonusConfigDTO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.stereotype.Component;

import java.util.*;

/**
 * @description: 用于计算复购券收益、复购券均分收益
 * @author: sui q
 * @time: 2023/4/28 16:12
 * @classname: BonusSettleRepCouponsHandle
 * @package_name: com.hzs.member.bonus.service.impl
 * version 1.0.0
 */
@Component
public class BonusSettleRepCouponsHandle extends BonusSettleHandle {

    @DubboReference
    private IActivityServiceApi activityServiceApi;

    /*
     * @description: 计算复购券收益
     * @author: sui q
     * @date: 2023/4/28 16:11
     * @param: null null
     **/
    List<CuMemberBonusDetail> calculateRepurchaseCoupons(Date settleDate, List<SaOrderExt> firPurchaseOrderList, Integer period, BonusConfigDTO bonusConfigDTO, Map<Long, CuMemberBonus> cuMemberBonusMap, Map<Long, CuMemberSettleExt> cuMemberSettleExtMap) {
        // 查询复购券发放规则
        R<List<AcRepurCouponsRuleConfig>> acRepurchaseCouponsRule = activityServiceApi.queryAcRepurchaseCouponsRule(settleDate);
        List<CuMemberBonusDetail> cuMemberBonusDetailList = new ArrayList<>();
        if (acRepurchaseCouponsRule.isSuccess()) {
            List<AcRepurCouponsRuleConfig> repurCouponsRuleConfigList = acRepurchaseCouponsRule.getData();
            if (CollectionUtil.isNotEmpty(repurCouponsRuleConfigList)) {
                Map<String, Map<Long, List<AcRepurCouponsRuleConfig>>> couponsMap = new HashMap<>();
                for (AcRepurCouponsRuleConfig acRepurCouponsRuleConfig : repurCouponsRuleConfigList) {
                    String key = acRepurCouponsRuleConfig.getPkCountry().toString() + "_" + acRepurCouponsRuleConfig.getOrderType();
                    if (couponsMap.containsKey(key)) {
                        Map<Long, List<AcRepurCouponsRuleConfig>> couponsListMap = couponsMap.get(key);
                        if (couponsListMap.containsKey(acRepurCouponsRuleConfig.getPkBaseId())) {
                            couponsListMap.get(acRepurCouponsRuleConfig.getPkBaseId()).add(acRepurCouponsRuleConfig);
                        } else {
                            List<AcRepurCouponsRuleConfig> acRepurCouponsRuleConfigList = new ArrayList<>();
                            acRepurCouponsRuleConfigList.add(acRepurCouponsRuleConfig);
                            couponsListMap.put(acRepurCouponsRuleConfig.getPkBaseId(), acRepurCouponsRuleConfigList);
                        }
                    } else {
                        Map<Long, List<AcRepurCouponsRuleConfig>> couponsListMap = new HashMap<>();
                        List<AcRepurCouponsRuleConfig> acRepurCouponsRuleConfigList = new ArrayList<>();
                        acRepurCouponsRuleConfigList.add(acRepurCouponsRuleConfig);
                        couponsListMap.put(acRepurCouponsRuleConfig.getPkBaseId(), acRepurCouponsRuleConfigList);
                        couponsMap.put(key, couponsListMap);
                    }
                }
                // 查询订单
                for (SaOrderExt saOrderExt : firPurchaseOrderList) {
                    CuMemberSettleExt sourceMemberSettleExt = cuMemberSettleExtMap.get(saOrderExt.getPkMember());
                    if (sourceMemberSettleExt == null) {
                        continue;
                    }
                    String key = saOrderExt.getPkCountry().toString() + "_" + saOrderExt.getOrderType();
                    if (couponsMap.containsKey(key)) {
                        // 计算复购券收益
                        Map<Long, List<AcRepurCouponsRuleConfig>> couponsListMap = couponsMap.get(key);
                        for (Long pkBaseId : couponsListMap.keySet()) {
                            for (AcRepurCouponsRuleConfig acRepurCouponsRuleConfig : couponsListMap.get(pkBaseId)) {
                                if (ComputeUtil.compareValue(saOrderExt.getOrderAchieve(), acRepurCouponsRuleConfig.getOrderAchieve())) {
                                    // 获得奖金
                                    CuMemberSettleExt targetMemberSettleExt = cuMemberSettleExtMap.get(saOrderExt.getPkReference());
                                    if (targetMemberSettleExt == null || validatePkBonusItems(bonusConfigDTO, targetMemberSettleExt.getPkSettleCountry(), EBonusItems.REPURCHASE_COUPONS.getValue())) {
                                        continue;
                                    }
                                    CuMemberBonus cuMemberBonus = getCuMemberBonus(period, cuMemberBonusMap, saOrderExt, targetMemberSettleExt);
                                    CuMemberBonusDetail cuMemberBonusDetail = getCuMemberBonusDetail(saOrderExt, targetMemberSettleExt, cuMemberBonus);
                                    // 计算类型
                                    cuMemberBonusDetail.setCalType(acRepurCouponsRuleConfig.getRatioType());
                                    // 计算值
                                    cuMemberBonusDetail.setCalValue(acRepurCouponsRuleConfig.getRatioValue());
                                    cuMemberBonusDetail.setPkBonusItems(getPkBonusItems(bonusConfigDTO, targetMemberSettleExt.getPkSettleCountry(), EBonusItems.REPURCHASE_COUPONS.getValue()));
                                    if (ECalType.RATIO.getValue() == cuMemberBonusDetail.getCalType()) {
                                        cuMemberBonusDetail.setPretaxIncome(ComputeUtil.computeBonusMultiply(saOrderExt.getOrderAchieve(), acRepurCouponsRuleConfig.getRatioValue()));
                                    } else {
                                        cuMemberBonusDetail.setPretaxIncome(acRepurCouponsRuleConfig.getRatioValue());
                                    }
                                    BdBonusDeduct bdBonusDeduct = setBonusDetailDeduct(bonusConfigDTO, cuMemberBonusDetail, saOrderExt.getOrderAmount(), saOrderExt.getInExchangeRate());
                                    if (EBonusIncomeStatus.NORMAL.getValue() == cuMemberBonusDetail.getIncomeStatus()) {
                                        cuMemberBonus.setRepurCoupon(ComputeUtil.computeAdd(cuMemberBonus.getRepurCoupon(), cuMemberBonusDetail.getPretaxIncome()));
                                        setRepurchaseCouponTotal(bdBonusDeduct, cuMemberBonus, cuMemberBonusDetail);
                                    }
                                    repurchaseCouponsRemark(saOrderExt, cuMemberBonusDetail, sourceMemberSettleExt, targetMemberSettleExt);
                                    cuMemberBonusDetailList.add(cuMemberBonusDetail);
                                    break;
                                }
                            }
                        }
                    }
                }
            }
        }
        return cuMemberBonusDetailList;
    }
}
