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.JackJSON;
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.cache.BaseConfigStaticCache;
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.*;

@Component
public class DataButtonAction extends DefaultAction {
    //查询按钮组件的默认数据(KEY)
    private static final String SYSTEM_SETTINGS_DS_QUERY_BUTTON_JSON="system.settings.ds.query.button.json";
    //表单按钮组件的默认数据(KEY)
    private static final String SYSTEM_SETTINGS_DS_FORM_BUTTON_JSON="system.settings.ds.form.button.json";
    @Resource
    private BaseService baseService;
    @Resource
    private UserService userService;
    @Resource
    private ApiService apiService;
    @Resource
    private KHCRedisCache khcRedisCache;
    private static DataButtonAction 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> initButton(String jsonStr, JackJSONObject retJson, HttpServletRequest request){
        JackJSONObject json= JackJSONObject.fromObject(jsonStr);
        Map<String,Object> map=new HashMap<>();
        String parentUUID=String.valueOf(json.getJackJSONObject("queryParam").get("page_parent_uuid"));
        String sql="SELECT count(1) FROM tbl_data_button WHERE status=0 and page_parent_uuid='"+parentUUID+"'";
        String queryCountSql="select count(1) from tbl_data_query where uuid='"+parentUUID+"'";
        Integer queryCount=this.getJdbcTemplate().queryForObject(queryCountSql,Integer.class);
        if(queryCount>0){
            List<Map<String,Object>> list= JackJSON.parseObject(String.valueOf(BaseConfigStaticCache.get(SYSTEM_SETTINGS_DS_QUERY_BUTTON_JSON)), ArrayList.class);
            for (int i = 0; i < list.size(); i++) {
                Integer count=this.getJdbcTemplate().queryForObject(sql+" and button_method='"+list.get(i).get("button_method")+"'",Integer.class);
                if(count>0){
                    continue;
                }
                String uuid= UUID.randomUUID().toString().replaceAll("-","");
                list.get(i).put("uuid",uuid);
                list.get(i).put("page_parent_uuid",parentUUID);
                action.baseService.save("tbl_data_button", list.get(i));
                action.khcRedisCache.deleteAllButton();//清除缓存的按钮信息
            }
        }
        String formCountSql="select count(1) from tbl_data_form where uuid='"+parentUUID+"'";
        Integer formCount=this.getJdbcTemplate().queryForObject(formCountSql,Integer.class);
        if(formCount>0){
            List<Map<String,Object>> list= JackJSON.parseObject(String.valueOf(BaseConfigStaticCache.get(SYSTEM_SETTINGS_DS_FORM_BUTTON_JSON)), ArrayList.class);
            for (int i = 0; i < list.size(); i++) {
                Integer count=this.getJdbcTemplate().queryForObject(sql+" and button_method='"+list.get(i).get("button_method")+"'",Integer.class);
                if(count>0){
                    continue;
                }
                String uuid= UUID.randomUUID().toString().replaceAll("-","");
                list.get(i).put("uuid",uuid);
                list.get(i).put("page_parent_uuid",parentUUID);
                action.baseService.save("tbl_data_button", list.get(i));
                action.khcRedisCache.deleteAllButton();//清除缓存的按钮信息
            }
        }
        map.put("message","同步成功");
        return map;
    }

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

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