package jnpf.util;


import jnpf.model.personnels.dto.range.FtbRangeConfigDIYDTO;
import jnpf.model.personnels.dto.range.RangeDTO;
import jnpf.model.personnels.vo.range.FtbRangeConfigDIYVO;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * 范围计算
 * @Author:peng.hao
 */
public class RangeConfigUtil {
    /**
     * 平均计算
     *
     * @param initialValue      起始值
     * @param increment         步长值
     * @param numberOfIntervals 档位数
     * @param previewValue
     */
    public static List<RangeDTO> generateRanges(int initialValue, int increment, int numberOfIntervals, String previewValue) {
        List<RangeDTO> RangeDTOList = Collections.synchronizedList(new ArrayList<>());
        int start = initialValue - increment;
        int end = initialValue;
        String[] split = previewValue.split("、");
        RangeDTO priceBandStartDTO = new RangeDTO(0, end, "0_" + end, split[0]);
        RangeDTOList.add(priceBandStartDTO);
        for (int i = 1; i < numberOfIntervals - 1; i++) {
            start += increment;
            end = start + increment;
            RangeDTO rangeDTO = new RangeDTO(start, end, start + "_" + end, split[i]);
            RangeDTOList.add(rangeDTO);
        }
        RangeDTO rangeDTO = new RangeDTO(end, null, end + "", split[split.length - 1]);
        RangeDTOList.add(rangeDTO);
        return RangeDTOList;
    }

