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.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.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.List;
import java.util.Map;

@Component
public class DataSimpleCardAction extends DefaultAction {
    @Resource
    private BaseService baseService;
    @Resource
    private UserService userService;
    @Resource
    private KHCRedisCache khcRedisCache;
    @Resource
    private ApiService apiService;
    private static DataSimpleCardAction 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> getSimpleCardInfo(String jsonStr, JackJSONObject retJson, HttpServletRequest request){
        JackJSONObject json= JackJSONObject.fromObject(jsonStr);
        Map<String,Object> retMap=new HashMap<>();
        Map<String,Object> map=new HashMap<>();
        String simpleCardCode= String.valueOf(json.get("simpleCardCode"));
        String sql="select * from tbl_data_simple_card where simple_card_code='"+simpleCardCode+"'";
        List<Map<String, Object>> list= this.getJdbcTemplate().queryForList(sql);
        String chartsDataType=String.valueOf(list.get(0).get("simple_card_data_type"));
        if(!chartsDataType.equals("null")&&chartsDataType.equals("2")){//1 静态json， 2 sql查询库
            String chartsSql=String.valueOf(list.get(0).get("simple_card_sql"));
            if(chartsSql.equals("")){
                retJson.put("status","no_code");
                retJson.put("message","数据源为sql获取，当前sql配置为空（"+simpleCardCode+"）");
                return new HashMap<>();
            }
            chartsSql=chartsSql.replaceAll("&nbsp;"," ");
            chartsSql=chartsSql.replaceAll("&quot;","'");//todo 这个转换写一个通用方法
            User user=action.userService.getUser(request);
            chartsSql=chartsSql.replace("${org_id}",String.valueOf(user.getOrgId()));
            chartsSql=chartsSql.replace("${user_id}",String.valueOf(user.getId()));
            String str=this.getJdbcTemplate().queryForObject(chartsSql,String.class);
            list.get(0).put("data",str);
        }
        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= ApiDao.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){
            List<Map<String, Object>> dictList=action.khcRedisCache.getDictInfoByDictCodeToList(radioDict);
            list.get(0).put("radio_dict",dictList);
        }
        retMap.put("message","查询成功");
        retMap.put("simpleCardInfo",list.get(0));
        return retMap;
    }
}
