SceneDrawServiceImpl.java 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  1. package com.fdkankan.scene.service.impl;
  2. import cn.hutool.core.collection.CollUtil;
  3. import cn.hutool.core.util.StrUtil;
  4. import com.alibaba.fastjson.JSON;
  5. import com.alibaba.fastjson.JSONObject;
  6. import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
  7. import com.fdkankan.common.constant.CommonStatus;
  8. import com.fdkankan.common.constant.UploadFilePath;
  9. import com.fdkankan.redis.constant.RedisKey;
  10. import com.fdkankan.redis.util.RedisClient;
  11. import com.fdkankan.scene.bean.TagBean;
  12. import com.fdkankan.scene.entity.Scene;
  13. import com.fdkankan.scene.entity.SceneEditInfoExt;
  14. import com.fdkankan.scene.service.*;
  15. import com.fdkankan.scene.vo.BaseJsonArrayParamVO;
  16. import com.fdkankan.scene.vo.DeleteSidListParamVO;
  17. import com.google.common.collect.Lists;
  18. import org.springframework.beans.factory.annotation.Autowired;
  19. import org.springframework.stereotype.Service;
  20. import javax.annotation.Resource;
  21. import java.io.IOException;
  22. import java.util.*;
  23. import java.util.stream.Collectors;
  24. @Service
  25. public class SceneDrawServiceImpl implements ISceneDrawService {
  26. private final String SCENE_DRAW_JSON_NAME = "sceneDraw.json";
  27. @Autowired
  28. private SceneService sceneService;
  29. @Autowired
  30. private RedisClient redisClient;
  31. @Autowired
  32. private SceneEditInfoExtService sceneEditInfoExtService;
  33. @Autowired
  34. private SceneEditInfoService sceneEditInfoService;
  35. @Resource
  36. private FYunFileService fYunFileService;
  37. @Autowired
  38. private SceneFileMappingService sceneFileMappingService;
  39. @Override
  40. public void saveSceneDraw(BaseJsonArrayParamVO param) throws Exception {
  41. Scene scenePlus = sceneService.getByNum(param.getNum(), param.getSubgroup());
  42. this.addOrUpdate(param.getNum(), param.getSubgroup(), param.getData());
  43. //保存数据库
  44. SceneEditInfoExt sceneEditInfoExt = sceneEditInfoExtService.getByScenePlusId(scenePlus.getId());
  45. this.updateDb(param.getNum(), param.getSubgroup(), scenePlus.getId());
  46. sceneEditInfoService.upgradeVersionById(sceneEditInfoExt.getEditInfoId());
  47. this.publicSceneDraw(param.getNum(), param.getSubgroup());
  48. }
  49. private void updateDb(String num, Integer subgroup, Long scenePlusId){
  50. //查询缓存是否包含热点数据
  51. String key = String.format(RedisKey.SCENE_DRAW, RedisKey.getNumStr(num, subgroup));
  52. Map<String, String> map = redisClient.hmget(key);
  53. boolean hasSceneDraw= false;
  54. for (Map.Entry<String, String> tagMap : map.entrySet()) {
  55. if(StrUtil.isEmpty(tagMap.getValue())){
  56. continue;
  57. }
  58. hasSceneDraw = true;
  59. break;
  60. }
  61. //更改热点状态
  62. sceneEditInfoExtService.update(
  63. new LambdaUpdateWrapper<SceneEditInfoExt>()
  64. .set(SceneEditInfoExt::getSceneDraw, hasSceneDraw ? CommonStatus.YES.code().intValue() : CommonStatus.NO.code().intValue())
  65. .eq(SceneEditInfoExt::getScenePlusId,scenePlusId));
  66. }
  67. private void addOrUpdate(String num, Integer subgroup, List<JSONObject> data) throws Exception{
  68. Map<String, String> addOrUpdateMap = new HashMap<>();
  69. int i = 0;
  70. for (JSONObject jsonObject : data) {
  71. jsonObject.put("createTime", Calendar.getInstance().getTimeInMillis() + i++);
  72. addOrUpdateMap.put(jsonObject.getString("sid"), JSON.toJSONString(jsonObject));
  73. }
  74. // this.syncFileToRedis(num);
  75. //处理新增和修改数据
  76. this.addOrUpdateHandler(num, subgroup, addOrUpdateMap);
  77. }
  78. private void addOrUpdateHandler(String num, Integer subgroup, Map<String, String> addOrUpdateMap){
  79. if(CollUtil.isEmpty(addOrUpdateMap))
  80. return;
  81. //批量写入缓存
  82. String key = String.format(RedisKey.SCENE_DRAW, RedisKey.getNumStr(num, subgroup));
  83. redisClient.hmset(key, addOrUpdateMap);
  84. //写入本地文件,作为备份
  85. // this.writeFile(num);
  86. }
  87. // private void writeFile(String num){
  88. // String lockKey = String.format(RedisLockKey.LOCK_SCENE_DRAW, num);
  89. // String lockVal = cn.hutool.core.lang.UUID.randomUUID().toString();
  90. // boolean lock = redisLockUtil.lock(lockKey, lockVal, RedisKey.EXPIRE_TIME_1_MINUTE);
  91. // if(!lock){
  92. // return;
  93. // }
  94. // try{
  95. // String dataKey = String.format(RedisKey.SCENE_DRAW, num);
  96. // String sceneDrawJsonPath = String.format(ConstantFilePath.SCENE_USER_PATH_V4, num) + SCENE_DRAW_JSON_NAME;
  97. // if(!redisUtil.hasKey(dataKey)){
  98. // FileUtil.del(sceneDrawJsonPath);
  99. // return;
  100. // }
  101. // Map<String, String> map = redisUtil.hmget(dataKey);
  102. // List<JSONObject> list = map.entrySet().stream().map(entry->JSON.parseObject(entry.getValue())).collect(Collectors.toList());
  103. // FileUtil.writeUtf8String(JSON.toJSONString(list), sceneDrawJsonPath);
  104. // }finally {
  105. // redisLockUtil.unlockLua(lockKey, lockVal);
  106. // }
  107. // }
  108. // private void syncFileToRedis(String num) throws Exception{
  109. //
  110. // String key = String.format(RedisKey.SCENE_DRAW, num);
  111. // boolean exist = redisUtil.hasKey(key);
  112. // if(exist){
  113. // return;
  114. // }
  115. // String lockKey = String.format(RedisLockKey.LOCK_SCENE_DRAW, num);
  116. // String lockVal = cn.hutool.core.lang.UUID.randomUUID().toString();
  117. // boolean lock = redisLockUtil.lock(lockKey, lockVal, RedisKey.EXPIRE_TIME_1_MINUTE);
  118. // if(!lock){
  119. // throw new BusinessException(ErrorCode.SYSTEM_BUSY);
  120. // }
  121. // try{
  122. // exist = redisUtil.hasKey(key);
  123. // if(exist){
  124. // return;
  125. // }
  126. // String sceneDrawFilePath = String.format(ConstantFilePath.SCENE_USER_PATH_V4, num) + SCENE_DRAW_JSON_NAME;
  127. // String sceneDrawData = FileUtils.readUtf8String(sceneDrawFilePath);
  128. // if(StrUtil.isEmpty(sceneDrawData)){
  129. // return;
  130. // }
  131. // JSONArray tagsArr = JSON.parseArray(sceneDrawData);
  132. // if(CollUtil.isEmpty(tagsArr)){
  133. // return;
  134. // }
  135. // Map<String, String> map = new HashMap<>();
  136. // for (Object o : tagsArr) {
  137. // JSONObject jo = (JSONObject)o;
  138. // map.put(jo.getString("sid"), jo.toJSONString());
  139. // }
  140. // redisUtil.hmset(key, map);
  141. // }finally {
  142. // redisLockUtil.unlockLua(lockKey, lockVal);
  143. // }
  144. // }
  145. @Override
  146. public List<JSONObject> listSceneDraw(String num, Integer subgroup) throws Exception {
  147. List<JSONObject> tags = new ArrayList<>();
  148. // this.syncFileToRedis(num);
  149. //获取裁剪模型数据
  150. String key = String.format(RedisKey.SCENE_DRAW, RedisKey.getNumStr(num, subgroup));
  151. List<String> list = redisClient.hgetValues(key);
  152. if(CollUtil.isNotEmpty(list)){
  153. List<TagBean> sortList = list.stream().map(str -> {
  154. JSONObject jsonObject = JSON.parseObject(str);
  155. TagBean tagBean = new TagBean();
  156. tagBean.setCreateTime(jsonObject.getLong("createTime"));
  157. jsonObject.remove("createTime");
  158. tagBean.setTag(jsonObject);
  159. return tagBean;
  160. }).collect(Collectors.toList());
  161. sortList.sort(Comparator.comparingLong(TagBean::getCreateTime).reversed());
  162. tags = sortList.stream().map(item -> item.getTag()).collect(Collectors.toList());
  163. }
  164. return tags;
  165. }
  166. @Override
  167. public void deleteSceneDraw(DeleteSidListParamVO param) throws Exception {
  168. Scene scenePlus = sceneService.getByNum(param.getNum(), param.getSubgroup());
  169. List<String> deleteSidList = param.getSidList();
  170. // this.syncFileToRedis(param.getNum());
  171. //处理删除状态数据
  172. this.deleteCache(param.getNum(), param.getSubgroup(), deleteSidList);
  173. //写入本地文件,作为备份
  174. // this.writeFile(param.getNum());
  175. //保存数据库
  176. SceneEditInfoExt sceneEditInfoExt = sceneEditInfoExtService.getByScenePlusId(scenePlus.getId());
  177. this.updateDb(param.getNum(), param.getSubgroup(), scenePlus.getId());
  178. sceneEditInfoService.upgradeVersionById(sceneEditInfoExt.getEditInfoId());
  179. this.publicSceneDraw(param.getNum(), param.getSubgroup());
  180. }
  181. private List<String> deleteCache(String num, Integer subgroup, List<String> deleteSidList) {
  182. if(CollUtil.isEmpty(deleteSidList)){
  183. return null;
  184. }
  185. //从redis中加载热点数据
  186. String key = String.format(RedisKey.SCENE_DRAW, RedisKey.getNumStr(num, subgroup));
  187. List<String> deletDataList = redisClient.hMultiGet(key, deleteSidList);
  188. if(CollUtil.isNotEmpty(deletDataList)){
  189. redisClient.hdel(key, deleteSidList);
  190. }
  191. return deletDataList;
  192. }
  193. @Override
  194. public void publicSceneDraw(String sceneNum, Integer subgroup) throws IOException {
  195. String Key = String.format(RedisKey.SCENE_DRAW, RedisKey.getNumStr(sceneNum, subgroup));
  196. String userEditPath = String.format(UploadFilePath.USER_VIEW_PATH, sceneNum) + SCENE_DRAW_JSON_NAME;
  197. List<String> list = redisClient.hgetValues(Key);
  198. if(CollUtil.isEmpty(list)){
  199. fYunFileService.deleteFile(sceneNum, subgroup, userEditPath);
  200. return;
  201. }
  202. List<JSONObject> collect = list.stream().map(str -> JSON.parseObject(str)).collect(Collectors.toList());
  203. fYunFileService.uploadFile(sceneNum, subgroup, JSON.toJSONString(collect).getBytes(), userEditPath);
  204. }
  205. }