package com.unis.mobileInfo.utils;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson2.JSON;
import com.unis.mobileInfo.modular.gsqCarQueue.param.GsqCarQueueEditParam;
import lombok.extern.slf4j.Slf4j;

import javax.validation.constraints.NotBlank;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;

/**
 * @author qingsong.han
 * @description: 排序重组算法
 * @create 2024-04-29 10:39
 */
@Slf4j
public class SortRegroupUtil {

    // 重组排序算法 paramList按照源数据排序顺序重组
    public static synchronized List<GsqCarQueueEditParam> getRegroup(List<GsqCarQueueEditParam> paramList) {
        // 获取最小值
        if (ObjectUtil.isNotEmpty(paramList)) {
            Optional<GsqCarQueueEditParam> optional = paramList.stream().min(Comparator.comparing(GsqCarQueueEditParam::getSortNo));
            if (optional.isPresent()) {
                GsqCarQueueEditParam param = optional.get();
                String id = param.getId();
                Integer sortNo = param.getSortNo();
                log.info("param id: {},param sortNo: {}", id, sortNo);
                for (GsqCarQueueEditParam paramSource : paramList) {
                    if (!id.equals(paramSource.getId())) {
                        paramSource.setSortNo(++sortNo);
                    }
                }
            }
            log.info("return paramList new: {}", JSON.toJSON(paramList));
        }
        return paramList;
    }

    public static synchronized Integer getInitSort(List<GsqCarQueueEditParam> paramList) {
        if (ObjectUtil.isNotEmpty(paramList)) {
            Optional<GsqCarQueueEditParam> optional = paramList.stream().max(Comparator.comparing(GsqCarQueueEditParam::getSortNo));
            if (optional.isPresent())
                return optional.get().getSortNo() + 1;

        }
        return 1;
    }

    /*public static void main(String[] args) {
        List<GsqCarQueueEditParam> paramList = new ArrayList<>();
        GsqCarQueueEditParam param1 = new GsqCarQueueEditParam();
        param1.setSortNo(1);
        param1.setId("1");
        GsqCarQueueEditParam param2 = new GsqCarQueueEditParam();
        param2.setSortNo(3);
        param2.setId("2");
        GsqCarQueueEditParam param3 = new GsqCarQueueEditParam();
        param3.setSortNo(5);
        param3.setId("3");
        paramList.add(param1);
        paramList.add(param2);
        paramList.add(param3);
        // 获取最小值
        getRegroup(paramList);
    }*/

}
