package com.energy.manage.service.controller.system;

import com.alibaba.fastjson.JSONObject;
import com.energy.manage.common.base.Page;
import com.energy.manage.common.constant.Constants;
import com.energy.manage.common.po.system.SysRolePO;
import com.energy.manage.common.reponse.RespMsg;
import com.energy.manage.common.reponse.ResultResp;
import com.energy.manage.service.config.annotations.UserLoginToken;
import com.energy.manage.service.controller.base.BaseServiceController;
import com.energy.manage.service.domain.dto.system.*;
import com.energy.manage.service.domain.vo.system.PermissionVo;
import com.energy.manage.service.domain.vo.system.SysRelationVo;
import com.energy.manage.service.domain.vo.system.SystemPermissionVo;
import com.energy.manage.service.service.system.SysPermissionService;
import com.energy.manage.service.service.system.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * @menu: 后台角色管理
 *
 * @author sy
 * @date 2021-10-18 13:32:05
 */
@RestController
@RequestMapping("sysrole")
@CrossOrigin
@Api(value = "SysRoleController", tags = "系统角色管理")
public class SysRoleController  extends BaseServiceController {

	@Autowired
	private SysRoleService sysRoleService;

	@Autowired
	private SysPermissionService permissionService;

	/**
	 * 添加角色
	 * @return
	 */
	@UserLoginToken
	@PostMapping(value = "/addRole")
	@ApiOperation(value = "添加角色")
	public ResultResp addRole(@RequestBody RoleAddDto roleAddDto) {
		if (StringUtils.isEmpty(roleAddDto.getRoleName()) || StringUtils.isEmpty(roleAddDto.getRoleCode())) {
			return ResultResp.NOTNULLPARAM();
		}
		SysRolePO rolePO = new SysRolePO();
		rolePO.setRoleCode(roleAddDto.getRoleCode());
		rolePO = sysRoleService.selectOne(rolePO);
		if (null != rolePO) {
			return ResultResp.FAIL("该角色已存在");
		}
		rolePO = new SysRolePO();
		rolePO.setRoleCode(roleAddDto.getRoleCode());
		rolePO.setRoleDescription(roleAddDto.getRoleName());
		rolePO.setRoleType(roleAddDto.getRoleType());
		rolePO.setRoleAvailable(Constants.GLOBAL_IN_USE);
		rolePO.setCreateTime(new Date());
		rolePO.setUpdateTime(new Date());
		boolean flag = sysRoleService.addRole(rolePO);
		if (flag) {
			return ResultResp.SUCCESS();
		}
		return ResultResp.FAIL();
	}

	/**
	 * 删除角色
	 * @return
	 */
	@UserLoginToken
	@PostMapping(value = "/deleteRole")
	@ApiOperation(value = "删除角色")
	public ResultResp deleteRole(@RequestBody RoleDelDto roleDelDto) {
		if (!Optional.ofNullable(roleDelDto.getRoleId()).isPresent()) {
			return ResultResp.NOTNULLPARAM();
		}
		SysRolePO rolePO = new SysRolePO();
		rolePO.setId(roleDelDto.getRoleId());
		return sysRoleService.deleteRole(rolePO);
	}

	/**
	 * 更新角色
	 * @return
	 */
	@UserLoginToken
	@PostMapping(value = "/updateRole")
	@ApiOperation(value = "更新角色")
	public ResultResp updateRole(@RequestBody RoleUpdateDto roleUpdateDto) {
		if (!Optional.ofNullable(roleUpdateDto.getRoleId()).isPresent() || StringUtils.isEmpty(roleUpdateDto.getRoleName())) {
			return ResultResp.NOTNULLPARAM();
		}
		SysRolePO rolePO = new SysRolePO();
		rolePO.setId(roleUpdateDto.getRoleId());
		rolePO.setRoleDescription(roleUpdateDto.getRoleName());
		rolePO.setRoleCode(roleUpdateDto.getRoleCode());
		rolePO.setRoleType(roleUpdateDto.getRoleType());
		boolean flag = sysRoleService.updateRole(rolePO);
		if (flag) {
			return ResultResp.SUCCESS();
		}
		return ResultResp.FAIL();
	}

	@UserLoginToken
	@PostMapping(value = "/updateRoleAvailable")
	@ApiOperation(value = "更新角色状态")
	public ResultResp updateRoleAvailable(@RequestBody RoleUpdateStateDto updateStateDto) {

		boolean flag = sysRoleService.updateRoleAvailable(updateStateDto);
		if (flag) {
			return ResultResp.SUCCESS();
		}
		return ResultResp.FAIL();
	}

