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.Autowired;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.cache.annotation.CacheEvict; 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.context.annotation.Lazy;
import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
@ -546,9 +546,9 @@ public class ResourceServiceImpl extends CrudServiceImpl<ResourceDao, ResourceEn
} }
@Override @Override
@CachePut(cacheNames = {selectDeptListKey}, key = "#p1") @Cacheable(value = selectDeptListKey, key = "#p0")
public Object selectDeptList(JSONObject jsonObject, String type) { public Object selectDeptList(JSONObject jsonObject, String type) {
List<Map> resultList = new CopyOnWriteArrayList<Map>(); List<Map> resultList = new CopyOnWriteArrayList<>();
HashMap<String, Object> resourceMap = new HashMap<>(); HashMap<String, Object> resourceMap = new HashMap<>();
resourceMap.put("type", "全部能力目录"); resourceMap.put("type", "全部能力目录");
QueryWrapper<ResourceEntity> queryWrapper = new QueryWrapper<>(); QueryWrapper<ResourceEntity> queryWrapper = new QueryWrapper<>();
@ -561,7 +561,9 @@ public class ResourceServiceImpl extends CrudServiceImpl<ResourceDao, ResourceEn
} }
Map<String, List<Map<String, Object>>> listMap = typeMapList.stream().collect(Collectors.groupingBy(m -> m.get("type").toString())); 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 -> { List<CompletableFuture> tasks =
listMap.entrySet().stream().filter(index -> !"区级".equals(index.getKey())).map(item -> {
CompletableFuture task = CompletableFuture.runAsync(() -> {
HashMap<String, Object> map = new HashMap<>(); HashMap<String, Object> map = new HashMap<>();
map.put("type", item.getKey()); map.put("type", item.getKey());
Integer integer = resourceDao.selectTypeCountByDept(item.getKey(), jsonObject.getString("type")); Integer integer = resourceDao.selectTypeCountByDept(item.getKey(), jsonObject.getString("type"));
@ -572,6 +574,10 @@ public class ResourceServiceImpl extends CrudServiceImpl<ResourceDao, ResourceEn
resultList.add(map); 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<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()))); 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<>(); HashMap<Object, Object> areaMap = new HashMap<>();
@ -581,8 +587,10 @@ public class ResourceServiceImpl extends CrudServiceImpl<ResourceDao, ResourceEn
integer = 0; integer = 0;
} }
areaMap.put("total", integer); 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) -> { areaTypeList.orElse(new HashMap<>()).forEach((key, value) -> {
CompletableFuture task = CompletableFuture.runAsync(() -> {
HashMap<String, Object> map = new HashMap<>(); HashMap<String, Object> map = new HashMap<>();
map.put("type", key); map.put("type", key);
map.put("total", resourceDao.selectTypeCountByDist(key, jsonObject.getString("type"))); map.put("total", resourceDao.selectTypeCountByDist(key, jsonObject.getString("type")));
@ -590,6 +598,9 @@ public class ResourceServiceImpl extends CrudServiceImpl<ResourceDao, ResourceEn
map.put("dataList", value); map.put("dataList", value);
areaListTemp.add(map); areaListTemp.add(map);
}); });
tasksArea.add(task);
});
CompletableFuture.allOf(tasksArea.toArray(new CompletableFuture[tasksArea.size()])).join();
areaMap.put("dataList", areaListTemp); areaMap.put("dataList", areaListTemp);
if (integer != 0) { if (integer != 0) {
resultList.add(areaMap); resultList.add(areaMap);
@ -1185,22 +1196,22 @@ public class ResourceServiceImpl extends CrudServiceImpl<ResourceDao, ResourceEn
@Override @Override
public List<Map<String, Object>> resourceBusinessUseDetails(Map<String, Object> params) { public List<Map<String, Object>> resourceBusinessUseDetails(Map<String, Object> params) {
List<Map<String, Object>> maps = new CopyOnWriteArrayList<>(); 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); maps.addAll(list);
//分别根据部门获取应用组件数和收藏组件数 //分别根据部门获取应用组件数和收藏组件数
Map<String,Object> paraMap = new ConcurrentHashMap<>(); Map<String, Object> paraMap = new ConcurrentHashMap<>();
paraMap.put("resourceType",params.get("resourceType")); paraMap.put("resourceType", params.get("resourceType"));
CompletableFuture<Void> voidCompletableFuture01 = CompletableFuture.runAsync(() -> { CompletableFuture<Void> voidCompletableFuture01 = CompletableFuture.runAsync(() -> {
maps.forEach(m -> { maps.forEach(m -> {
paraMap.put("id", m.get("id")); paraMap.put("id", m.get("id"));
Map<String, Object> maps1 = new HashMap<>(); Map<String, Object> maps1 = new HashMap<>();
maps1 = baseDao.assemblyCarByDept(paraMap); maps1 = baseDao.assemblyCarByDept(paraMap);
if(maps1==null){ if (maps1 == null) {
m.put("carNum",0); m.put("carNum", 0);
}else{ } else {
m.put("carNum", maps1.get("carNum") == null ? 0:maps1.get("carNum")); 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")); paraMap.put("id", m.get("id"));
Map<String, Object> maps2 = new HashMap<>(); Map<String, Object> maps2 = new HashMap<>();
maps2 = baseDao.assemblyUseByDept(paraMap); maps2 = baseDao.assemblyUseByDept(paraMap);
if(maps2==null){ if (maps2 == null) {
m.put("useNum",0); m.put("useNum", 0);
}else{ } else {
m.put("useNum", maps2.get("useNum") == null ? 0 : maps2.get("useNum")); 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 `name`;
alter table `tb_data_resource` drop index `deptId`; alter table `tb_data_resource` drop index `deptId`;
alter table `sys_dept` drop index `type`; 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_collection` ADD INDEX `resourceid`(`resource_id`) USING BTREE comment '收藏的资源id';
alter table`tb_resource_car` ADD INDEX `resourceid`(`resource_id`) USING BTREE comment '加入申购车的id'; alter table`tb_resource_car` ADD INDEX `resourceid`(`resource_id`) USING BTREE comment '加入申购车的id';
@ -22,3 +23,4 @@ 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 FULLTEXT INDEX `name`(`name`);
ALTER TABLE `tb_data_resource` ADD INDEX `deptId`(`dept_id`) USING BTREE; 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;