package lingtao.net.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;
import org.apache.shiro.SecurityUtils;
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.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

import lingtao.net.bean.Msg;
import lingtao.net.bean.SysRole;
import lingtao.net.bean.SysUser;
import lingtao.net.service.SysRoleService;

/**
 * 角色权限
 */
@Controller
public class SysRoleController {

	@Autowired
	private SysRoleService sysRoleService;

	// 用户跳转页面
	@RequestMapping("/role/index")
	public void index(HttpServletRequest request, HttpServletResponse response) throws Exception {
		response.sendRedirect(request.getContextPath() + "/views/system/role/role.jsp");
	}

	/**
	 * 根据条件查询角色
	 */
	@RequestMapping("/getRoleList")
	@ResponseBody
	private Msg roleList(@RequestParam(value = "page", defaultValue = "1") Integer page,
			@RequestParam(value = "limit", defaultValue = "10") Integer limit, SysRole role) {
		PageHelper.startPage(page, limit);
		List<SysRole> list = sysRoleService.getRoles(role);
		// 查询所有的角色，不分页:用于查询
		List<SysRole> allRoleList = sysRoleService.getRoles(role);
		PageInfo<SysRole> pageInfo = new PageInfo<SysRole>(list);
		return Msg.success().add("roleList", pageInfo).add("allRoleList", allRoleList);
	}

	/**
	 * 添加角色
	 */
	@ResponseBody
	@RequestMapping("/addRole")
	public Msg addRole(SysRole role, HttpSession session) {
		return sysRoleService.addRole(role, session);
	}

	/**
	 * 修改角色
	 */
	@ResponseBody
	@RequestMapping("/updateRole")
	public Msg updateRoleById(SysRole role) {
		return sysRoleService.updateRoleById(role);
	}

	/**
	 * 改变角色状态
	 */
	@ResponseBody
	@RequestMapping("/changeRoleStatus")
	public Msg changeRoleStatus(@RequestParam(value = "roleId") String roleId) {
		return sysRoleService.changeRoleStatus(roleId);
	}

	/**
	 * 根据角色id查询角色拥有的权限
	 * 
	 * @param roleId
	 * @return List<perId>
	 */
	@RequestMapping("/rolePers")
	@ResponseBody
	public List<Integer> rolePers(Integer roleId) throws Exception {
		return sysRoleService.getPerIdsByRoleId(roleId);
	}

	/**
	 * 根据角色id授权(先删除已有的权限,再插入新的权限集合)
	 */
	@RequestMapping("/assignPers")
	@ResponseBody
	public Map<String, Object> assignPers(Integer roleId, String persIds) throws Exception {
		Map<String, Object> map = new HashMap<>();
		sysRoleService.deleteRolePermissions(roleId);
		sysRoleService.addRolePermissions(roleId, persIds.split(","));
		return map;
	}

	/**
	 * 角色名称字段转换
	 * 
	 * @return
	 */
	@RequestMapping("/changeRoleName")
	@ResponseBody
	public Map<Number, String> changeRoleName() {
		Map<Number, String> map = new HashMap<Number, String>();
		List<SysRole> allRoleNames = sysRoleService.getAllRoleName(null);

		for (SysRole sysRole : allRoleNames) {
			map.put(sysRole.getRoleId(), sysRole.getRoleName());
		}

		return map;
	}

	/**
	 * 获取用户拥有的角色列表
	 * 
	 * @return
	 */
	@ResponseBody
	@RequestMapping("/getUserRoles")
	public Msg userRoles(String flag) {
		SysUser user = (SysUser) SecurityUtils.getSubject().getPrincipal();
		// 用户角色集合
		List<SysRole> userRoleList = null;
		// 用户所拥有的角色
		String[] roleList = user.getRole().split(",");
		// 判断是否拥有超管身份标识
		boolean isSuper = false;
		boolean isAllShop = false;
		for (int i = 0; i < roleList.length; i++) {
			// 有‘超级管理员’身份,状态改为true
			if ("1".equals(roleList[i])) {
				isSuper = true;
				break;
			}

			// 客服数据管理页面
			if (StringUtils.isNotEmpty(flag)) {
				// 拥有‘所有店铺’身份，状态为ture
				if ("777".equals(roleList[i])) {
					isAllShop = true;
					break;
				}
			}
		}

		// 如果有‘超级管理员’身份，查询所有角色;如果没，就查询用户所拥有的且允许被创建的角色
		if (isSuper) {
			userRoleList = sysRoleService.getRoles(null);
		} else {
			// 如果有‘所有店铺’身份，查询所有允许被创建的角色;如果没，就查询用户所拥有的且允许被创建的角色
			if (isAllShop) {
				userRoleList = sysRoleService.getAllRoleName("1");
			} else {
				userRoleList = sysRoleService.getRolesByUserId(user.getUserId());
			}
		}
		return Msg.success().add("userRoleList", userRoleList);
	}
}
