package com.judong.chuanyiserver.controller;

import com.judong.chuanyiserver.annotation.PermissionControl;
import com.judong.chuanyiserver.entity.Role;
import com.judong.chuanyiserver.entity.User;
import com.judong.chuanyiserver.enums.ResultEnum;
import com.judong.chuanyiserver.service.RoleService;
import com.judong.chuanyiserver.util.Blank;
import com.judong.chuanyiserver.util.Result;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("role")
public class RoleController {

    @Autowired
    private RoleService roleService;

    /**
     * 新增角色
     *
     * @param role
     * @return
     */
    @PostMapping("/addRole")
    @PermissionControl
    public Result addRole(Role role) {
        if (Blank.isEmpty(role, role.getRoleName(), role.getRoleCode())) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return roleService.addRole(role);
    }

    /**
     * 修改角色信息
     *
     * @param role
     * @return
     */
    @PostMapping("/updateRole")
    @PermissionControl
    public Result updateRole(Role role) {
        if (Blank.isEmpty(role)) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return roleService.updateRole(role);
    }

    /**
     * 分页查询所有的角色信息
     *
     * @param page
     * @param limit
     * @return
     */
    @GetMapping("/getRoleListByPage")
    @PermissionControl
    public Result getRoleListByPage(Integer page, Integer limit, String roleName, String roleCode) {
        if (Blank.isEmpty(page, limit) || page < 1 || limit < 1) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return roleService.getRoleListByPage(page, limit, roleName, roleCode);
    }

    /**
     * 通过角色id查看角色信息
     *
     * @param id
     * @return
     */
    @GetMapping("/getRoleById")
//    @PermissionControl
    public Result getRoleById(Integer id) {
        if (Blank.isEmpty(id)) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return roleService.getRoleById(id);
    }

    /**
     * 通过传入的角色集合获取菜单信息
     *
     * @param user
     * @return
     */
    @GetMapping("/getMenuListByRoleList")
    public Result getMenuListByRoleList(@RequestBody User user) {
        if (Blank.isEmpty(user, user.getRoleList())) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return roleService.getMenuListByRoleList(user.getRoleList());
    }

    /**
     * 通过角色id逻辑删除角色
     *
     * @param roleId
     * @return
     */
    @PostMapping("/deleteRoleById")
    public Result deleteRoleById(Integer roleId) {
        if (Blank.isEmpty(roleId)) {

        }
        return roleService.deleteRoleById(roleId);
    }

    /**
     * 角色分配菜单
     *
     * @param role
     * @return
     */
    @PostMapping("/assignMenu")
    public Result assignMenu(@RequestBody Role role) {
        if (Blank.isEmpty(role, role.getId(), role.getMenuList())) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return roleService.assignMenu(role.getId(), role.getMenuList());
    }

    /**
     * 通过角色集合获取菜单信息，并返回成树
     *
     * @param roleIdList
     * @return
     */
    @GetMapping("/getMenuTreeByRoleList")
    public Result getMenuTreeByRoleList(@RequestParam("roleIdList") List<Integer> roleIdList) {
        if (Blank.isEmpty(roleIdList)) {
            return Result.no(ResultEnum.REQUEST_WRONGPARAMS.getRespCode(), ResultEnum.REQUEST_WRONGPARAMS.getRespMsg());
        }
        return roleService.getMenuTreeByRoleList(roleIdList);
    }
}
