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.BaseConfigStaticCache;
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 javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;

@Component
public class DataTemplateAction extends DefaultAction {
    //系统配置(KEY)
    private static final String SYSTEM_SETTINGS_DS_SYSTEM_CONFIG_JSON="system.settings.ds.system.config.json";
    @Resource
    private BaseService baseService;
    @Resource
    private UserService userService;
    @Resource
    private ApiService apiService;
    @Resource
    private KHCRedisCache khcRedisCache;
    private static DataTemplateAction 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> findOnePasswordPolicy(String jsonStr, JackJSONObject retJson, HttpServletRequest request){
        String sql="select base_value from tbl_data_base_config where status=0 and base_status=0 and base_code='"+SYSTEM_SETTINGS_DS_SYSTEM_CONFIG_JSON+"'";
        String value = this.getJdbcTemplate().queryForObject(sql,String.class);
        value=value.replaceAll("&nbsp;"," ");
        JackJSONObject jsonData=JackJSONObject.fromObject(value);
        Map<String,Object> map=new HashMap<>();
        map.put("message","查询成功");
        map.put("data",jsonData);
        return map;
    }

    public JackJSONArray saveBeforePasswordPolicy(String jsonStr, JackJSONObject retJson, HttpServletRequest request, JackJSONArray array) {
        JackJSONObject json =array.getJackJSONObject(0);
        Map<String,Object> map=new HashMap<>();
        JackJSONObject passwordPolicyJson=new JackJSONObject();
        passwordPolicyJson.put("password_error_num",json.get("password_error_num"));
        passwordPolicyJson.put("password_valid_day_num",json.get("password_valid_day_num"));
        passwordPolicyJson.put("password_remind_day_num",json.get("password_remind_day_num"));
        passwordPolicyJson.put("menu_render_type",json.get("menu_render_type"));
        array=new JackJSONArray();//重置
        array.add(passwordPolicyJson);//只保留配置字段
        String passwordPolicyStr= JackJSON.toJSONString(passwordPolicyJson);
        this.getJdbcTemplate().execute("update tbl_data_base_config set base_value='"+passwordPolicyStr+"' where status=0 and base_code ='"+SYSTEM_SETTINGS_DS_SYSTEM_CONFIG_JSON+"'");
        map.put("saveData",array);
        retJson.putAll(JackJSONObject.fromObject(action.apiService.getReturnDataByCode("ok")));
        retJson.put("data",map);
        BaseConfigStaticCache.init(action.khcRedisCache);//更新缓存
        return new JackJSONArray();
    }
}
