41 changed files with 918 additions and 583 deletions
File diff suppressed because one or more lines are too long
@ -0,0 +1,294 @@ |
|||||
|
package com.dreamchaser.depository_manage; |
||||
|
|
||||
|
import com.alibaba.fastjson.JSON; |
||||
|
import com.alibaba.fastjson.JSONArray; |
||||
|
import com.alibaba.fastjson.JSONObject; |
||||
|
import com.dreamchaser.depository_manage.entity.Depository; |
||||
|
import com.dreamchaser.depository_manage.entity.MaterialType; |
||||
|
import com.dreamchaser.depository_manage.entity.Place; |
||||
|
import com.dreamchaser.depository_manage.mapper.DepositoryMapper; |
||||
|
import com.dreamchaser.depository_manage.mapper.PlaceMapper; |
||||
|
import com.dreamchaser.depository_manage.service.impl.DepositoryServiceImpl; |
||||
|
import org.junit.Test; |
||||
|
import org.junit.runner.RunWith; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.boot.test.context.SpringBootTest; |
||||
|
import org.springframework.test.context.junit4.SpringRunner; |
||||
|
|
||||
|
import java.util.*; |
||||
|
import java.util.concurrent.*; |
||||
|
|
||||
|
@SpringBootTest |
||||
|
@RunWith(SpringRunner.class) |
||||
|
public class TestForDepositoryTree { |
||||
|
|
||||
|
@Autowired |
||||
|
DepositoryMapper depositoryMapper; |
||||
|
|
||||
|
@Autowired |
||||
|
PlaceMapper placeMapper; |
||||
|
|
||||
|
@Test |
||||
|
public void test(){ |
||||
|
List<Object> objectList1 = InitTreeMenus("116"); |
||||
|
|
||||
|
System.out.println(objectList1); |
||||
|
} |
||||
|
public List<Object> InitTreeMenus(String adminorg) { |
||||
|
// 定义结果集
|
||||
|
List<Object> list = new ArrayList<>(); |
||||
|
|
||||
|
// 定义仓库id列表
|
||||
|
List<Integer> depositoryIdList = new ArrayList<>(); |
||||
|
// 获取所有仓库
|
||||
|
List<Depository> depositoryAll = depositoryMapper.findDepositoryAll(); |
||||
|
Integer totalVal = depositoryAll.size(); |
||||
|
|
||||
|
// 定义分页数量
|
||||
|
double size = 10.0; |
||||
|
|
||||
|
// 定义线程数
|
||||
|
Integer threadSize = (int) Math.ceil(totalVal / size); |
||||
|
|
||||
|
// 定义开启线程数目
|
||||
|
Integer openThreadSize = 0; |
||||
|
// 开启对应数量的线程
|
||||
|
ExecutorService exs = Executors.newFixedThreadPool(threadSize); |
||||
|
// 线程结果集
|
||||
|
List<Future<Object>> futureList = new ArrayList<Future<Object>>(); |
||||
|
|
||||
|
// 1.定义CompletionService
|
||||
|
CompletionService<Object> completionService = new ExecutorCompletionService<Object>(exs); |
||||
|
for (int i = 0; i < depositoryAll.size(); i++) { |
||||
|
// 获取当前类型
|
||||
|
Depository depository = depositoryAll.get(i); |
||||
|
if (((i + 1) % 100) == 0) { // 如果有100个开启线程进行处理
|
||||
|
depositoryIdList.add(depository.getId()); |
||||
|
Future<Object> future = completionService.submit(new Task(depositoryIdList,adminorg)); |
||||
|
openThreadSize++; |
||||
|
futureList.add(future); // 添加到结果集
|
||||
|
depositoryIdList = new ArrayList<>(); // 情况列表
|
||||
|
} else { |
||||
|
// 添加id到列表中
|
||||
|
depositoryIdList.add(depository.getId()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
if (depositoryIdList.size() > 0) { |
||||
|
// 如果有剩余,开启线程进行处理
|
||||
|
Future<Object> future = completionService.submit(new Task(depositoryIdList,adminorg)); |
||||
|
futureList.add(future); |
||||
|
openThreadSize++; |
||||
|
} |
||||
|
|
||||
|
// 3.获取结果
|
||||
|
for (int i = 0; i < openThreadSize; i++) { |
||||
|
Object result = null; |
||||
|
try { |
||||
|
result = completionService.take().get(); |
||||
|
} catch (InterruptedException e) { |
||||
|
e.printStackTrace(); |
||||
|
} catch (ExecutionException e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
list.addAll((Collection<?>) result); |
||||
|
} |
||||
|
exs.shutdown(); |
||||
|
for (int i = 0; i < list.size(); i++) { |
||||
|
JSONObject jsonObject = new JSONObject((Map<String, Object>) list.get(i)); |
||||
|
Integer parentId = jsonObject.getInteger("parentId"); |
||||
|
if(Integer.compare(parentId,0) != 0) { |
||||
|
if (!checkList(list, parentId)) { |
||||
|
// 如果当前列表中不存在其父级
|
||||
|
Depository depositoryById = depositoryMapper.findDepositoryById(parentId); |
||||
|
list.add(InitTreeMenus2(depositoryById, new ArrayList<>())); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
return list; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
// 用于执行线程任务
|
||||
|
class Task implements Callable<Object> { |
||||
|
|
||||
|
// 仓库
|
||||
|
List<Integer> depositoryIdList; |
||||
|
// 部门
|
||||
|
String adminorg; |
||||
|
|
||||
|
public Task(List<Integer> depositoryIdList,String adminorg){ |
||||
|
this.depositoryIdList = depositoryIdList; |
||||
|
this.adminorg = adminorg; |
||||
|
} |
||||
|
@Override |
||||
|
public Object call() throws Exception { |
||||
|
/** |
||||
|
* 获取当前仓库id对应的仓库 |
||||
|
*/ |
||||
|
List<Depository> depositories = depositoryMapper.selectDepositoryByIds(depositoryIdList); |
||||
|
|
||||
|
// 定义树结构结果集
|
||||
|
List<Object> list = new ArrayList<>(); |
||||
|
// 定义线程池
|
||||
|
ExecutorService exs = Executors.newFixedThreadPool(depositories.size()); |
||||
|
// 线程结果集
|
||||
|
List<Future<Object>> futureList = new ArrayList<Future<Object>>(); |
||||
|
|
||||
|
// 1.定义CompletionService用于获取结果
|
||||
|
CompletionService<Object> completionService = new ExecutorCompletionService<Object>(exs); |
||||
|
|
||||
|
// 定义开启线程数
|
||||
|
Integer openThreadSize = 0; |
||||
|
for (int i = 0; i < depositories.size(); i++) { |
||||
|
// 获取当前仓库信息
|
||||
|
Depository depository = depositories.get(i); |
||||
|
if("".equals(adminorg) ){ |
||||
|
Future<Object> submit = completionService.submit(new PlaceTask(depository)); |
||||
|
futureList.add(submit); |
||||
|
openThreadSize++; |
||||
|
}else{ |
||||
|
if(adminorg.equals(depository.getAdminorg())){ |
||||
|
Future<Object> submit = completionService.submit(new PlaceTask(depository)); |
||||
|
futureList.add(submit); |
||||
|
openThreadSize++; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
} |
||||
|
for (int i = 0; i < openThreadSize; i++) { |
||||
|
Object result = null; |
||||
|
try { |
||||
|
result = completionService.take().get(); |
||||
|
} catch (InterruptedException e) { |
||||
|
e.printStackTrace(); |
||||
|
} catch (ExecutionException e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
list.add(result); |
||||
|
} |
||||
|
return list; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* 用于给仓库添加对应的库位 |
||||
|
*/ |
||||
|
class PlaceTask implements Callable<Object>{ |
||||
|
|
||||
|
Depository d; |
||||
|
|
||||
|
PlaceTask(Depository d){ |
||||
|
this.d = d; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public Object call() throws Exception { |
||||
|
// 获取当前仓库下的库位
|
||||
|
List<Object> objectList = AddPlaceByDid(d); |
||||
|
|
||||
|
// 将其构造成对应的树形结构类型
|
||||
|
Map<String, Object> map = InitTreeMenus2(d, objectList); |
||||
|
return map; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
// 在仓库后添加库位信息
|
||||
|
public List<Object> AddPlaceByDid(Depository d) { |
||||
|
if (d != null) { |
||||
|
// 获取当前仓库下的所有库位
|
||||
|
List<Object> result = new ArrayList<>(); |
||||
|
// 获取该仓库的排
|
||||
|
List<Integer> placeXByDid = placeMapper.findPlaceXByDid(d.getId()); |
||||
|
if (placeXByDid.size() > 0) { |
||||
|
for (int i = 1; i <= placeXByDid.get(0); i++) { |
||||
|
Map<String, Object> placeX = new HashMap<>(); |
||||
|
placeX.put("title", "第" + i + "排货架"); |
||||
|
placeX.put("id", -1); |
||||
|
Map<String, Object> param = new HashMap<>(); |
||||
|
param.put("did", d.getId()); |
||||
|
param.put("x", i); |
||||
|
List<Place> placeByCondition = placeMapper.findPlaceByCondition(param); |
||||
|
if (placeByCondition.size() > 0) { |
||||
|
List<Object> children = new ArrayList<>(); |
||||
|
for (int k = 1; k <= placeByCondition.size(); k++) { |
||||
|
Map<String, Object> map = new HashMap<>(); |
||||
|
map.put("title", placeByCondition.get(k - 1).getCode()); |
||||
|
map.put("id", d.getId() + "-" + placeByCondition.get(k - 1).getId()); |
||||
|
children.add(map); |
||||
|
} |
||||
|
placeX.put("children", children); |
||||
|
} |
||||
|
// 获取该仓库该排的列数
|
||||
|
result.add(placeX); |
||||
|
} |
||||
|
} |
||||
|
return result; |
||||
|
} else { |
||||
|
return null; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
// 构造树形组件数据模板
|
||||
|
public Map<String, Object> InitTreeMenus2(Depository d, List<Object> children) { |
||||
|
if (d != null) { |
||||
|
Map<String, Object> map = new HashMap<>(); |
||||
|
map.put("title", d.getDname()); |
||||
|
map.put("id", d.getId()); |
||||
|
map.put("children", children); |
||||
|
map.put("parentId",d.getParentId()); |
||||
|
return map; |
||||
|
} else { |
||||
|
return null; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* 用于构造树结构 |
||||
|
* @param list 树列表 |
||||
|
* @param parentId 父级id |
||||
|
* @return |
||||
|
*/ |
||||
|
public List<Object> buildTree_New(List<Object> list,Integer parentId){ |
||||
|
// 定义树结构
|
||||
|
List<Object> result = new ArrayList<>(); |
||||
|
for (int i = 0; i < list.size(); i++) { |
||||
|
// 构造为jsonObject类
|
||||
|
JSONObject jsonObject = new JSONObject((Map<String, Object>) list.get(i)); |
||||
|
// 获取当前父级id
|
||||
|
Long parentId1 = jsonObject.getLong("parentId"); |
||||
|
if(Long.compare(parentId,parentId1) == 0){ // 如果当前类型是其父类
|
||||
|
List<Object> objectList = buildTree_New(list, jsonObject.getInteger("id")); // 获取当前类型的子类
|
||||
|
JSONArray children = jsonObject.getJSONArray("children"); |
||||
|
children.addAll(objectList); |
||||
|
result.add(jsonObject); |
||||
|
} |
||||
|
} |
||||
|
return result; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
/** |
||||
|
* 判断当前父级id是否在列表中 |
||||
|
* @param list |
||||
|
* @param parentId |
||||
|
* @return |
||||
|
*/ |
||||
|
public Boolean checkList(List<Object> list,Integer parentId){ |
||||
|
for (int i = 0; i < list.size(); i++) { |
||||
|
JSONObject jsonObject = new JSONObject((Map<String, Object>) list.get(i)); |
||||
|
Integer pid = jsonObject.getInteger("id"); |
||||
|
if(Integer.compare(pid,parentId) == 0){ |
||||
|
return true; |
||||
|
} |
||||
|
} |
||||
|
return false; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
|
||||
|
} |
||||
@ -0,0 +1,163 @@ |
|||||
|
package com.dreamchaser.depository_manage; |
||||
|
|
||||
|
import com.alibaba.fastjson.JSONArray; |
||||
|
import com.alibaba.fastjson.JSONObject; |
||||
|
import com.dreamchaser.depository_manage.entity.Material; |
||||
|
import com.dreamchaser.depository_manage.entity.MaterialType; |
||||
|
import com.dreamchaser.depository_manage.mapper.MaterialTypeMapper; |
||||
|
import com.dreamchaser.depository_manage.service.MaterialTypeService; |
||||
|
import com.dreamchaser.depository_manage.service.impl.MaterialTypeServiceImpl; |
||||
|
import org.junit.Test; |
||||
|
import org.junit.runner.RunWith; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.boot.test.context.SpringBootTest; |
||||
|
import org.springframework.test.context.junit4.SpringRunner; |
||||
|
|
||||
|
import java.util.*; |
||||
|
import java.util.concurrent.*; |
||||
|
|
||||
|
@SpringBootTest |
||||
|
@RunWith(SpringRunner.class) |
||||
|
public class TestForMaterialTypeTree { |
||||
|
@Autowired |
||||
|
MaterialTypeMapper materialTypeMapper; |
||||
|
|
||||
|
|
||||
|
@Test |
||||
|
public void Test(){ |
||||
|
|
||||
|
} |
||||
|
|
||||
|
|
||||
|
public List<Object> InitTreeMenus() { |
||||
|
// 获取所有物料类型
|
||||
|
List<MaterialType> materialTypeAll = materialTypeMapper.findMaterialTypeAll(); |
||||
|
// 物料总数
|
||||
|
Integer totalVal = materialTypeAll.size(); |
||||
|
|
||||
|
// 定义分页数量
|
||||
|
double size = 100.0; |
||||
|
|
||||
|
// 定义线程数
|
||||
|
Integer threadSize = (int) Math.ceil(totalVal / size); |
||||
|
|
||||
|
// 定义开启线程数
|
||||
|
Integer openThreadSize = 0; |
||||
|
|
||||
|
// 开启对应数量的线程
|
||||
|
ExecutorService exs = Executors.newFixedThreadPool(threadSize); |
||||
|
// 树结构结果集
|
||||
|
List<Object> list = new ArrayList<>(); |
||||
|
// 线程结果集
|
||||
|
List<Future<Object>> futureList = new ArrayList<Future<Object>>(); |
||||
|
|
||||
|
// 1.定义CompletionService
|
||||
|
CompletionService<Object> completionService = new ExecutorCompletionService<Object>(exs); |
||||
|
// 物料类型id列表
|
||||
|
List<Long> materialTypeList = new ArrayList<>(); |
||||
|
for (int i = 0; i < materialTypeAll.size(); i++) { |
||||
|
// 获取当前类型
|
||||
|
MaterialType materialType = materialTypeAll.get(i); |
||||
|
if (((i + 1) % 100) == 0) { // 如果有100个开启线程进行处理
|
||||
|
materialTypeList.add(materialType.getOldId()); |
||||
|
Future<Object> future = completionService.submit(new TaskTest_New(materialTypeList)); |
||||
|
openThreadSize++; |
||||
|
futureList.add(future); // 添加到结果集
|
||||
|
materialTypeList = new ArrayList<>(); // 情况列表
|
||||
|
} else { |
||||
|
// 添加id到列表中
|
||||
|
materialTypeList.add(materialType.getOldId()); |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
if (materialTypeList.size() > 0) { |
||||
|
// 如果有剩余,开启线程进行处理
|
||||
|
Future<Object> future = completionService.submit(new TaskTest_New(materialTypeList)); |
||||
|
futureList.add(future); |
||||
|
openThreadSize++; |
||||
|
} |
||||
|
|
||||
|
// 3.获取结果
|
||||
|
for (int i = 0; i < openThreadSize; i++) { |
||||
|
Object result = null; |
||||
|
try { |
||||
|
result = completionService.take().get(); |
||||
|
} catch (InterruptedException e) { |
||||
|
e.printStackTrace(); |
||||
|
} catch (ExecutionException e) { |
||||
|
e.printStackTrace(); |
||||
|
} |
||||
|
list.addAll((Collection<?>) result); |
||||
|
} |
||||
|
exs.shutdown(); |
||||
|
// 进行最终的封装
|
||||
|
return list; |
||||
|
} |
||||
|
|
||||
|
// 用于执行测试新算法
|
||||
|
class TaskTest_New implements Callable<Object> { |
||||
|
|
||||
|
// 待处理的物料类型id列表
|
||||
|
List<Long> materialTypeIdList; |
||||
|
|
||||
|
public TaskTest_New(List<Long> materialTypeByCondition) { |
||||
|
this.materialTypeIdList = materialTypeByCondition; |
||||
|
} |
||||
|
|
||||
|
@Override |
||||
|
public Object call() throws Exception { |
||||
|
|
||||
|
// 定义树结构结果集
|
||||
|
List<Object> list = new ArrayList<>(); |
||||
|
// 查询当前物料类型id列表中的物料类型
|
||||
|
List<MaterialType> materialTypeByOldIds = materialTypeMapper.findMaterialTypeByOldIds(materialTypeIdList); |
||||
|
|
||||
|
for (int i = 0; i < materialTypeByOldIds.size(); i++) { |
||||
|
MaterialType mt = materialTypeByOldIds.get(i); |
||||
|
Map<String, Object> map = InitTreeMenus(mt, new ArrayList<>()); |
||||
|
list.add(map); |
||||
|
} |
||||
|
return list; |
||||
|
} |
||||
|
|
||||
|
|
||||
|
} |
||||
|
|
||||
|
// 构造树形组件数据模板
|
||||
|
public Map<String, Object> InitTreeMenus(MaterialType mt, List<Object> children) { |
||||
|
if (mt != null) { |
||||
|
Map<String, Object> map = new HashMap<>(); |
||||
|
map.put("title", mt.getTname()); |
||||
|
map.put("id", mt.getOldId()); |
||||
|
map.put("parentId",mt.getParentId()); |
||||
|
map.put("children",children); |
||||
|
return map; |
||||
|
} else { |
||||
|
return null; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 用于构造树结构 |
||||
|
* @param list 树列表 |
||||
|
* @param parentId 父级id |
||||
|
* @return |
||||
|
*/ |
||||
|
public List<Object> buildTree_New(List<Object> list,Long parentId){ |
||||
|
// 定义树结构
|
||||
|
List<Object> result = new ArrayList<>(); |
||||
|
for (int i = 0; i < list.size(); i++) { |
||||
|
// 构造为jsonObject类
|
||||
|
JSONObject jsonObject = new JSONObject((Map<String, Object>) list.get(i)); |
||||
|
// 获取当前父级id
|
||||
|
Long parentId1 = jsonObject.getLong("parentId"); |
||||
|
if(Long.compare(parentId,parentId1) == 0){ // 如果当前类型是其父类
|
||||
|
List<Object> objectList = buildTree_New(list, jsonObject.getLong("id")); // 获取当前类型的子类
|
||||
|
JSONArray children = jsonObject.getJSONArray("children"); |
||||
|
children.addAll(objectList); |
||||
|
result.add(jsonObject); |
||||
|
} |
||||
|
} |
||||
|
return result; |
||||
|
} |
||||
|
} |
||||
Loading…
Reference in new issue