package com.zcloud.controller.system;

import com.zcloud.controller.base.BaseController;
import com.zcloud.entity.PageData;
import com.zcloud.entity.system.Menu;
import com.zcloud.entity.system.Role;
import com.zcloud.entity.system.User;
import com.zcloud.service.system.*;
import com.zcloud.util.RightsHelper;
import com.zcloud.util.Tools;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.*;

/**
 * 说明：登录成功访问后台的处理类
 * 作者：luoxiaobao
 * 官网：www.qdkjchina.com
 */
@Controller
@RequestMapping("/app/main")
public class AppMainController extends BaseController {

	@Autowired
    private UsersService usersService;
	@Autowired
    private RoleService roleService;
    @Autowired
    private MenuService menuService;
    @Autowired
    private ButtonrightsService buttonrightsService;
    @Autowired
    private FhButtonService fhButtonService;
	@Autowired
    private FHlogService FHLOG;

	/**登录进来第一个访问的方法,初始用户权限以及缓存
	 * @throws Exception
	 * @return
	 */
	@RequestMapping(value="/index")
	@ResponseBody
	public Map<String, Object> loginPage() throws Exception{
		Map<String,Object> map = new HashMap<String,Object>();
		String errInfo = "success";
		PageData pd = new PageData();
		pd = this.getPageData();
		User user = usersService.getUserAndRoleById(pd.getString("USER_ID"));				//通过用户ID读取用户信息和角色信息
		if(user == null ){
//			pd.put("PUSH_CID","");
//			usersService.updatePushCid(pd);
			errInfo = "error";
			map.put("result", errInfo);
		}else{
			String USERNAME = user.getUSERNAME();
			Role role = user.getRole();													//获取用户角色
			String roleRights = role!=null ? role.getRIGHTS() : "";						//角色权限(菜单权限)
			String ROLE_IDS = user.getROLE_IDS();
			List<Menu> menuList = new ArrayList<Menu>();
			Collection<String> shiroSet= new HashSet<String>();							//存放菜单权限标识
			menuList = this.getAttributeMenu(USERNAME, roleRights, getArrayRoleRights(ROLE_IDS));		//菜单缓存以及赋值权限
			map.put("menuList", menuList);
			map.put("result", errInfo);

		}
		return map;
	}

	/**菜单缓存以及赋值权限
	 * @param USERNAME 用户名
	 * @param roleRights 主职角色加密的权限字符串
	 * @param arrayRoleRights 副职角色加密的权限字符串列表
	 * @return
	 * @throws Exception
	 */
	@SuppressWarnings("unchecked")
	public List<Menu> getAttributeMenu(String USERNAME, String roleRights, List<String> arrayRoleRights) throws Exception{
		List<Menu> allmenuList = new ArrayList<Menu>();
		allmenuList = menuService.listAllMenuQx("0");											//获取所有菜单
		if(Tools.notEmpty(roleRights)){
			allmenuList = this.readMenu(USERNAME, allmenuList, roleRights, arrayRoleRights);				//根据角色权限获取本权限的菜单列表
		}
		return allmenuList;
	}

	/**获取副职角色权限List
	 * @param ROLE_IDS 副职角色ID列表
	 * @return
	 * @throws Exception
	 */
	public List<String> getArrayRoleRights(String ROLE_IDS) throws Exception{
		if(Tools.notEmpty(ROLE_IDS)){
			List<String> list = new ArrayList<String>();
			String arryROLE_ID[] = ROLE_IDS.split(",");
			for(int i=0;i<arryROLE_ID.length;i++){
				PageData pd = new PageData();
				pd.put("ROLE_ID", arryROLE_ID[i]);
				pd = roleService.findById(pd);
				if(null != pd){
					String RIGHTS = pd.getString("RIGHTS");
					if(Tools.notEmpty(RIGHTS)){
						list.add(RIGHTS);
					}
				}
			}
			return list.size() == 0 ? null : list;
		}else{
			return null;
		}
	}

	/**根据角色权限获取本权限的菜单列表(递归处理)
	 * @param USERNAME 用户名
	 * @param menuList 传入的总菜单
	 * @param roleRights 主职角色加密的权限字符串
	 * @param arrayRoleRights 副职角色加密的权限字符串列表
	 * @return
	 */
	public List<Menu> readMenu(String USERNAME, List<Menu> menuList,String roleRights, List<String> arrayRoleRights){
		for(int i=0;i<menuList.size();i++){
			Boolean b1 = RightsHelper.testRights(roleRights, menuList.get(i).getMENU_ID());
			menuList.get(i).setHasMenu(b1); //赋予主职角色菜单权限
			if(menuList.get(i).isHasMenu()){		//判断是否有此菜单权限
				this.readMenu(USERNAME, menuList.get(i).getSubMenu(), roleRights, arrayRoleRights);//是：继续排查其子菜单
			}
		}
		return menuList;
	}
}
