
package com.tmerclub.cloud.group.listener;

import cn.hutool.core.collection.CollUtil;
import com.tmerclub.cloud.api.group.bo.GroupOrderNotifyBO;
import com.tmerclub.cloud.api.group.vo.GroupOrderVO;
import com.tmerclub.cloud.common.rocketmq.config.RocketMqConstant;
import com.tmerclub.cloud.common.util.BeanUtil;
import com.tmerclub.cloud.group.bo.mongo.MongoGroupOrderBO;
import com.tmerclub.cloud.group.service.GroupOrderService;
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.List;
import java.util.Objects;

/**
 * 拼团订单支付成功 更新订单保存到mongodb
 * @author TRACK
 */
@Component
@RocketMQMessageListener(topic = RocketMqConstant.GROUP_ORDER_SUCCESS_TOPIC, consumerGroup = RocketMqConstant.GROUP_ORDER_SAVE_GROUP)
public class GroupOrderSaveGroupConsumer implements RocketMQListener<GroupOrderNotifyBO> {

    @Autowired
    private GroupOrderService groupOrderService;

    /**
     * 团购活动订单保存mongodb
     */
    @Override
    public void onMessage(GroupOrderNotifyBO groupOrderNotifyBO) {
        if (CollUtil.isNotEmpty(groupOrderNotifyBO.getGroupOrders())) {
            if (Objects.equals(groupOrderNotifyBO.getIsJoin(), 0)) {
                // 机器人参团
                List<MongoGroupOrderBO> mongoGroupOrderBOList = BeanUtil.mapAsList(groupOrderNotifyBO.getGroupOrders(), MongoGroupOrderBO.class);
                groupOrderService.saveBatchToMongodb(mongoGroupOrderBOList);
            } else if (Objects.equals(groupOrderNotifyBO.getIsJoin(), 1)) {
                GroupOrderVO groupOrderVO = groupOrderNotifyBO.getGroupOrders().get(0);
                // 用户参团
                groupOrderService.updateToPaySuccess(groupOrderVO.getOrderId(), null, null);
            }
        }
    }
}
