
package com.tmerclub.cloud.rbac.controller;

import cn.hutool.core.util.BooleanUtil;
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.exception.LuckException;
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.MenuPermissionDTO;
import com.tmerclub.cloud.rbac.model.MenuPermission;
import com.tmerclub.cloud.rbac.service.MenuPermissionService;
import com.tmerclub.cloud.rbac.vo.MenuPermissionVO;
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.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;

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

/**
 * @author tmerclub
 * @date 2020/09/02
 */
@RequestMapping("/mp/menu_permission")
@RestController
@Tag(name = "权限接口")
public class MenuPermissionController {

    @Value("${tmerclub.expose.operation.auth:}")
    private Boolean permission;

    @Autowired
    private MenuPermissionService menuPermissionService;


    @GetMapping("/list_by_menu")
    @Operation(summary = "获取菜单资源列表", description = "分页获取菜单资源列表")
    public ServerResponseEntity<List<MenuPermissionVO>> listByMenuId(Long menuId) {
        List<MenuPermissionVO> menuPermissionVOList = menuPermissionService.listByMenuId(menuId);
        return ServerResponseEntity.success(menuPermissionVOList);
    }

    @GetMapping
    @Operation(summary = "获取菜单资源", description = "根据menuPermissionId获取菜单资源")
    public ServerResponseEntity<MenuPermissionVO> getByMenuPermissionId(@RequestParam Long menuPermissionId) {
        return ServerResponseEntity.success(menuPermissionService.getByMenuPermissionId(menuPermissionId));
    }

    @PostMapping
    @Operation(summary = "保存菜单资源", description = "保存菜单资源")
    public ServerResponseEntity<Void> save(@Valid @RequestBody MenuPermissionDTO menuPermissionDTO) {
        if (BooleanUtil.isFalse(permission)) {
            return ServerResponseEntity.showFailMsg("暂无权限修改菜单");
        }
        MenuPermission menuPermission = BeanUtil.map(menuPermissionDTO, MenuPermission.class);
        menuPermission.setMenuPermissionId(null);
        return menuPermissionService.save(menuPermission);
    }

    @PutMapping
    @Operation(summary = "更新菜单资源", description = "更新菜单资源")
    public ServerResponseEntity<Void> update(@Valid @RequestBody MenuPermissionDTO menuPermissionDTO) {
        if (BooleanUtil.isFalse(permission)) {
            return ServerResponseEntity.showFailMsg("暂无权限修改菜单");
        }
        MenuPermission menuPermission = BeanUtil.map(menuPermissionDTO, MenuPermission.class);
        return menuPermissionService.update(menuPermission);
    }

    @DeleteMapping
    @Operation(summary = "删除菜单资源", description = "根据菜单资源id删除菜单资源")
    public ServerResponseEntity<Void> delete(@RequestParam Long menuPermissionId) {
        if (BooleanUtil.isFalse(permission)) {
            return ServerResponseEntity.showFailMsg("暂无权限修改菜单");
        }
        MenuPermissionVO menuPermissionVO = menuPermissionService.getByMenuPermissionId(menuPermissionId);
        if (Objects.isNull(menuPermissionVO)) {
            throw new LuckException("找不到对应资源");
        }
        menuPermissionService.deleteById(menuPermissionId, menuPermissionVO.getBizType());
        return ServerResponseEntity.success();
    }

    @GetMapping("/list")
    @Operation(summary = "获取当前用户拥有的权限", description = "当前用户所拥有的所有权限，精确到按钮，实际上element admin里面的roles就可以理解成权限")
    public ServerResponseEntity<List<String>> permissions() {
        UidInfoBO uidInfoBO = AuthUserContext.get();
        return ServerResponseEntity.success(menuPermissionService.listUserPermissions(uidInfoBO.getUserId(),
                uidInfoBO.getSysType(), com.tmerclub.cloud.common.util.BooleanUtil.isTrue(uidInfoBO.getIsAdmin())));
    }

    @GetMapping("/page")
    @Operation(summary = "获取当前用户拥有的权限", description = "当前用户所拥有的所有权限，精确到按钮，实际上element admin里面的roles就可以理解成权限")
    public ServerResponseEntity<PageVO<MenuPermissionVO>> page(PageDTO pageDTO, MenuPermissionDTO menuPermissionDTO) {
        PageVO<MenuPermissionVO> permissionPage = menuPermissionService.page(pageDTO, menuPermissionDTO);
        return ServerResponseEntity.success(permissionPage);
    }
}
