package com.zcxk.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zcxk.dto.RoleDto;
import com.zcxk.dto.RoleRequestDto;
import com.zcxk.entity.Role;
import com.zcxk.entity.RoleOrg;
import com.zcxk.entity.UserEntity;
import com.zcxk.logAdvice.LogAnnotation;
import com.zcxk.model.AjaxMessage;
import com.zcxk.model.Pagination;
import com.zcxk.model.ResultStatus;
import com.zcxk.service.RoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 角色(Role)控制层
 *
 * @author makejava
 * @since 2020-10-27 09:13:25
 */
@RestController
@RequestMapping("/role")
@Api(tags = "角色")
public class RoleController {
    /**
     * 服务对象
     */
    @Autowired
    private RoleService roleService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 参数对象
     * @return 单条数据
     */
    @RequestMapping(value = "get", method = RequestMethod.POST)
    @ApiOperation(value = "查询角色")
    public AjaxMessage<Role> selectOne(
            @ApiParam(value = "角色id", required = true) @RequestParam Integer id) {

        Role result = roleService.selectById(id);

        return new AjaxMessage<>(ResultStatus.OK, result);
    }

    /**
     * 新增一条数据
     *
     * @param role 实体类
     * @return Response对象
     */
    @LogAnnotation(module = "【角色管理】新增")
    @RequestMapping(value = "insert", method = RequestMethod.POST)
    @ApiOperation(value = "插入角色")
    public AjaxMessage<Integer> insert(@ApiParam(value = "设置配置", required = true) @RequestBody RoleRequestDto role) {
        int result = roleService.insert(role);

        return new AjaxMessage<>(ResultStatus.OK, result);
    }

    /**
     * 修改一条数据
     *
     * @param role 实体类
     * @return Response对象
     */
    @LogAnnotation(module = "【角色管理】修改")
    @RequestMapping(value = "update", method = RequestMethod.POST)
    @ApiOperation(value = "更新角色")
    public AjaxMessage<Integer> update(@ApiParam(value = "设置配置", required = true) @RequestBody RoleRequestDto role) {
        int result = roleService.update(role);
        if (result == -1) {
            return new AjaxMessage<>(ResultStatus.ROLE_HAS_USER);
        }
        return new AjaxMessage<>(ResultStatus.OK, result);

    }


    /**
     * 分页查询
     *
     * @param pageNum  偏移
     * @param pageSize 条数
     * @return Response对象
     */
    @RequestMapping(value = "selectPage", method = RequestMethod.POST)
    @ApiOperation(value = "查询角色列表")
    public AjaxMessage<Pagination<RoleDto>> selectPage(Integer pageNum, Integer pageSize, String roleName) {
        Role role = new Role();
        role.setRoleName(roleName);
        IPage<RoleDto> iPage = new Page<>(pageNum, pageSize);
        iPage = roleService.selectPage(role, iPage);
        Pagination<RoleDto> pages = new Pagination<>(iPage);
        return new AjaxMessage<>(ResultStatus.OK, pages);
    }

    @RequestMapping(value = "selectAll", method = RequestMethod.POST)
    @ApiOperation(value = "查询所有角色")
    public AjaxMessage<List<Role>> selectAll() {
        Role role = new Role();
        return new AjaxMessage<>(ResultStatus.OK, roleService.selectList(role));
    }

    @RequestMapping(value = "findUsersByUserRole", method = RequestMethod.POST)
    @ApiOperation(value = "查询角色对应user")
    AjaxMessage<Pagination<UserEntity>> findUsersByRole(@ApiParam(value = "角色id", required = true)
                                                  @RequestParam Integer id,
          Integer pageNum, Integer pageSize) {
        Role role = new Role();
        role.setId(id);
        IPage<UserEntity> iPage = new Page<>(pageNum, pageSize);
        iPage = roleService.findUsersByRole(role,iPage);
        Pagination<UserEntity> pages = new Pagination<>(iPage);
        return new AjaxMessage<>(ResultStatus.OK,
                pages);
    }
    @LogAnnotation(module = "【角色管理】删除")
    @RequestMapping(value = "deleteAll", method = RequestMethod.POST)
    @ApiOperation(value = "删除角色")
    AjaxMessage<List<String>> deleteAll(@ApiParam(value = "角色id", required = true) @RequestBody List<Integer> ids) {

        return new AjaxMessage<>(ResultStatus.OK,
                roleService.deleteAll(ids));
    }

    @LogAnnotation(module = "【角色管理】配置角色菜单")
    @RequestMapping(value = "confirmRoleMunu", method = RequestMethod.POST)
    @ApiOperation(value = "配置角色菜单")
    AjaxMessage<Integer> confirmRoleMunu(@ApiParam(value = "角色id", required = true) @RequestBody RoleRequestDto role) {

        return new AjaxMessage<>(ResultStatus.OK,
                roleService.updateRoleMenuRelations(role));
    }
    @RequestMapping(value = "getRoleOrgs", method = RequestMethod.POST)
    @ApiOperation(value = "配置角色菜单")
    AjaxMessage<List<RoleOrg>> getRoleOrgs(@ApiParam(value = "角色id", required = true) @RequestParam Integer id) {
        RoleOrg roleOrg = new RoleOrg();
        roleOrg.setRoleId(id);
        return new AjaxMessage<>(ResultStatus.OK,
                roleService.getRoleOrgs(roleOrg));
    }

    @RequestMapping(value = "getRoleMenuByRoleId", method = RequestMethod.POST)
    @ApiOperation(value = "根据角色ID查询菜单")
    AjaxMessage<Role> getRoleMenuByRoleId(@ApiParam(value = "角色id", required = true) @RequestParam Integer id) {
        return new AjaxMessage<Role>(ResultStatus.OK, roleService.getRoleMenuByRoleId(id));
    }
}