package jnpf.cultivate.utils;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import jnpf.model.personnels.po.FtbPersonnelsStaffRoster;
import jnpf.permission.vo.v2.organzie.OrganizeGeneralDetailVO;
import jnpf.permission.vo.v2.position.PositionBaseInfoVO;
import jnpf.permission.vo.v2.position.PositionVO;
import jnpf.personnels.service.FtbPersonnelsStaffRosterService;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @Title: CultivatePerUtils
 * @Author: peng.hao
 * @create: 2024/3/21 11:25
 */
@Component
public class CultivatePerUtils {


    @Autowired
    private UserApiV2Util userApiV2Util;

    @Autowired
    FtbPersonnelsStaffRosterService staffRosterService;


    /**
     * 转换组织id集合
     *
     * @param str 组织编码
     * @return 组织id集合
     */
    public List<String> convertOrganizationalIdCollection(String str) {
        String[] split = str.split(",");
        // 组织id输入系统展示id
        List<String> list = Arrays.asList(split);
        List<OrganizeGeneralDetailVO> organizeEntityList = userApiV2Util.organizesOrHaveChildByOrganizeEncodes(list, null);
        return organizeEntityList.stream().map(OrganizeGeneralDetailVO::getId).collect(Collectors.toList());
    }

    /**
     * 转换岗位id集合
     *
     * @param str
     * @return
     */
    public List<String> convertPositionIdCollection(String str) {
        String[] split = str.split(",");
        // 岗位id输入系统展示id
        List<String> list = Arrays.asList(split);
//        List<PositionEntity> positionEntities = positionApi.checkOrgPositionGradesBound(list);
        List<PositionBaseInfoVO> positionEntities = userApiV2Util.listPositionBaseInfoByEncodes(list, null);
        return positionEntities.stream().map(PositionBaseInfoVO::getId).collect(Collectors.toList());
    }

    /**
     * 转换员工id集合
     *
     * @param str
     * @return
     */
    public List<String> convertEmployeeIdCollection(String str) {
        String[] split = str.split(",");
        // 员工id输入系统展示id
        List<String> list = Arrays.asList(split);
        List<FtbPersonnelsStaffRoster> ftbPersonnelsStaffRosters = staffRosterService.queryUserInfoByWorkerId(list);
        return ftbPersonnelsStaffRosters.stream().map(FtbPersonnelsStaffRoster::getUserId).collect(Collectors.toList());
    }

    /**
     * 根据组织id查询展示组织id
     *
     * @param list
     * @return
     */
    public Map<String, String> convertOrganizationalId(List<String> list) {
//        List<OrganizeEntity> organizeByIds = organizeApi.getOrganizeByIds(list);
        List<OrganizeGeneralDetailVO> organizeByIds = userApiV2Util.organizesByOrganizeIds(list, null);
        if (CollUtil.isEmpty(organizeByIds)) {
            return new HashMap<>();
        }
        Map<String, String> map = new HashMap<>(); //组织id-》组织编码
        for (OrganizeGeneralDetailVO organizeById : organizeByIds) {
            if (StringUtils.isEmpty(organizeById.getEnCode())) {
                map.put(organizeById.getId(), "");
            } else {
                map.put(organizeById.getId(), organizeById.getEnCode());
            }
        }
        return map;
    }


    /**
     * 根据岗位id查询展示岗位id
     *
     * @param list
     * @return
     */
    public Map<String, String> convertPostId(List<String> list) {
//        ActionResult<List<PositionInfoNewVO>> positionListByIds = positionApi.getPositionListByIds(list);
        List<PositionVO> positionListByIds = userApiV2Util.listPositionDetailInfoByIds(list, null);
        if (CollUtil.isEmpty(positionListByIds)) {
            return new HashMap<>();
        }
        return positionListByIds.stream().map(item -> {
            PostSysEntity postSysEntity = new PostSysEntity();
            postSysEntity.setId(item.getId());
            postSysEntity.setEncode(item.getEnCode());
            return postSysEntity;
        }).collect(Collectors.toMap(
                CultivatePerUtils.PostSysEntity::getId,
                CultivatePerUtils.PostSysEntity::getEncode,
                (a, b) -> a));
    }


    /**
     * 根据员工id查询展示员工id
     *
     * @param list
     * @return
     */
    public Map<String, String> coverPersonalIds(List<String> list) {
        return staffRosterService.queryWorkerIdByUserIds(list).stream().map(item -> {
            UserSysEntity userSysEntity = new UserSysEntity();
            userSysEntity.setId(item.getUserId());
            userSysEntity.setEncode(item.getSystemWokerId());
            return userSysEntity;
        }).collect(Collectors.toMap(UserSysEntity::getId, UserSysEntity::getEncode, (a, b) -> a));
    }

