package cn.com.capcloud.core.cache;


import cn.com.capcloud.api.action.service.ApiDao;
import cn.com.capcloud.api.action.service.ApiService;
import cn.com.capcloud.core.system.user.User;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.dao.DataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Component
public class KHCRedisCache {
    @Resource
    private ApiService apiService;
    @Resource
    private JdbcTemplate jdbcTemplate;
    /**
     * 获取用户菜单信息，并放入缓存
     */
    @Cacheable(value = "getMenuByParentId",key="#userId", unless="#result.size()==0")
    public List<Map<String, Object>> getMenuByParentId(long userId, String parentId){
        return ApiDao.getMenuByParentId(userId,parentId,jdbcTemplate);
    }

    /**
     * 清除单个用户菜单信息(用户注销时可使用此方法)
     */
    @CacheEvict(value = "getMenuByParentId",key="#userId")
    public void deleteMenuByUserId(long userId){
    }
    /**
     * 清除全部缓存菜单信息
     */
    @CacheEvict(value = "getMenuByParentId", allEntries=true)
    public void deleteAllMenu(){
    }
    /**
     * 获取tree字典信息，并放入缓存
     */
    @Cacheable(value = "getTreeInfoByTreeCodeToList",key="#code+'_'+#pId+'_'+#userId", unless="#result.size()==0")
    public List<Map<String, Object>> getTreeInfoByTreeCodeToList(String code,String pId,String userId, User user){
        return ApiDao.getTreeInfoByTreeCodeToList(code,pId,user,jdbcTemplate);
    }
    /**
     * 清除全部tree字典信息
     */
    @CacheEvict(value = "getTreeInfoByTreeCodeToList", allEntries=true)
    public void deleteAllTree(){
    }

    /**
     * 获取内部字典信息，并放入缓存
     */
    @Cacheable(value = "getDictInfoByDictCodeToList",key="#code", unless="#result.size()==0")
    public List<Map<String, Object>> getDictInfoByDictCodeToList(String code){
        return ApiDao.getDictInfoByDictCodeToList(code,jdbcTemplate);
    }
    /**
     * 清除全部内部字典信息
     */
    @CacheEvict(value = "getDictInfoByDictCodeToList", allEntries=true)
    public void deleteAllDict(){
    }

    /**
     * 获取表单字段信息，并放入缓存
     */
    @Cacheable(value = "getFormInfoByUuid",key="#uuid", unless="#result.size()==0")
    public List<Map<String, Object>> getFormInfoByUuid(String uuid){
        String sql="SELECT * FROM tbl_data_form_info WHERE STATUS=0 AND page_parent_uuid='"+uuid+"' ORDER BY field_sort ASC ";
        List<Map<String, Object>> dataList = jdbcTemplate.queryForList(sql);
        return dataList;
    }
    /**
     * 根据formId和版本获取表单字段信息，并放入缓存
     */
    @Cacheable(value = "getFormInfoByUuid",key="#uuid+'_'+#version", unless="#result.size()==0")
    public List<Map<String, Object>> getFormInfoByUuidAndVersion(String uuid,String version){
        String sql="SELECT * FROM tbl_data_form_info_h WHERE STATUS=0 AND page_parent_uuid='"+uuid+"' and form_version="+version+" ORDER BY field_sort ASC ";
        List<Map<String, Object>> dataList = jdbcTemplate.queryForList(sql);
        return dataList;
    }
    /**
     * 清除全部表单字段信息
     */
    @CacheEvict(value = "getFormInfoByUuid", allEntries=true)
    public void deleteAllFormInfo(){
    }

    /**
     * 获取查询组件字段信息，并放入缓存
     */
    @Cacheable(value = "getQueryInfoByUuid",key="#uuid", unless="#result.size()==0")
    public List<Map<String, Object>> getQueryInfoByUuid(String uuid){
        String sql="SELECT * FROM tbl_data_query_info WHERE STATUS=0 AND page_parent_uuid='"+uuid+"' ORDER BY field_sort ASC ";
        List<Map<String, Object>> dataList = jdbcTemplate.queryForList(sql);
        return dataList;
    }
    /**
     * 清除全部查询组件字段信息
     */
    @CacheEvict(value = "getQueryInfoByUuid", allEntries=true)
    public void deleteAllQueryInfo(){
    }

    /**
     * 获取查询组件字段信息，并放入缓存
     */
    @Cacheable(value = "getButtonByParentUUID",key="#parentUUID+#whereSql", unless="#result.size()==0")
    public List<Map<String, Object>> getButtonByParentUUID(String parentUUID,String whereSql){
        return ApiDao.getButtonByParentUUID(parentUUID,whereSql,jdbcTemplate);
    }
    /**
     * 清除全部按钮信息
     */
    @CacheEvict(value = "getButtonByParentUUID", allEntries=true)
    public void deleteAllButton(){
    }
    /**
     * 获取配置表里的配置信息，并放入缓存
     */
    @Cacheable(value = "getBaseConfigByCode",key="#code", unless="#result == null")
    public String getBaseConfigByCode(String code){
        String sql="select base_value from tbl_data_base_config where status=0 and base_status=0 and base_code='"+code+"'";
        String value= null;
        try {
            value = jdbcTemplate.queryForObject(sql,String.class);
        } catch (DataAccessException e) {
            value="";
        }
        value=value.replaceAll("&nbsp;"," ");
        return value;
    }
    /**
     * 获取任务信息，并放入缓存
     */
    @Cacheable(value = "getTaskInfoByUUID",key="#uuid", unless="#result == null")
    public Map<String,Object> getTaskInfoByUUID(String uuid){
        String sql="select * from tbl_data_task where status=0 and uuid='"+uuid+"'";
        Map<String,Object> taskInfo;
        try {
            taskInfo = jdbcTemplate.queryForMap(sql);
        } catch (DataAccessException e) {
            taskInfo=new HashMap<>();
        }
        return taskInfo;
    }
    /**
     * 清除全部缓存的配置
     */
    @CacheEvict(value = "getTaskInfoByUUID", allEntries=true)
    public void deleteAllTaskInfo(){
    }
    /**
     * 获取任务信息，并放入缓存
     */
    @Cacheable(value = "getTaskInfoByBeanClass",key="#beanClass", unless="#result == null")
    public Map<String,Object> getTaskInfoByBeanClass(String beanClass){
        String sql="select * from tbl_data_task where status=0 and task_bean_class='"+beanClass+"'";
        Map<String,Object> taskInfo;
        try {
            taskInfo = jdbcTemplate.queryForMap(sql);
        } catch (DataAccessException e) {
            taskInfo=new HashMap<>();
        }
        return taskInfo;
    }
    /**
     * 清除全部缓存的配置
     */
    @CacheEvict(value = "getTaskInfoByBeanClass", allEntries=true)
    public void getTaskInfoByBeanClass(){
    }
    /**
     * 清除全部缓存的配置
     */
    @CacheEvict(value = "getBaseConfigByCode", allEntries=true)
    public void deleteAllBaseConfig(){
    }

    //设置getTestStr过期时间为30秒，根据业务和场景可调整过期时间
    @Cacheable(value = "getTestStr#30") //value =${前缀}#${过期时间(秒)}
    public String getTestStr(){
        return "test ttl str";
    }

    //登录获取验证码5分钟
    @Cacheable(value = "getYZM#300",key="#url", unless="#result == null")
    public String getYZMByURL(String url,String smsCode){
        return smsCode;
    }
    @CacheEvict(value = "getYZM#300",key="#url")
    public void deleteYZMByURL(String url){
    }

}
