package com.gic.haoban.manage.web.controller.commission;

import com.alibaba.fastjson.JSON;
import com.gic.api.base.commons.ServiceResponse;
import com.gic.haoban.commission.api.dto.commissiondetail.CommissionSettleDetailDTO;
import com.gic.haoban.commission.api.qdto.commissiondetail.CommissionSettleOrderItemQDTO;
import com.gic.haoban.commission.api.qdto.commissiondetail.CommissionSettleOrderQDTO;
import com.gic.haoban.commission.api.service.local.CommissionSettleDetailApiService;
import com.gic.haoban.manage.web.controller.goods.GoodsInfoAdaptor;
import com.gic.store.goods.dto.goods.GoodsSpuInfoDTO;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Author MUSI
 * @Date 2023/6/16 4:30 PM
 * @Description
 * @Version
 * 佣金管理
 **/
@Slf4j
@Component
public class CommissionManager {

    @Autowired
    private GoodsInfoAdaptor goodsInfoAdaptor;
    @Autowired
    private CommissionSettleDetailApiService commissionSettleDetailApiService;

    /**
     * 查询商品佣金
     * @param enterpriseId
     * @param storeId
     * @param clerkId
     * @param goodsIds
     * @return
     */
    public Map<String, BigDecimal> calcGoodsCommission(String enterpriseId, String storeId, String clerkId, List<String> goodsIds) {

        if (CollectionUtils.isEmpty(goodsIds)) {
            return Collections.emptyMap();
        }
        if (StringUtils.isAnyBlank(storeId, clerkId, enterpriseId)) {
            log.info("查询商品佣金异常 参数缺失 storeId:{} clerkID:{}", storeId, clerkId);
            return Collections.emptyMap();
        }
        Map<String, GoodsSpuInfoDTO> minPriceMap = goodsInfoAdaptor.queryGoodsMinPrice(enterpriseId, goodsIds);
        if (MapUtils.isEmpty(minPriceMap)) {
            log.info("计算商品佣金 参数异常");
            return Collections.emptyMap();
        }
        List<CommissionSettleOrderItemQDTO> settleOrderItems = new ArrayList<>();
        for (Map.Entry<String, GoodsSpuInfoDTO> entry : minPriceMap.entrySet()) {
            String goodsId = entry.getKey();
            GoodsSpuInfoDTO goodsSpuInfoDTO = entry.getValue();
            CommissionSettleOrderItemQDTO commissionSettleOrderItemQDTO = new CommissionSettleOrderItemQDTO();
            commissionSettleOrderItemQDTO.setGoodsId(goodsId);
            commissionSettleOrderItemQDTO.setGoodsCode(goodsSpuInfoDTO.getGoodsCode());
            commissionSettleOrderItemQDTO.setGoodsNum(1);
            commissionSettleOrderItemQDTO.setGoodsPayAmount(goodsSpuInfoDTO.getMinPrice().doubleValue());
            settleOrderItems.add(commissionSettleOrderItemQDTO);
        }

        CommissionSettleOrderQDTO commissionSettleOrderQDTO = new CommissionSettleOrderQDTO();
        commissionSettleOrderQDTO.setIsOrder(0);
        commissionSettleOrderQDTO.setOrderChannel(1);
        commissionSettleOrderQDTO.setEnterpriseId(enterpriseId);
        commissionSettleOrderQDTO.setOrderStoreId(storeId);
        commissionSettleOrderQDTO.setOrderClerkId(clerkId);
        commissionSettleOrderQDTO.setItems(settleOrderItems);
        log.info("计算商品佣金参数: {}", JSON.toJSONString(commissionSettleOrderQDTO));
        ServiceResponse<List<CommissionSettleDetailDTO>> serviceResponse = commissionSettleDetailApiService.calcDetailByOrder(commissionSettleOrderQDTO);
        log.info("计算商品佣金结果: {}", JSON.toJSONString(serviceResponse));
        if (!serviceResponse.isSuccess() || CollectionUtils.isEmpty(serviceResponse.getResult())) {
            log.info("计算商品佣金, 商品佣金为空 {}", JSON.toJSONString(serviceResponse));
            return Collections.emptyMap();
        }
        return serviceResponse.getResult()
                .stream()
                .collect(Collectors.toMap(CommissionSettleDetailDTO::getGoodsId, item -> {
                    Double commissionAmount = item.getCommissionAmount();
                    return Optional.ofNullable(commissionAmount).map(BigDecimal::new).orElse(BigDecimal.ZERO);
                }, (v1, v2) -> v1));
    }
}
