package com.liquidnet.service.consumer.sweet.receiver;

import com.liquidnet.commons.lang.util.CollectionUtil;
import com.liquidnet.commons.lang.util.JsonUtils;
import com.liquidnet.service.base.constant.MQConst;
import com.liquidnet.service.consumer.sweet.service.impl.SweetWechatTemplateMsgServiceImpl;
import com.liquidnet.service.sweet.dto.param.SweetOpenSendMsgParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.connection.stream.MapRecord;
import org.springframework.data.redis.connection.stream.StreamRecords;
import org.springframework.stereotype.Component;

import java.util.HashMap;

@Slf4j
@Component
public class ConsumerSweetTemplateMsgReceiver extends AbstractSqlRedisReceiver {

    @Autowired
    SweetWechatTemplateMsgServiceImpl sweetWechatTemplateService;

    @Override
    protected String getRedisStreamKey() {
        return MQConst.SweetQueue.SWEET_TEMPLATE_MSG.getKey();
    }

    @Override
    protected String getRedisStreamGroup() {
        return MQConst.SweetQueue.SWEET_TEMPLATE_MSG.getGroup();
    }

    @Override
    public void onMessage(MapRecord<String, String, String> message) {
        log.debug("CONSUMER SQL[streamKey:{},messageId:{},stream:{},body:{}]",
                this.getRedisStreamKey(), message.getId(), message.getStream(), message.getValue());

        boolean result = this.consumerSqlDaoHandler(message.getValue().get("message"));
        log.info("CONSUMER SQL RESULT:{} ==> MESSAGE_ID:{}", result, message.getId());

        try {
            stringRedisTemplate.opsForStream().acknowledge(getRedisStreamGroup(), message);
            stringRedisTemplate.opsForStream().delete(this.getRedisStreamKey(), message.getId());
        } catch (Exception e) {
            log.error("#CONSUMER SQL RESULT:{} ==> DEL_REDIS_QUEUE_MSG_EXCEPTION[MESSAGE_ID:{},MSG:{}]", result, message.getId(), JsonUtils.toJson(message), e);
        } finally {
            try {
                stringRedisTemplate.opsForStream().acknowledge(getRedisStreamGroup(), message);
                stringRedisTemplate.opsForStream().delete(this.getRedisStreamKey(), message.getId());
            } catch (Exception ignored) {
            }
        }
    }

    private boolean consumerSqlDaoHandler(String msg) {
        Boolean aBoolean = false;
        try {
            SweetOpenSendMsgParam sweetOpenSendMsgParam = JsonUtils.fromJson(msg, SweetOpenSendMsgParam.class);
            if (sweetOpenSendMsgParam == null) {
                aBoolean = true;
            } else {
                aBoolean = sweetWechatTemplateService.openSendMsg(sweetOpenSendMsgParam);
            }
        } catch (Exception e) {
            log.error("CONSUMER SQL FAIL ==> {}", e.getMessage(), e);
        } finally {
            if (!aBoolean) {
                HashMap<String, String> map = CollectionUtil.mapStringString();
                map.put("message", msg);
                stringRedisTemplate.opsForStream().add(StreamRecords.mapBacked(map).withStreamKey(this.getRedisStreamKey()));
            }
        }
        return aBoolean;
    }
}
