
package com.tmerclub.cloud.multishop.listener;

import cn.hutool.core.collection.CollUtil;
import com.tmerclub.cloud.api.order.feign.OrderFeignClient;
import com.tmerclub.cloud.api.payment.constant.PaySysType;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.order.bo.PayNotifyBO;
import com.tmerclub.cloud.common.order.vo.OrderSimpleAmountInfoBO;
import com.tmerclub.cloud.common.response.ResponseEnum;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.rocketmq.config.RocketMqConstant;
import com.tmerclub.cloud.common.vo.ShopSubstituteSalesVO;
import com.tmerclub.cloud.multishop.service.ShopSubstituteSalesService;
import com.tmerclub.cloud.multishop.service.ShopWalletService;
import com.tmerclub.cloud.multishop.vo.ShopWalletVO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
import org.apache.rocketmq.spring.core.RocketMQListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

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

/**
 * @author tmerclub
 */
@Component
@RocketMQMessageListener(topic = RocketMqConstant.ORDER_PURCHASE_NOTIFY_SHOP_TOPIC, consumerGroup = RocketMqConstant.ORDER_PURCHASE_NOTIFY_SHOP_TOPIC)
public class OrderPurchaseNotifyShopConsumer implements RocketMQListener<PayNotifyBO> {

    @Autowired
    private ShopWalletService shopWalletService;

    @Autowired
    private ShopSubstituteSalesService shopSubstituteSalesService;

    @DubboReference
    private OrderFeignClient orderFeignClient;


    /**
     * 订单支付成功 分账
     */
    @Override
    public void onMessage(PayNotifyBO message) {

        ServerResponseEntity<List<OrderSimpleAmountInfoBO>> ordersShopWalletInfoResponse = orderFeignClient.getOrdersSimpleAmountInfo(message.getOrderIds());
        if (!ordersShopWalletInfoResponse.isSuccess()) {
            throw new LuckException(ResponseEnum.EXCEPTION);
        }
        List<OrderSimpleAmountInfoBO> orderShopWalletInfos = ordersShopWalletInfoResponse.getData();
        List<Long> shopIds = orderShopWalletInfos.stream().map(OrderSimpleAmountInfoBO::getShopId).collect(Collectors.toList());
        List<Long> allinpayShopIds = orderShopWalletInfos.stream().filter(o -> Objects.equals(o.getPaySysType(), PaySysType.ALLINPAY.value())).map(OrderSimpleAmountInfoBO::getShopId).collect(Collectors.toList());
        List<Long> defaultShopIds = orderShopWalletInfos.stream().filter(o -> Objects.equals(o.getPaySysType(), PaySysType.DEFAULT.value())).map(OrderSimpleAmountInfoBO::getShopId).collect(Collectors.toList());
        // 获取店铺已结算金额map
        Map<String, Long> settledAmountMap = new HashMap<>(1);
        if (CollUtil.isNotEmpty(defaultShopIds)) {
            List<ShopWalletVO> shopWalletList = shopWalletService.listByShopIds(defaultShopIds, PaySysType.DEFAULT.value());
            for (ShopWalletVO shopWalletVO : shopWalletList) {
                settledAmountMap.put(shopWalletVO.getShopId() + ":" + PaySysType.DEFAULT.value(), shopWalletVO.getSettledAmount());
            }
        }
        if (CollUtil.isNotEmpty(allinpayShopIds)) {
            List<ShopWalletVO> allinpayList = shopWalletService.listByShopIds(allinpayShopIds, PaySysType.ALLINPAY.value());
            for (ShopWalletVO shopWalletVO : allinpayList) {
                settledAmountMap.put(shopWalletVO.getShopId() + ":" + PaySysType.ALLINPAY.value(), shopWalletVO.getSettledAmount());
            }
        }
        // 根据shopIds获取商家的代销配置
        List<ShopSubstituteSalesVO> shopSubstituteSalesList = shopSubstituteSalesService.listByShopIds(shopIds);
        Map<Long, Integer> salesMap = shopSubstituteSalesList.stream().collect(Collectors.toMap(ShopSubstituteSalesVO::getShopId, ShopSubstituteSalesVO::getOrderShape));
        shopWalletService.addAmountByOrder(orderShopWalletInfos, message, settledAmountMap, salesMap);
    }
}
