package com.inspect.system.controller;

import com.inspect.base.core.utils.poi.ExcelUtil;
import com.inspect.base.core.web.controller.BaseController;
import com.inspect.base.core.web.domain.AjaxResult;
import com.inspect.base.core.web.page.TableDataInfo;
import com.inspect.common.log.annotation.Log;
import com.inspect.common.log.enums.BizType;
import com.inspect.common.security.annotation.RequiresPermissions;
import com.inspect.common.security.utils.SecurityUtils;
import com.inspect.system.base.domain.SysRole;
import com.inspect.system.base.domain.SysUser;
import com.inspect.system.domain.SysUserRole;
import com.inspect.system.service.ISysRoleService;
import com.inspect.system.service.ISysUserService;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping({"/role"})
public class SysRoleController extends BaseController {
    private final ISysRoleService roleService;
    private final ISysUserService userService;

    public SysRoleController(ISysRoleService roleService, ISysUserService userService) {
        this.roleService = roleService;
        this.userService = userService;
    }

    @RequiresPermissions({"system:role:list"})
    @GetMapping({"/list"})
    public TableDataInfo list(SysRole role) {
        this.startPage();
        List<SysRole> list = this.roleService.selectRoleList(role);
        return this.getDataTable(list);
    }

    @Log(
            title = "角色管理",
            businessType = BizType.EXPORT
    )
    @RequiresPermissions({"system:role:export"})
    @PostMapping({"/export"})
    public void export(HttpServletResponse response, SysRole role) {
        List<SysRole> list = this.roleService.selectRoleList(role);
        ExcelUtil<SysRole> util = new ExcelUtil<>(SysRole.class);
        util.exportExcel(response, list, "角色数据");
    }

    @RequiresPermissions({"system:role:query"})
    @GetMapping({"/{roleId}"})
    public AjaxResult getInfo(@PathVariable Long roleId) {
        this.roleService.checkRoleDataScope(roleId);
        return AjaxResult.success(this.roleService.selectRoleById(roleId));
    }

    @RequiresPermissions({"system:role:add"})
    @Log(
            title = "角色管理",
            businessType = BizType.INSERT
    )
    @PostMapping
    public AjaxResult add(@Validated @RequestBody SysRole role) {
        if ("1".equals(this.roleService.checkRoleNameUnique(role))) {
            return AjaxResult.error("新增角色'" + role.getRoleName() + "'失败，角色名称已存在");
        } else if ("1".equals(this.roleService.checkRoleKeyUnique(role))) {
            return AjaxResult.error("新增角色'" + role.getRoleName() + "'失败，角色权限已存在");
        } else {
            role.setCreateBy(SecurityUtils.getUsername());
            return this.toAjax(this.roleService.insertRole(role));
        }
    }

    @RequiresPermissions({"system:role:edit"})
    @Log(
            title = "角色管理",
            businessType = BizType.UPDATE
    )
    @PostMapping({"/edit"})
    public AjaxResult edit(@Validated @RequestBody SysRole role) {
        this.roleService.checkRoleAllowed(role);
        this.roleService.checkRoleDataScope(role.getRoleId());
        if ("1".equals(this.roleService.checkRoleNameUnique(role))) {
            return AjaxResult.error("修改角色'" + role.getRoleName() + "'失败，角色名称已存在");
        } else if ("1".equals(this.roleService.checkRoleKeyUnique(role))) {
            return AjaxResult.error("修改角色'" + role.getRoleName() + "'失败，角色权限已存在");
        } else {
            role.setUpdateBy(SecurityUtils.getUsername());
            return this.toAjax(this.roleService.updateRole(role));
        }
    }

    @RequiresPermissions({"system:role:edit"})
    @Log(
            title = "角色管理",
            businessType = BizType.UPDATE
    )
    @PostMapping({"/dataScope"})
    public AjaxResult dataScope(@RequestBody SysRole role) {
        this.roleService.checkRoleAllowed(role);
        this.roleService.checkRoleDataScope(role.getRoleId());
        return this.toAjax(this.roleService.authDataScope(role));
    }

    @RequiresPermissions({"system:role:edit"})
    @Log(
            title = "角色管理",
            businessType = BizType.UPDATE
    )
    @PostMapping({"/changeStatus"})
    public AjaxResult changeStatus(@RequestBody SysRole role) {
        this.roleService.checkRoleAllowed(role);
        this.roleService.checkRoleDataScope(role.getRoleId());
        role.setUpdateBy(SecurityUtils.getUsername());
        return this.toAjax(this.roleService.updateRoleStatus(role));
    }

    @RequiresPermissions({"system:role:remove"})
    @Log(
            title = "角色管理",
            businessType = BizType.DELETE
    )
    @PostMapping({"/remove/{roleIds}"})
    public AjaxResult remove(@PathVariable Long[] roleIds) {
        return this.toAjax(this.roleService.deleteRoleByIds(roleIds));
    }

    @RequiresPermissions({"system:role:query"})
    @GetMapping({"/optionselect"})
    public AjaxResult optionSelect() {
        return AjaxResult.success(this.roleService.selectRoleAll());
    }

    @RequiresPermissions({"system:role:list"})
    @GetMapping({"/authUser/allocatedList"})
    public TableDataInfo allocatedList(SysUser user) {
        this.startPage();
        List<SysUser> list = this.userService.selectAllocatedList(user);
        return this.getDataTable(list);
    }

    @RequiresPermissions({"system:role:list"})
    @GetMapping({"/authUser/unallocatedList"})
    public TableDataInfo unallocatedList(SysUser user) {
        this.startPage();
        List<SysUser> list = this.userService.selectUnallocatedList(user);
        return this.getDataTable(list);
    }

    @RequiresPermissions({"system:role:edit"})
    @Log(
            title = "角色管理",
            businessType = BizType.GRANT
    )
    @PostMapping({"/authUser/cancel"})
    public AjaxResult cancelAuthUser(@RequestBody SysUserRole userRole) {
        return this.toAjax(this.roleService.deleteAuthUser(userRole));
    }

    @RequiresPermissions({"system:role:edit"})
    @Log(
            title = "角色管理",
            businessType = BizType.GRANT
    )
    @PostMapping({"/authUser/cancelAll"})
    public AjaxResult cancelAuthUserAll(Long roleId, Long[] userIds) {
        return this.toAjax(this.roleService.deleteAuthUsers(roleId, userIds));
    }

    @RequiresPermissions({"system:role:edit"})
    @Log(
            title = "角色管理",
            businessType = BizType.GRANT
    )
    @PostMapping({"/authUser/selectAll"})
    public AjaxResult selectAuthUserAll(Long roleId, Long[] userIds) {
        this.roleService.checkRoleDataScope(roleId);
        return this.toAjax(this.roleService.insertAuthUsers(roleId, userIds));
    }
}
