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

import com.hzs.common.core.utils.ComputeUtil;
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.springframework.stereotype.Component;

import java.util.*;

/**
 * @Description: 计算商城订单
 * @Author: sui q
 * @Time: 2023/2/10 17:56
 * @Classname: BonusSettleMallHandle
 * @PackageName: com.hzs.member.bonus.service.impl
 */
@Component
public class BonusSettleMallHandle extends BonusSettleHandle {

    /**
     * @Description: 计算商城业绩，用于计算商城业绩
     * @return: void
     * @Author: sui q
     * @Date: 2022/11/10 14:17
     */
    void calculateMallBonus(Map<Long, CuMemberSettleExt> cuMemberSettleExtMap, String settleTableName, List<SaOrderExt> mallSaOrderList, BonusConfigDTO bonusConfigDTO) {
        List<SaOrderExt> queryMallSaOrderList = new ArrayList<>();
        mallSaOrderList.forEach(saOrderExt -> {
            if (!cuMemberSettleExtMap.containsKey(saOrderExt.getPkMember())) {
                queryMallSaOrderList.add(saOrderExt);
            }
        });
        if (queryMallSaOrderList.size() > 0) {
            List<CuMemberSettleExt> cuMemberSettleExtList = iCuMemberTreeService.batchQueryMemberSettleBySaOrder(settleTableName, queryMallSaOrderList);
            getMemberSettle(cuMemberSettleExtMap, cuMemberSettleExtList);
        }
        mallSaOrderList.forEach(saOrderExt -> {
            CuMemberSettleExt cuMemberSettleExt = cuMemberSettleExtMap.get(saOrderExt.getPkMember());
            if (cuMemberSettleExt == null) {
                return;
            }
            cuMemberSettleExt.setMallAmount(ComputeUtil.computeAdd(cuMemberSettleExt.getMallAmount(), convertLocalCurrency(saOrderExt, bonusConfigDTO)));
            cuMemberSettleExt.setMallPv(ComputeUtil.computeAdd(cuMemberSettleExt.getMallPv(), saOrderExt.getOrderAchieve()));
            cuMemberSettleExt.setMallNewAmount(ComputeUtil.computeAdd(cuMemberSettleExt.getMallNewAmount(), convertLocalCurrency(saOrderExt, bonusConfigDTO)));
            cuMemberSettleExt.setMallNewPv(ComputeUtil.computeAdd(cuMemberSettleExt.getMallNewPv(), saOrderExt.getOrderAchieve()));
        });
    }


}
