selectDeptList 使用全异步处理与加二级缓存

This commit is contained in:
wangliwen 2022-06-28 11:49:54 +08:00
parent 2954ede07a
commit d16d7add39
2 changed files with 46 additions and 33 deletions

View File

@ -38,7 +38,7 @@ import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.annotation.Lazy;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
@ -546,9 +546,9 @@ public class ResourceServiceImpl extends CrudServiceImpl<ResourceDao, ResourceEn
}
@Override
@CachePut(cacheNames = {selectDeptListKey}, key = "#p1")
@Cacheable(value = selectDeptListKey, key = "#p0")
public Object selectDeptList(JSONObject jsonObject, String type) {
List<Map> resultList = new CopyOnWriteArrayList<Map>();
List<Map> resultList = new CopyOnWriteArrayList<>();
HashMap<String, Object> resourceMap = new HashMap<>();
resourceMap.put("type", "全部能力目录");
QueryWrapper<ResourceEntity> queryWrapper = new QueryWrapper<>();
@ -561,17 +561,23 @@ public class ResourceServiceImpl extends CrudServiceImpl<ResourceDao, ResourceEn
}
Map<String, List<Map<String, Object>>> listMap = typeMapList.stream().collect(Collectors.groupingBy(m -> m.get("type").toString()));
//区级要根据行政区划多加一层结构
listMap.entrySet().parallelStream().filter(index -> !"区级".equals(index.getKey())).forEach(item -> {
HashMap<String, Object> map = new HashMap<>();
map.put("type", item.getKey());
Integer integer = resourceDao.selectTypeCountByDept(item.getKey(), jsonObject.getString("type"));
map.put("total", integer);
item.getValue().forEach(item1 -> item1.remove("type"));
map.put("dataList", item.getValue());
if (integer != 0) {
resultList.add(map);
}
});
List<CompletableFuture> tasks =
listMap.entrySet().stream().filter(index -> !"区级".equals(index.getKey())).map(item -> {
CompletableFuture task = CompletableFuture.runAsync(() -> {
HashMap<String, Object> map = new HashMap<>();
map.put("type", item.getKey());
Integer integer = resourceDao.selectTypeCountByDept(item.getKey(), jsonObject.getString("type"));
map.put("total", integer);
item.getValue().forEach(item1 -> item1.remove("type"));
map.put("dataList", item.getValue());
if (integer != 0) {
resultList.add(map);
}
});
return task;
}).collect(Collectors.toList());
CompletableFuture.allOf(tasks.toArray(new CompletableFuture[tasks.size()])).join();
Optional<List<Map<String, Object>>> areaList = Optional.ofNullable(listMap.get("区级"));
Optional<Map<String, List<Map<String, Object>>>> areaTypeList = Optional.ofNullable(areaList.orElse(new ArrayList<>()).stream().collect(Collectors.groupingBy(m -> m.get("districtName").toString())));
HashMap<Object, Object> areaMap = new HashMap<>();
@ -581,15 +587,20 @@ public class ResourceServiceImpl extends CrudServiceImpl<ResourceDao, ResourceEn
integer = 0;
}
areaMap.put("total", integer);
ArrayList<Map> areaListTemp = new ArrayList<>();
List<Map> areaListTemp = new CopyOnWriteArrayList<>();
List<CompletableFuture> tasksArea = new ArrayList<>();
areaTypeList.orElse(new HashMap<>()).forEach((key, value) -> {
HashMap<String, Object> map = new HashMap<>();
map.put("type", key);
map.put("total", resourceDao.selectTypeCountByDist(key, jsonObject.getString("type")));
value.forEach(item1 -> item1.remove("type"));
map.put("dataList", value);
areaListTemp.add(map);
CompletableFuture task = CompletableFuture.runAsync(() -> {
HashMap<String, Object> map = new HashMap<>();
map.put("type", key);
map.put("total", resourceDao.selectTypeCountByDist(key, jsonObject.getString("type")));
value.forEach(item1 -> item1.remove("type"));
map.put("dataList", value);
areaListTemp.add(map);
});
tasksArea.add(task);
});
CompletableFuture.allOf(tasksArea.toArray(new CompletableFuture[tasksArea.size()])).join();
areaMap.put("dataList", areaListTemp);
if (integer != 0) {
resultList.add(areaMap);
@ -1185,22 +1196,22 @@ public class ResourceServiceImpl extends CrudServiceImpl<ResourceDao, ResourceEn
@Override
public List<Map<String, Object>> resourceBusinessUseDetails(Map<String, Object> params) {
List<Map<String, Object>> maps = new CopyOnWriteArrayList<>();
Object[] ps = {params.get("id"),params.get("id"),(Integer.parseInt(params.get("page").toString()) - 1) * Integer.parseInt(params.get("limit").toString()),Integer.parseInt(params.get("limit").toString())};
Object[] ps = {params.get("id"), params.get("id"), (Integer.parseInt(params.get("page").toString()) - 1) * Integer.parseInt(params.get("limit").toString()), Integer.parseInt(params.get("limit").toString())};
//获取部门列表
List<Map<String,Object>> list = jdbcTemplate.queryForList("SELECT id,name FROM sys_dept WHERE id = ? OR INSTR(pids,?) LIMIT ?,?",ps);
List<Map<String, Object>> list = jdbcTemplate.queryForList("SELECT id,name FROM sys_dept WHERE id = ? OR INSTR(pids,?) LIMIT ?,?", ps);
maps.addAll(list);
//分别根据部门获取应用组件数和收藏组件数
Map<String,Object> paraMap = new ConcurrentHashMap<>();
paraMap.put("resourceType",params.get("resourceType"));
Map<String, Object> paraMap = new ConcurrentHashMap<>();
paraMap.put("resourceType", params.get("resourceType"));
CompletableFuture<Void> voidCompletableFuture01 = CompletableFuture.runAsync(() -> {
maps.forEach(m -> {
paraMap.put("id", m.get("id"));
Map<String, Object> maps1 = new HashMap<>();
maps1 = baseDao.assemblyCarByDept(paraMap);
if(maps1==null){
m.put("carNum",0);
}else{
m.put("carNum", maps1.get("carNum") == null ? 0:maps1.get("carNum"));
if (maps1 == null) {
m.put("carNum", 0);
} else {
m.put("carNum", maps1.get("carNum") == null ? 0 : maps1.get("carNum"));
}
});
});
@ -1210,9 +1221,9 @@ public class ResourceServiceImpl extends CrudServiceImpl<ResourceDao, ResourceEn
paraMap.put("id", m.get("id"));
Map<String, Object> maps2 = new HashMap<>();
maps2 = baseDao.assemblyUseByDept(paraMap);
if(maps2==null){
m.put("useNum",0);
}else{
if (maps2 == null) {
m.put("useNum", 0);
} else {
m.put("useNum", maps2.get("useNum") == null ? 0 : maps2.get("useNum"));
}
});

View File

@ -10,6 +10,7 @@ alter table `tb_data_attr` drop index `attr_value`;
alter table `tb_data_resource` drop index `name`;
alter table `tb_data_resource` drop index `deptId`;
alter table `sys_dept` drop index `type`;
alter table `sys_dept` drop index `district`;
-- 创建索引
alter table `tb_resource_collection` ADD INDEX `resourceid`(`resource_id`) USING BTREE comment '收藏的资源id';
alter table`tb_resource_car` ADD INDEX `resourceid`(`resource_id`) USING BTREE comment '加入申购车的id';
@ -21,4 +22,5 @@ alter table `tb_data_resource` ADD FULLTEXT INDEX `type`(`type`);
alter table `tb_data_attr` ADD FULLTEXT INDEX `attr_value`(`attr_value`);
alter table `tb_data_resource` ADD FULLTEXT INDEX `name`(`name`);
ALTER TABLE `tb_data_resource` ADD INDEX `deptId`(`dept_id`) USING BTREE;
ALTER TABLE `sys_dept` ADD INDEX `type`(`type`) USING BTREE;
ALTER TABLE `sys_dept` ADD INDEX `type`(`type`) USING BTREE;
ALTER TABLE `sys_dept` ADD INDEX `district`(`district`) USING BTREE;