DeviceService.java 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484
  1. package com.gyee.alarm.service;
  2. import com.alibaba.fastjson.JSONObject;
  3. import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
  4. import com.baomidou.mybatisplus.core.metadata.IPage;
  5. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  6. import com.google.common.collect.Lists;
  7. import com.gyee.alarm.init.Cache.PointUnitCache;
  8. import com.gyee.alarm.init.Cache.WindturbineCache;
  9. import com.gyee.alarm.mapper.auto.*;
  10. import com.gyee.alarm.model.auto.*;
  11. import com.gyee.alarm.model.vo.TreeNode;
  12. import com.gyee.alarm.service.auto.impl.ProBasicDeviceMetricsServiceImpl;
  13. import com.gyee.alarm.service.auto.impl.ProBasicDeviceModelMetricsServiceImpl;
  14. import com.gyee.alarm.service.auto.impl.ProBasicEquipmentPointServiceImpl;
  15. import com.gyee.alarm.service.auto.initalcache.CacheService;
  16. import com.gyee.alarm.util.SnowflakeGenerator;
  17. import com.gyee.alarm.util.StringUtils;
  18. import lombok.extern.slf4j.Slf4j;
  19. import org.springframework.stereotype.Service;
  20. import javax.annotation.Resource;
  21. import java.util.ArrayList;
  22. import java.util.HashMap;
  23. import java.util.List;
  24. import java.util.Map;
  25. import java.util.stream.Collectors;
  26. /**
  27. * 设备管理相关数据服务
  28. */
  29. @Service
  30. @Slf4j
  31. public class DeviceService {
  32. @Resource
  33. private WindturbineCache windturbineCache;
  34. @Resource
  35. private ProBasicDeviceMetricsServiceImpl proBasicDeviceMetricsService;
  36. @Resource
  37. private ProBasicDeviceModelMetricsServiceImpl proBasicDeviceModelMetricsService;
  38. @Resource
  39. private ProBasicEquipmentPointServiceImpl proBasicEquipmentPointService;
  40. @Resource
  41. private PointUnitCache pointUnitCache;
  42. @Resource
  43. private ProBasicDeviceMapper proBasicDeviceMapper;
  44. @Resource
  45. private ProBasicDeviceStructureMapper proBasicDeviceStructureMapper;
  46. @Resource
  47. private ProBasicDeviceMetricsMapper proBasicDeviceMetricsMapper;
  48. @Resource
  49. private ProBasicDeviceModelMapper proBasicDeviceModelMapper;
  50. @Resource
  51. private ProBasicDeviceModelMetricsMapper roBasicDeviceModelMetricsMapper;
  52. public JSONObject getTestingPointByStructCode(String deviceId, String structCode, String windturbineId, int pageSize, int pageNum) {
  53. JSONObject resultObject = new JSONObject();
  54. List<JSONObject> jsonArrayList = new ArrayList<>();
  55. String modelId = "";
  56. ProBasicEquipment windturbine = windturbineCache.getWindturbine(windturbineId);
  57. if (windturbine != null)
  58. {
  59. modelId = windturbine.getModelId();
  60. }
  61. if (StringUtils.isNotBlank(structCode)) {
  62. List<ProBasicDeviceMetrics> metricsList = proBasicDeviceMetricsService.getDeviceMetricsByStructureCode(deviceId, structCode);
  63. List<String> collect = metricsList.stream().map(s -> s.getMetricCode()).collect(Collectors.toList());
  64. if (collect.size() > 0) {
  65. List<ProBasicDeviceModelMetrics> deviceModelMetricsList = proBasicDeviceModelMetricsService.getDeviceModelMetrics(collect, modelId);
  66. List<String> uniformcodeList = deviceModelMetricsList.stream().map(s -> s.getUniformCode()).collect(Collectors.toList());
  67. List<String> uniformcodeList_AI = uniformcodeList.stream().filter(s -> s.toUpperCase().contains("AI")).collect(Collectors.toList());
  68. List<String> uniformcodeList_DI = uniformcodeList.stream().filter(s -> s.toUpperCase().contains("DI")).collect(Collectors.toList());
  69. List<ProBasicEquipmentPoint> testingPointAIS = new ArrayList<>();
  70. if (uniformcodeList_AI.size() > 0)
  71. testingPointAIS = proBasicEquipmentPointService.getPointListAI(windturbineId, uniformcodeList_AI);
  72. List<ProBasicEquipmentPoint> testingPointDIS = new ArrayList<>();
  73. if (uniformcodeList_DI.size() > 0)
  74. testingPointDIS = proBasicEquipmentPointService.getPointListDI(windturbineId, uniformcodeList_DI);
  75. Map<String, ProBasicDeviceMetrics> mapByModel = pointUnitCache.getMapByModel(modelId);
  76. List<JSONObject> array_ai = new ArrayList<>();
  77. List<JSONObject> array_di = new ArrayList<>();
  78. for (ProBasicEquipmentPoint ai :
  79. testingPointAIS) {
  80. if(ai.getUniformCode()==null||!ai.getUniformCode().toUpperCase().startsWith("AI"))
  81. continue;
  82. JSONObject obj = new JSONObject();
  83. obj.put("englishName", ai.getEnglishName());
  84. obj.put("modelId", ai.getModelId());
  85. obj.put("name", ai.getName());
  86. obj.put("valueUnit", ai.getValueUnit());
  87. obj.put("model", ai.getModel());
  88. obj.put("typeId", ai.getTypeId());
  89. obj.put("id", ai.getId());
  90. obj.put("uniformCode", ai.getUniformCode());
  91. String unitName = "";
  92. String unitNameCn = "";
  93. if (mapByModel != null) {
  94. if (mapByModel.containsKey(ai.getUniformCode())) {
  95. unitName = mapByModel.get(ai.getUniformCode()).getUnitName();
  96. unitNameCn = mapByModel.get(ai.getUniformCode()).getUnitNamecn();
  97. }
  98. }
  99. obj.put("unitName", unitName);
  100. obj.put("unitNameCn", unitNameCn);
  101. array_ai.add(obj);
  102. }
  103. for (ProBasicEquipmentPoint di :
  104. testingPointDIS) {
  105. if(di.getUniformCode()==null||!di.getUniformCode().toUpperCase().startsWith("DI"))
  106. continue;
  107. JSONObject obj = new JSONObject();
  108. obj.put("englishName", di.getEnglishName());
  109. obj.put("modelId", di.getModelId());
  110. obj.put("name", di.getName());
  111. obj.put("valueUnit", di.getValueUnit());
  112. obj.put("model", di.getModel());
  113. obj.put("typeId", di.getTypeId());
  114. obj.put("id", di.getId());
  115. obj.put("uniformCode", di.getUniformCode());
  116. String unitName = "";
  117. String unitNameCn = "";
  118. if (mapByModel != null) {
  119. if (mapByModel.containsKey(di.getUniformCode())) {
  120. unitName = mapByModel.get(di.getUniformCode()).getUnitName();
  121. unitNameCn = mapByModel.get(di.getUniformCode()).getUnitNamecn();
  122. }
  123. }
  124. obj.put("unitName", unitName);
  125. obj.put("unitNameCn", unitNameCn);
  126. array_di.add(obj);
  127. }
  128. jsonArrayList.addAll(array_ai);
  129. jsonArrayList.addAll(array_di);
  130. List<List<JSONObject>> partition = Lists.partition(jsonArrayList, pageSize);
  131. if (partition.size() > 0)
  132. resultObject.put("records", partition.get(pageNum - 1));
  133. else
  134. resultObject.put("records", new ArrayList<>());
  135. resultObject.put("total", jsonArrayList.size());
  136. resultObject.put("pages", partition.size());
  137. resultObject.put("current", pageNum);
  138. }
  139. } else {
  140. List<ProBasicEquipmentPoint> aiPointList = proBasicEquipmentPointService.getPointListAI(windturbineId, new ArrayList<>());
  141. List<ProBasicEquipmentPoint> diPointList = proBasicEquipmentPointService.getPointListDI(windturbineId, new ArrayList<>());
  142. Map<String, ProBasicDeviceMetrics> mapByModel = pointUnitCache.getMapByModel(modelId);
  143. List<JSONObject> array_ai = new ArrayList<>();
  144. List<JSONObject> array_di = new ArrayList<>();
  145. for (ProBasicEquipmentPoint ai :
  146. aiPointList) {
  147. if(ai.getUniformCode()==null||!ai.getUniformCode().toUpperCase().startsWith("AI"))
  148. continue;
  149. JSONObject obj = new JSONObject();
  150. obj.put("englishName", ai.getEnglishName());
  151. obj.put("modelId", ai.getModelId());
  152. obj.put("name", ai.getName());
  153. obj.put("valueUnit", ai.getValueUnit());
  154. obj.put("model", ai.getModel());
  155. obj.put("typeId", ai.getTypeId());
  156. obj.put("id", ai.getId());
  157. obj.put("uniformCode", ai.getUniformCode());
  158. String unitName = "";
  159. String unitNameCn = "";
  160. if (mapByModel != null) {
  161. if (mapByModel.containsKey(ai.getUniformCode())) {
  162. unitName = mapByModel.get(ai.getUniformCode()).getUnitName();
  163. unitNameCn = mapByModel.get(ai.getUniformCode()).getUnitNamecn();
  164. }
  165. }
  166. obj.put("unitName", unitName);
  167. obj.put("unitNameCn", unitNameCn);
  168. array_ai.add(obj);
  169. }
  170. for (ProBasicEquipmentPoint di :
  171. diPointList) {
  172. if(di.getUniformCode()==null||!di.getUniformCode().toUpperCase().startsWith("DI"))
  173. continue;
  174. JSONObject obj = new JSONObject();
  175. obj.put("englishName", di.getEnglishName());
  176. obj.put("modelId", di.getModelId());
  177. obj.put("name", di.getName());
  178. obj.put("valueUnit", di.getValueUnit());
  179. obj.put("model", di.getModel());
  180. obj.put("typeId", di.getTypeId());
  181. obj.put("id", di.getId());
  182. obj.put("uniformCode", di.getUniformCode());
  183. String unitName = "";
  184. String unitNameCn = "";
  185. if (mapByModel != null) {
  186. if (mapByModel.containsKey(di.getUniformCode())) {
  187. unitName = mapByModel.get(di.getUniformCode()).getUnitName();
  188. unitNameCn = mapByModel.get(di.getUniformCode()).getUnitNamecn();
  189. }
  190. }
  191. obj.put("unitName", unitName);
  192. obj.put("unitNameCn", unitNameCn);
  193. array_di.add(obj);
  194. }
  195. jsonArrayList.addAll(array_ai);
  196. jsonArrayList.addAll(array_di);
  197. List<List<JSONObject>> partition = Lists.partition(jsonArrayList, pageSize);
  198. if (partition.size() > 0)
  199. resultObject.put("records", partition.get(pageNum - 1));
  200. else
  201. resultObject.put("records", new ArrayList<>());
  202. resultObject.put("total", jsonArrayList.size());
  203. resultObject.put("pages", partition.size());
  204. resultObject.put("current", pageNum);
  205. }
  206. return resultObject;
  207. }
  208. public List<ProBasicDevice> getAllDevice() {
  209. QueryWrapper<ProBasicDevice> queryWrapper = new QueryWrapper<>();
  210. return proBasicDeviceMapper.selectList(queryWrapper);
  211. }
  212. public List<ProBasicDeviceStructure> getAllDeviceStructure() {
  213. QueryWrapper<ProBasicDeviceStructure> queryWrapper = new QueryWrapper<>();
  214. return proBasicDeviceStructureMapper.selectList(queryWrapper);
  215. }
  216. public List<ProBasicDeviceStructure> getDeviceStructureByDeviceId(String deviceId) {
  217. return proBasicDeviceStructureMapper.getDeviceStructureByDeviceId(deviceId);
  218. }
  219. public List<ProBasicDeviceStructure> getDeviceStructureChildNode(String deviceId, String nodeCode) {
  220. return proBasicDeviceStructureMapper.getDeviceStructureChildNode(deviceId, nodeCode);
  221. }
  222. public ProBasicDeviceStructure getDeviceStructureByCode(String deviceId, String nodeCode) {
  223. return proBasicDeviceStructureMapper.getDeviceStructureByCode(deviceId, nodeCode);
  224. }
  225. public TreeNode<ProBasicDeviceStructure> getDeviceStructureTree(String deviceId) {
  226. if (CacheService.deviceStructureMap == null) {
  227. CacheService.deviceStructureMap = new HashMap<>();
  228. }
  229. if (CacheService.deviceStructureMap.containsKey(deviceId))
  230. return CacheService.deviceStructureMap.get(deviceId);
  231. List<ProBasicDeviceStructure> lst = proBasicDeviceStructureMapper.getDeviceStructureChildNode(deviceId, "0");
  232. if (lst != null && lst.size() > 0) {
  233. ProBasicDeviceStructure ds = lst.get(0);
  234. TreeNode<ProBasicDeviceStructure> treeNode = getSubDeviceStructureTreeNode(ds);
  235. CacheService.deviceStructureMap.put(ds.getDeviceId(), treeNode);
  236. return treeNode;
  237. }
  238. return null;
  239. }
  240. public TreeNode<ProBasicDeviceStructure> getDeviceStructureTree(String deviceId, String nodeCode) {
  241. ProBasicDeviceStructure ds = proBasicDeviceStructureMapper.getDeviceStructureByCode(deviceId, nodeCode);
  242. return getSubDeviceStructureTreeNode(ds);
  243. }
  244. private TreeNode<ProBasicDeviceStructure> getSubDeviceStructureTreeNode(ProBasicDeviceStructure ds) {
  245. TreeNode<ProBasicDeviceStructure> rootNode = new TreeNode<>();
  246. if (ds == null)
  247. return rootNode;
  248. rootNode.setNode(ds);
  249. List<ProBasicDeviceStructure> dsList = proBasicDeviceStructureMapper.getDeviceStructureChildNode(ds.getDeviceId(), ds.getCode());
  250. if (dsList != null && dsList.size() > 0) {
  251. List<TreeNode> children = new ArrayList<>();
  252. for (ProBasicDeviceStructure ds1 : dsList) {
  253. children.add(getSubDeviceStructureTreeNode(ds1));
  254. }
  255. rootNode.setChildren(children);
  256. }
  257. return rootNode;
  258. }
  259. public int deleteDeviceStructure(String dsId) {
  260. int code = proBasicDeviceStructureMapper.deleteById(dsId);
  261. if (code >= 1) {
  262. //树结构变化清空树结构缓存
  263. CacheService.deviceStructureMap.clear();
  264. }
  265. return code;
  266. }
  267. public int saveOrUpdateDeviceStructure(ProBasicDeviceStructure ds) {
  268. int result = 0;
  269. if (StringUtils.empty(ds.getId())) {
  270. //如果id为空,则通过雪花算法 生成一个id
  271. ds.setId(String.valueOf(SnowflakeGenerator.generateId()));
  272. result = proBasicDeviceStructureMapper.insert(ds);
  273. } else {
  274. ProBasicDeviceStructure oldData = proBasicDeviceStructureMapper.selectById(ds.getId());
  275. if (oldData != null) {
  276. result = proBasicDeviceStructureMapper.updateById(ds);
  277. } else {
  278. result = proBasicDeviceStructureMapper.insert(ds);
  279. }
  280. }
  281. //树结构变化清空树结构缓存
  282. CacheService.deviceStructureMap.clear();
  283. return result;
  284. }
  285. public List<ProBasicDeviceMetrics> getDeviceMetricsByDeviceId(String deviceId) {
  286. return proBasicDeviceMetricsMapper.getDeviceMetricsByDeviceId(deviceId);
  287. }
  288. public List<ProBasicDeviceMetrics> getDeviceMetricsByStructureCode(String deviceId, String structureCode) {
  289. return proBasicDeviceMetricsMapper.getDeviceMetricsByStructureCode(deviceId, structureCode);
  290. }
  291. public ProBasicDeviceMetrics getDeviceMetricsByCode(String deviceId, String metricCode) {
  292. return proBasicDeviceMetricsMapper.getDeviceMetricsByCode(deviceId, metricCode);
  293. }
  294. public int deleteDeviceMetric(String metricId) {
  295. return proBasicDeviceMetricsMapper.deleteById(metricId);
  296. }
  297. public int saveOrUpdateDeviceMetric(ProBasicDeviceMetrics metrics, List<ProBasicDeviceModelMetrics> deviceModelMetrics) {
  298. int result = 0;
  299. if ( StringUtils.empty(metrics.getId())) {
  300. //如果id为空,则通过雪花算法 生成一个id
  301. metrics.setId(String.valueOf(SnowflakeGenerator.generateId()));
  302. result = proBasicDeviceMetricsMapper.insert(metrics);
  303. } else {
  304. ProBasicDeviceMetrics oldData = proBasicDeviceMetricsMapper.selectById(metrics.getId());
  305. if (oldData != null) {
  306. result = proBasicDeviceMetricsMapper.updateById(metrics);
  307. } else {
  308. result = proBasicDeviceMetricsMapper.insert(metrics);
  309. }
  310. }
  311. if (deviceModelMetrics != null && deviceModelMetrics.size() > 0) {
  312. for (ProBasicDeviceModelMetrics deviceModelMetrics1 : deviceModelMetrics) {
  313. if (StringUtils.empty(deviceModelMetrics1.getId())) {
  314. //如果id为空,则通过雪花算法 生成一个id
  315. deviceModelMetrics1.setId(String.valueOf(SnowflakeGenerator.generateId()));
  316. result = roBasicDeviceModelMetricsMapper.insert(deviceModelMetrics1);
  317. } else {
  318. ProBasicDeviceModelMetrics deviceModelMetrics2 = roBasicDeviceModelMetricsMapper.selectById(deviceModelMetrics1.getId());
  319. if (deviceModelMetrics2 != null) {
  320. result = roBasicDeviceModelMetricsMapper.updateById(deviceModelMetrics1);
  321. } else {
  322. result = roBasicDeviceModelMetricsMapper.insert(deviceModelMetrics1);
  323. }
  324. }
  325. }
  326. }
  327. return result;
  328. }
  329. public List<ProBasicDeviceModel> getDeviceModelsById(String id) {
  330. return proBasicDeviceModelMapper.getDeviceModelsById(id);
  331. }
  332. public int deleteDeviceModel(int id) {
  333. return proBasicDeviceModelMapper.deleteById(id);
  334. }
  335. public ProBasicDeviceModel getDeviceModelByCode(String deviceid, String code) {
  336. return proBasicDeviceModelMapper.getDeviceModelByCode(deviceid, code);
  337. }
  338. public int saveOrUpdateDeviceModel(ProBasicDeviceModel deviceModel) {
  339. int result = 0;
  340. if (StringUtils.notEmp(deviceModel.getId())) {
  341. //如果id为空,则通过雪花算法 生成一个id
  342. deviceModel.setId(String.valueOf(SnowflakeGenerator.generateId()));
  343. result = proBasicDeviceModelMapper.insert(deviceModel);
  344. } else {
  345. ProBasicDeviceModel oldData = proBasicDeviceModelMapper.selectById(deviceModel.getId());
  346. if (oldData != null) {
  347. result = proBasicDeviceModelMapper.updateById(deviceModel);
  348. } else {
  349. result = proBasicDeviceModelMapper.insert(deviceModel);
  350. }
  351. }
  352. return result;
  353. }
  354. public List<ProBasicDeviceModel> getDeviceModelList() {
  355. QueryWrapper<ProBasicDeviceModel> queryWrapper = new QueryWrapper<>();
  356. return proBasicDeviceModelMapper.selectList(queryWrapper);
  357. }
  358. public List<ProBasicDeviceModelMetrics> getDeviceModelMetricsList() {
  359. QueryWrapper<ProBasicDeviceModelMetrics> queryWrapper = new QueryWrapper<>();
  360. return roBasicDeviceModelMetricsMapper.selectList(queryWrapper);
  361. }
  362. public IPage<ProBasicDeviceMetrics> pageQueryAll(Page page, String deviceId, String structureCode) {
  363. if(StringUtils.empty(deviceId))
  364. {
  365. deviceId=null;
  366. }
  367. if(StringUtils.empty(structureCode))
  368. {
  369. structureCode=null;
  370. }
  371. return proBasicDeviceMetricsMapper.pageQueryAll(page, deviceId, structureCode);
  372. }
  373. public int insertOrUpdate(List<ProBasicDeviceMetrics> deviceMetrics) {
  374. int s = 0;
  375. for (ProBasicDeviceMetrics deviceMetrics1 : deviceMetrics) {
  376. ProBasicDeviceMetrics deviceMetrics2 = proBasicDeviceMetricsMapper.selectByMetriccode(deviceMetrics1.getMetricCode());
  377. if (deviceMetrics2 != null) {
  378. deviceMetrics1.setId(deviceMetrics2.getId());
  379. s = proBasicDeviceMetricsMapper.updateById(deviceMetrics1);
  380. } else {
  381. deviceMetrics1.setDeviceId("windturbine");
  382. deviceMetrics1.setId(String.valueOf(SnowflakeGenerator.generateId()));
  383. s = proBasicDeviceMetricsMapper.insert(deviceMetrics1);
  384. }
  385. }
  386. return s;
  387. }
  388. public IPage<ProBasicDeviceModel> pageQueryDeviceModel(Page<ProBasicDeviceModel> page, String code, String stationname) {
  389. QueryWrapper<ProBasicDeviceModel> wrapper = new QueryWrapper<>();
  390. if (StringUtils.isNotBlank(code))
  391. wrapper.like("code", code);
  392. if (StringUtils.isNotBlank(stationname))
  393. wrapper.like("station_name", stationname);
  394. return proBasicDeviceModelMapper.selectPage(page, wrapper);
  395. }
  396. }