package jnpf.cultivate.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.google.common.base.Joiner;
import jnpf.cultivate.service.CultivateIdentifyApiService;
import jnpf.cultivate.utils.UserApiV2Util;
import jnpf.model.cultivate.vo.identify.UserOrgInfoAllVo;
import jnpf.permission.PositionApi;
import jnpf.permission.UserApi;
import jnpf.permission.model.position.PositionGradesInfoBoundVO;
import jnpf.permission.model.user.UserInfoVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.IntStream;

@Service
public class CultivateIdentifyApiServiceImpl implements CultivateIdentifyApiService {

    @Autowired
    private UserApi userApi;

    @Autowired
    private UserApiV2Util userApiV2Util;

    @Qualifier("jnpf.permission.PositionApi")
    @Autowired
    private PositionApi positionApi;

    @Override
    @Deprecated(since = "培训优化1.1", forRemoval = true)
    public UserInfoVO selectUserDirectLeadership(String userId) {
        return userApiV2Util.getBossByUserId(userId, null);
    }

    @Override
    public UserInfoVO selectUserDirectLeadershipNew(String orgId, String userId, String positionId, String positionGradesId) {
        return userApi.getLeaderInfo(orgId, userId, positionId, positionGradesId);
    }

    @Override
    public Map<String, List<UserOrgInfoAllVo>> batchSelectUserOrgInfoByUserIds(List<String> userIds) {
        List<PositionGradesInfoBoundVO> organizeListApiVOList = positionApi.getOrgPositionInfoList(Joiner.on(",").join(userIds));
        List<UserOrgInfoAllVo> userOrgInfoAllVoList = CollectionUtil.newArrayList();
        if (CollectionUtil.isEmpty(organizeListApiVOList)) {
            return null;
        }
        for (PositionGradesInfoBoundVO item : organizeListApiVOList) {
            List<String> organizeIds = item.getOrganizeIds();
            List<String> organizeNames = item.getOrganizeNames();
            if (CollectionUtil.isEmpty(organizeIds)) {
                continue;
            }
            IntStream.range(0, organizeIds.size()).forEach(i -> {
                UserOrgInfoAllVo orgInfoAllVo = new UserOrgInfoAllVo();
                orgInfoAllVo.setUseId(item.getUserId());
                orgInfoAllVo.setUserOrgCode(organizeIds.get(i));
                orgInfoAllVo.setUserOrgName(organizeNames.get(i));
                orgInfoAllVo.setUserPostCode(item.getPositionId());
                orgInfoAllVo.setUserPostName(item.getPositionName());
                orgInfoAllVo.setUserOfficialRankCode(item.getPositionGradesId());
                orgInfoAllVo.setUserOfficialRankName(item.getPositionGradesName());
                userOrgInfoAllVoList.add(orgInfoAllVo);
            });
        }
        return userOrgInfoAllVoList.stream().collect(Collectors.groupingBy(UserOrgInfoAllVo::getUseId));
    }

    @Override
    public Map<String, List<UserOrgInfoAllVo>> batchSelectOfficialRankInfo(List<String> officialRankCodes) {
        List<PositionGradesInfoBoundVO> organizeListApiVOList = positionApi.listPositionGradesInfoByPositionGrades(Joiner.on(",").join(officialRankCodes));
        List<UserOrgInfoAllVo> userOrgInfoAllVoList = CollectionUtil.newArrayList();
        if (CollectionUtil.isEmpty(organizeListApiVOList)) {
            return null;
        }
        for (PositionGradesInfoBoundVO item : organizeListApiVOList) {
            List<String> organizeIds = item.getOrganizeIds();
            List<String> organizeNames = item.getOrganizeNames();
            if (CollectionUtil.isEmpty(organizeIds)) {
                continue;
            }
            IntStream.range(0, organizeIds.size()).forEach(i -> {
                UserOrgInfoAllVo orgInfoAllVo = new UserOrgInfoAllVo();
                orgInfoAllVo.setUseId(item.getUserId());
                orgInfoAllVo.setUserOrgCode(organizeIds.get(i));
                orgInfoAllVo.setUserOrgName(organizeNames.get(i));
                orgInfoAllVo.setUserPostCode(item.getPositionId());
                orgInfoAllVo.setUserPostName(item.getPositionName());
                orgInfoAllVo.setUserOfficialRankCode(item.getPositionGradesId());
                orgInfoAllVo.setUserOfficialRankName(item.getPositionGradesName());
                userOrgInfoAllVoList.add(orgInfoAllVo);
            });
        }
        return userOrgInfoAllVoList.stream().collect(Collectors.groupingBy(UserOrgInfoAllVo::getUseId));
    }
}
