package com.liquidnet.service.goblin.util;

import com.liquidnet.common.cache.redis.util.RedisDataSourceUtil;
import com.liquidnet.commons.lang.util.CollectionUtil;
import com.liquidnet.service.base.constant.MQConst;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.stream.StreamRecords;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Component
public class QueueUtils {

    //    @Autowired
//    private RabbitTemplate rabbitTemplate;

    @Autowired
    private RedisDataSourceUtil redisDataSourceUtil;

    /**
     * 发送消息 - RABBIT
     *
     * @param exchange 交换机
     * @param routeKey 路径
     * @param jsonMsg  Json字符串
     */
//    public void sendMsgByRabbit(String exchange, String routeKey, String jsonMsg) {
//        rabbitTemplate.convertAndSend(exchange, routeKey, jsonMsg);
//    }

    /**
     * 发送消息 - REDIS
     *
     * @param streamKey Redis消费Key
     * @param jsonMsg   Json字符串
     */
    public void sendMsgByRedis(String streamKey, String jsonMsg) {
        HashMap<String, String> map = CollectionUtil.mapStringString();
        map.put(MQConst.QUEUE_MESSAGE_KEY, jsonMsg);
        redisDataSourceUtil.getRedisQueueUtil().getStringRedisTemplate().opsForStream().add(StreamRecords.mapBacked(map).withStreamKey(streamKey));
    }

    /**
     * 发送消息 - REDIS [XLS]
     *
     * @param xlsPath xls 对应的OSS 全量路径
     * @param type    [1-添加|2-删除|3-白名单|4-白名单]
     * @param skuId   skuId
     */
    public void sendMsgByRedisXls(String xlsPath, String type, String skuId) {
        if (xlsPath == null || xlsPath.equals("") || type == null || type.equals("") || skuId == null || skuId.equals("")) {

        } else {
            HashMap<String, String> map = CollectionUtil.mapStringString();
            map.put(MQConst.QUEUE_MESSAGE_KEY, xlsPath);
            map.put("type", type);
            map.put("skuId", skuId);
            redisDataSourceUtil.getRedisQueueUtil().getStringRedisTemplate().opsForStream().add(StreamRecords.mapBacked(map).withStreamKey(MQConst.GoblinQueue.GOBLIN_XLS_OPERA.getKey()));
        }
    }


    /**
     * 发送消息 - REDIS [XLS]
     *
     * @param nXlsPath xls 新地址
     * @param oXlsPath xls 旧地址
     * @param type     [1-添加|2-删除|3-白名单|4-白名单]
     * @param skuId    skuId
     */
    public void sendMsgByRedisXls(String listId,String nXlsPath, String oXlsPath, String type, String skuId) {
        HashMap<String, String> map = CollectionUtil.mapStringString();
        map.put(MQConst.QUEUE_MESSAGE_KEY, nXlsPath.concat(",").concat(oXlsPath));
        map.put("listId", listId);
        map.put("type", type);
        map.put("skuId", skuId);
        redisDataSourceUtil.getRedisQueueUtil().getStringRedisTemplate().opsForStream().add(StreamRecords.mapBacked(map).withStreamKey(MQConst.GoblinQueue.GOBLIN_XLS_OPERA.getKey()));
    }
}
