package cn.api.system;

import java.util.Map;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import com.alibaba.fastjson2.JSONObject;
import cn.facade.abs.IService;
import cn.common.annotation.Log;
import cn.common.enums.BizOpType;
import cn.common.enums.ResultCode;
import cn.common.page.response.Result;
import cn.pojo.entity.TSysRole;
import cn.pojo.entity.TSysRolePerm;
import cn.service.system.IRoleService;
import cn.util.core.IsKit;
import cn.util.string.JsonKit;

import javax.annotation.Resource;

/**角色控制器*/
@RestController
@RequestMapping("/api/role")
public class RoleCtrl extends IService
{
	@Resource
	private IRoleService roleService;
	
	/**修改保存角色菜单之间的关系
	 * 前端：queryRolePerms 或 updateRoleMenu_2P
	 */
	@PutMapping
	@Log(title = "角色管理-修改角色菜单权限-updateRoleMenu", businessType = BizOpType.UPDATE)
	public Result<?> updateRoleMenu(@Validated @RequestBody TSysRole entity) 
	{
		int checkNo = roleService.checkRoleUnique(entity);
		if (checkNo == 1){
			return Result.warnMsg( "修改角色'" +entity.getRoleName() + "'失败，角色名称已存在");
		}
		if (checkNo == 2){
			return Result.warnMsg("修改角色'" +entity.getRoleName() + "'失败，角色权限已存在");
		}
		if (roleService.updateRoleMenu(entity) > 0){
			return Result.ok();
		}
		return Result.warnMsg( "修改角色'" + entity.getRoleName() + "'失败，请联系管理员");
	}
	
	/**修改保存角色权限: 参数含有：[]*/
	@PutMapping("/saveOrUpdateRolePerms")
	@Log(title = "角色管理-修改角色按钮权限-saveOrUpdateRolePerms", businessType = BizOpType.UPDATE)
	public Result<?> saveOrUpdateRolePerms(@RequestBody JSONObject entity) 
	{
		TSysRolePerm bean = JsonKit.json2Bean_fast(entity, TSysRolePerm.class);
		if(IsKit.isNull(bean)){
			Result.warnMsg(ResultCode.PARAM_ERR.getMsg());
		}
		//检查有无冲突; true存在冲突
		if(roleService.ifExistRoleMenuPerm(bean.getRoleId(),bean.getMenuId())) {
			return Result.warnMsg("新增权限中存在已分配权限的菜单");
		}
		if (roleService.updateRolePerm(bean) >= 0l){
			return Result.ok();
		}
		return Result.warnMsg(ResultCode.SQL_RUN.getMsg());
	}
	/**获取满足条件的记录，全部返回，为el-table准备数据*/
	@GetMapping("/getRolePermList")
	@Log(title = "角色管理-获取全部角色权限-getRolePermList", businessType = BizOpType.SELECT)
	public Result<?> getRolePermList()
	{
		return Result.ok(roleService.getRolePermList());
	}
	
	/**查询角色权限*/
	@GetMapping("/getRolePerms")
	@Log(title = "角色管理-查询角色权限-getRolePerms", businessType = BizOpType.SELECT)
	public Result<?> getRolePerms( @RequestParam Map<String,Object> paramMap)
	{
		return Result.ok(roleService.getRolePerms(paramMap));
	}

	/**保留：删除角色 */
	@DeleteMapping("/{roleIds}")
	@Log(title = "角色管理-删除角色-deleteRole", businessType = BizOpType.DELETE)
	public Result<?> deleteRole(@PathVariable Long[] roleIds) {
		return Result.ok(roleService.deleteRoleByIds(roleIds));
	}

	/**保留：新增角色*/
	@PostMapping
	@Log(title = "角色管理-新增角色-addRole", businessType = BizOpType.INSERT)
	public Result<?> addRole(@Validated @RequestBody TSysRole entity)
	{
		//校验参数键名是否唯一
		int checkNo = roleService.checkRoleUnique(entity);
		if (checkNo == 1){
			return Result.warnMsg("新增角色'" +entity.getRoleName() + "'失败，角色名称已存在");
		}
		if (checkNo == 2){
			return Result.warnMsg("新增角色'" +entity.getRoleName() + "'失败，角色权限已存在");
		}
		//0正常
		if(IsKit.isNullOrEmpty(entity.getStatus())) {
			entity.setStatus("0");
		}		
		return Result.ok(roleService.insertRole(entity));
	}
	
	/**保留：根据角色编号获取详细信息; 在进入修改框之前  先或调用此方法获取数据*/
	@GetMapping(value = "/{roleId}")
	@Log(title = "角色管理-根据ID查询角色-queryRoleById", businessType = BizOpType.SELECT)
	public Result<?> queryRoleById(@PathVariable Long roleId) {
		return Result.ok(roleService.getById(roleId));
	}
}
