package jnpf.model.cultivate.v2.promotion.vo;

import cn.hutool.core.collection.CollUtil;
import jnpf.model.cultivate.po.promotion.FtbCultivatePromotionMemberNew;
import jnpf.model.cultivate.po.promotion.FtbCultivatePromotionPostNew;
import jnpf.permission.vo.v2.grades.GradeVO;
import jnpf.permission.vo.v2.position.PositionVO;
import lombok.Data;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;


@Data
public class PromotionLevelInfoVo {
    /**
     * 岗位id
     */
    private String postId;
    /**
     * 岗位名称
     */
    private String postName;

    /**
     * 职级id
     */
    private String gradeId;

    /**
     * 职级名称
     */
    private String gradeName;

    /**
     * 选择标识 0：未选择 1：已选择
     */
    private Integer userSelect;

    /**
     * 进度
     */
    private String progress;


    public static List<PromotionLevelInfoVo> convertToPositionAndGrade(List<FtbCultivatePromotionPostNew> postList,
                                                                       Map<String, GradeVO> stringGradeVOMap,
                                                                       Map<String, PositionVO> stringPositionVOMap,
                                                                       List<FtbCultivatePromotionMemberNew> memberNewList,
                                                                       Map<String, PositionProgressVo> progressVoMap
    ) {
        List<String> hasSelectId = new ArrayList<>();
        if (CollUtil.isNotEmpty(memberNewList)) {
            hasSelectId = memberNewList.stream().map(FtbCultivatePromotionMemberNew::getPromotionPostId).collect(Collectors.toList());
        }
        List<PromotionLevelInfoVo> ret = new ArrayList<>();
        if (CollUtil.isEmpty(postList)) {
            return ret;
        }
        for (FtbCultivatePromotionPostNew ftbCultivatePromotionPostNew : postList) {
            PositionProgressVo positionProgressVo = progressVoMap.get(ftbCultivatePromotionPostNew.getPostId());
            PromotionLevelInfoVo vo = new PromotionLevelInfoVo();
            vo.setPostId(ftbCultivatePromotionPostNew.getPostId());
            PositionVO positionVO = stringPositionVOMap.get(ftbCultivatePromotionPostNew.getPostId());
            if (positionVO != null) {
                vo.setPostName(positionVO.getFullName());
            }
            if (StringUtils.isNotEmpty(ftbCultivatePromotionPostNew.getGradeId())) {
                vo.setGradeId(ftbCultivatePromotionPostNew.getGradeId());
                GradeVO gradeVO = stringGradeVOMap.get(ftbCultivatePromotionPostNew.getGradeId());
                if (gradeVO != null) {
                    vo.setGradeName(gradeVO.getFullName());
                }
            }
            if (hasSelectId.contains(ftbCultivatePromotionPostNew.getId())) {
                vo.setUserSelect(1);
            }else{
                vo.setUserSelect(0);
            }
            if (positionProgressVo != null) {
                vo.setProgress(dealWithRate(new BigDecimal(positionProgressVo.getComplete()).divide(new BigDecimal(positionProgressVo.getTotal()), 4, RoundingMode.HALF_UP), ""));
            }
            ret.add(vo);
        }
        return ret;
    }

    public static String dealWithRate(BigDecimal rate, String suffix) {
        if (ObjectUtils.isEmpty(rate) || rate.compareTo(BigDecimal.ZERO) == 0) {
            return "0" + suffix;
        }
        BigDecimal result = rate.multiply(BigDecimal.valueOf(100));
        return result.setScale(2, RoundingMode.HALF_UP).toString().concat(suffix);
    }

    public static Map<String, List<V2CultivatePostAndGradeVo>> convertToPositionAndGradeNextUser(Map<String, GradeVO> stringGradeVOMap, Map<String, PositionVO> stringPositionVOMap, List<FtbCultivatePromotionMemberNew> memberNewList) {


        Map<String, List<FtbCultivatePromotionMemberNew>> memberNewMap = memberNewList.stream().collect(Collectors.groupingBy(FtbCultivatePromotionMemberNew::getPromotionId));
        Map<String, List<V2CultivatePostAndGradeVo>> ret = new HashMap<>();
        for (String promotionId : memberNewMap.keySet()) {
            List<FtbCultivatePromotionMemberNew> memberNewItemList = memberNewMap.get(promotionId);
            if (CollUtil.isEmpty(memberNewItemList)) {
                continue;
            }
            List<V2CultivatePostAndGradeVo> retItem = new ArrayList<>();
            for (FtbCultivatePromotionMemberNew memberNew : memberNewItemList) {
                V2CultivatePostAndGradeVo item = new V2CultivatePostAndGradeVo();
                item.setPostId(memberNew.getPositionId());
                PositionVO positionVO = stringPositionVOMap.get(memberNew.getPositionId());
                if (positionVO != null) {
                    item.setPostName(positionVO.getFullName());
                } else {
                    item.setPostName(memberNew.getPositionName());
                }
                item.setGradeId(memberNew.getGradeId());
                GradeVO gradeVO = stringGradeVOMap.get(memberNew.getGradeId());
                if (gradeVO != null) {
                    item.setGradeName(gradeVO.getFullName());
                } else {
                    item.setGradeName(memberNew.getGradeName());
                }
                retItem.add(item);
            }
            ret.put(promotionId, retItem);
        }
        return ret;

    }
}
