权重 优先级 分单
作者:互联网
/**
* @Description: 查询总共待分单的数量
* @Date: 9:13 2021/5/21
*/
public List<Ulapplication> getOrderList() {
List<Ulapplication> list=new ArrayList<>();
Ulapplication u1=new Ulapplication();
u1.setId("1");
Ulapplication u2=new Ulapplication();
u2.setId("2");
Ulapplication u3=new Ulapplication();
u3.setId("3");
Ulapplication u4=new Ulapplication();
u4.setId("4");
Ulapplication u5=new Ulapplication();
u5.setId("5");
Ulapplication u6=new Ulapplication();
u6.setId("6");
Ulapplication u7=new Ulapplication();
u7.setId("7");
Ulapplication u8=new Ulapplication();
u8.setId("8");
Ulapplication u9=new Ulapplication();
u9.setId("9");
Ulapplication u10=new Ulapplication();
u10.setId("10");
Ulapplication u11=new Ulapplication();
u11.setId("11");
list.add(u1);
list.add(u2);
list.add(u3);
list.add(u4);
list.add(u5);
list.add(u6);
list.add(u7);
list.add(u8);
list.add(u9);
list.add(u10);
list.add(u11);
return list;
}
/**
* @Description: 查询待分配的银行
* @Date: 9:13 2021/5/21
*/
public List<ViUlbank> getBankList() {
List<ViUlbank> list=new ArrayList<>();
ViUlbank v1=new ViUlbank();
v1.setBankcode("BBC");
v1.setBankname("中国银行");
short a=1;
v1.setPriority(a);
v1.setWeight(a);
short start =1;
short start2 =0;
v1.setStatus(start);
ViUlbank v2=new ViUlbank();
v2.setBankcode("CCB");
v2.setBankname("建设银行");
short b=2;
v2.setPriority(b);
v2.setWeight(b);
v2.setStatus(start);
ViUlbank v3=new ViUlbank();
v3.setBankcode("ABC");
v3.setBankname("农业银行");
short c=3;
v3.setPriority(c);
v3.setWeight(c);
v3.setStatus(start);
list.add(v2);
list.add(v1);
list.add(v3);
return list;
}
/**
* @Description: 查询待分配的银行
* @Date: 9:13 2021/5/21
*/
public void getWaitBankList(Ulapplication entity) {
//把已经分配的订单分配存入待推单
UlWaitapplication dr =new UlWaitapplication();
}
private void lottery() {
//未分配单子
List<Ulapplication> orderList = getOrderList();
//待分配银行
List<ViUlbank> bankList = getBankList();
//按照优先级排序
bankList = bankList.stream()
.sorted(Comparator.comparing(ViUlbank::getPriority).reversed().thenComparing(ViUlbank::getPriority))
.collect(Collectors.toList());
//定义结果返回集
Map<String, Offset> userOffsetMap = new HashMap<>();
//如果待推单大于0则执行方法
if(orderList.size()>0){
userOffsetMap = recursion(bankList, orderList, userOffsetMap);
}
for (String bankName : userOffsetMap.keySet()) {
Offset offset = userOffsetMap.get(bankName);
System.out.println("银行: " + bankName + ", 轮次: " + offset.getLunci()+ ", 优先级: " + offset.getPriority()+ ", 开关状态: " + offset.getStatus()+ ", 数量: " + offset.getCout());
}
}
@Override
public WebApiResult Submenu() {
return null;
}
public Map<String, Offset> recursion(List<ViUlbank> bankList,List<Ulapplication> orderList ,Map<String, Offset> userOffsetMap) {
int j=0;
for (ViUlbank viUlbank : bankList) {
//判断银行的优先级是不是最高 还有开关是不是打开
Optional<ViUlbank> max = bankList.stream().filter(e -> StringUtils.equals(String.valueOf(e.getStatus()),"1" )).max(Comparator.comparingLong(ViUlbank::getPriority));
boolean flag = max.isPresent();
//如果开关全部关闭 怎么判断轮次打开
if(max.isPresent()){
ViUlbank vi = max.get();
//如果银行权重大于0 怎随机分配单子给银行
if(vi.getWeight()>0){
//如果当前待分配的订单总数小于权重 贼全部分配给该银行
int size1 = orderList.size();
if(vi.getWeight()>orderList.size()){
j =orderList.size();
}else{
j =vi.getWeight();
}
//总的订单必须大于0
if(orderList.size()>0){
for (int i=0;i<j;i++){
Random random = new Random();
//根据权重取随机订单
int index =random.nextInt(orderList.size());
Ulapplication dto = orderList.get(index);
//订单分配银行code
dto.setBankno(vi.getBankcode());
//把分配成功的订单给对应的银行生成银行待推单
getWaitBankList(dto);
//移除订单中已经分配
orderList.remove(index);
}
//银行轮次加 1
vi.setAllotTurn(vi.getAllotTurn()==null?(short)1:(short)(vi.getAllotTurn()+1));
//该轮次 该银行的权重全部分配完 则把状态关闭
if(j==vi.getWeight()){
vi.setStatus((short)0);
}
int k=0;
if(userOffsetMap.get(vi.getBankname())!=null){
k = (short)(userOffsetMap.get(vi.getBankname()).getCout() + j);
}
userOffsetMap.put(vi.getBankname(), new Offset(vi.getAllotTurn(),vi.getStatus(), (short) (k==0?j:k),vi.getPriority()));
}
}
}else{
short status=1;
//判断银行的优先级是不是最高 还有开关是不是打开
bankList = bankList.stream()
.filter(e -> StringUtils.equals(String.valueOf(e.getStatus()),"0" ))
.sorted(Comparator.comparing(ViUlbank::getPriority).reversed().thenComparing(ViUlbank::getPriority))
.collect(Collectors.toList());
//修改开关状态为 1 打开状态
for (ViUlbank e : bankList) {
e.setStatus(status);
}
//改变状态后继续执行
recursion(bankList, orderList, userOffsetMap);
}
}
if(orderList.size()>0){
recursion(bankList, orderList, userOffsetMap);
}
return userOffsetMap;
}
@Data
class Offset {
private Short lunci;
private Short status;
private Short cout;
private Short priority;
public Offset(Short lunci, Short status, Short cout,Short priority) {
this.lunci = lunci;
this.status = status;
this.cout = cout;
this.priority = priority;
}
}
标签:ViUlbank,优先级,分单,权重,orderList,list,add,Ulapplication,new 来源: https://blog.csdn.net/weixin_41169393/article/details/117127577