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.JackJSONArray;
import cn.com.capcloud.ccui.core.common.json.JackJSONObject;
import cn.com.capcloud.core.cache.BaseConfigStaticCache;
import cn.com.capcloud.core.noentitydb.BaseService;
import cn.com.capcloud.core.noentitydb.DefaultAction;
import cn.com.capcloud.core.quartz.task.TaskService;
import cn.com.capcloud.core.websocket.RedisPublication;
import cn.com.capcloud.core.system.user.User;
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 DataTaskAction extends DefaultAction {
    @Resource
    private BaseService baseService;
    @Resource
    private TaskService taskService;
    @Resource
    private ApiService apiService;
    @Resource
    private UserService userService;
    @Resource
    private RedisPublication redisPublication;
    private static DataTaskAction action;
    @PostConstruct
    public void init(){
        action=this;
        action.baseService=this.baseService;
        action.taskService=this.taskService;
        action.apiService =this.apiService;
        action.redisPublication =this.redisPublication;
    }
    public JackJSONArray taskSaveBefore(String jsonStr, JackJSONObject retJson, HttpServletRequest request, JackJSONArray maps) {
        //保存之前校验自定义表达式
        String taskCronSelf=String.valueOf(maps.getJackJSONObject(0).get("task_cron_self"));
        if(!"null".equals(taskCronSelf)&&taskCronSelf.length()>0){
            if(action.taskService.checkTaskCron(taskCronSelf).equals("")){
                retJson.putAll(JackJSONObject.fromObject(action.apiService.getReturnDataByCode("7040")));
                retJson.put("message",retJson.get("message")+" task_cron_self："+taskCronSelf);
                return new JackJSONArray();
            }
        }
        return super.saveAfter(jsonStr, retJson, request, maps);
    }
    public JackJSONArray taskSaveAfter(String jsonStr, JackJSONObject retJson, HttpServletRequest request, JackJSONArray maps) {
        BaseConfigStaticCache.getRedisCache().deleteAllTaskInfo();
        BaseConfigStaticCache.getRedisCache().getTaskInfoByBeanClass();
        return super.saveAfter(jsonStr, retJson, request, maps);
    }

    @Override
    public String buildSql(String jsonStr, JackJSONObject retJson, HttpServletRequest request) {
        /*this.getJdbcTemplate().execute("update tbl_data_task set task_bean_status=''");//重置所有状态
        action.redisPublication.sendUpdateTaskStatus();*/
        return super.buildSql(jsonStr, retJson, request);
    }

    public Map<String,Object> startTask(String jsonStr, JackJSONObject retJson, HttpServletRequest request){
        JackJSONObject json= JackJSONObject.fromObject(jsonStr);
        String uuid=String.valueOf(json.get("uuid"));
        Map<String,Object> map=new HashMap<>();
        User user=action.userService.getUser(request);
        action.redisPublication.sendTask(uuid,"start",String.valueOf(user.getId()));
        map.put("message","启动成功");
        return map;
    }

    public Map<String,Object> stopTask(String jsonStr, JackJSONObject retJson, HttpServletRequest request){
        JackJSONObject json= JackJSONObject.fromObject(jsonStr);
        String uuid=String.valueOf(json.get("uuid"));
        Map<String,Object> map=new HashMap<>();
        User user=action.userService.getUser(request);
        action.redisPublication.sendTask(uuid,"stop",String.valueOf(user.getId()));
        map.put("message","停止成功");
        return map;
    }

    public Map<String,Object> restartTask(String jsonStr, JackJSONObject retJson, HttpServletRequest request){
        JackJSONObject json= JackJSONObject.fromObject(jsonStr);
        String uuid=String.valueOf(json.get("uuid"));
        Map<String,Object> map=new HashMap<>();
        User user=action.userService.getUser(request);
        action.redisPublication.sendTask(uuid,"restart",String.valueOf(user.getId()));
        map.put("message","重启成功");
        return map;
    }

    public Map<String,Object> startTaskOne(String jsonStr, JackJSONObject retJson, HttpServletRequest request){
        JackJSONObject json= JackJSONObject.fromObject(jsonStr);
        String uuid=String.valueOf(json.get("uuid"));
        Map<String,Object> map=new HashMap<>();
        User user=action.userService.getUser(request);
        action.redisPublication.sendTask(uuid,"startNow",String.valueOf(user.getId()));
        map.put("message","启动成功");
        return map;
    }
}
