package com.bweb.config.msg;

import com.hai.enum_type.MerStoreAmountTypeEnum;
import com.hai.msg.entity.MsgTopic;
import com.hai.service.HighMerchantStoreAccountService;
import com.hai.service.HighOrderService;
import org.apache.commons.collections4.MapUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Component
public class MerStoreAccountChgHandler {

    private static Logger logger = LoggerFactory.getLogger(MerStoreAccountChgHandler.class);

    @Autowired
    private RedisTemplate<String, Object> redisTemplate;

    @Resource
    private HighMerchantStoreAccountService merchantStoreAccountService;

    @Resource
    private HighOrderService orderService;

    @PostConstruct
    public void init() {
        new Thread(() -> {
            {
                //消息处理
                while (true){
                    try {
                        //构建的队列为左进右出
                        Object o = redisTemplate.opsForList().rightPop(MsgTopic.MerStoreAccount.getName());
                        if (o != null) {
                            //处理消息
                            logger.info("消息通道："+o);

                            Map<String, Object> param = (Map<String, Object>) o;

                            if (MapUtils.getInteger(param, "businessType").equals(MerStoreAmountTypeEnum.type1.getType())) {
                                Map<String, Object> otherParam = new HashMap<>();
                                otherParam.put("sourceType", MapUtils.getString(param, "sourceType"));
                                otherParam.put("sourceId", MapUtils.getString(param, "sourceId"));
                                otherParam.put("sourceContent", MapUtils.getString(param, "sourceContent"));
                                otherParam.put("opUserId", MapUtils.getLong(param, "opUserId"));
                                otherParam.put("opUserName", MapUtils.getString(param, "opUserName"));

                                merchantStoreAccountService.recharge(
                                        MapUtils.getLong(param, "storeId"),
                                        new BigDecimal(MapUtils.getString(param, "price")),
                                        otherParam);
                            }

                            if (MapUtils.getInteger(param, "businessType").equals(MerStoreAmountTypeEnum.type2.getType())) {
                                Map<String, Object> otherParam = new HashMap<>();
                                otherParam.put("sourceType", MapUtils.getString(param, "sourceType"));
                                otherParam.put("sourceId", MapUtils.getString(param, "sourceId"));
                                otherParam.put("sourceContent", MapUtils.getString(param, "sourceContent"));
                                otherParam.put("opUserId", MapUtils.getLong(param, "opUserId"));
                                otherParam.put("opUserName", MapUtils.getString(param, "opUserName"));

                                merchantStoreAccountService.consume(
                                        MapUtils.getLong(param, "storeId"),
                                        new BigDecimal(MapUtils.getString(param, "price")),
                                        otherParam);
                            }

                        } else {
                            Thread.sleep(1000);
                        }
                    }catch (Exception e){
                        logger.error("监听订单状态变更错误",e);
                    }
                }
            }
        }).start();
    }
}
