package com.pj.project4sp.role4permission;

import java.util.List;
import java.util.stream.Collectors;

import cn.hutool.core.util.StrUtil;
import com.pj.constants.RoleEnum;
import com.pj.current.satoken.StpUserUtil;
import com.pj.project4sp.role.SpRole;
import com.pj.project4sp.role.SpRoleMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.pj.current.satoken.AuthConst;
import com.pj.project4sp.role.SpRoleUtil;
import com.pj.utils.sg.AjaxJson;

import cn.dev33.satoken.stp.StpUtil;

import javax.annotation.Resource;

/**
 * Controller: 角色与权限的中间表 
 * @author kong
 *
 */
@RestController
@RequestMapping("/SpRolePermission/")
public class SpRolePermissionController {

	
	/** 底层Service */
	@Autowired
	SpRolePermissionService spRolePermissionService;
	@Resource
	private SpRoleMapper spRoleMapper;
	
	
	
	/**
	 * 拉取权限id列表  根据指定roleId 
	 * @param roleId
	 * @return
	 */
	@RequestMapping("getPcodeByRid")
    public AjaxJson getPcodeByRid(@RequestParam String roleId){
		// 鉴权  
		StpUtil.checkPermission(AuthConst.AUTH);
		StpUtil.checkPermission(AuthConst.ROLE_LIST);	
		// 防止拉出全部 	
		if(StrUtil.isEmpty(roleId)){
			return AjaxJson.getError("roleId不能为null");
		}
		return AjaxJson.getSuccessData(spRolePermissionService.getPcodeByRid2(roleId));
	}
	
	
	/** 拉取菜单id列表  根据当前用户roleId  */
	@RequestMapping("getPcodeByCurrRid")
	public AjaxJson getPcodeByCurrRid(){
		String roleId = SpRoleUtil.getCurrRoleId();

		List<Object> list = spRolePermissionService.getPcodeByRid2(roleId);
		return AjaxJson.getSuccessData(list);
	}
	/** 拉取菜单id列表  根据当前用户roleId  */
	@RequestMapping("getCustomerPcCode")
	public AjaxJson getCustomerPcCode(){
		String roleId = SpRoleUtil.getCurrRoleId();
		List<SpRole>roles= spRoleMapper.getByIds(StrUtil.splitTrim(roleId,",").stream().map(Long::valueOf).collect(Collectors.toList()));
		long count=roles.stream().filter(spRole -> StrUtil.equals(spRole.getType(), RoleEnum.ADMIN_ROLE.getType())).count();
		if (count>0){
			List<String>list= spRolePermissionService.getAdminPcode();
			return AjaxJson.getSuccessData(list);
		}
		List<Object> list = spRolePermissionService.getPcodeByRid2(roleId);
		return AjaxJson.getSuccessData(list);
	}
	

	/**
	 * 修改指定角色的拥有的权限 
	 * @param roleId 角色id
	 * @param code 拥有的权限码集合 
	 * @return
	 */
	@RequestMapping("updatePcodeByRid")
	public AjaxJson updatePcodeByRid(long roleId, String[] code){
		StpUtil.checkPermission(AuthConst.AUTH);
		return AjaxJson.getSuccessData(spRolePermissionService.updateRoleMenu(roleId, code));
	}
	


	
	


	
	
	
}
