package com.hs.admin.controller;

import com.hs.admin.common.Result;
import com.hs.admin.common.annotations.CurrentUser;
import com.hs.admin.common.annotations.Token;
import com.hs.admin.model.SysRole;
import com.hs.admin.model.reqmodel.RoleReq;
import com.hs.admin.service.SysRoleService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
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;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;
import java.util.List;

@Api(tags = "角色API", produces = "produces", consumes = "consumes", protocols = "protocols")
@RequestMapping("/role")
@RestController
public class RoleController {
    @Autowired
    private SysRoleService sysRoleService;

    @PostMapping("all")
    @Token
    @ApiOperation("查询角色列表")
    public Object getAll(@Valid @RequestBody RoleReq.GetRoleReq role) throws Exception {
        List<SysRole> list = sysRoleService.getAll(role);
        return Result.success(list);
    }

    @PostMapping("info")
    @Token
    @ApiOperation("查询角色详细信息")
    public Object getById(@RequestBody RoleReq.InfoRoleReq role) throws Exception {
        SysRole info = sysRoleService.info(role.getRoleId());
        return Result.success(info);
    }

    @PostMapping("add")
    @Token
    @ApiOperation("添加角色")
    public Object addUser(@ApiIgnore @CurrentUser Integer userId, @Valid @RequestBody RoleReq.AddRoleReq role) throws Exception {
        SysRole sysRole = sysRoleService.findByCode(role.getRoleCode());
        if (sysRole != null) {
            return Result.error("该角色已存在！");
        }
        boolean result = sysRoleService.addRole(Long.valueOf(userId), role);
        if (result)
            return Result.success(null);
        else
            return Result.error();
    }

    @PostMapping("delete")
    @Token
    @ApiOperation("删除角色")
    public Object addUser(@RequestBody RoleReq.DeleteRoleReq role) throws Exception {
        boolean result = sysRoleService.deleteRole(role);
        if (result)
            return Result.success(null);
        else
            return Result.error();
    }

    @PostMapping("update")
    @Token
    @ApiOperation("修改角色")
    public Object addUser(@Valid @RequestBody RoleReq.UpdateRoleReq role) throws Exception {
        SysRole reqRole = sysRoleService.info(role.getRoleId());
        SysRole codeRole = sysRoleService.findByCode(role.getRoleCode());
        if (reqRole != null) {
            if (codeRole != null && !codeRole.getRoleCode().equals(reqRole.getRoleCode())) {
                return Result.error("该角色已存在");
            }
        }
        boolean result = sysRoleService.updateRole(role);
        if (result)
            return Result.success(null);
        else
            return Result.error();
    }

    @PostMapping("saveuserole")
    @Token
    @ApiOperation("保存用户角色")
    public Object saveUserOrg(@ApiIgnore @CurrentUser Integer userId, @RequestBody RoleReq.SavaUserRoleReq role) throws Exception {
        boolean result = sysRoleService.saveUserRole(Long.valueOf(userId), role.getUserId(), role.getRoleId());
        if (result)
            return Result.success(null);
        else
            return Result.error();
    }
}
