package com.bizmatics.service.impl; import cn.afterturn.easypoi.excel.ExcelExportUtil; import cn.afterturn.easypoi.excel.ExcelImportUtil; import cn.afterturn.easypoi.excel.entity.ExportParams; import cn.afterturn.easypoi.excel.entity.ImportParams; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.core.toolkit.Wrappers; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.bizmatics.common.core.bean.CommonPage; import com.bizmatics.common.core.exception.BusinessException; import com.bizmatics.common.core.util.BeanMapperUtils; import com.bizmatics.common.core.util.FileUtils; import com.bizmatics.common.mvc.base.AbstractCrudService; import com.bizmatics.common.spring.util.GlobalUtils; import com.bizmatics.model.*; import com.bizmatics.model.system.SysUser; import com.bizmatics.model.vo.*; import com.bizmatics.persistence.mapper.DeviceMapper; import com.bizmatics.service.DeviceAnalogVariableListService; import com.bizmatics.service.DeviceAttributeService; import com.bizmatics.service.DeviceService; import com.bizmatics.service.DeviceStatusService; import com.bizmatics.service.enums.DeviceStatusCode; import com.bizmatics.service.enums.DeviceType; import com.bizmatics.service.util.SecurityUtils; import com.bizmatics.service.vo.CorrespondDeviceExportVO; import com.bizmatics.service.vo.DeviceCountVO; import com.bizmatics.service.vo.DeviceExportVO; import com.bizmatics.service.vo.DeviceImportVo; import org.apache.poi.ss.usermodel.Workbook; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.interceptor.TransactionAspectSupport; import org.springframework.web.multipart.MultipartFile; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.text.SimpleDateFormat; import java.util.ArrayList; import java.util.Date; import java.util.List; import java.util.Optional; /** * 设备 * * @author ya * @since 2021-07-07 */ @Service public class DeviceServiceImpl extends AbstractCrudService implements DeviceService { @Autowired private DeviceAnalogVariableListService deviceAnalogVariableListService; @Autowired private DeviceStatusService deviceStatusService; @Autowired private DeviceAttributeService deviceAttributeService; @Override public DeviceCountVO selectDeviceCount(Date startTime, Date endTime) { Integer userId = SecurityUtils.getLoginUser().getUser().getUserId().intValue(); DeviceCountVO deviceCountVo = new DeviceCountVO(); deviceCountVo.setNormalCount(baseMapper .selectCount(userId, null, DeviceStatusCode.NORMAL.getValue(), startTime, endTime, null)); deviceCountVo.setOffLineCount(baseMapper .selectCount(userId, null, DeviceStatusCode.OFFLINE.getValue(), startTime, endTime, null)); deviceCountVo.setDeviceCount(baseMapper .selectCount(userId, null, DeviceStatusCode.DEVICE.getValue(), startTime, endTime, null)); deviceCountVo.setFaultCount(baseMapper .selectCount(userId, null, DeviceStatusCode.FAULT.getValue(), startTime, endTime, null)); deviceCountVo.setCount(baseMapper .selectCount(userId, null, null, startTime, endTime, null)); return deviceCountVo; } @Override public DeviceCountVO selectDeviceCountByType(Integer site) { DeviceCountVO deviceCountVo = new DeviceCountVO(); Integer userId = SecurityUtils.getLoginUser().getUser().getUserId().intValue(); int oneEightThreeEp = baseMapper.selectCount(userId, site, null, null, null, DeviceType.ONE_EIGHT_THREE_EP.getValue()); int oneSevenOneEp = baseMapper.selectCount(userId, site, null, null, null, DeviceType.ONE_SEVEN_ONE_EP.getValue()); int oneSevenThreeEp = baseMapper.selectCount(userId, site, null, null, null, DeviceType.ONE_SEVEN_THREE_EP.getValue()); int video = baseMapper.selectCount(userId, site, null, null, null, DeviceType.VODEO_MONITROING.getValue()); deviceCountVo.setEpCount(oneEightThreeEp + oneSevenOneEp + oneSevenThreeEp); deviceCountVo.setVideoCount(video); return deviceCountVo; } @Override public List list(Integer userId, Integer siteId, Integer deviceStatus, Date startTime, Date endTime, String type) { return baseMapper.list(userId, siteId, deviceStatus, startTime, endTime, type); } /** * 汇总 * * @param list * @param deviceStatus * @param type * @return */ public Long getCount(List list, Integer deviceStatus, String type) { return list.stream() .filter(device -> Optional.ofNullable(deviceStatus).map(ds -> ds.equals(device.getDeviceStatus())).orElse(true)) .filter(device -> Optional.ofNullable(type).map(ds -> ds.equals(device.getDeviceType())).orElse(true)) .count(); } @Override public List deviceList(String siteId) { List DeviceList = null; DeviceList = baseMapper.DeviceList(Integer.parseInt(siteId)); return DeviceList; } @Override public List dataManagementDeviceList(int siteId, int deviceType) { List deviceOneVo = new ArrayList<>(); LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery(); queryWrapper.eq(DeviceAttribute::getStatus, 1).eq(DeviceAttribute::getSiteId, siteId); List deviceList = deviceAttributeService.list(queryWrapper); List deviceAnalogVariableList = baseMapper.deviceAnalogVariableList(siteId); if (deviceList.size() > 0) { for (int i = 0; i < deviceList.size(); i++) { DeviceOneVo deviceOneVoOne = new DeviceOneVo(); List deviceAnalogVariableListTwo = new ArrayList<>(); deviceOneVoOne.setId(deviceList.get(i).getId()); deviceOneVoOne.setDeviceCode(deviceList.get(i).getMonitorDeviceCode()); deviceOneVoOne.setDeviceName(deviceList.get(i).getMonitorDeviceName()); if (deviceAnalogVariableList.size() > 0) { for (int j = 0; j < deviceAnalogVariableList.size(); j++) { if (deviceList.get(i).getId().equals(deviceAnalogVariableList.get(j).getMonitoringEquipment())) { DeviceAnalogVariableList deviceAnalogVariableListThree = new DeviceAnalogVariableList(); deviceAnalogVariableListThree.setVariableCoding(deviceAnalogVariableList.get(j).getVariableCoding()+"_"+deviceList.get(i).getId()); deviceAnalogVariableListThree.setVariableName(deviceAnalogVariableList.get(j).getVariableName()); deviceAnalogVariableListThree.setId(deviceAnalogVariableList.get(j).getId()); deviceAnalogVariableListTwo.add(deviceAnalogVariableListThree); } } } deviceOneVoOne.setChildren(deviceAnalogVariableListTwo); deviceOneVo.add(deviceOneVoOne); } } return deviceOneVo; } @Override public List deviceBoxList(Integer siteId){ List deviceOneVo = baseMapper.deviceBoxList(siteId); return deviceOneVo; } @Override public void correspondDeviceAdd(Device device) { SysUser user = SecurityUtils.getLoginUser().getUser(); device.setEnable(1); device.setInstallTime(new Date()); device.setCreator(user.getUserName()); this.save(device); String deviceCode = device.getDeviceCode(); Integer siteId = device.getSiteId(); DeviceStatus deviceStatus = new DeviceStatus(); deviceStatus.setDeviceStatus(1); deviceStatus.setDeviceCode(deviceCode); deviceStatus.setStatusTime(new Date()); deviceStatus.setSiteId(siteId); deviceStatusService.save(deviceStatus); } @Override public void correspondDeviceUpdate(Device device) { this.updateById(device); } @Override public void correspondDeviceDel(int id) { Device device = new Device(); device.setId(id); device.setEnable(0); this.updateById(device); } @Override public CommonPage correspondDeviceList(String deviceName, int size, int current) { List correspondDeviceListOne = baseMapper.CorrespondDeviceList(deviceName, null, null); int total = 0; if (correspondDeviceListOne.size() > 0) { total = correspondDeviceListOne.size(); } int startCurrent = (current - 1) * size; List correspondDeviceList = baseMapper.CorrespondDeviceList(deviceName, startCurrent, size); return new CommonPage<>(correspondDeviceList, total, size, current); } @Override public List correspondDeviceListEcho(int id) { LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery(); queryWrapper.eq(Device::getEnable, 1).eq(Device::getId, id); List deviceList = this.list(queryWrapper); LambdaQueryWrapper queryWrapperOne = Wrappers.lambdaQuery(); queryWrapperOne.eq(DeviceAnalogVariableList::getStatus, 1).eq(DeviceAnalogVariableList::getCommunicationEquipment, deviceList.get(0).getId()); List deviceAnalogVariableList = deviceAnalogVariableListService.list(queryWrapperOne); List list = new ArrayList<>(); list.add(CorrespondDeviceListVO.builder().deviceCode(deviceList.get(0).getDeviceCode()).deviceName(deviceList.get(0).getDeviceName()) .deviceType(deviceList.get(0).getDeviceType()).deviceAddress(deviceList.get(0).getDeviceAddress()) .creator(deviceList.get(0).getCreator()).enable(deviceList.get(0).getEnable()).floor(deviceList.get(0).getFloor()) .id(deviceList.get(0).getId()).sim(deviceList.get(0).getSim()).siteId(deviceList.get(0).getSiteId()) .installTime(deviceList.get(0).getInstallTime()).deviceAnalogVariableList(deviceAnalogVariableList).build()); return list; } @Override public CommonPage videoMonitoringDeviceList(String deviceName, Integer deviceType, Integer siteId, Integer size, Integer current) { IPage page = new Page(current,size); LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery(); queryWrapper.eq(Device::getSiteId, siteId).eq(Device::getEnable, 1); if (deviceType != null && deviceType != 0) { queryWrapper.eq(Device::getDeviceType, deviceType); } if (null != deviceName) { queryWrapper.like(Device::getDeviceName, deviceName); } page = this.page(page, queryWrapper); this.ToCommonPage(page); return new CommonPage<>(page.getRecords(), page.getTotal(), page.getSize(),page.getCurrent() ); } @Override public void variableCloning(Integer type, String newDeviceCode, String oldDeviceCode, String deviceName) { SysUser user = SecurityUtils.getLoginUser().getUser(); //查询出旧设备配置 LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery(); queryWrapper.eq(Device::getEnable, 1).eq(Device::getDeviceCode, oldDeviceCode); List deviceList = this.list(queryWrapper); Integer pd = 0; if (type == 1) { LambdaQueryWrapper queryWrapperFour = Wrappers.lambdaQuery(); queryWrapperFour.eq(Device::getEnable, 1).eq(Device::getDeviceCode, newDeviceCode); List deviceListrFour = this.list(queryWrapperFour); if (deviceListrFour.size()>0){ throw new BusinessException(newDeviceCode+"设备编号重复"); }else { //设备表新增 Device device = new Device(); device.setEnable(1); device.setInstallTime(new Date()); device.setCreator(user.getUserName()); device.setDeviceCode(newDeviceCode); device.setDeviceName(deviceName); device.setFloor(deviceList.get(0).getFloor()); device.setSiteId(deviceList.get(0).getSiteId()); device.setDeviceAddress(deviceList.get(0).getDeviceAddress()); device.setDeviceType(deviceList.get(0).getDeviceType()); device.setSim(deviceList.get(0).getSim()); this.save(device); Integer deviceId = device.getId(); String deviceCode = device.getDeviceCode(); Integer siteId = device.getSiteId(); //设备状态表新增 DeviceStatus deviceStatus = new DeviceStatus(); deviceStatus.setDeviceStatus(1); deviceStatus.setDeviceCode(deviceCode); deviceStatus.setStatusTime(new Date()); deviceStatus.setSiteId(siteId); deviceStatusService.save(deviceStatus); //变量配置查询 LambdaQueryWrapper queryWrapperOne = Wrappers.lambdaQuery(); queryWrapperOne.eq(DeviceAnalogVariableList::getStatus, 1).eq(DeviceAnalogVariableList::getCommunicationEquipment, deviceList.get(0).getId()); List deviceAnalogVariableList = deviceAnalogVariableListService.list(queryWrapperOne); if (deviceAnalogVariableList.size() > 0) { for (int i = 0; i < deviceAnalogVariableList.size(); i++) { DeviceAnalogVariableList deviceAnalogVariableListOne = new DeviceAnalogVariableList(); deviceAnalogVariableListOne.setDeviceCode(newDeviceCode); deviceAnalogVariableListOne.setVariableName(deviceAnalogVariableList.get(i).getVariableName()); deviceAnalogVariableListOne.setVariableCoding(newDeviceCode+"_"+deviceAnalogVariableList.get(i).getVariableCoding().split("_")[1]); deviceAnalogVariableListOne.setMonitoringEquipment(0); deviceAnalogVariableListOne.setCommunicationEquipment(deviceId); deviceAnalogVariableListOne.setDataAddress(deviceAnalogVariableList.get(i).getDataAddress()); deviceAnalogVariableListOne.setDataType(deviceAnalogVariableList.get(i).getDataType()); deviceAnalogVariableListOne.setCoefficient(deviceAnalogVariableList.get(i).getCoefficient()); deviceAnalogVariableListOne.setSaveCycle(deviceAnalogVariableList.get(i).getSaveCycle()); deviceAnalogVariableListOne.setDataArea(deviceAnalogVariableList.get(i).getDataArea()); deviceAnalogVariableListOne.setCreator(user.getUserName()); deviceAnalogVariableListOne.setCreateTime(new Date()); deviceAnalogVariableListOne.setStatus(1); deviceAnalogVariableListService.save(deviceAnalogVariableListOne); } } } } else { LambdaQueryWrapper queryWrapperOneA = Wrappers.lambdaQuery(); queryWrapperOneA.eq(DeviceAnalogVariableList::getStatus, 1).eq(DeviceAnalogVariableList::getCommunicationEquipment, deviceList.get(0).getId()); List deviceAnalogVariableListOne = deviceAnalogVariableListService.list(queryWrapperOneA); //克隆设备查询是否存在 LambdaQueryWrapper queryWrapperTwo = Wrappers.lambdaQuery(); queryWrapperTwo.eq(Device::getEnable, 1).eq(Device::getDeviceCode, newDeviceCode); List deviceListTwo = this.list(queryWrapperTwo); LambdaQueryWrapper queryWrapperTwoA = Wrappers.lambdaQuery(); queryWrapperTwoA.eq(DeviceAnalogVariableList::getStatus, 1).eq(DeviceAnalogVariableList::getCommunicationEquipment, deviceListTwo.get(0).getId()); List deviceAnalogVariableListTwo = deviceAnalogVariableListService.list(queryWrapperTwoA); if (deviceAnalogVariableListTwo.size() > 0) { for (int i = 0; i < deviceAnalogVariableListTwo.size(); i++) { DeviceAnalogVariableList deviceAnalogVariableList = new DeviceAnalogVariableList(); deviceAnalogVariableList.setStatus(0); deviceAnalogVariableList.setId(deviceAnalogVariableListTwo.get(i).getId()); deviceAnalogVariableListService.updateById(deviceAnalogVariableList); } } if (deviceAnalogVariableListOne.size() > 0) { for (int i = 0; i < deviceAnalogVariableListOne.size(); i++) { DeviceAnalogVariableList deviceAnalogVariableList = new DeviceAnalogVariableList(); deviceAnalogVariableList.setDeviceCode(newDeviceCode); deviceAnalogVariableList.setVariableName(deviceAnalogVariableListOne.get(i).getVariableName()); deviceAnalogVariableList.setVariableCoding(newDeviceCode+"_"+deviceAnalogVariableListOne.get(i).getVariableCoding().split("_")[1]); deviceAnalogVariableList.setMonitoringEquipment(0); deviceAnalogVariableList.setCommunicationEquipment(deviceListTwo.get(0).getId()); deviceAnalogVariableList.setDataType(deviceAnalogVariableListOne.get(i).getDataType()); deviceAnalogVariableList.setDataAddress(deviceAnalogVariableListOne.get(i).getDataAddress()); deviceAnalogVariableList.setSaveCycle(deviceAnalogVariableListOne.get(i).getSaveCycle()); deviceAnalogVariableList.setCoefficient(deviceAnalogVariableListOne.get(i).getCoefficient()); deviceAnalogVariableList.setDataArea(deviceAnalogVariableListOne.get(i).getDataArea()); deviceAnalogVariableList.setCreator(user.getUserName()); deviceAnalogVariableList.setCreateTime(new Date()); deviceAnalogVariableList.setStatus(1); deviceAnalogVariableListService.save(deviceAnalogVariableList); } } } } @Override public List deviceListOne(Integer siteId, Integer deviceType) { LambdaQueryWrapper queryWrapperTwo = Wrappers.lambdaQuery(); queryWrapperTwo.eq(Device::getEnable, 1).eq(Device::getSiteId, siteId).eq(Device::getDeviceType, deviceType); List deviceListTwo = this.list(queryWrapperTwo); return deviceListTwo; } @Override public String deviceExport(String deviceName, Integer deviceType, Integer siteId) { SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Workbook workbook = null; File file = null; try { ExportParams params = new ExportParams(null, "通信设备列表"); workbook = ExcelExportUtil.exportBigExcel(params, DeviceExportVO.class, (o, i) -> { List Active = new ArrayList(); Page page = new Page<>(i, 30); LambdaQueryWrapper queryWrapper = Wrappers.lambdaQuery(); queryWrapper.eq(Device::getEnable, 1); if (siteId!=0){ queryWrapper.eq(Device::getSiteId, siteId); } if (deviceType != 0) { queryWrapper.eq(Device::getDeviceType, deviceType); } if (null != deviceName) { queryWrapper.like(Device::getDeviceName, deviceName); } page = this.page(page, queryWrapper); for (int j = 0; j < page.getRecords().size(); j++){ DeviceOne deviceOne = new DeviceOne(); deviceOne.setDeviceCode(page.getRecords().get(j).getDeviceCode()); deviceOne.setDeviceName(page.getRecords().get(j).getDeviceName()); deviceOne.setFloor(page.getRecords().get(j).getFloor()); deviceOne.setSiteId(page.getRecords().get(j).getSiteId()); deviceOne.setDeviceAddress(page.getRecords().get(j).getDeviceAddress()); deviceOne.setDeviceType(page.getRecords().get(j).getDeviceType()); deviceOne.setInstallTime(longSdf.format(page.getRecords().get(j).getInstallTime())); deviceOne.setCreator(page.getRecords().get(j).getCreator()); deviceOne.setEnable(page.getRecords().get(j).getEnable()); deviceOne.setSim(page.getRecords().get(j).getSim()); deviceOne.setDeviceStatus(page.getRecords().get(j).getDeviceStatus()); deviceOne.setInstalledCapacity(page.getRecords().get(j).getInstalledCapacity()); Active.add(deviceOne); } // this.ToCommonPage(page); return new ArrayList<>(BeanMapperUtils.mapList(Active, DeviceOne.class, DeviceExportVO.class)); }, null); if (null != workbook) { file = FileUtils.getFile(GlobalUtils.getTempBaseDir(), String.format("%s-%s.xlsx", "通信设备列表", System.currentTimeMillis() + "")); FileUtils.createFile(file.getAbsolutePath()); FileOutputStream allListingFileOutputStream = new FileOutputStream(file); workbook.write(allListingFileOutputStream); } else { throw new BusinessException("表格数据为空"); } } catch (Exception e) { log.error("导出文件失败", e); throw new BusinessException("导出文件失败"); } finally { if (workbook != null) { try { workbook.close(); } catch (IOException e) { log.error("===export spec=== 关闭workbook失败", e); } } } return file.getName(); } @Override public String correspondDeviceExport(String deviceName) { SimpleDateFormat longSdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); Workbook workbook = null; File file = null; try { ExportParams params = new ExportParams(null, "通信设备列表"); workbook = ExcelExportUtil.exportBigExcel(params, CorrespondDeviceExportVO.class, (o, i) -> { List Active = new ArrayList(); int startCurrent = (i - 1) * 30; List correspondDeviceList = baseMapper.CorrespondDeviceList(deviceName, startCurrent, 30); for (int j = 0; j (BeanMapperUtils.mapList(Active, CorrespondDeviceTwoVO.class, CorrespondDeviceExportVO.class)); }, null); if (null != workbook) { file = FileUtils.getFile(GlobalUtils.getTempBaseDir(), String.format("%s-%s.xlsx", "通信设备列表", System.currentTimeMillis() + "")); FileUtils.createFile(file.getAbsolutePath()); FileOutputStream allListingFileOutputStream = new FileOutputStream(file); workbook.write(allListingFileOutputStream); } else { throw new BusinessException("表格数据为空"); } } catch (Exception e) { log.error("导出文件失败", e); throw new BusinessException("导出文件失败"); } finally { if (workbook != null) { try { workbook.close(); } catch (IOException e) { log.error("===export spec=== 关闭workbook失败", e); } } } return file.getName(); } @Transactional @Override public void deviceImport(MultipartFile multipartFile) { SysUser user = SecurityUtils.getLoginUser().getUser(); ImportParams params = new ImportParams(); params.setHeadRows(1); String err="文件导入失败"; try { List deviceImportVos = ExcelImportUtil.importExcel(multipartFile.getInputStream(), DeviceImportVo.class, params); if (CollectionUtils.isNotEmpty(deviceImportVos)){ int rot=0; for (DeviceImportVo deviceImportVo:deviceImportVos) { DeviceStatus deviceStatus = new DeviceStatus(); Device device = BeanMapperUtils.map(deviceImportVo, Device.class); device.setEnable(1); device.setCreator(user.getUserName()); device.setInstallTime(new Date()); try{ this.save(device); String deviceCode = device.getDeviceCode(); Integer siteId = device.getSiteId(); deviceStatus.setDeviceStatus(1); deviceStatus.setDeviceCode(deviceCode); deviceStatus.setStatusTime(new Date()); deviceStatus.setSiteId(siteId); deviceStatusService.save(deviceStatus); }catch (Exception e){ err="文件导入失败,第"+rot+"行数据导入失败"; throw new BusinessException("文件导入失败,第"+rot+"行数据导入失败"); } rot++; } }else { err="文件不能为空"; throw new BusinessException("文件不能为空"); } }catch (Exception e){ TransactionAspectSupport.currentTransactionStatus().setRollbackOnly(); throw new BusinessException(err); } } }