package com.boco.nbd.wios.manage.controller;

import com.boco.nbd.cams.core.constant.MessageConstant;
import com.boco.nbd.wios.manage.contants.WiosConstant;
import com.boco.nbd.wios.manage.entity.bo.AbstractTreeNode;
import com.boco.nbd.wios.manage.entity.bo.MenuTreeVo;
import com.boco.nbd.wios.manage.entity.bo.RoleBo;
import com.boco.nbd.wios.manage.entity.bo.Role;
import com.boco.nbd.wios.manage.service.impl.RoleService;
import com.ihidea.component.api.v2.BaseResponse;
import com.ihidea.core.support.exception.ServiceException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.List;

/**
 * 角色相关api
 *
 * @author lilin
 * @version [版本号, 2020年5月20日]
 */
@RestController
@RequestMapping("api")
@Api(tags = "角色管理接口")
@ApiIgnore
public class RoleController {
    /**
     * 角色service
     */
    @Resource
    private RoleService roleService;

    /**
     * 查询角色列表
     *
     * @param role 查询条件
     * @return 角色列表
     */
    @GetMapping(value = "role/list")
    @ApiOperation(value = "查询角色列表")
    public BaseResponse<List<Role>> queryList(RoleBo role) {
        return new BaseResponse<List<Role>>(roleService.queryList(role));
    }

    /**
     * 新增角色
     *
     * @param role 角色信息
     * @return 新增成功的数量
     */
    @PostMapping(value = "role/add")
    @ApiOperation(value = "添加角色")
    public BaseResponse<Integer> addRole(RoleBo role) {
        if (!StringUtils.isEmpty(role.getName()) && role.getName().length() > WiosConstant.MAX_ROLE_NAME_LENGTH) {
            throw new ServiceException("角色名称过长");
        }
        return new BaseResponse<>(roleService.addRole(role));
    }

    /**
     * 修改角色
     *
     * @param role 角色信息
     * @return 修改成功的数量
     */
    @PostMapping(value = "role/update")
    @ApiOperation(value = "修改角色")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "主键", dataType = "int", paramType = "query", required = true),})
    public BaseResponse<Integer> updateRole(RoleBo role) {
        if (role.getId() == null) {
            throw new ServiceException(MessageConstant.MISSING_PARAM);
        }
        return new BaseResponse<>(roleService.updateRole(role));
    }

    /**
     * 删除角色
     *
     * @param id 角色id
     * @return 删除成功的数量
     */
    @PostMapping(value = "role/delete")
    @ApiOperation(value = "删除角色")
    @ApiImplicitParams({@ApiImplicitParam(name = "id", value = "主键", dataType = "int", paramType = "query", required = true),})
    public BaseResponse<Integer> deleteRole(Integer id) {
        return new BaseResponse<>(roleService.deleteRole(id));
    }


    /**
     * 查询角色菜单权限
     *
     * @param roleId 角色id
     * @return 角色菜单权限
     */
    @GetMapping(value = "role/menuList")
    @ApiOperation(value = "查询角色菜单权限")
    @ApiImplicitParams({@ApiImplicitParam(name = "roleId", value = "角色id", dataType = "int", paramType = "query", required = true),
    })
    public BaseResponse<List<MenuTreeVo>> getRoleMenuList(Integer roleId) {
        return new BaseResponse<>(roleService.getRoleMenuList(roleId));
    }

    /**
     * 获取左侧菜单列表
     *
     * @param accountId
     * @return
     */
    @GetMapping(value = "menuList")
    @ApiOperation(value = "获取左侧菜单列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "accountId", value = "账号id", dataType = "int", paramType = "query", required = true),})
    public BaseResponse<List<AbstractTreeNode>> getMenuList(Integer accountId) {
        return new BaseResponse<>(roleService.getMenuList(accountId));
    }

    /**
     * 获取某个页面配置的按钮列表
     *
     * @param menuId
     * @return
     */
    @GetMapping(value = "role/getBtnList")
    @ApiOperation(value = "获取某个页面配置的按钮列表")
    @ApiImplicitParams({@ApiImplicitParam(name = "menuId", value = "菜单id", dataType = "String", paramType = "query", required = true),})
    public BaseResponse<Object> getPageBtnList(String menuId) {
        if (StringUtils.isEmpty(menuId)) {
            throw new ServiceException(MessageConstant.MISSING_PARAM);
        }
        return new BaseResponse<Object>(roleService.getPageBtnList(menuId));
    }

    /**
     * 角色启用/禁用
     *
     * @param roleId
     * @param status
     * @return
     */
    @PostMapping(value = "role/updateStatus")
    @ApiOperation(value = "角色启用/禁用")
    @ApiImplicitParams({@ApiImplicitParam(name = "roleId", value = "角色id", dataType = "int", paramType = "query", required = true),
        @ApiImplicitParam(name = "status", value = "状态(0:禁用,1:正常)", dataType = "int", paramType = "query", required = true),})
    public BaseResponse<Object> updateStatus(Integer roleId, Integer status) {
        if (roleId == null || status == null) {
            throw new ServiceException(MessageConstant.MISSING_PARAM);
        }
        roleService.updateStatus(roleId, status);
        return new BaseResponse<>();
    }
}