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

import cn.com.capcloud.api.action.service.ApiDao;
import cn.com.capcloud.api.action.service.ApiService;
import cn.com.capcloud.api.action.service.util.DictUtil;
import cn.com.capcloud.ccui.core.common.json.JackJSONArray;
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.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.*;

@Component
public class DataTreeAction extends DefaultAction {
    @Autowired
    private BaseService baseService;
    @Autowired
    private UserService userService;
    @Autowired
    private ApiService apiService;
    @Autowired
    private KHCRedisCache khcRedisCache;
    private static DataTreeAction action;
    @PostConstruct
    public void init(){
        action=this;
        action.baseService=this.baseService;
        action.userService=this.userService;
        action.apiService =this.apiService;
        action.khcRedisCache =this.khcRedisCache;
    }

    public Map<String,Object> reFunction(String jsonStr, JackJSONObject retJson, HttpServletRequest request){
        JackJSONObject json= JackJSONObject.fromObject(jsonStr);
        String treeCode=String.valueOf(json.get("tree_code"));
        Map<String,Object> treeInfo=this.getJdbcTemplate().queryForMap("SELECT * FROM tbl_data_tree WHERE status=0 and tree_code='"+treeCode+"'");
        DictUtil.initTreeFunction(treeInfo,this.getJdbcTemplate());
        Map<String,Object> map=new HashMap<>();
        map.put("message","重置成功");
        return map;
    }

    @Override
    public JackJSONArray saveAfter(String jsonStr, JackJSONObject retJson, HttpServletRequest request, JackJSONArray array) {
        action.khcRedisCache.deleteAllTree();//清除缓存的tree字典信息
        return super.saveAfter(jsonStr, retJson, request, array);
    }

    @Override
    public String deleteAfter(String jsonStr, JackJSONObject retJson, HttpServletRequest request) {
        action.khcRedisCache.deleteAllTree();//清除缓存的tree字典信息
        return super.deleteAfter(jsonStr, retJson, request);
    }
}

