
package com.tmerclub.cloud.combo.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import com.tmerclub.cloud.api.order.feign.OrderFeignClient;
import com.tmerclub.cloud.combo.service.ComboOrderService;
import com.tmerclub.cloud.combo.service.ComboService;
import com.tmerclub.cloud.common.constant.Constant;
import com.tmerclub.cloud.common.exception.LuckException;
import com.tmerclub.cloud.common.order.constant.OrderActivityType;
import com.tmerclub.cloud.common.order.vo.OrderActivityVO;
import com.tmerclub.cloud.common.product.dto.ComboDTO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 套餐订单表
 *
 * @author tmerclub
 * @date 2022-08-16 09:18:06
 */
@Service
public class ComboOrderServiceImpl implements ComboOrderService {

    @Autowired
    private ComboService comboService;
    @DubboReference
    private OrderFeignClient orderFeignClient;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void comboOrderPaySucceed(List<Long> orderIds) {
        if (CollUtil.isEmpty(orderIds)) {
            throw new LuckException("订单id不能为空");
        }
        //套餐订单变为已支付
        List<OrderActivityVO> orderActivityVOList = orderFeignClient.getActivitySalesQuantity(orderIds, OrderActivityType.COMBO.value());
        if (CollUtil.isEmpty(orderActivityVOList)) {
            return;
        }
        //套餐销量增加
        Map<Long, ComboDTO> map = new HashMap<>(Constant.INITIAL_CAPACITY);
        for (OrderActivityVO orderActivityVO : orderActivityVOList) {
            ComboDTO combo = new ComboDTO();
            combo.setComboId(orderActivityVO.getActivityId());
            combo.setSoldNum(orderActivityVO.getCount());
            map.put(orderActivityVO.getActivityId(), combo);
        }

        if (MapUtil.isNotEmpty(map)) {
            comboService.comboSoldNumAdd(new ArrayList<>(map.values()));
        }
    }
}
