
package com.tmerclub.cloud.rbac.controller;

import com.tmerclub.cloud.api.auth.bo.UidInfoBO;
import com.tmerclub.cloud.common.database.dto.PageDTO;
import com.tmerclub.cloud.common.database.vo.PageVO;
import com.tmerclub.cloud.common.response.ResponseEnum;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.common.util.BeanUtil;
import com.tmerclub.cloud.rbac.dto.RoleDTO;
import com.tmerclub.cloud.rbac.model.Role;
import com.tmerclub.cloud.rbac.service.RoleService;
import com.tmerclub.cloud.rbac.vo.RoleVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * 角色
 *
 * @author tmerclub
 * @date 2020-09-17 19:15:44
 */
@RestController
@RequestMapping("/mp/role")
@Tag(name = "角色")
public class RoleController {

    @Autowired
    private RoleService roleService;


    @GetMapping("/page")
    @Operation(summary = "分页获取角色列表", description = "分页获取角色列表")
    public ServerResponseEntity<PageVO<RoleVO>> page(@Valid PageDTO pageDTO, String roleName) {
        UidInfoBO uidInfoBO = AuthUserContext.get();
        PageVO<RoleVO> rolePage = roleService.page(pageDTO, uidInfoBO.getSysType(), uidInfoBO.getTenantId(), roleName);
        return ServerResponseEntity.success(rolePage);
    }

    @GetMapping("/list")
    @Operation(summary = "获取角色列表", description = "分页获取角色列表")
    public ServerResponseEntity<List<RoleVO>> list() {
        UidInfoBO uidInfoBO = AuthUserContext.get();
        return ServerResponseEntity.success(roleService.list(uidInfoBO.getSysType(), uidInfoBO.getTenantId()));
    }

    @GetMapping
    @Operation(summary = "获取角色", description = "根据roleId获取角色")
    public ServerResponseEntity<RoleVO> getByRoleId(@RequestParam Long roleId) {
        return ServerResponseEntity.success(roleService.getByRoleId(roleId));
    }

    @PostMapping
    @Operation(summary = "保存角色", description = "保存角色")
    public ServerResponseEntity<Void> save(@Valid @RequestBody RoleDTO roleDTO) {
        Role role = BeanUtil.map(roleDTO, Role.class);
        UidInfoBO uidInfoBO = AuthUserContext.get();
        role.setBizType(uidInfoBO.getSysType());
        role.setRoleId(null);
        role.setCreateUserId(uidInfoBO.getUserId());
        role.setTenantId(uidInfoBO.getTenantId());
        roleService.save(role, roleDTO.getMenuIds(), roleDTO.getMenuPermissionIds());
        return ServerResponseEntity.success();
    }

    @PutMapping
    @Operation(summary = "更新角色", description = "更新角色")
    public ServerResponseEntity<Void> update(@Valid @RequestBody RoleDTO roleDTO) {
        UidInfoBO uidInfoBO = AuthUserContext.get();

        RoleVO dbRole = roleService.getByRoleId(roleDTO.getRoleId());

        if (!Objects.equals(dbRole.getBizType(), uidInfoBO.getSysType()) || !Objects.equals(dbRole.getTenantId(), uidInfoBO.getTenantId())) {
            return ServerResponseEntity.fail(ResponseEnum.UNAUTHORIZED);
        }
        Role role = BeanUtil.map(roleDTO, Role.class);
        role.setBizType(uidInfoBO.getSysType());
        role.setTenantId(uidInfoBO.getTenantId());
        roleService.update(role, roleDTO.getMenuIds(), roleDTO.getMenuPermissionIds());
        return ServerResponseEntity.success();
    }

    @DeleteMapping
    @Operation(summary = "删除角色", description = "根据角色id删除角色")
    public ServerResponseEntity<Void> delete(@RequestParam Long roleId) {
        UidInfoBO uidInfoBO = AuthUserContext.get();

        RoleVO dbRole = roleService.getByRoleId(roleId);

        if (!Objects.equals(dbRole.getBizType(), uidInfoBO.getSysType()) || !Objects.equals(dbRole.getTenantId(), uidInfoBO.getTenantId())) {
            return ServerResponseEntity.fail(ResponseEnum.UNAUTHORIZED);
        }

        roleService.deleteById(roleId, uidInfoBO.getSysType());
        return ServerResponseEntity.success();
    }
}