    /**
     * 获取当前组织下的所有组织
     *
     * @param organizeId
     * @return
     */
    public List<String> getAllOrganize(String organizeId) {
//        List<String> subsetsBasedOnParent = organizeApi.findSubsetsBasedOnParent(List.of(organizeId));
        List<OrganizeGeneralDetailVO> organizeGeneralDetailVOS = userApiV2Util.organizesOrHaveChildByOrganizeIds(List.of(organizeId), true, null);
        if (ObjectUtil.isEmpty(organizeGeneralDetailVOS)) {
            return new ArrayList<>();
        }
        return organizeGeneralDetailVOS.stream().map(item -> item.getId()).collect(Collectors.toList());
    }

    /**
     * 获取当前组织下的所有组织
     *
     * @param orgIds 组织ID列表
     * @return 组织ID列表，包括当前组织及其所有子组织
     */
    public List<String> batchAllOrganize(List<String> orgIds) {
        return getAllOrganizationInformation(orgIds);
    }


    /**
     * 获取当前组织下的所有组织
     *
     * @param orgIds 组织ID列表
     * @return 组织ID列表，包括当前组织及其所有子组织
     */
    public List<String> getAllOrganizationInformation(List<String> orgIds) {
        List<OrganizeGeneralDetailVO> organizeGeneralDetailVOS = userApiV2Util.organizesOrHaveChildByOrganizeIds(orgIds, true, null);
        if (ObjectUtil.isEmpty(organizeGeneralDetailVOS)) {
            return new ArrayList<>();
        }
        return organizeGeneralDetailVOS.stream().map(OrganizeGeneralDetailVO::getId).collect(Collectors.toList());
    }

    /**
     * 根据用户id查询员工姓名
     *
     * @param userIds
     * @return
     */
    public Map<String, String> queryRosterNameBasedOnUserId(List<String> userIds) {
        LambdaQueryWrapper<FtbPersonnelsStaffRoster> wrapper = new LambdaQueryWrapper<>();
        wrapper.in(FtbPersonnelsStaffRoster::getUserId, userIds);
        List<FtbPersonnelsStaffRoster> rosters = staffRosterService.list(wrapper);
        if (CollUtil.isEmpty(rosters)) {
            return new HashMap<>();
        }
        return rosters.stream().collect(Collectors.toMap(FtbPersonnelsStaffRoster::getUserId, FtbPersonnelsStaffRoster::getName, (a, b) -> a));
    }

    /**
     * 根据组织id查询组织名称
     *
     * @param ids
     * @return
     */
    public Map<String, String> queryOrgNames(List<String> ids) {
//        List<OrganizeEntity> entityList = organizeApi.getOrganizeName(ids);
        List<OrganizeGeneralDetailVO> entityList = userApiV2Util.organizesByOrganizeIds(ids, null);
        if (CollUtil.isEmpty(entityList)) {
            return new HashMap<>();
        }
        return entityList.stream().collect(Collectors.toMap(OrganizeGeneralDetailVO::getId, OrganizeGeneralDetailVO::getName, (a, b) -> a));
    }

    public static BigDecimal computeDivision(Integer a, Integer b) {
        // a/b 除数为0
        if (b == 0) {
            return BigDecimal.ZERO;
        }
        BigDecimal a0 = new BigDecimal(a);
        BigDecimal b0 = new BigDecimal(b);
        BigDecimal divide = a0.divide(b0, 2, RoundingMode.HALF_UP);
        return divide.multiply(BigDecimal.valueOf(100));
    }

    public static BigDecimal computeDivision(BigDecimal a) {
        // a/b 除数为0
        if (BigDecimal.ZERO.compareTo(a) == 0) {
            return BigDecimal.ZERO;
        }
        BigDecimal a0 = a;
        BigDecimal b0 = new BigDecimal(60 * 60);
        BigDecimal divide = a0.divide(b0, 2, RoundingMode.HALF_UP);
        return divide;
    }

    @Data
    public class OrganizeSysEntity {
        private String id;
        private String encode;
    }

    @Data
    public class PostSysEntity {
        private String id;
        private String encode;
    }

    @Data
    public class UserSysEntity {
        private String id;
        private String encode;
    }
}
