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

@Component
public class DataNavAction extends DefaultAction {
    @Resource
    private BaseService baseService;
    @Resource
    private UserService userService;
    @Resource
    private KHCRedisCache khcRedisCache;
    @Resource
    private ApiService apiService;
    private static DataNavAction 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> getNavInfo(String jsonStr, JackJSONObject retJson, HttpServletRequest request){
        JackJSONObject json= JackJSONObject.fromObject(jsonStr);
        Map<String,Object> retMap=new HashMap<>();
        Map<String,Object> map=new HashMap<>();
        String navCode= String.valueOf(json.get("navCode"));
        String sql="select * from tbl_data_quick_nav where status=0 and nav_code='"+navCode+"'";
        List<Map<String, Object>> list= this.getJdbcTemplate().queryForList(sql);
        String queryNavItemSql="select * from tbl_data_quick_nav_item where status=0 and page_parent_uuid='"+list.get(0).get("uuid")+"' order by sort asc ";
        List<Map<String, Object>> navList= this.getJdbcTemplate().queryForList(queryNavItemSql);
        list.get(0).put("navItems", getNavFilter(navList,request));
        retMap.put("message","查询成功");
        retMap.put("navInfo",list.get(0));
        return retMap;
    }
    //如果没有按钮角色权限则过滤掉。
    public List<Map<String, Object>> getNavFilter(List<Map<String, Object>> buttons,HttpServletRequest request){
        List<Map<String, Object>> filterButtons=new ArrayList<>();
        if(buttons.size()>0){
            User user=action.userService.getUser(request);
            for (int i = 0; i < buttons.size(); i++) {
                String roleStr=String.valueOf(buttons.get(i).get("view_role_id"));
                if(roleStr.equals("")){//空则不控制按钮权限
                    filterButtons.add(buttons.get(i));
                }else{
                    String[] roleArr=roleStr.split(",");
                    for (int j = 0; j < roleArr.length; j++) {
                        if(action.userService.isExistsRole(roleArr[j], user.getId())) {
                            filterButtons.add(buttons.get(i));
                            break;
                        }
                    }
                }
            }
        }
        return filterButtons;
    }
}
