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 javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Component
public class DataInfoReleaseAction extends DefaultAction {
    @Resource
    private BaseService baseService;
    @Resource
    private UserService userService;
    @Resource
    private KHCRedisCache khcRedisCache;
    @Resource
    private ApiService apiService;
    @Resource
    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;
    }
}
