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

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.collection.CollectionUtils;
import cn.iocoder.yudao.module.system.api.user.dto.AdminUserRespDTO;
import cn.iocoder.yudao.module.system.api.user.dto.UserSaveRespDTO;
import cn.iocoder.yudao.module.system.api.user.vo.AdminUserRpcVO;
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 AdminUserApi {

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

    @GetMapping(PREFIX + "/get")
    @Operation(summary = "通过用户 ID 查询用户")
    @Parameter(name = "id", description = "用户编号", example = "1", required = true)
    CommonResult<AdminUserRespDTO> getUser(@RequestParam("id") Long id);

    @GetMapping(PREFIX + "/list")
    @Operation(summary = "通过用户 ID 查询用户们")
    @Parameter(name = "ids", description = "部门编号数组", example = "1,2", required = true)
    CommonResult<List<AdminUserRespDTO>> getUserList(@RequestParam("ids") Collection<Long> ids);

    @GetMapping(PREFIX + "/list-by-dept-id")
    @Operation(summary = "获得指定部门的用户数组")
    @Parameter(name = "deptIds", description = "部门编号数组", example = "1,2", required = true)
    CommonResult<List<AdminUserRespDTO>> getUserListByDeptIds(@RequestParam("deptIds") Collection<Long> deptIds);

    @GetMapping(PREFIX + "/getByDeptIdAll")
    @Operation(summary = "获得指定部门及下级部门开启状态的用户数组（除去当前登录者用户）")
    @Parameter(name = "deptId", description = "部门编号", required = true, example = "117")
    CommonResult<List<AdminUserRespDTO>> getUserByDeptIdAll(@RequestParam("deptId") Long deptId);

    @GetMapping(PREFIX + "/list-by-post-id")
    @Operation(summary = "获得指定岗位的用户数组")
    @Parameter(name = "postIds", description = "岗位编号数组", example = "2,3", required = true)
    CommonResult<List<AdminUserRespDTO>> getUserListByPostIds(@RequestParam("postIds") Collection<Long> postIds);

    /**
     * 获得用户 Map
     *
     * @param ids 用户编号数组
     * @return 用户 Map
     */
    default Map<Long, AdminUserRespDTO> getUserMap(Collection<Long> ids) {
        List<AdminUserRespDTO> users = getUserList(ids).getCheckedData();
        return CollectionUtils.convertMap(users, AdminUserRespDTO::getId);
    }

    @GetMapping(PREFIX + "/valid")
    @Operation(summary = "校验用户们是否有效")
    @Parameter(name = "ids", description = "用户编号数组", example = "3,5", required = true)
    CommonResult<Boolean> validateUserList(@RequestParam("ids") Set<Long> ids);


    @GetMapping(PREFIX + "/getUserIdsByUserIdGroupByDept")
    @Operation(summary = "获取同部门所有用户id")
    @Parameter(name = "userId", description = "用户id", example = "1024", required = true)
    CommonResult<List<Long>> getUserIdsByUserIdGroupByDept(@RequestParam("userId") Long userId);

    @PostMapping(PREFIX + "/create")
    @Operation(summary = "创建用户")
    CommonResult<Long> createUser(@RequestBody UserSaveRespDTO saveRespDTO);

    @PostMapping(PREFIX + "/updateFieldwork")
    @Operation(summary = "修改用户外勤打卡权限")
    @Parameter(name = "userId", description = "用户id", example = "1024", required = true)
    @Parameter(name = "fieldworkFlag", description = "是否可外勤打卡 | 0否 1是", example = "1", required = true)
    void updateFieldworkType(@RequestParam("userId") Long userId,
                         @RequestParam("fieldworkFlag") Integer fieldworkFlag);

    @PostMapping(PREFIX + "/updateUserStaffing")
    @Operation(summary = "修改用户信息")
    @Parameter(name = "userId", description = "用户id", example = "1024", required = true)
    @Parameter(name = "userStaffing", description = "用户编制", example = "1", required = true)
    void updateUserStaffing(@RequestParam("userId") Long userId,
                             @RequestParam("userStaffing") Integer userStaffing);

    @GetMapping(PREFIX + "/getUserIdsByUserNature")
    @Operation(summary = "获取所有用户性质为外勤的用户")
    @Parameter(name = "userNature", description = "用户性质", example = "3", required = true)
    CommonResult<List<Long>> getUserIdsByUserNature(@RequestParam("userNature") Integer userNature);

    @GetMapping(PREFIX + "/getFactoryUsers")
    @Operation(summary = "获取同部门所有用户id")
    @Parameter(name = "factoryId", description = "工厂id", example = "1024", required = false)
    CommonResult<List<AdminUserRpcVO>> getFactoryUsers(@RequestParam(name = "factoryId", required = false) Long factoryId);

}