    /**
     * 计算区间
     *
     * @param priceBandVOListConfig 自定义区间
     */
    public static List<RangeDTO> generateTakeOutRangesDto(List<FtbRangeConfigDIYDTO> priceBandVOListConfig) {
        List<RangeDTO> RangeDTOList = new ArrayList<>();
        List<FtbRangeConfigDIYDTO> collected = priceBandVOListConfig.stream().sorted(Comparator.comparing(FtbRangeConfigDIYDTO::getIntervalCount)).collect(Collectors.toList());
        Map<Integer, FtbRangeConfigDIYDTO> bandVOMap = collected.stream().collect(Collectors.toMap(FtbRangeConfigDIYDTO::getIntervalCount, Function.identity()));
        Set<Integer> keySet = bandVOMap.keySet();
        for (Integer integer : keySet) {
            FtbRangeConfigDIYDTO priceBandVO = bandVOMap.get(integer);
            Integer initialValue = priceBandVO.getInitialValue();
            // 10以下
            if (priceBandVO.getIntervalCount() == 1) {
                RangeDTO RangeDTO = new RangeDTO(0, initialValue, "0_" + initialValue, initialValue + "以下");
                RangeDTOList.add(RangeDTO);
            }
            // 结束值
            Integer endInterval = priceBandVO.getEndInterval();
            // 间隔值
            Integer intervalValue = priceBandVO.getIntervalValue();
            if (priceBandVO.getIsAbove() == 1) {
                initialValue = priceBandVO.getStartInterval();
                for (int i = initialValue; i < endInterval; i += intervalValue) {
                    if (i < initialValue) continue;
                    RangeDTO RangeDTO = new RangeDTO(i,
                            i + intervalValue,
                            i + "_" + (i + intervalValue),
                            i + "-" + (i + intervalValue));
                    RangeDTOList.add(RangeDTO);
                    // 最后一个
                    if (integer == keySet.size()) {
                        RangeDTO newRangeDTO = new RangeDTO((i + intervalValue),
                                (i + intervalValue + intervalValue),
                                (i + intervalValue) + "_" + (i + intervalValue + intervalValue),
                                (i + intervalValue) + "-" + (i + intervalValue + intervalValue));
                        RangeDTOList.add(newRangeDTO);
                    }
                }

            } else {
                for (int i = initialValue; i < endInterval; i += intervalValue) {
                    if (i < initialValue) continue;
                    RangeDTO RangeDTO;
                    if (endInterval < i + intervalValue) {
                        RangeDTO = new RangeDTO(i, endInterval, i + "_" + endInterval, i + "-" + endInterval);
                    } else {
                        RangeDTO = new RangeDTO(i,
                                i + intervalValue,
                                i + "_" + (i + intervalValue),
                                i + "-" + (i + intervalValue));
                    }
                    RangeDTOList.add(RangeDTO);
                    // 最后一个
                    if (integer == keySet.size() ) {
                        RangeDTO newRangeDTO = new RangeDTO((i + intervalValue),
                                (i + intervalValue + intervalValue),
                                (i + intervalValue) + "_" + (i + intervalValue + intervalValue),
                                (i + intervalValue) + "-" + (i + intervalValue + intervalValue));
                        RangeDTOList.add(newRangeDTO);
                    }

                }
                // 只有一个
                if ( integer == 1) {
                    return RangeDTOList;
                }
            }
        }
        return RangeDTOList;
    }
    /**
     * 计算区间
     *
     * @param priceBandVOListConfig 自定义区间
     */
    public static List<RangeDTO> generateTakeOutRangesVo(List<FtbRangeConfigDIYVO> priceBandVOListConfig) {
        List<RangeDTO> RangeDTOList = new ArrayList<>();
        List<FtbRangeConfigDIYVO> collected = priceBandVOListConfig.stream().sorted(Comparator.comparing(FtbRangeConfigDIYVO::getIntervalCount)).collect(Collectors.toList());
        Map<Integer, FtbRangeConfigDIYVO> bandVOMap = collected.stream().collect(Collectors.toMap(FtbRangeConfigDIYVO::getIntervalCount, Function.identity()));
        Set<Integer> keySet = bandVOMap.keySet();
        for (Integer integer : keySet) {
            FtbRangeConfigDIYVO priceBandVO = bandVOMap.get(integer);
            Integer initialValue = priceBandVO.getInitialValue();
            // 10以下
            if (priceBandVO.getIntervalCount() == 1) {
                RangeDTO RangeDTO = new RangeDTO(0, initialValue, "0_" + initialValue, initialValue + "以下");
                RangeDTOList.add(RangeDTO);
            }
            // 结束值
            Integer endInterval = priceBandVO.getEndInterval();
            // 间隔值
            Integer intervalValue = priceBandVO.getIntervalValue();
            if (priceBandVO.getIsAbove() == 1) {
                initialValue = priceBandVO.getStartInterval();
                for (int i = initialValue; i < endInterval; i += intervalValue) {
                    if (i < initialValue) continue;
                    RangeDTO RangeDTO = new RangeDTO(i,
                            i + intervalValue,
                            i + "_" + (i + intervalValue),
                            i + "-" + (i + intervalValue));
                    RangeDTOList.add(RangeDTO);
                    // 最后一个
                    if (integer == keySet.size()) {
                        RangeDTO newRangeDTO = new RangeDTO((i + intervalValue),
                                (i + intervalValue + intervalValue),
                                (i + intervalValue) + "_" + (i + intervalValue + intervalValue),
                                (i + intervalValue) + "-" + (i + intervalValue + intervalValue));
                        RangeDTOList.add(newRangeDTO);
                    }
                }

            } else {
                for (int i = initialValue; i < endInterval; i += intervalValue) {
                    if (i < initialValue) continue;
                    RangeDTO RangeDTO;
                    if (endInterval < i + intervalValue) {
                        RangeDTO = new RangeDTO(i, endInterval, i + "_" + endInterval, i + "-" + endInterval);
                    } else {
                        RangeDTO = new RangeDTO(i,
                                i + intervalValue,
                                i + "_" + (i + intervalValue),
                                i + "-" + (i + intervalValue));
                    }
                    RangeDTOList.add(RangeDTO);
                    // 最后一个
                    if (integer == keySet.size() ) {
                        RangeDTO newRangeDTO = new RangeDTO((i + intervalValue),
                                (i + intervalValue + intervalValue),
                                (i + intervalValue) + "_" + (i + intervalValue + intervalValue),
                                (i + intervalValue) + "-" + (i + intervalValue + intervalValue));
                        RangeDTOList.add(newRangeDTO);
                    }

                }
                // 只有一个
                if ( integer == 1) {
                    return RangeDTOList;
                }
            }
        }
        return RangeDTOList;
    }


}
