package com.mingzuozhibi.modules.core;

import com.google.gson.JsonObject;
import com.mingzuozhibi.commons.base.BaseSupport;
import com.mingzuozhibi.commons.mylog.JmsEnums.Name;
import com.mingzuozhibi.commons.mylog.JmsEnums.Type;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.annotation.JmsListener;
import org.springframework.stereotype.Component;

import java.time.Instant;

@Slf4j
@Component
public class MessageListener extends BaseSupport {

    @Autowired
    private MessageRepository messageRepository;

    @JmsListener(destination = "listenJmsLog")
    public void listenJmsLog(String json) {
        Message message = gson.fromJson(json, Message.class);
        messageRepository.save(message.withAccept());
    }

    @JmsListener(destination = "module.message")
    public void moduleMessage(String json) {
        JsonObject root = gson.fromJson(json, JsonObject.class);
        JsonObject data = root.get("data").getAsJsonObject();
        Message message = new Message();
        setName(root.get("name").getAsString(), message);
        setType(data.get("string").getAsString(), message);
        message.setText(data.get("text").getAsString());
        message.setCreateOn(Instant.ofEpochMilli(data.get("createOn").getAsLong()));
        messageRepository.save(message.withAccept());
        log.debug("JMS <- module.message [name={}, data={}]", root.get("name").getAsString(), data);
    }

    private void setType(String type, Message message) {
        switch (type) {
            case "info":
                message.setType(Type.INFO);
                break;
            case "notify":
                message.setType(Type.NOTIFY);
                break;
            case "success":
                message.setType(Type.SUCCESS);
                break;
            case "warning":
                message.setType(Type.WARNING);
                break;
            case "danger":
                message.setType(Type.ERROR);
                break;
            default:
                message.setType(Type.DEBUG);
                break;
        }
    }

    private void setName(String name, Message message) {
        switch (name) {
            case "mzzb_disc_spider":
                message.setName(Name.SPIDER_CONTENT);
                break;
            case "mzzb_disc_shelfs":
                message.setName(Name.SPIDER_HISTORY);
                break;
            default:
                message.setName(Name.DEFAULT);
                break;
        }
    }

}

@@ -753,53 +753,447 @@
     
-messageRepository.save(message.withAccept());
+saveMessage(message);%0A    %7D%0A%0A    private void saveMessage(Message message) %7B%0A        try %7B%0A            if (message.getText().length() %3E 1000) %7B%0A                message.setText(message.getText().substring(0, 1000));%0A                log.info(%22saveMessage(%7B%7D)%22, message);%0A            %7D%0A            messageRepository.save(message.withAccept());%0A        %7D catch (Exception e) %7B%0A            log.warn(%22saveMessage(%7B%7D): %7B%7D%22, message, e);%0A        %7D
 %0A   
@@ -1726,37 +1726,26 @@
         
-messageRepository.sav
+saveMessag
 e(messag
@@ -1741,37 +1741,24 @@
 sage(message
-.withAccept()
 );%0A        l

check message text length