package jnpf.util;

import cn.hutool.core.collection.CollectionUtil;
import jnpf.authority.FtbAuthorityApi;
import jnpf.base.ActionResult;
import jnpf.permission.V2UserApi;
import jnpf.permission.eum.v2.UserWorkStatusEnums;
import jnpf.permission.model.user.PartUserInfoVo;
import jnpf.permission.vo.v2.user.UserBoundVO;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * V2版本用户查询工具类，提供多种方式获取用户信息的方法。
 */
@Component
public class V2UserQueryUtil {

    @Resource
    private V2UserApi v2UserApi;
    @Resource
    private FtbAuthorityApi authorityApi;

    /**
     * 根据用户ID获取单个用户信息
     * @param id 用户唯一标识符
     * @return PartUserInfoVo 用户部分信息对象
     */
    public PartUserInfoVo getUserById(String id) {
        return getUserListToPartUserInfoVo(List.of(id)).get(0);
    }

    /**
     * 批量根据用户ID列表获取用户信息列表
     * @param userIds 用户ID集合
     * @return List<PartUserInfoVo> 用户信息列表
     */
    public List<PartUserInfoVo> getUserListToPartUserInfoVo(List<String> userIds) {
        ActionResult<List<UserBoundVO>> allUserInfoBatch = v2UserApi.getAllUserInfoBatch(userIds, UserProvider.getUser().getTenantId());
        List<UserBoundVO> userList = allUserInfoBatch.getData();
        if (CollectionUtil.isEmpty(userList)) {
            return new ArrayList<>();
        }
        return userList.stream().map(user -> {
            PartUserInfoVo vo = new PartUserInfoVo();
            vo.setUserId(user.getId());
            vo.setRealName(user.getName());
            vo.setNickName(user.getNickname());
            vo.setOrganizeId(user.getOrganizeId());
            vo.setOrganizeName(user.getOrganizeName());
            vo.setPositionId(user.getPositionId());
            vo.setPositionName(user.getPositionName());
            vo.setHeadIcon(user.getHeadIcon());
            vo.setMobilePhone(user.getPhone());
            vo.setAccount(user.getAccount());
            return vo;
        }).collect(Collectors.toList());
    }

    /**
     * 获取用户列表（包含离职用户）
     * @param userIds 用户ID集合
     * @param tenantId 租户ID
     * @return List<PartUserInfoVo> 用户信息列表（含离职用户）
     */
    public List<PartUserInfoVo> getUserListAndCopy(List<String> userIds, String tenantId) {
        List<UserBoundVO> userBoundVos = v2UserApi.userListAndCopy(userIds, null, tenantId);
        if (CollectionUtil.isEmpty(userBoundVos)) {
            return new ArrayList<>();
        }
        return userBoundVos.stream().map(user -> {
            PartUserInfoVo vo = new PartUserInfoVo();
            vo.setUserId(user.getId());
            vo.setRealName(user.getName());
            vo.setNickName(user.getNickname());
            vo.setOrganizeId(user.getOrganizeId());
            vo.setOrganizeName(user.getOrganizeName());
            vo.setPositionId(user.getPositionId());
            vo.setPositionName(user.getPositionName());
            vo.setHeadIcon(user.getHeadIcon());
            vo.setMobilePhone(user.getPhone());
            vo.setAccount(user.getAccount());
            return vo;
        }).collect(Collectors.toList());
    }

    /**
     * 获取指定组织及其子组织下的所有用户
     * @param organizeId 组织ID
     * @return List<UserBoundVO> 用户绑定信息列表
     */
    public List<UserBoundVO> getOrganizeChildUserList(String organizeId) {
        return authorityApi.listTargetOrganizeIdAuthApi(organizeId, List.of(UserWorkStatusEnums.ONBOARDING_FAILED,
                UserWorkStatusEnums.PENDING_RESIGNATION,
                UserWorkStatusEnums.RESIGNED));
    }
}
