package jnpf.authority.service.impl;

import cn.hutool.core.collection.CollUtil;
import jnpf.authority.service.FtbPermissionUsersService;
import jnpf.base.ActionResult;
import jnpf.base.vo.PageListVO;
import jnpf.model.authority.dto.permission.AuthGetTargetUserInfoBatchDTO;
import jnpf.model.personnels.dto.turnover.FtbDepUserDTO;
import jnpf.permission.V2UserApi;
import jnpf.permission.dto.v2.user.*;
import jnpf.permission.eum.v2.UserWorkStatusEnums;
import jnpf.permission.vo.v2.TargetAuthIdsVO;
import jnpf.permission.vo.v2.user.UserBoundVO;
import jnpf.personnels.service.FtbPersonnelsTurnoverManagementService;
import jnpf.util.UserProvider;
import jnpf.util.auth.V2AuthPermissionUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

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

/**
 * 带权限用的用户管理功能
 *
 * @author Flynn Chan
 * @create 2025-05-21
 */
@Service
@Slf4j
public class FtbPermissionUsersServiceImpl implements FtbPermissionUsersService {
    @Resource
    private V2AuthPermissionUtils authPermissionUtils;
    @Resource
    private V2UserApi v2UserApi;
    @Resource
    private FtbPersonnelsTurnoverManagementService turnoverManagementService;

    @Override
    public PageListVO<UserBoundVO> pagePost(QueryPageUserDTO dto) {
        TargetAuthIdsVO targetAuthIdsVO = authPermissionUtils.processAuthIds();
        dto.setTenantId(UserProvider.getUser().getTenantId());
        dto.setHavaAuth(targetAuthIdsVO != null);
        dto.setTargetAuthIdsVO(targetAuthIdsVO);
        return v2UserApi.pagePost(dto).getData();
    }

    @Override
    public PageListVO<UserBoundVO> pagePostMoreKeyword(QueryPageUserMoreKeywordDTO dto) {
        TargetAuthIdsVO targetAuthIdsVO = authPermissionUtils.processAuthIds();
        dto.setTenantId(UserProvider.getUser().getTenantId());
        dto.setHavaAuth(targetAuthIdsVO != null);
        dto.setTargetAuthIdsVO(targetAuthIdsVO);
        return v2UserApi.pagePostMoreKeyword(dto).getData();
    }

    @Override
    public List<UserBoundVO> authGetAllUserInfoBatch() {
        TargetAuthIdsVO targetAuthIdsVO = authPermissionUtils.processAuthIds();
        AuthUserBoundInfoBatchDTO dto = new AuthUserBoundInfoBatchDTO(targetAuthIdsVO, UserProvider.getUser().getTenantId());
        dto.setHavaAuth(targetAuthIdsVO != null);
        return v2UserApi.authGetAllUserInfoBatch(dto).getData();
    }

    @Override
    public List<UserBoundVO> authGetAllUserInfoBatch(List<UserWorkStatusEnums> userWorkStatusEnumsNotList) {
        TargetAuthIdsVO targetAuthIdsVO = authPermissionUtils.processAuthIds();
        AuthUserBoundInfoBatchDTO dto = new AuthUserBoundInfoBatchDTO(targetAuthIdsVO, UserProvider.getUser().getTenantId());
        dto.setHavaAuth(targetAuthIdsVO != null);
        dto.setUserWorkStatusEnumsList(userWorkStatusEnumsNotList);
        return v2UserApi.authGetAllUserInfoBatch(dto).getData();
    }

    @Override
    public List<String> authGetPermissionScopeUserIds() {
        TargetAuthIdsVO targetAuthIdsVO = authPermissionUtils.processAuthIds();
        AuthUserBoundInfoBatchDTO dto = new AuthUserBoundInfoBatchDTO(targetAuthIdsVO, UserProvider.getUser().getTenantId());
        dto.setHavaAuth(targetAuthIdsVO != null);
        ActionResult<List<String>> result = v2UserApi.authGetPermissionScopeUserIds(dto);
        return (result != null && result.getData() != null) ? result.getData() : new ArrayList<>();
    }

    @Override
    public List<UserBoundVO> listTargetOrganizeIdAuth(String organizeId, List<UserWorkStatusEnums> userWorkStatusEnumsList) {
        TargetAuthIdsVO targetAuthIdsVO = authPermissionUtils.processAuthIds();
        AuthUserListTargetOrganizeDTO dto = new AuthUserListTargetOrganizeDTO(targetAuthIdsVO);
        dto.setHavaAuth(targetAuthIdsVO != null);
        dto.setOrganizeId(organizeId);
        if (CollUtil.isNotEmpty(userWorkStatusEnumsList)) {
            dto.setUserWorkStatusEnumsList(userWorkStatusEnumsList);
        }
        return v2UserApi.listTargetOrganizeIdAuth(dto);
    }

    @Override
    public List<UserBoundVO> listTargetOrganizeIdsAuth(List<String> organizeIds, List<UserWorkStatusEnums> userWorkStatusEnumsList) {
        TargetAuthIdsVO targetAuthIdsVO = authPermissionUtils.processAuthIds();
        AuthUserListTargetOrganizeIdsDTO dto = new AuthUserListTargetOrganizeIdsDTO(targetAuthIdsVO);
        dto.setHavaAuth(targetAuthIdsVO != null);
        dto.setOrganizeIds(organizeIds);
        if (CollUtil.isNotEmpty(userWorkStatusEnumsList)) {
            dto.setUserWorkStatusEnumsList(userWorkStatusEnumsList);
        }

        return v2UserApi.listTargetOrganizeIdsAuth(dto);
    }

    @Override
    public List<UserBoundVO> authGetTargetUserInfoBatch(AuthGetTargetUserInfoBatchDTO dto) {
        TargetAuthIdsVO targetAuthIdsVO = authPermissionUtils.processAuthIds();
        AuthTargetUserBatchDTO authTargetUserBatchDTO = new AuthTargetUserBatchDTO(targetAuthIdsVO, UserProvider.getUser().getTenantId());
        authTargetUserBatchDTO.setUserIds(dto.getUserIds());

        return v2UserApi.authGetTargetUserInfoBatch(authTargetUserBatchDTO);
    }

    @Override
    public List<UserBoundVO> getTargetUserInfoBatchByIds(List<String> ids) {
        if (CollUtil.isEmpty(ids)) {
            log.error("getTargetUserInfoBatchByIds(List<String> ids), ids为空");
            return new ArrayList<>();
        }
        List<UserBoundVO> userBoundVOS = v2UserApi.getAllUserInfoBatch(ids, UserProvider.getUser().getTenantId()).getData();
        //查询离职人员
        // 提取已查到的用户ID
        Set<String> foundIds = userBoundVOS.stream()
                .map(UserBoundVO::getId)
                .collect(Collectors.toSet());

        // 找出没查到的（可能离职或被删除的）
        List<String> missingIds = ids.stream()
                .filter(id -> !foundIds.contains(id))
                .collect(Collectors.toList());

        if (CollUtil.isNotEmpty(missingIds)) {
            FtbDepUserDTO ftbDepUserDTO = new FtbDepUserDTO();
            ftbDepUserDTO.setUserIds(missingIds);
            List<UserBoundVO> ftbDepUsers = turnoverManagementService.getInformationAboutTheDepartingPerson(ftbDepUserDTO);
            if (CollUtil.isNotEmpty(ftbDepUsers)) {
                userBoundVOS.addAll(ftbDepUsers);
            }
        }
        return userBoundVOS;
    }
}
