package com.njcn.rdms.module.system.api.user;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.map.MapUtil;
import com.njcn.rdms.framework.common.pojo.CommonResult;
import com.njcn.rdms.framework.common.util.collection.CollectionUtils;
import com.njcn.rdms.module.system.api.user.dto.AdminUserRespDTO;
import com.njcn.rdms.module.system.api.user.dto.UserManagementRelationRespDTO;
import com.njcn.rdms.module.system.enums.ApiConstants;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Collection;
import java.util.List;
import java.util.Map;
import java.util.Set;

@FeignClient(name = ApiConstants.NAME)
@Tag(name = "RPC 服务 - 用户管理链路")
public interface UserManagementRelationApi {

    String PREFIX = ApiConstants.PREFIX + "/user-management-relation";

    @GetMapping(PREFIX + "/list-by-manager")
    @Operation(summary = "根据管理者用户ID获得管理链路列表")
    @Parameter(name = "managerUserId", description = "管理者用户ID", example = "1", required = true)
    CommonResult<List<UserManagementRelationRespDTO>> getRelationListByManagerUserId(@RequestParam("managerUserId") Long managerUserId);

    @GetMapping(PREFIX + "/list-by-subordinate")
    @Operation(summary = "根据被管理者用户ID获得管理链路列表")
    @Parameter(name = "subordinateUserId", description = "被管理者用户ID", example = "2", required = true)
    CommonResult<List<UserManagementRelationRespDTO>> getRelationListBySubordinateUserId(@RequestParam("subordinateUserId") Long subordinateUserId);

    @GetMapping(PREFIX + "/direct-manager")
    @Operation(summary = "根据用户ID获得当前生效的直属上级")
    @Parameter(name = "userId", description = "用户ID", example = "2", required = true)
    CommonResult<AdminUserRespDTO> getDirectManager(@RequestParam("userId") Long userId);

    @GetMapping(PREFIX + "/list")
    @Operation(summary = "获得管理链路列表")
    @Parameter(name = "ids", description = "关系编号数组", example = "1,2", required = true)
    CommonResult<List<UserManagementRelationRespDTO>> getRelationList(@RequestParam("ids") Collection<Long> ids);

    @GetMapping(PREFIX + "/all-subordinate-user-ids")
    @Operation(summary = "获取某管理者全部下属用户 ID")
    @Parameter(name = "managerUserId", description = "管理者用户 ID", example = "1", required = true)
    CommonResult<Set<Long>> getAllSubordinateUserIds(@RequestParam("managerUserId") Long managerUserId);

    default Map<Long, UserManagementRelationRespDTO> getRelationMap(Collection<Long> ids) {
        if (CollUtil.isEmpty(ids)) {
            return MapUtil.empty();
        }
        List<UserManagementRelationRespDTO> list = getRelationList(ids).getData();
        return CollectionUtils.convertMap(list, UserManagementRelationRespDTO::getId);
    }

}
