修改分配规则

master
bynt 3 years ago
parent a38fffcbfd
commit 9065fabf13

@ -1,5 +1,6 @@
package com.baiye.service;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.NumberUtil;
import com.baiye.constant.DefaultNumberConstants;
import com.baiye.http.CommonResponse;
@ -14,7 +15,9 @@ import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;
/**
* @author Enzo
@ -41,21 +44,28 @@ public class AssignDataService {
if (sum != DefaultNumberConstants.ONE_HUNDRED) {
return CommonResponse.createByErrorMessage(ResponseCode.DECRYPTION_FAILED.getDesc());
}
// 进行排序保证最大比例在后
Map<Long, Double> deptMap =
deptIds.stream().collect(Collectors.toMap(key -> key, key -> weights.get(deptIds.indexOf(key))));
Map<Long, Double> longDoubleMap = MapUtil.sortByValue(deptMap, Boolean.FALSE);
CopyOnWriteArrayList<String> value;
List<DistributeResponseDTO> list = new ArrayList<>();
DistributeResponseDTO distributeResponseDTO;
for (int i = DefaultNumberConstants.ZERO_NUMBER; i < deptIds.size(); i++) {
Double aDouble = weights.get(i);
int num = DefaultNumberConstants.ZERO_NUMBER;
for (Map.Entry<Long, Double> longDoubleEntry : longDoubleMap.entrySet()) {
Long key = longDoubleEntry.getKey();
Double aDouble = longDoubleEntry.getValue();
distributeResponseDTO = new DistributeResponseDTO();
if (size - DefaultNumberConstants.ONE_NUMBER > i) {
if (size - DefaultNumberConstants.ONE_NUMBER > num) {
num++;
double div = NumberUtil.div(aDouble,
Double.valueOf(DefaultNumberConstants.ONE_HUNDRED), DefaultNumberConstants.TWO_HUNDRED);
int count = (int) Math.floor(NumberUtil.mul(resourceSize, div));
if (count == DefaultNumberConstants.ONE_NUMBER) {
int middle = (int) Math.floor(copyOnWriteArrayList.size() / 2);
int middle = copyOnWriteArrayList.size() / DefaultNumberConstants.TWO_NUMBER;
value = Lists.newCopyOnWriteArrayList(Lists.newArrayList(copyOnWriteArrayList.get(middle)));
distributeResponseDTO.setResponseList(value);
distributeResponseDTO.setDeptId(deptIds.get(i));
distributeResponseDTO.setDeptId(key);
list.add(distributeResponseDTO);
copyOnWriteArrayList.remove(middle);
continue;
@ -63,13 +73,13 @@ public class AssignDataService {
value = Lists.newCopyOnWriteArrayList
(copyOnWriteArrayList.subList(DefaultNumberConstants.ZERO_NUMBER, count));
distributeResponseDTO.setResponseList(value);
distributeResponseDTO.setDeptId(deptIds.get(i));
distributeResponseDTO.setDeptId(key);
list.add(distributeResponseDTO);
copyOnWriteArrayList.removeAll(value);
} else {
value = Lists.newCopyOnWriteArrayList(Lists.newArrayList(copyOnWriteArrayList));
distributeResponseDTO.setResponseList(value);
distributeResponseDTO.setDeptId(deptIds.get(i));
distributeResponseDTO.setDeptId(longDoubleEntry.getKey());
list.add(distributeResponseDTO);
}
}

@ -22,8 +22,8 @@ public class WeightRandomStrategyX<K, V extends Number> {
private double factor=0D;
public WeightRandomStrategyX(List<Pair<K, V>> list) {
for (Pair<K, V> pair : list) {
this.weightMap.put(factor, pair.getKey());
this.factor+=pair.getValue().doubleValue();
double lastWeight = this.weightMap.size() == 0 ? 0 : this.weightMap.lastKey();
this.weightMap.put(pair.getValue().doubleValue() + lastWeight, pair.getKey());
}
}

@ -1,5 +1,6 @@
package com.baiye;
import cn.hutool.core.map.MapUtil;
import cn.hutool.json.JSONUtil;
import com.baiye.model.dto.DistributeDTO;
import com.baiye.service.AssignDataService;
@ -11,9 +12,8 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.*;
import java.util.stream.Collectors;
/**
* @author Enzo
@ -36,7 +36,7 @@ public class SettingTest {
List<String> strings = Lists.newArrayList("a", "b", "c", "d","e","f","g","h","i","j");
List<Long> longs = Lists.newArrayList(12360L, 12361L,12362L,12363L,12364L,12365L);
List<Double> doubleList = Lists.newArrayList(16.66, 16.66,16.66,16.66,16.66,16.7);
List<Double> doubleList = Lists.newArrayList(40.0, 20.0,10.0,15.0,10.0,5.0);
distributeDTO.setResourceList(strings);
distributeDTO.setWeights(doubleList);
distributeDTO.setDeptIds(longs);
@ -45,6 +45,8 @@ public class SettingTest {
}
public static <T> List<List<T>> fixedGrouping2(List<T> source, int n) {
if (null == source || source.size() == 0 || n <= 0)
@ -169,4 +171,14 @@ public class SettingTest {
}
}
@Test
public void sortMap(){
List<Double> doubles = Lists.newArrayList(123.00, 122.00);
List<Long> longs = Lists.newArrayList(12362L,12361L);
Map<Long, Double> collect = longs.stream().collect(Collectors.toMap(key -> key, key -> doubles.get(longs.indexOf(key))));
TreeMap<Long, Double> sort = MapUtil.sort(collect);
}
}

Loading…
Cancel
Save