package cn.iocoder.yudao.module.system.api.dept;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.system.api.dept.dto.DeptRespDTO;
import cn.iocoder.yudao.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.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
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) // TODO 芋艿：fallbackFactory =
@Tag(name = "RPC 服务 - 部门")
public interface DeptApi {

    String PREFIX = ApiConstants.PREFIX + "/dept";

    @GetMapping(PREFIX + "/get")
    @Operation(summary = "获得部门信息")
    @Parameter(name = "id", description = "部门编号", example = "1024", required = true)
    CommonResult<DeptRespDTO> getDept(@RequestParam("id") Long id);

    @GetMapping(PREFIX + "/list")
    @Operation(summary = "获得部门信息数组")
    @Parameter(name = "ids", description = "部门编号数组", example = "1,2", required = true)
    CommonResult<List<DeptRespDTO>> getDeptList(@RequestParam("ids") Collection<Long> ids);

    @GetMapping(PREFIX + "/valid")
    @Operation(summary = "校验部门是否合法")
    @Parameter(name = "ids", description = "部门编号数组", example = "1,2", required = true)
    CommonResult<Boolean> validateDeptList(@RequestParam("ids") Collection<Long> ids);

    @GetMapping(PREFIX + "/getByLeaderId")
    @Operation(summary = "获取特定部门信息")
    @Parameter(name = "leaderUserId", description = "部门负责人", example = "12", required = true)
    CommonResult<List<DeptRespDTO>> getDeptByLeaderId(@RequestParam("leaderUserId")Long leaderUserId);

    @GetMapping(PREFIX + "/getList")
    @Operation(summary = "获取所有部门信息")
    CommonResult<List<DeptRespDTO>> getDeptList();

    @PostMapping(PREFIX + "/create")
    @Operation(summary = "创建部门")
    CommonResult<Long> createDept(@RequestBody DeptRespDTO deptRespDTO);

    @PostMapping(PREFIX + "/update")
    @Operation(summary = "修改部门信息")
    void updateDept(@RequestBody DeptRespDTO deptRespDTO);

    @PostMapping(PREFIX + "/delete")
    @Operation(summary = "删除部门信息")
    @Parameter(name = "factoryId", description = "工厂编号", example = "100001", required = true)
    void deleteDept(@RequestParam("factoryId") Long factoryId);

    @PostMapping(PREFIX + "/getByFactoryId")
    @Operation(summary = "根据工厂ID获得部门信息")
    @Parameter(name = "factoryId", description = "工厂编号", example = "100001", required = true)
    CommonResult<DeptRespDTO> getDeptByFactoryId(@RequestParam("factoryId") Long factoryId);

    /**
     * 获得指定编号的部门 Map
     *
     * @param ids 部门编号数组
     * @return 部门 Map
     */
    default Map<Long, DeptRespDTO> getDeptMap(Set<Long> ids) {
        List<DeptRespDTO> list = getDeptList(ids).getCheckedData();
        return CollectionUtils.convertMap(list, DeptRespDTO::getId);
    }
}
