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 DataQueryAction extends DefaultAction {
    //查询按钮组件的默认数据(KEY)
    private static final String SYSTEM_SETTINGS_DS_QUERY_BUTTON_JSON="system.settings.ds.query.button.json";
    @Resource
    private BaseService baseService;
    @Resource
    private UserService userService;
    @Resource
    private ApiService apiService;
    @Resource
    private KHCRedisCache khcRedisCache;
    private static DataQueryAction 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> copy(String jsonStr, JackJSONObject retJson, HttpServletRequest request){
        JackJSONObject json= JackJSONObject.fromObject(jsonStr);
        Map<String,Object> map=new HashMap<>();
        String pageParentUuid = String.valueOf(json.get("page_parent_uuid"));
        String oldCode = String.valueOf(json.get("oldCode"));
        String newCode = String.valueOf(json.get("newCode"));
        String sql = "SELECT * FROM  tbl_data_query_info WHERE status=0 and page_parent_uuid='"+pageParentUuid+"'";
        List<Map<String, Object>> list = this.getJdbcTemplate().queryForList(sql);
        String queryUUIDSql="SELECT uuid FROM `tbl_data_query` WHERE status=0 and query_code= '"+newCode+"'";
        String queryUUID=this.getJdbcTemplate().queryForObject(queryUUIDSql,String.class);
        for (Map listInfo : list) {
            listInfo.remove("id");
            listInfo.put("page_parent_uuid", queryUUID);
            action.baseService.save("tbl_data_query_info", listInfo);
        }
        map.put("message","复制成功");
        return map;
    }

    @Override
    public JackJSONArray saveAfter(String jsonStr, JackJSONObject retJson, HttpServletRequest request, JackJSONArray array) {
        String parentUUID=String.valueOf(array.getJackJSONObject(0).get("uuid"));
        String sql="SELECT count(1) FROM tbl_data_button WHERE status=0 and page_parent_uuid='"+parentUUID+"'";
        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();//清除缓存的按钮信息
        }
        return array;
    }
}
