DeviceServiceImpl.java 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  1. package com.bizmatics.service.impl;
  2. import cn.afterturn.easypoi.excel.ExcelExportUtil;
  3. import cn.afterturn.easypoi.excel.entity.ExportParams;
  4. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  5. import com.baomidou.mybatisplus.core.metadata.IPage;
  6. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  7. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  8. import com.bizmatics.common.core.bean.CommonPage;
  9. import com.bizmatics.common.core.exception.BusinessException;
  10. import com.bizmatics.common.core.util.BeanMapperUtils;
  11. import com.bizmatics.common.core.util.FileUtils;
  12. import com.bizmatics.common.mvc.base.AbstractCrudService;
  13. import com.bizmatics.common.spring.util.GlobalUtils;
  14. import com.bizmatics.model.Device;
  15. import com.bizmatics.model.DeviceAnalogVariableList;
  16. import com.bizmatics.model.DeviceList;
  17. import com.bizmatics.model.DeviceStatus;
  18. import com.bizmatics.model.system.SysUser;
  19. import com.bizmatics.model.vo.CorrespondDeviceListVO;
  20. import com.bizmatics.model.vo.CorrespondDeviceVO;
  21. import com.bizmatics.model.vo.DeviceAnalogVariableListOneVo;
  22. import com.bizmatics.model.vo.DeviceOneVo;
  23. import com.bizmatics.persistence.mapper.DeviceMapper;
  24. import com.bizmatics.service.DeviceAnalogVariableListService;
  25. import com.bizmatics.service.DeviceService;
  26. import com.bizmatics.service.DeviceStatusService;
  27. import com.bizmatics.service.enums.DeviceStatusCode;
  28. import com.bizmatics.service.enums.DeviceType;
  29. import com.bizmatics.service.util.SecurityUtils;
  30. import com.bizmatics.service.vo.DeviceAnalogVariableListExportVO;
  31. import com.bizmatics.service.vo.DeviceCountVO;
  32. import com.bizmatics.service.vo.DeviceExportVO;
  33. import org.apache.poi.ss.usermodel.Workbook;
  34. import org.springframework.beans.factory.annotation.Autowired;
  35. import org.springframework.stereotype.Service;
  36. import java.io.File;
  37. import java.io.FileOutputStream;
  38. import java.io.IOException;
  39. import java.util.ArrayList;
  40. import java.util.Date;
  41. import java.util.List;
  42. import java.util.Optional;
  43. /**
  44. * 设备
  45. *
  46. * @author ya
  47. * @since 2021-07-07
  48. */
  49. @Service
  50. public class DeviceServiceImpl extends AbstractCrudService<DeviceMapper, Device> implements DeviceService {
  51. @Autowired
  52. private DeviceAnalogVariableListService deviceAnalogVariableListService;
  53. @Autowired
  54. private DeviceStatusService deviceStatusService;
  55. @Override
  56. public DeviceCountVO selectDeviceCount(Date startTime, Date endTime) {
  57. Integer userId = SecurityUtils.getLoginUser().getUser().getUserId().intValue();
  58. DeviceCountVO deviceCountVo = new DeviceCountVO();
  59. deviceCountVo.setNormalCount(baseMapper
  60. .selectCount(userId, null, DeviceStatusCode.NORMAL.getValue(), startTime, endTime, null));
  61. deviceCountVo.setOffLineCount(baseMapper
  62. .selectCount(userId, null, DeviceStatusCode.OFFLINE.getValue(), startTime, endTime, null));
  63. deviceCountVo.setDeviceCount(baseMapper
  64. .selectCount(userId, null, DeviceStatusCode.DEVICE.getValue(), startTime, endTime, null));
  65. deviceCountVo.setFaultCount(baseMapper
  66. .selectCount(userId, null, DeviceStatusCode.FAULT.getValue(), startTime, endTime, null));
  67. deviceCountVo.setCount(baseMapper
  68. .selectCount(userId, null, null, startTime, endTime, null));
  69. return deviceCountVo;
  70. }
  71. @Override
  72. public DeviceCountVO selectDeviceCountByType(Integer site) {
  73. DeviceCountVO deviceCountVo = new DeviceCountVO();
  74. Integer userId = SecurityUtils.getLoginUser().getUser().getUserId().intValue();
  75. int oneEightThreeEp = baseMapper.selectCount(userId, site, null, null, null, DeviceType.ONE_EIGHT_THREE_EP.getValue());
  76. int oneSevenOneEp = baseMapper.selectCount(userId, site, null, null, null, DeviceType.ONE_SEVEN_ONE_EP.getValue());
  77. int oneSevenThreeEp = baseMapper.selectCount(userId, site, null, null, null, DeviceType.ONE_SEVEN_THREE_EP.getValue());
  78. int video = baseMapper.selectCount(userId, site, null, null, null, DeviceType.VODEO_MONITROING.getValue());
  79. deviceCountVo.setEpCount(oneEightThreeEp + oneSevenOneEp + oneSevenThreeEp);
  80. deviceCountVo.setVideoCount(video);
  81. return deviceCountVo;
  82. }
  83. @Override
  84. public List<Device> list(Integer userId, Integer siteId, Integer deviceStatus, Date startTime, Date endTime, String type) {
  85. return baseMapper.list(userId, siteId, deviceStatus, startTime, endTime, type);
  86. }
  87. /**
  88. * 汇总
  89. *
  90. * @param list
  91. * @param deviceStatus
  92. * @param type
  93. * @return
  94. */
  95. public Long getCount(List<Device> list, Integer deviceStatus, String type) {
  96. return list.stream()
  97. .filter(device -> Optional.ofNullable(deviceStatus).map(ds -> ds.equals(device.getDeviceStatus())).orElse(true))
  98. .filter(device -> Optional.ofNullable(type).map(ds -> ds.equals(device.getDeviceType())).orElse(true))
  99. .count();
  100. }
  101. @Override
  102. public List<DeviceList> deviceList(String siteId) {
  103. List<DeviceList> DeviceList = null;
  104. DeviceList = baseMapper.DeviceList(Integer.parseInt(siteId));
  105. return DeviceList;
  106. }
  107. @Override
  108. public List<DeviceOneVo> dataManagementDeviceList(int siteId, int deviceType) {
  109. List<DeviceOneVo> deviceOneVo = new ArrayList<>();
  110. LambdaQueryWrapper<Device> queryWrapper = Wrappers.lambdaQuery();
  111. queryWrapper.eq(Device::getEnable, 1).eq(Device::getSiteId, siteId);
  112. List<Device> deviceList = this.list(queryWrapper);
  113. List<DeviceAnalogVariableList> deviceAnalogVariableList = baseMapper.deviceAnalogVariableList(siteId);
  114. if (deviceList.size() > 0) {
  115. for (int i = 0; i < deviceList.size(); i++) {
  116. DeviceOneVo deviceOneVoOne = new DeviceOneVo();
  117. List<DeviceAnalogVariableList> deviceAnalogVariableListTwo = new ArrayList<>();
  118. deviceOneVoOne.setId(deviceList.get(i).getId());
  119. deviceOneVoOne.setDeviceCode(deviceList.get(i).getDeviceCode());
  120. deviceOneVoOne.setDeviceName(deviceList.get(i).getDeviceName());
  121. if (deviceAnalogVariableList.size() > 0) {
  122. for (int j = 0; j < deviceAnalogVariableList.size(); j++) {
  123. if (deviceList.get(i).getId().equals(deviceAnalogVariableList.get(j).getCommunicationEquipment())) {
  124. deviceAnalogVariableListTwo.add(deviceAnalogVariableList.get(j));
  125. }
  126. }
  127. }
  128. deviceOneVoOne.setChildren(deviceAnalogVariableListTwo);
  129. deviceOneVo.add(deviceOneVoOne);
  130. }
  131. }
  132. return deviceOneVo;
  133. }
  134. @Override
  135. public void correspondDeviceAdd(Device device) {
  136. SysUser user = SecurityUtils.getLoginUser().getUser();
  137. device.setEnable(1);
  138. device.setInstallTime(new Date());
  139. device.setCreator(user.getUserName());
  140. this.save(device);
  141. String deviceCode = device.getDeviceCode();
  142. Integer siteId = device.getSiteId();
  143. DeviceStatus deviceStatus = new DeviceStatus();
  144. deviceStatus.setDeviceStatus(1);
  145. deviceStatus.setDeviceCode(deviceCode);
  146. deviceStatus.setStatusTime(new Date());
  147. deviceStatus.setSiteId(siteId);
  148. deviceStatusService.save(deviceStatus);
  149. }
  150. @Override
  151. public void correspondDeviceUpdate(Device device) {
  152. this.updateById(device);
  153. }
  154. @Override
  155. public void correspondDeviceDel(int id) {
  156. Device device = new Device();
  157. device.setId(id);
  158. device.setEnable(0);
  159. this.updateById(device);
  160. }
  161. @Override
  162. public CommonPage<CorrespondDeviceVO> correspondDeviceList(String deviceName, int size, int current) {
  163. List<CorrespondDeviceVO> correspondDeviceListOne = baseMapper.CorrespondDeviceList(deviceName, null, null);
  164. int total = 0;
  165. if (correspondDeviceListOne.size() > 0) {
  166. total = correspondDeviceListOne.size();
  167. }
  168. int startCurrent = (current - 1) * size;
  169. List<CorrespondDeviceVO> correspondDeviceList = baseMapper.CorrespondDeviceList(deviceName, startCurrent, size);
  170. return new CommonPage<>(correspondDeviceList, total, size, current);
  171. }
  172. @Override
  173. public List<CorrespondDeviceListVO> correspondDeviceListEcho(int id) {
  174. LambdaQueryWrapper<Device> queryWrapper = Wrappers.lambdaQuery();
  175. queryWrapper.eq(Device::getEnable, 1).eq(Device::getId, id);
  176. List<Device> deviceList = this.list(queryWrapper);
  177. LambdaQueryWrapper<DeviceAnalogVariableList> queryWrapperOne = Wrappers.lambdaQuery();
  178. queryWrapperOne.eq(DeviceAnalogVariableList::getStatus, 1).eq(DeviceAnalogVariableList::getCommunicationEquipment, deviceList.get(0).getId());
  179. List<DeviceAnalogVariableList> deviceAnalogVariableList = deviceAnalogVariableListService.list(queryWrapperOne);
  180. List<CorrespondDeviceListVO> list = new ArrayList<>();
  181. list.add(CorrespondDeviceListVO.builder().deviceCode(deviceList.get(0).getDeviceCode()).deviceName(deviceList.get(0).getDeviceName())
  182. .deviceType(deviceList.get(0).getDeviceType()).deviceAddress(deviceList.get(0).getDeviceAddress())
  183. .creator(deviceList.get(0).getCreator()).enable(deviceList.get(0).getEnable()).floor(deviceList.get(0).getFloor())
  184. .id(deviceList.get(0).getId()).sim(deviceList.get(0).getSim()).siteId(deviceList.get(0).getSiteId())
  185. .installTime(deviceList.get(0).getInstallTime()).deviceAnalogVariableList(deviceAnalogVariableList).build());
  186. return list;
  187. }
  188. @Override
  189. public CommonPage<Device> videoMonitoringDeviceList(String deviceName, Integer deviceType, Integer siteId, Integer size, Integer current) {
  190. IPage<Device> page = new Page<Device>(size, current);
  191. LambdaQueryWrapper<Device> queryWrapper = Wrappers.lambdaQuery();
  192. queryWrapper.eq(Device::getSiteId, siteId).eq(Device::getEnable, 1);
  193. if (deviceType != null && deviceType != 0) {
  194. queryWrapper.eq(Device::getDeviceType, deviceType);
  195. }
  196. if (null != deviceName) {
  197. queryWrapper.like(Device::getDeviceName, deviceName);
  198. }
  199. page = this.page(page, queryWrapper);
  200. this.ToCommonPage(page);
  201. return new CommonPage<>(page.getRecords(), page.getTotal(), page.getCurrent(), page.getSize());
  202. }
  203. @Override
  204. public void variableCloning(Integer type, String newDeviceCode, String oldDeviceCode, String deviceName) {
  205. SysUser user = SecurityUtils.getLoginUser().getUser();
  206. //查询出旧设备配置
  207. LambdaQueryWrapper<Device> queryWrapper = Wrappers.lambdaQuery();
  208. queryWrapper.eq(Device::getEnable, 1).eq(Device::getDeviceCode, oldDeviceCode);
  209. List<Device> deviceList = this.list(queryWrapper);
  210. if (type == 1) {
  211. //设备表新增
  212. Device device = new Device();
  213. device.setEnable(1);
  214. device.setInstallTime(new Date());
  215. device.setCreator(user.getUserName());
  216. device.setDeviceCode(newDeviceCode);
  217. device.setDeviceName(deviceName);
  218. device.setFloor(deviceList.get(0).getFloor());
  219. device.setSiteId(deviceList.get(0).getSiteId());
  220. device.setDeviceAddress(deviceList.get(0).getDeviceAddress());
  221. device.setDeviceType(deviceList.get(0).getDeviceType());
  222. device.setSim(deviceList.get(0).getSim());
  223. this.save(device);
  224. Integer deviceId = device.getId();
  225. String deviceCode = device.getDeviceCode();
  226. Integer siteId = device.getSiteId();
  227. //设备状态表新增
  228. DeviceStatus deviceStatus = new DeviceStatus();
  229. deviceStatus.setDeviceStatus(1);
  230. deviceStatus.setDeviceCode(deviceCode);
  231. deviceStatus.setStatusTime(new Date());
  232. deviceStatus.setSiteId(siteId);
  233. deviceStatusService.save(deviceStatus);
  234. //变量配置查询
  235. LambdaQueryWrapper<DeviceAnalogVariableList> queryWrapperOne = Wrappers.lambdaQuery();
  236. queryWrapperOne.eq(DeviceAnalogVariableList::getStatus, 1).eq(DeviceAnalogVariableList::getCommunicationEquipment, deviceList.get(0).getId());
  237. List<DeviceAnalogVariableList> deviceAnalogVariableList = deviceAnalogVariableListService.list(queryWrapperOne);
  238. if (deviceAnalogVariableList.size() > 0) {
  239. for (int i = 0; i < deviceAnalogVariableList.size(); i++) {
  240. DeviceAnalogVariableList deviceAnalogVariableListOne = new DeviceAnalogVariableList();
  241. deviceAnalogVariableListOne.setDeviceCode(newDeviceCode);
  242. deviceAnalogVariableListOne.setVariableName(deviceAnalogVariableList.get(i).getVariableName());
  243. deviceAnalogVariableListOne.setVariableCoding(deviceAnalogVariableList.get(i).getVariableCoding());
  244. deviceAnalogVariableListOne.setMonitoringEquipment(0);
  245. deviceAnalogVariableListOne.setCommunicationEquipment(deviceId);
  246. deviceAnalogVariableListOne.setDataAddress(deviceAnalogVariableList.get(i).getDataAddress());
  247. deviceAnalogVariableListOne.setDataType(deviceAnalogVariableList.get(i).getDataType());
  248. deviceAnalogVariableListOne.setCoefficient(deviceAnalogVariableList.get(i).getCoefficient());
  249. deviceAnalogVariableListOne.setSaveCycle(deviceAnalogVariableList.get(i).getSaveCycle());
  250. deviceAnalogVariableListOne.setDataArea(deviceAnalogVariableList.get(i).getDataArea());
  251. deviceAnalogVariableListOne.setCreator(user.getUserName());
  252. deviceAnalogVariableListOne.setCreateTime(new Date());
  253. deviceAnalogVariableListOne.setStatus(1);
  254. deviceAnalogVariableListService.save(deviceAnalogVariableListOne);
  255. }
  256. }
  257. } else {
  258. LambdaQueryWrapper<DeviceAnalogVariableList> queryWrapperOneA = Wrappers.lambdaQuery();
  259. queryWrapperOneA.eq(DeviceAnalogVariableList::getStatus, 1).eq(DeviceAnalogVariableList::getCommunicationEquipment, deviceList.get(0).getId());
  260. List<DeviceAnalogVariableList> deviceAnalogVariableListOne = deviceAnalogVariableListService.list(queryWrapperOneA);
  261. //克隆设备查询是否存在
  262. LambdaQueryWrapper<Device> queryWrapperTwo = Wrappers.lambdaQuery();
  263. queryWrapperTwo.eq(Device::getEnable, 1).eq(Device::getDeviceCode, newDeviceCode);
  264. List<Device> deviceListTwo = this.list(queryWrapperTwo);
  265. LambdaQueryWrapper<DeviceAnalogVariableList> queryWrapperTwoA = Wrappers.lambdaQuery();
  266. queryWrapperTwoA.eq(DeviceAnalogVariableList::getStatus, 1).eq(DeviceAnalogVariableList::getCommunicationEquipment, deviceListTwo.get(0).getId());
  267. List<DeviceAnalogVariableList> deviceAnalogVariableListTwo = deviceAnalogVariableListService.list(queryWrapperTwoA);
  268. if (deviceAnalogVariableListTwo.size() > 0) {
  269. for (int i = 0; i < deviceAnalogVariableListTwo.size(); i++) {
  270. DeviceAnalogVariableList deviceAnalogVariableList = new DeviceAnalogVariableList();
  271. deviceAnalogVariableList.setStatus(0);
  272. deviceAnalogVariableList.setId(deviceAnalogVariableListTwo.get(i).getId());
  273. deviceAnalogVariableListService.updateById(deviceAnalogVariableList);
  274. }
  275. }
  276. if (deviceAnalogVariableListOne.size() > 0) {
  277. for (int i = 0; i < deviceAnalogVariableListOne.size(); i++) {
  278. DeviceAnalogVariableList deviceAnalogVariableList = new DeviceAnalogVariableList();
  279. deviceAnalogVariableList.setDeviceCode(newDeviceCode);
  280. deviceAnalogVariableList.setVariableName(deviceAnalogVariableListOne.get(i).getVariableName());
  281. deviceAnalogVariableList.setVariableCoding(deviceAnalogVariableListOne.get(i).getVariableCoding());
  282. deviceAnalogVariableList.setMonitoringEquipment(0);
  283. deviceAnalogVariableList.setCommunicationEquipment(deviceListTwo.get(0).getId());
  284. deviceAnalogVariableList.setDataType(deviceAnalogVariableListOne.get(i).getDataType());
  285. deviceAnalogVariableList.setDataAddress(deviceAnalogVariableListOne.get(i).getDataAddress());
  286. deviceAnalogVariableList.setSaveCycle(deviceAnalogVariableListOne.get(i).getSaveCycle());
  287. deviceAnalogVariableList.setCoefficient(deviceAnalogVariableListOne.get(i).getCoefficient());
  288. deviceAnalogVariableList.setDataArea(deviceAnalogVariableListOne.get(i).getDataArea());
  289. deviceAnalogVariableList.setCreator(user.getUserName());
  290. deviceAnalogVariableList.setCreateTime(new Date());
  291. deviceAnalogVariableList.setStatus(1);
  292. deviceAnalogVariableListService.save(deviceAnalogVariableList);
  293. }
  294. }
  295. }
  296. }
  297. @Override
  298. public List<Device> deviceListOne(Integer siteId, Integer deviceType) {
  299. LambdaQueryWrapper<Device> queryWrapperTwo = Wrappers.lambdaQuery();
  300. queryWrapperTwo.eq(Device::getEnable, 1).eq(Device::getSiteId, siteId).eq(Device::getDeviceType, deviceType);
  301. List<Device> deviceListTwo = this.list(queryWrapperTwo);
  302. return deviceListTwo;
  303. }
  304. @Override
  305. public String deviceExport(String deviceName, Integer deviceType, Integer siteId) {
  306. Workbook workbook = null;
  307. File file = null;
  308. try {
  309. ExportParams params = new ExportParams(null, "通信设备列表");
  310. workbook = ExcelExportUtil.exportBigExcel(params, DeviceExportVO.class,
  311. (o, i) -> {
  312. Page<Device> page = new Page<>(i, 30);
  313. LambdaQueryWrapper<Device> queryWrapper = Wrappers.lambdaQuery();
  314. queryWrapper.eq(Device::getSiteId, siteId).eq(Device::getEnable, 1);
  315. if (deviceType != 0) {
  316. queryWrapper.eq(Device::getDeviceType, deviceType);
  317. }
  318. if (null != deviceName) {
  319. queryWrapper.like(Device::getDeviceName, deviceName);
  320. }
  321. page = this.page(page, queryWrapper);
  322. // this.ToCommonPage(page);
  323. return new ArrayList<>(BeanMapperUtils.mapList(page.getRecords(), Device.class, DeviceExportVO.class));
  324. }, null);
  325. if (null != workbook) {
  326. file = FileUtils.getFile(GlobalUtils.getTempBaseDir(), String.format("%s-%s.xlsx", "通信设备列表", System.currentTimeMillis() + ""));
  327. FileUtils.createFile(file.getAbsolutePath());
  328. FileOutputStream allListingFileOutputStream = new FileOutputStream(file);
  329. workbook.write(allListingFileOutputStream);
  330. } else {
  331. throw new BusinessException("表格数据为空");
  332. }
  333. } catch (Exception e) {
  334. log.error("导出文件失败", e);
  335. throw new BusinessException("导出文件失败");
  336. } finally {
  337. if (workbook != null) {
  338. try {
  339. workbook.close();
  340. } catch (IOException e) {
  341. log.error("===export spec=== 关闭workbook失败", e);
  342. }
  343. }
  344. }
  345. return file.getName();
  346. }
  347. }