package cn.com.capcloud.api.action.myself.system.data;

import cn.com.capcloud.api.action.service.ApiService;
import cn.com.capcloud.ccui.core.common.json.JackJSONObject;
import cn.com.capcloud.core.websocket.RedisPublication;
import cn.com.capcloud.core.cache.KHCRedisCache;
import cn.com.capcloud.core.noentitydb.BaseService;
import cn.com.capcloud.core.noentitydb.DefaultAction;
import cn.com.capcloud.core.system.user.UserService;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import org.springframework.beans.factory.annotation.Autowired;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Component
public class DataInfoReleaseAction extends DefaultAction {
    @Autowired
    private BaseService baseService;
    @Autowired
    private UserService userService;
    @Autowired
    private KHCRedisCache khcRedisCache;
    @Autowired
    private ApiService apiService;
    @Autowired
    private RedisPublication redisPublication;
    private static DataInfoReleaseAction action;
    @PostConstruct
    public void init(){
        action=this;
        action.baseService=this.baseService;
        action.userService=this.userService;
        action.apiService =this.apiService;
        action.khcRedisCache =this.khcRedisCache;
        action.redisPublication =this.redisPublication;
    }
    public Map<String,Object> publish(String jsonStr, JackJSONObject retJson, HttpServletRequest request){
        JackJSONObject json= JackJSONObject.fromObject(jsonStr);
        JackJSONObject content=json.getJackJSONObject("content");
        String[] toUserIds=String.valueOf(content.get("to_user_id")).split(",");
        for (int i = 0; i < toUserIds.length; i++) {
            String message="【信息发布】所属栏目名称："+content.get("column_name")+"，栏目标题："+content.get("title");
            action.redisPublication.sendMessage(message,toUserIds[i]);
        }
        Map<String,Object> map=new HashMap<>();
        map.put("message","启动成功");
        return map;
    }
}