	/**
	 * 通过角色id ，获取角色详情
	 * @return
	 */
	@UserLoginToken
	@PostMapping(value = "/getRoleBId")
	@ApiOperation(value = "通过角色id ，获取角色详情")
	public ResultResp<SysRolePO> getRoleBId(@RequestBody RoleDelDto roleDelDto) {
		if (!Optional.ofNullable(roleDelDto.getRoleId()).isPresent()) {
			return ResultResp.NOTNULLPARAM();
		}
		SysRolePO rolePO = new SysRolePO();
		rolePO.setId(roleDelDto.getRoleId());
		rolePO = sysRoleService.selectOne(rolePO);
		return ResultResp.SUCCESS(rolePO);
	}

	/**
	 * 通过角色名称查询角色列表
	 * @return
	 */
	@UserLoginToken
	@PostMapping(value = "/通过角色名称查询角色列表")
	@ApiOperation(value = "角色分页列表")
	public ResultResp<Page<SysRolePO>> getRoleListByName(@RequestBody RoleListDto roleListDto) {
		Page<SysRolePO> page = sysRoleService.getRoleListByName(roleListDto.getPageNum(), roleListDto.getPageSize(), roleListDto.getRoleName(),roleListDto.getRoleAvailable());
		return ResultResp.SUCCESS(page);
	}

	/**
	 * 通过角色获取权限
	 * @return
	 */
	@UserLoginToken
	@PostMapping(value = "/getRolePermission")
	@ApiOperation(value = "通过角色获取权限")
	public ResultResp<List<SystemPermissionVo> > getRolePermission(@RequestBody RoleDelDto roleDelDto) {
		if (!Optional.ofNullable(roleDelDto.getRoleId()).isPresent()) {
			return ResultResp.NOTNULLPARAM();
		}
		List<SystemPermissionVo> list = permissionService.getRolePermission(roleDelDto.getRoleId());
		return ResultResp.SUCCESS(list);
	}

	/**
	 * 通过角色获取组织架构权限
	 * @return
	 */
	@UserLoginToken
	@PostMapping(value = "/getRoleOrganization")
	@ApiOperation(value = "通过角色获取组织架构权限")
	public ResultResp<List<Map<String, Object>>> getRoleOrganization(@RequestBody RoleDelDto roleDelDto) {
		if (!Optional.ofNullable(roleDelDto.getRoleId()).isPresent()) {
			return ResultResp.NOTNULLPARAM();
		}
		List<Map<String, Object>> list = permissionService.getRoleOrganization(roleDelDto.getRoleId());
		return ResultResp.SUCCESS(list);
	}




	/**
	 * 更新角色权限
	 * @return
	 */
	@UserLoginToken
	@PostMapping(value = "/updateRolePermission")
	@ApiOperation(value = "更新角色菜单权限")
	public ResultResp updateRolePermission(@RequestBody RoleUpdatePermissionDto roleUpdatePermissionDto) {
		if (!Optional.ofNullable(roleUpdatePermissionDto.getRoleId()).isPresent() || StringUtils.isEmpty(roleUpdatePermissionDto.getPermissionIds())) {
			return ResultResp.NOTNULLPARAM();
		}
		boolean flag = permissionService.updateRolePermission(roleUpdatePermissionDto.getRoleId(), roleUpdatePermissionDto.getPermissionIds());
		if (flag) {
			return ResultResp.SUCCESS();
		}
		return ResultResp.FAIL();
	}

	/**
	 * 更新角色权限
	 * @return
	 */
	@UserLoginToken
	@PostMapping(value = "/updateRoleOrganization")
	@ApiOperation(value = "更新角色组织架构权限")
	public ResultResp updateRoleOrganizationAuth(@RequestBody RoleUpdateOrganizationDto roleUpdateOrganizationDto) {
		if (!Optional.ofNullable(roleUpdateOrganizationDto.getRoleId()).isPresent() || CollectionUtils.isEmpty(roleUpdateOrganizationDto.getOrganizationCodes())) {
			return ResultResp.NOTNULLPARAM();
		}
		boolean flag = permissionService.updateRoleOrganization(roleUpdateOrganizationDto.getRoleId(), roleUpdateOrganizationDto.getOrganizationCodes(),getUserId(),1);
		if (flag) {
			return ResultResp.SUCCESS();
		}
		return ResultResp.FAIL();
	}

}
