package cn.com.capcloud.api.action.myself.system;

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

@Component
public class RoleAction extends DefaultAction {
    @Resource
    private BaseService baseService;
    @Resource
    private UserService userService;
    @Resource
    private ApiService apiService;
    @Resource
    private KHCRedisCache khcRedisCache;
    private static RoleAction 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> getAllRoleByMenuId(String jsonStr, JackJSONObject retJson, HttpServletRequest request){
        JackJSONObject json= JackJSONObject.fromObject(jsonStr);
        String menuId= String.valueOf(json.get("menuId"));
        String sql="select * from tbl_system_menurole where menu_id="+menuId;
        List<Map<String, Object>> list= this.getJdbcTemplate().queryForList(sql);
        String roleStr="";
        for (int i = 0; i < list.size(); i++) {
            roleStr+=list.get(i).get("role_id")+",";
        }
        if(roleStr!=""){
            roleStr=roleStr.substring(0,roleStr.length()-1);
        }
        Map<String,Object> map=new HashMap<>();
        map.put("message","查询成功");
        map.put("data",roleStr);
        return map;
    }

    public JackJSONArray saveBeforeMenuRole(String jsonStr, JackJSONObject retJson, HttpServletRequest request, JackJSONArray array){
        JackJSONObject json =array.getJackJSONObject(0);
        String menuId=String.valueOf(json.get("menu_id"));
        String roles=String.valueOf(json.get("role_id"));
        action.getJdbcTemplate().execute("delete from tbl_system_menurole where menu_id="+menuId);
        if(!roles.equals("")){
            String[] arr = roles.split(",");
            for (int i = 0; i < arr.length; i++) {
                if(!arr[i].equals("")){
                    Map<String,Object> map=new HashMap<>();
                    map.put("role_id",arr[i]);
                    map.put("menu_id",menuId);
                    action.baseService.save("tbl_system_menurole",map);
                }
            }
        }
        retJson.putAll(JackJSONObject.fromObject(action.apiService.getReturnDataByCode("ok")));
        Map<String,Object> map=new HashMap<>();
        map.put("saveData",array);
        retJson.put("data",map);
        action.khcRedisCache.deleteAllMenu();//更新缓存
        return new JackJSONArray();
    }

}
