DeviceServiceImpl.java 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523
  1. package com.bizmatics.service.impl;
  2. import cn.afterturn.easypoi.excel.ExcelExportUtil;
  3. import cn.afterturn.easypoi.excel.ExcelImportUtil;
  4. import cn.afterturn.easypoi.excel.entity.ExportParams;
  5. import cn.afterturn.easypoi.excel.entity.ImportParams;
  6. import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
  7. import com.baomidou.mybatisplus.core.metadata.IPage;
  8. import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
  9. import com.baomidou.mybatisplus.core.toolkit.Wrappers;
  10. import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
  11. import com.bizmatics.common.core.bean.CommonPage;
  12. import com.bizmatics.common.core.exception.BusinessException;
  13. import com.bizmatics.common.core.util.BeanMapperUtils;
  14. import com.bizmatics.common.core.util.FileUtils;
  15. import com.bizmatics.common.mvc.base.AbstractCrudService;
  16. import com.bizmatics.common.spring.util.GlobalUtils;
  17. import com.bizmatics.model.*;
  18. import com.bizmatics.model.system.SysUser;
  19. import com.bizmatics.model.vo.*;
  20. import com.bizmatics.persistence.mapper.DeviceMapper;
  21. import com.bizmatics.service.DeviceAnalogVariableListService;
  22. import com.bizmatics.service.DeviceAttributeService;
  23. import com.bizmatics.service.DeviceService;
  24. import com.bizmatics.service.DeviceStatusService;
  25. import com.bizmatics.service.enums.DeviceStatusCode;
  26. import com.bizmatics.service.enums.DeviceType;
  27. import com.bizmatics.service.util.SecurityUtils;
  28. import com.bizmatics.service.vo.CorrespondDeviceExportVO;
  29. import com.bizmatics.service.vo.DeviceCountVO;
  30. import com.bizmatics.service.vo.DeviceExportVO;
  31. import com.bizmatics.service.vo.DeviceImportVo;
  32. import org.apache.poi.ss.usermodel.Workbook;
  33. import org.springframework.beans.factory.annotation.Autowired;
  34. import org.springframework.stereotype.Service;
  35. import org.springframework.transaction.annotation.Transactional;
  36. import org.springframework.transaction.interceptor.TransactionAspectSupport;
  37. import org.springframework.web.multipart.MultipartFile;
  38. import java.io.File;
  39. import java.io.FileOutputStream;
  40. import java.io.IOException;
  41. import java.text.SimpleDateFormat;
  42. import java.util.ArrayList;
  43. import java.util.Date;
  44. import java.util.List;
  45. import java.util.Optional;
  46. /**
  47. * 设备
  48. *
  49. * @author ya
  50. * @since 2021-07-07
  51. */
  52. @Service
  53. public class DeviceServiceImpl extends AbstractCrudService<DeviceMapper, Device> implements DeviceService {
  54. @Autowired
  55. private DeviceAnalogVariableListService deviceAnalogVariableListService;
  56. @Autowired
  57. private DeviceStatusService deviceStatusService;
  58. @Autowired
  59. private DeviceAttributeService deviceAttributeService;
  60. @Override
  61. public DeviceCountVO selectDeviceCount(Date startTime, Date endTime) {
  62. Integer userId = SecurityUtils.getLoginUser().getUser().getUserId().intValue();
  63. DeviceCountVO deviceCountVo = new DeviceCountVO();
  64. deviceCountVo.setNormalCount(baseMapper
  65. .selectCount(userId, null, DeviceStatusCode.NORMAL.getValue(), startTime, endTime, null));
  66. deviceCountVo.setOffLineCount(baseMapper
  67. .selectCount(userId, null, DeviceStatusCode.OFFLINE.getValue(), startTime, endTime, null));
  68. deviceCountVo.setDeviceCount(baseMapper
  69. .selectCount(userId, null, DeviceStatusCode.DEVICE.getValue(), startTime, endTime, null));
  70. deviceCountVo.setFaultCount(baseMapper
  71. .selectCount(userId, null, DeviceStatusCode.FAULT.getValue(), startTime, endTime, null));
  72. deviceCountVo.setCount(baseMapper
  73. .selectCount(userId, null, null, startTime, endTime, null));
  74. return deviceCountVo;
  75. }
  76. @Override
  77. public DeviceCountVO selectDeviceCountByType(Integer site) {
  78. DeviceCountVO deviceCountVo = new DeviceCountVO();
  79. Integer userId = SecurityUtils.getLoginUser().getUser().getUserId().intValue();
  80. int oneEightThreeEp = baseMapper.selectCount(userId, site, null, null, null, DeviceType.ONE_EIGHT_THREE_EP.getValue());
  81. int oneSevenOneEp = baseMapper.selectCount(userId, site, null, null, null, DeviceType.ONE_SEVEN_ONE_EP.getValue());
  82. int oneSevenThreeEp = baseMapper.selectCount(userId, site, null, null, null, DeviceType.ONE_SEVEN_THREE_EP.getValue());
  83. int video = baseMapper.selectCount(userId, site, null, null, null, DeviceType.VODEO_MONITROING.getValue());
  84. deviceCountVo.setEpCount(oneEightThreeEp + oneSevenOneEp + oneSevenThreeEp);
  85. deviceCountVo.setVideoCount(video);
  86. return deviceCountVo;
  87. }
  88. @Override
  89. public List<Device> list(Integer userId, Integer siteId, Integer deviceStatus, Date startTime, Date endTime, String type) {
  90. return baseMapper.list(userId, siteId, deviceStatus, startTime, endTime, type);
  91. }
  92. /**
  93. * 汇总
  94. *
  95. * @param list
  96. * @param deviceStatus
  97. * @param type
  98. * @return
  99. */
  100. public Long getCount(List<Device> list, Integer deviceStatus, String type) {
  101. return list.stream()
  102. .filter(device -> Optional.ofNullable(deviceStatus).map(ds -> ds.equals(device.getDeviceStatus())).orElse(true))
  103. .filter(device -> Optional.ofNullable(type).map(ds -> ds.equals(device.getDeviceType())).orElse(true))
  104. .count();
  105. }
  106. @Override
  107. public List<DeviceList> deviceList(String siteId) {
  108. List<DeviceList> DeviceList = null;
  109. DeviceList = baseMapper.DeviceList(Integer.parseInt(siteId));
  110. return DeviceList;
  111. }
  112. @Override
  113. public List<DeviceOneVo> dataManagementDeviceList(int siteId, int deviceType) {
  114. List<DeviceOneVo> deviceOneVo = new ArrayList<>();
  115. LambdaQueryWrapper<DeviceAttribute> queryWrapper = Wrappers.lambdaQuery();
  116. queryWrapper.eq(DeviceAttribute::getStatus, 1).eq(DeviceAttribute::getSiteId, siteId);
  117. List<DeviceAttribute> deviceList = deviceAttributeService.list(queryWrapper);
  118. List<DeviceAnalogVariableList> deviceAnalogVariableList = baseMapper.deviceAnalogVariableList(siteId);
  119. if (deviceList.size() > 0) {
  120. for (int i = 0; i < deviceList.size(); i++) {
  121. DeviceOneVo deviceOneVoOne = new DeviceOneVo();
  122. List<DeviceAnalogVariableList> deviceAnalogVariableListTwo = new ArrayList<>();
  123. deviceOneVoOne.setId(deviceList.get(i).getId());
  124. deviceOneVoOne.setDeviceCode(deviceList.get(i).getMonitorDeviceCode());
  125. deviceOneVoOne.setDeviceName(deviceList.get(i).getMonitorDeviceName());
  126. if (deviceAnalogVariableList.size() > 0) {
  127. for (int j = 0; j < deviceAnalogVariableList.size(); j++) {
  128. if (deviceList.get(i).getId().equals(deviceAnalogVariableList.get(j).getMonitoringEquipment())) {
  129. DeviceAnalogVariableList deviceAnalogVariableListThree = new DeviceAnalogVariableList();
  130. deviceAnalogVariableListThree.setVariableCoding(deviceAnalogVariableList.get(j).getVariableCoding()+"_"+deviceList.get(i).getId());
  131. deviceAnalogVariableListThree.setVariableName(deviceAnalogVariableList.get(j).getVariableName());
  132. deviceAnalogVariableListThree.setId(deviceAnalogVariableList.get(j).getId());
  133. deviceAnalogVariableListTwo.add(deviceAnalogVariableListThree);
  134. }
  135. }
  136. }
  137. deviceOneVoOne.setChildren(deviceAnalogVariableListTwo);
  138. deviceOneVo.add(deviceOneVoOne);
  139. }
  140. }
  141. return deviceOneVo;
  142. }
  143. @Override
  144. public List<DeviceOneVo> deviceBoxList(Integer siteId){
  145. List<DeviceOneVo> deviceOneVo = baseMapper.deviceBoxList(siteId);
  146. return deviceOneVo;
  147. }
  148. @Override
  149. public void correspondDeviceAdd(Device device) {
  150. SysUser user = SecurityUtils.getLoginUser().getUser();
  151. device.setEnable(1);
  152. device.setInstallTime(new Date());
  153. device.setCreator(user.getUserName());
  154. this.save(device);
  155. String deviceCode = device.getDeviceCode();
  156. Integer siteId = device.getSiteId();
  157. DeviceStatus deviceStatus = new DeviceStatus();
  158. deviceStatus.setDeviceStatus(1);
  159. deviceStatus.setDeviceCode(deviceCode);
  160. deviceStatus.setStatusTime(new Date());
  161. deviceStatus.setSiteId(siteId);
  162. deviceStatusService.save(deviceStatus);
  163. }
  164. @Override
  165. public void correspondDeviceUpdate(Device device) {
  166. this.updateById(device);
  167. }
  168. @Override
  169. public void correspondDeviceDel(int id) {
  170. Device device = new Device();
  171. device.setId(id);
  172. device.setEnable(0);
  173. this.updateById(device);
  174. }
  175. @Override
  176. public CommonPage<CorrespondDeviceVO> correspondDeviceList(String deviceName, int size, int current) {
  177. List<CorrespondDeviceVO> correspondDeviceListOne = baseMapper.CorrespondDeviceList(deviceName, null, null);
  178. int total = 0;
  179. if (correspondDeviceListOne.size() > 0) {
  180. total = correspondDeviceListOne.size();
  181. }
  182. int startCurrent = (current - 1) * size;
  183. List<CorrespondDeviceVO> correspondDeviceList = baseMapper.CorrespondDeviceList(deviceName, startCurrent, size);
  184. return new CommonPage<>(correspondDeviceList, total, size, current);
  185. }
  186. @Override
  187. public List<CorrespondDeviceListVO> correspondDeviceListEcho(int id) {
  188. LambdaQueryWrapper<Device> queryWrapper = Wrappers.lambdaQuery();
  189. queryWrapper.eq(Device::getEnable, 1).eq(Device::getId, id);
  190. List<Device> deviceList = this.list(queryWrapper);
  191. LambdaQueryWrapper<DeviceAnalogVariableList> queryWrapperOne = Wrappers.lambdaQuery();
  192. queryWrapperOne.eq(DeviceAnalogVariableList::getStatus, 1).eq(DeviceAnalogVariableList::getCommunicationEquipment, deviceList.get(0).getId());
  193. List<DeviceAnalogVariableList> deviceAnalogVariableList = deviceAnalogVariableListService.list(queryWrapperOne);
  194. List<CorrespondDeviceListVO> list = new ArrayList<>();
  195. list.add(CorrespondDeviceListVO.builder().deviceCode(deviceList.get(0).getDeviceCode()).deviceName(deviceList.get(0).getDeviceName())
  196. .deviceType(deviceList.get(0).getDeviceType()).deviceAddress(deviceList.get(0).getDeviceAddress())
  197. .creator(deviceList.get(0).getCreator()).enable(deviceList.get(0).getEnable()).floor(deviceList.get(0).getFloor())
  198. .id(deviceList.get(0).getId()).sim(deviceList.get(0).getSim()).siteId(deviceList.get(0).getSiteId())
  199. .installTime(deviceList.get(0).getInstallTime()).deviceAnalogVariableList(deviceAnalogVariableList).build());
  200. return list;
  201. }
  202. @Override
  203. public CommonPage<Device> videoMonitoringDeviceList(String deviceName, Integer deviceType, Integer siteId, Integer size, Integer current) {
  204. IPage<Device> page = new Page<Device>(current,size);
  205. LambdaQueryWrapper<Device> queryWrapper = Wrappers.lambdaQuery();
  206. queryWrapper.eq(Device::getSiteId, siteId).eq(Device::getEnable, 1);
  207. if (deviceType != null && deviceType != 0) {
  208. queryWrapper.eq(Device::getDeviceType, deviceType);
  209. }
  210. if (null != deviceName) {
  211. queryWrapper.like(Device::getDeviceName, deviceName);
  212. }
  213. page = this.page(page, queryWrapper);
  214. this.ToCommonPage(page);
  215. return new CommonPage<>(page.getRecords(), page.getTotal(), page.getSize(),page.getCurrent() );
  216. }
  217. @Override
  218. public void variableCloning(Integer type, String newDeviceCode, String oldDeviceCode, String deviceName) {
  219. SysUser user = SecurityUtils.getLoginUser().getUser();
  220. //查询出旧设备配置
  221. LambdaQueryWrapper<Device> queryWrapper = Wrappers.lambdaQuery();
  222. queryWrapper.eq(Device::getEnable, 1).eq(Device::getDeviceCode, oldDeviceCode);
  223. List<Device> deviceList = this.list(queryWrapper);
  224. Integer pd = 0;
  225. if (type == 1) {
  226. LambdaQueryWrapper<Device> queryWrapperFour = Wrappers.lambdaQuery();
  227. queryWrapperFour.eq(Device::getEnable, 1).eq(Device::getDeviceCode, newDeviceCode);
  228. List<Device> deviceListrFour = this.list(queryWrapperFour);
  229. if (deviceListrFour.size()>0){
  230. throw new BusinessException(newDeviceCode+"设备编号重复");
  231. }else {
  232. //设备表新增
  233. Device device = new Device();
  234. device.setEnable(1);
  235. device.setInstallTime(new Date());
  236. device.setCreator(user.getUserName());
  237. device.setDeviceCode(newDeviceCode);
  238. device.setDeviceName(deviceName);
  239. device.setFloor(deviceList.get(0).getFloor());
  240. device.setSiteId(deviceList.get(0).getSiteId());
  241. device.setDeviceAddress(deviceList.get(0).getDeviceAddress());
  242. device.setDeviceType(deviceList.get(0).getDeviceType());
  243. device.setSim(deviceList.get(0).getSim());
  244. this.save(device);
  245. Integer deviceId = device.getId();
  246. String deviceCode = device.getDeviceCode();
  247. Integer siteId = device.getSiteId();
  248. //设备状态表新增
  249. DeviceStatus deviceStatus = new DeviceStatus();
  250. deviceStatus.setDeviceStatus(1);
  251. deviceStatus.setDeviceCode(deviceCode);
  252. deviceStatus.setStatusTime(new Date());
  253. deviceStatus.setSiteId(siteId);
  254. deviceStatusService.save(deviceStatus);
  255. //变量配置查询
  256. LambdaQueryWrapper<DeviceAnalogVariableList> queryWrapperOne = Wrappers.lambdaQuery();
  257. queryWrapperOne.eq(DeviceAnalogVariableList::getStatus, 1).eq(DeviceAnalogVariableList::getCommunicationEquipment, deviceList.get(0).getId());
  258. List<DeviceAnalogVariableList> deviceAnalogVariableList = deviceAnalogVariableListService.list(queryWrapperOne);
  259. if (deviceAnalogVariableList.size() > 0) {
  260. for (int i = 0; i < deviceAnalogVariableList.size(); i++) {
  261. DeviceAnalogVariableList deviceAnalogVariableListOne = new DeviceAnalogVariableList();
  262. deviceAnalogVariableListOne.setDeviceCode(newDeviceCode);
  263. deviceAnalogVariableListOne.setVariableName(deviceAnalogVariableList.get(i).getVariableName());
  264. deviceAnalogVariableListOne.setVariableCoding(newDeviceCode+"_"+deviceAnalogVariableList.get(i).getVariableCoding().split("_")[1]);
  265. deviceAnalogVariableListOne.setMonitoringEquipment(0);
  266. deviceAnalogVariableListOne.setCommunicationEquipment(deviceId);
  267. deviceAnalogVariableListOne.setDataAddress(deviceAnalogVariableList.get(i).getDataAddress());
  268. deviceAnalogVariableListOne.setDataType(deviceAnalogVariableList.get(i).getDataType());
  269. deviceAnalogVariableListOne.setCoefficient(deviceAnalogVariableList.get(i).getCoefficient());
  270. deviceAnalogVariableListOne.setSaveCycle(deviceAnalogVariableList.get(i).getSaveCycle());
  271. deviceAnalogVariableListOne.setDataArea(deviceAnalogVariableList.get(i).getDataArea());
  272. deviceAnalogVariableListOne.setCreator(user.getUserName());
  273. deviceAnalogVariableListOne.setCreateTime(new Date());
  274. deviceAnalogVariableListOne.setStatus(1);
  275. deviceAnalogVariableListService.save(deviceAnalogVariableListOne);
  276. }
  277. }
  278. }
  279. } else {
  280. LambdaQueryWrapper<DeviceAnalogVariableList> queryWrapperOneA = Wrappers.lambdaQuery();
  281. queryWrapperOneA.eq(DeviceAnalogVariableList::getStatus, 1).eq(DeviceAnalogVariableList::getCommunicationEquipment, deviceList.get(0).getId());
  282. List<DeviceAnalogVariableList> deviceAnalogVariableListOne = deviceAnalogVariableListService.list(queryWrapperOneA);
  283. //克隆设备查询是否存在
  284. LambdaQueryWrapper<Device> queryWrapperTwo = Wrappers.lambdaQuery();
  285. queryWrapperTwo.eq(Device::getEnable, 1).eq(Device::getDeviceCode, newDeviceCode);
  286. List<Device> deviceListTwo = this.list(queryWrapperTwo);
  287. LambdaQueryWrapper<DeviceAnalogVariableList> queryWrapperTwoA = Wrappers.lambdaQuery();
  288. queryWrapperTwoA.eq(DeviceAnalogVariableList::getStatus, 1).eq(DeviceAnalogVariableList::getCommunicationEquipment, deviceListTwo.get(0).getId());
  289. List<DeviceAnalogVariableList> deviceAnalogVariableListTwo = deviceAnalogVariableListService.list(queryWrapperTwoA);
  290. if (deviceAnalogVariableListTwo.size() > 0) {
  291. for (int i = 0; i < deviceAnalogVariableListTwo.size(); i++) {
  292. DeviceAnalogVariableList deviceAnalogVariableList = new DeviceAnalogVariableList();
  293. deviceAnalogVariableList.setStatus(0);
  294. deviceAnalogVariableList.setId(deviceAnalogVariableListTwo.get(i).getId());
  295. deviceAnalogVariableListService.updateById(deviceAnalogVariableList);
  296. }
  297. }
  298. if (deviceAnalogVariableListOne.size() > 0) {
  299. for (int i = 0; i < deviceAnalogVariableListOne.size(); i++) {
  300. DeviceAnalogVariableList deviceAnalogVariableList = new DeviceAnalogVariableList();
  301. deviceAnalogVariableList.setDeviceCode(newDeviceCode);
  302. deviceAnalogVariableList.setVariableName(deviceAnalogVariableListOne.get(i).getVariableName());
  303. deviceAnalogVariableList.setVariableCoding(newDeviceCode+"_"+deviceAnalogVariableListOne.get(i).getVariableCoding().split("_")[1]);
  304. deviceAnalogVariableList.setMonitoringEquipment(0);
  305. deviceAnalogVariableList.setCommunicationEquipment(deviceListTwo.get(0).getId());
  306. deviceAnalogVariableList.setDataType(deviceAnalogVariableListOne.get(i).getDataType());
  307. deviceAnalogVariableList.setDataAddress(deviceAnalogVariableListOne.get(i).getDataAddress());
  308. deviceAnalogVariableList.setSaveCycle(deviceAnalogVariableListOne.get(i).getSaveCycle());
  309. deviceAnalogVariableList.setCoefficient(deviceAnalogVariableListOne.get(i).getCoefficient());
  310. deviceAnalogVariableList.setDataArea(deviceAnalogVariableListOne.get(i).getDataArea());
  311. deviceAnalogVariableList.setCreator(user.getUserName());
  312. deviceAnalogVariableList.setCreateTime(new Date());
  313. deviceAnalogVariableList.setStatus(1);
  314. deviceAnalogVariableListService.save(deviceAnalogVariableList);
  315. }
  316. }
  317. }
  318. }
  319. @Override
  320. public List<Device> deviceListOne(Integer siteId, Integer deviceType) {
  321. LambdaQueryWrapper<Device> queryWrapperTwo = Wrappers.lambdaQuery();
  322. queryWrapperTwo.eq(Device::getEnable, 1).eq(Device::getSiteId, siteId).eq(Device::getDeviceType, deviceType);
  323. List<Device> deviceListTwo = this.list(queryWrapperTwo);
  324. return deviceListTwo;
  325. }
  326. @Override
  327. public String deviceExport(String deviceName, Integer deviceType, Integer siteId) {
  328. SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  329. Workbook workbook = null;
  330. File file = null;
  331. try {
  332. ExportParams params = new ExportParams(null, "通信设备列表");
  333. workbook = ExcelExportUtil.exportBigExcel(params, DeviceExportVO.class,
  334. (o, i) -> {
  335. List<DeviceOne> Active = new ArrayList<DeviceOne>();
  336. Page<Device> page = new Page<>(i, 30);
  337. LambdaQueryWrapper<Device> queryWrapper = Wrappers.lambdaQuery();
  338. queryWrapper.eq(Device::getEnable, 1);
  339. if (siteId!=0){
  340. queryWrapper.eq(Device::getSiteId, siteId);
  341. }
  342. if (deviceType != 0) {
  343. queryWrapper.eq(Device::getDeviceType, deviceType);
  344. }
  345. if (null != deviceName) {
  346. queryWrapper.like(Device::getDeviceName, deviceName);
  347. }
  348. page = this.page(page, queryWrapper);
  349. for (int j = 0; j < page.getRecords().size(); j++){
  350. DeviceOne deviceOne = new DeviceOne();
  351. deviceOne.setDeviceCode(page.getRecords().get(j).getDeviceCode());
  352. deviceOne.setDeviceName(page.getRecords().get(j).getDeviceName());
  353. deviceOne.setFloor(page.getRecords().get(j).getFloor());
  354. deviceOne.setSiteId(page.getRecords().get(j).getSiteId());
  355. deviceOne.setDeviceAddress(page.getRecords().get(j).getDeviceAddress());
  356. deviceOne.setDeviceType(page.getRecords().get(j).getDeviceType());
  357. deviceOne.setInstallTime(longSdf.format(page.getRecords().get(j).getInstallTime()));
  358. deviceOne.setCreator(page.getRecords().get(j).getCreator());
  359. deviceOne.setEnable(page.getRecords().get(j).getEnable());
  360. deviceOne.setSim(page.getRecords().get(j).getSim());
  361. deviceOne.setDeviceStatus(page.getRecords().get(j).getDeviceStatus());
  362. deviceOne.setInstalledCapacity(page.getRecords().get(j).getInstalledCapacity());
  363. Active.add(deviceOne);
  364. }
  365. // this.ToCommonPage(page);
  366. return new ArrayList<>(BeanMapperUtils.mapList(Active, DeviceOne.class, DeviceExportVO.class));
  367. }, null);
  368. if (null != workbook) {
  369. file = FileUtils.getFile(GlobalUtils.getTempBaseDir(), String.format("%s-%s.xlsx", "通信设备列表", System.currentTimeMillis() + ""));
  370. FileUtils.createFile(file.getAbsolutePath());
  371. FileOutputStream allListingFileOutputStream = new FileOutputStream(file);
  372. workbook.write(allListingFileOutputStream);
  373. } else {
  374. throw new BusinessException("表格数据为空");
  375. }
  376. } catch (Exception e) {
  377. log.error("导出文件失败", e);
  378. throw new BusinessException("导出文件失败");
  379. } finally {
  380. if (workbook != null) {
  381. try {
  382. workbook.close();
  383. } catch (IOException e) {
  384. log.error("===export spec=== 关闭workbook失败", e);
  385. }
  386. }
  387. }
  388. return file.getName();
  389. }
  390. @Override
  391. public String correspondDeviceExport(String deviceName) {
  392. SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  393. Workbook workbook = null;
  394. File file = null;
  395. try {
  396. ExportParams params = new ExportParams(null, "通信设备列表");
  397. workbook = ExcelExportUtil.exportBigExcel(params, CorrespondDeviceExportVO.class,
  398. (o, i) -> {
  399. List<CorrespondDeviceTwoVO> Active = new ArrayList<CorrespondDeviceTwoVO>();
  400. int startCurrent = (i - 1) * 30;
  401. List<CorrespondDeviceVO> correspondDeviceList = baseMapper.CorrespondDeviceList(deviceName, startCurrent, 30);
  402. for (int j = 0; j <correspondDeviceList.size(); j++){
  403. CorrespondDeviceTwoVO correspondDeviceTwoVO = new CorrespondDeviceTwoVO();
  404. correspondDeviceTwoVO.setDeviceCode(correspondDeviceList.get(j).getDeviceCode());
  405. correspondDeviceTwoVO.setDeviceName(correspondDeviceList.get(j).getDeviceName());
  406. correspondDeviceTwoVO.setDeviceAddress(correspondDeviceList.get(j).getDeviceAddress());
  407. correspondDeviceTwoVO.setInstallTime(longSdf.format(correspondDeviceList.get(j).getInstallTime()));
  408. correspondDeviceTwoVO.setDeviceStatus(correspondDeviceList.get(j).getDeviceStatus());
  409. correspondDeviceTwoVO.setStatusTime(longSdf.format(correspondDeviceList.get(j).getStatusTime()));
  410. correspondDeviceTwoVO.setSiteId(correspondDeviceList.get(j).getSiteId());
  411. correspondDeviceTwoVO.setSiteName(correspondDeviceList.get(j).getSiteName());
  412. correspondDeviceTwoVO.setOfflineDuration(correspondDeviceList.get(j).getOfflineDuration());
  413. correspondDeviceTwoVO.setOnlineDuration(correspondDeviceList.get(j).getOnlineDuration());
  414. correspondDeviceTwoVO.setSim(correspondDeviceList.get(j).getSim());
  415. correspondDeviceTwoVO.setFloor(correspondDeviceList.get(j).getFloor());
  416. correspondDeviceTwoVO.setDeviceType(correspondDeviceList.get(j).getDeviceType());
  417. Active.add(correspondDeviceTwoVO);
  418. }
  419. return new ArrayList<>(BeanMapperUtils.mapList(Active, CorrespondDeviceTwoVO.class, CorrespondDeviceExportVO.class));
  420. }, null);
  421. if (null != workbook) {
  422. file = FileUtils.getFile(GlobalUtils.getTempBaseDir(), String.format("%s-%s.xlsx", "通信设备列表", System.currentTimeMillis() + ""));
  423. FileUtils.createFile(file.getAbsolutePath());
  424. FileOutputStream allListingFileOutputStream = new FileOutputStream(file);
  425. workbook.write(allListingFileOutputStream);
  426. } else {
  427. throw new BusinessException("表格数据为空");
  428. }
  429. } catch (Exception e) {
  430. log.error("导出文件失败", e);
  431. throw new BusinessException("导出文件失败");
  432. } finally {
  433. if (workbook != null) {
  434. try {
  435. workbook.close();
  436. } catch (IOException e) {
  437. log.error("===export spec=== 关闭workbook失败", e);
  438. }
  439. }
  440. }
  441. return file.getName();
  442. }
  443. @Transactional
  444. @Override
  445. public void deviceImport(MultipartFile multipartFile) {
  446. SysUser user = SecurityUtils.getLoginUser().getUser();
  447. ImportParams params = new ImportParams();
  448. params.setHeadRows(1);
  449. String err="文件导入失败";
  450. try {
  451. List<DeviceImportVo> deviceImportVos = ExcelImportUtil.importExcel(multipartFile.getInputStream(),
  452. DeviceImportVo.class, params);
  453. if (CollectionUtils.isNotEmpty(deviceImportVos)){
  454. int rot=0;
  455. for (DeviceImportVo deviceImportVo:deviceImportVos) {
  456. DeviceStatus deviceStatus = new DeviceStatus();
  457. Device device = BeanMapperUtils.map(deviceImportVo, Device.class);
  458. device.setEnable(1);
  459. device.setCreator(user.getUserName());
  460. device.setInstallTime(new Date());
  461. try{
  462. this.save(device);
  463. String deviceCode = device.getDeviceCode();
  464. Integer siteId = device.getSiteId();
  465. deviceStatus.setDeviceStatus(1);
  466. deviceStatus.setDeviceCode(deviceCode);
  467. deviceStatus.setStatusTime(new Date());
  468. deviceStatus.setSiteId(siteId);
  469. deviceStatusService.save(deviceStatus);
  470. }catch (Exception e){
  471. err="文件导入失败,第"+rot+"行数据导入失败";
  472. throw new BusinessException("文件导入失败,第"+rot+"行数据导入失败");
  473. }
  474. rot++;
  475. }
  476. }else {
  477. err="文件不能为空";
  478. throw new BusinessException("文件不能为空");
  479. }
  480. }catch (Exception e){
  481. TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
  482. throw new BusinessException(err);
  483. }
  484. }
  485. }