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.cache.KHCRedisCache;
import cn.com.capcloud.core.noentitydb.BaseService;
import cn.com.capcloud.core.noentitydb.DBCheck;
import cn.com.capcloud.core.noentitydb.DefaultAction;
import cn.com.capcloud.core.system.security.MyUserDetails;
import cn.com.capcloud.core.system.user.User;
import cn.com.capcloud.core.system.user.UserService;
import cn.com.capcloud.core.websocket.RedisPublication;
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 DataOnlineUserAction extends DefaultAction {
    @Resource
    private BaseService baseService;
    @Resource
    private UserService userService;
    @Resource
    private ApiService apiService;
    @Resource
    private KHCRedisCache khcRedisCache;
    @Resource
    private MyUserDetails myUserDetails;
    @Resource
    private RedisPublication redisPublication;
    private static DataOnlineUserAction action;
    @PostConstruct
    public void init(){
        action=this;
        action.baseService=this.baseService;
        action.userService=this.userService;
        action.apiService =this.apiService;
        action.khcRedisCache =this.khcRedisCache;
        action.myUserDetails=this.myUserDetails;
        action.redisPublication=this.redisPublication;
    }

    public String onlineUserBuildSql(String jsonStr, JackJSONObject retJson, HttpServletRequest request) {
        String sql=super.buildSql(jsonStr, retJson, request);
        //查询两小时内的数据（因为登录令牌存储在redis两小时失效）
        if(DBCheck.isKingbase()){
            sql+=" and update_time > (select NOW()-INTERVAL '2' HOUR)";//Kingbase写法有点不同
        }else{
            sql+=" and update_time > NOW()-INTERVAL 2 HOUR";//主流数据库写法
        }
        return sql;
    }

    public Map<String,Object> forceExit(String jsonStr, JackJSONObject retJson, HttpServletRequest request){
        JackJSONObject json= JackJSONObject.fromObject(jsonStr);
        String userId= String.valueOf(json.get("userId"));
        User user=action.userService.getUser(Long.valueOf(userId));
        action.myUserDetails.getUserInfoMap().remove(user.getUserLoginName());
        action.khcRedisCache.deleteMenuByUserId(user.getId());
        if(user.getIsMultiDeviceLogin().equals("N")){
            action.userService.deleteToken(user.getUserLoginName());
            //强退成功后将用户在线表信息更新
            this.getJdbcTemplate().execute("update tbl_data_online_user set status=1 where create_user_id='"+user.getId()+"'");
        }else{
            //多设备登录用户
            String token= String.valueOf(json.get("token"));
            action.userService.deleteToken(token);
            //强退成功后将用户在线表信息更新
            this.getJdbcTemplate().execute("update tbl_data_online_user set status=1 where token='"+token+"'");
        }
        //这里可以使用websocket广播消息
        Map<String,Object> map=new HashMap<>();
        map.put("message","强退成功");
        return map;
    }

    public Map<String,Object> sendMessageByUser(String jsonStr, JackJSONObject retJson, HttpServletRequest request){
        JackJSONObject json= JackJSONObject.fromObject(jsonStr);
        String userId= String.valueOf(json.get("userId"));
        String message= String.valueOf(json.get("message"));
        action.redisPublication.sendMessage("{\"wsCode\":\"testShowMessage\",\"message\":\""+message+"\"}",String.valueOf(userId),3000);
        //这里可以使用websocket广播消息
        Map<String,Object> map=new HashMap<>();
        map.put("message","操作成功");
        return map;
    }

}
