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.api.action.service.util.ReflectionUtil;
import cn.com.capcloud.api.action.service.util.TextUtil;
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.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class DataChartsAction extends DefaultAction {
    @Autowired
    private BaseService baseService;
    @Autowired
    private UserService userService;
    @Autowired
    private KHCRedisCache khcRedisCache;
    @Autowired
    private ApiService apiService;
    private static DataChartsAction 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> getChartsInfo(String jsonStr, JackJSONObject retJson, HttpServletRequest request){
        JackJSONObject json= JackJSONObject.fromObject(jsonStr);
        Map<String,Object> retMap=new HashMap<>();
        Map<String,Object> map=new HashMap<>();
        String chartsCode= String.valueOf(json.get("chartsCode"));
        String sql="select * from tbl_data_charts where charts_code='"+chartsCode+"'";
        List<Map<String, Object>> list= this.getJdbcTemplate().queryForList(sql);
        String chartsDataType=String.valueOf(list.get(0).get("charts_data_type"));
        if(!chartsDataType.equals("null")&&chartsDataType.equals("2")){//1 静态json， 2 sql查询库
            String chartsSql=String.valueOf(list.get(0).get("d_sql"));
            if(chartsSql.equals("")){
                retJson.put("status","no_code");
                retJson.put("message","数据源为sql获取，当前sql配置为空（"+chartsCode+"）");
                return new HashMap<>();
            }
            chartsSql= TextUtil.htmlDecode(chartsSql);
            List<Map<String, Object>> listMap=this.getJdbcTemplate().queryForList(chartsSql);
            list.get(0).put("data",listMap);
        }
        if(!chartsDataType.equals("null")&&chartsDataType.equals("3")){//3 扩展类
            String className=String.valueOf(list.get(0).get("my_action_class"));
            String methodName=String.valueOf(list.get(0).get("method_name"));
            Map<String, Object> valueMap= ReflectionUtil.mySelfMethodRetMap(className,methodName,jsonStr,retJson,request);
            list.get(0).put("data",valueMap.get("data"));
            //如果使用的是sql，这里需要执行sql查询
        }
        String radioDict=String.valueOf(list.get(0).get("radio_dict"));//字典编码不为空则将字典编码转换
        if(!radioDict.equals("null")&&radioDict.length()>0){
            if(radioDict.indexOf("tree_")==0){
                list.get(0).put("radio_dict", action.khcRedisCache.getTreeInfoByTreeCodeToList(radioDict,"","",null));
            }else if(radioDict.indexOf("o_")==0){//外部字典不放缓存。
                list.get(0).put("radio_dict", DictUtil.getOutDictInfoByDictCodeToList(radioDict,this.getJdbcTemplate()));
            }else{
                List<Map<String, Object>> dictList=action.khcRedisCache.getDictInfoByDictCodeToList(radioDict);
                list.get(0).put("radio_dict",dictList);
            }
        }
        retMap.put("message","查询成功");
        retMap.put("chartsInfo",list.get(0));
        return retMap;
    }
}

