package com.hys.app.controller.erp.use;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hys.app.framework.context.user.AdminUserContext;
import com.hys.app.framework.database.WebPage;
import com.hys.app.framework.exception.ServiceException;
import com.hys.app.model.support.LogClient;
import com.hys.app.model.support.validator.annotation.Log;
import com.hys.app.model.system.dos.RoleDO;
import com.hys.app.model.system.vo.RoleVO;
import com.hys.app.service.system.RoleManager;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.constraints.NotEmpty;
import java.util.List;


/**
 * 角色表控制器
 *
 * @author admin
 * @version v1.0.0
 * @since v7.0.0
 * 2018-04-17 16:48:27
 */
@RestController
@RequestMapping("/admin/systems/roles")
@Api(description = "角色表相关API")
public class RoleManagerController {

    @Autowired
    private RoleManager roleManager;


    @ApiOperation(value = "角色列表分页", response = RoleDO.class)
    @GetMapping
    public WebPage list(@ApiIgnore @NotEmpty(message = "页码不能为空") Long pageNo, @ApiIgnore @NotEmpty(message = "每页数量不能为空") Long pageSize, @ApiIgnore String keyword, @ApiIgnore Long companyId) {
        AdminUserContext.hasSuperAdminQuery(companyId);
        return this.roleManager.list(pageNo, pageSize, keyword,null);
    }

    @ApiOperation(value = "套餐角色列表", response = RoleDO.class)
    @GetMapping("packageList")
    public IPage list(@ApiIgnore @NotEmpty(message = "页码不能为空") Long pageNo, @ApiIgnore @NotEmpty(message = "每页数量不能为空") Long pageSize) {
        return this.roleManager.page(new Page<>(pageNo,pageSize),new LambdaQueryWrapper<RoleDO>().isNull(RoleDO::getCompanyId));
    }


    @ApiOperation(value = "角色列表分页", response = RoleDO.class)
    @GetMapping("packageSuper")
    public IPage packageSuper(@ApiIgnore @NotEmpty(message = "页码不能为空") Long pageNo, @ApiIgnore @NotEmpty(message = "每页数量不能为空") Long pageSize, @ApiIgnore String keyword) {
        return roleManager.page(new Page<>(pageNo, pageSize), new LambdaQueryWrapper<RoleDO>().isNull(RoleDO::getCompanyId));
    }


    @ApiOperation(value = "添加角色", response = RoleDO.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "roleVO", value = "角色", required = true, dataType = "RoleVO", paramType = "body")
    })
    @PostMapping
    @Log(client = LogClient.admin, detail = "添加名称为[${roleVO.roleName}]的角色信息")
    public RoleVO add(@RequestBody @ApiIgnore RoleVO roleVO) {
        AdminUserContext.hasAdminAndSuper();
        return this.roleManager.add(roleVO);
    }

    @PutMapping(value = "/{id}")
    @ApiOperation(value = "修改角色表", response = RoleDO.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键", required = true, dataType = "int", paramType = "path"),
            @ApiImplicitParam(name = "roleVO", value = "菜单", required = true, dataType = "RoleVO", paramType = "body")
    })
    @Log(client = LogClient.admin, detail = "修改ID为[${id}]的角色信息")
    public RoleVO edit(@RequestBody @ApiIgnore RoleVO roleVO, @PathVariable Long id) {
        AdminUserContext.hasAdminAndSuper();
        return this.roleManager.edit(roleVO, id);
    }


    @DeleteMapping(value = "/{id}")
    @ApiOperation(value = "删除角色")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "要删除的角色表主键", required = true, dataType = "int", paramType = "path")
    })
    @Log(client = LogClient.admin, detail = "删除ID为[${id}]的角色信息")
    public void delete(@PathVariable Long id) {
        AdminUserContext.hasAdminAndSuper();
        this.roleManager.delete(id);
    }


    @GetMapping(value = "/{id}")
    @ApiOperation(value = "查询一个角色表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "要查询的角色表主键", required = true, dataType = "int", paramType = "path")
    })
    public RoleVO get(@PathVariable Long id) {
        return this.roleManager.getRole(id);
    }

    @GetMapping(value = "/{id}/checked")
    @ApiOperation(value = "根据角色id查询所拥有的菜单权限")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "要查询的角色表主键", required = true, dataType = "int", paramType = "path")
    })
    public List<String> getCheckedMenu(@PathVariable Long id) {
        return this.roleManager.getRoleMenu(id);
    }


    @GetMapping(value = "/getRoleByDept/{deptId}")
    @ApiOperation(value = "根据部门获取部门下所有的角色")
    public List<RoleDO> getRoleByDept(@PathVariable String deptId) {
        return roleManager.list(new LambdaQueryWrapper<RoleDO>()
                .eq(RoleDO::getCompanyId, AdminUserContext.getCompanyId())
                .eq(RoleDO::getDeptId, deptId));
    }
}
