123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379 |
- package com.bizmatics.service.impl;
- import cn.afterturn.easypoi.excel.ExcelExportUtil;
- import cn.afterturn.easypoi.excel.entity.ExportParams;
- import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
- import com.baomidou.mybatisplus.core.metadata.IPage;
- 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.Device;
- import com.bizmatics.model.DeviceAnalogVariableList;
- import com.bizmatics.model.DeviceList;
- import com.bizmatics.model.DeviceStatus;
- import com.bizmatics.model.system.SysUser;
- import com.bizmatics.model.vo.CorrespondDeviceListVO;
- import com.bizmatics.model.vo.CorrespondDeviceVO;
- import com.bizmatics.model.vo.DeviceAnalogVariableListOneVo;
- import com.bizmatics.model.vo.DeviceOneVo;
- import com.bizmatics.persistence.mapper.DeviceMapper;
- import com.bizmatics.service.DeviceAnalogVariableListService;
- 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.DeviceAnalogVariableListExportVO;
- import com.bizmatics.service.vo.DeviceCountVO;
- import com.bizmatics.service.vo.DeviceExportVO;
- import org.apache.poi.ss.usermodel.Workbook;
- import org.springframework.beans.factory.annotation.Autowired;
- import org.springframework.stereotype.Service;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- 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<DeviceMapper, Device> implements DeviceService {
- @Autowired
- private DeviceAnalogVariableListService deviceAnalogVariableListService;
- @Autowired
- private DeviceStatusService deviceStatusService;
- @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<Device> 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<Device> 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> deviceList(String siteId) {
- List<DeviceList> DeviceList = null;
- DeviceList = baseMapper.DeviceList(Integer.parseInt(siteId));
- return DeviceList;
- }
- @Override
- public List<DeviceOneVo> dataManagementDeviceList(int siteId, int deviceType) {
- List<DeviceOneVo> deviceOneVo = new ArrayList<>();
- LambdaQueryWrapper<Device> queryWrapper = Wrappers.lambdaQuery();
- queryWrapper.eq(Device::getEnable, 1).eq(Device::getSiteId, siteId);
- List<Device> deviceList = this.list(queryWrapper);
- List<DeviceAnalogVariableList> deviceAnalogVariableList = baseMapper.deviceAnalogVariableList(siteId);
- if (deviceList.size() > 0) {
- for (int i = 0; i < deviceList.size(); i++) {
- DeviceOneVo deviceOneVoOne = new DeviceOneVo();
- List<DeviceAnalogVariableList> deviceAnalogVariableListTwo = new ArrayList<>();
- deviceOneVoOne.setId(deviceList.get(i).getId());
- deviceOneVoOne.setDeviceCode(deviceList.get(i).getDeviceCode());
- deviceOneVoOne.setDeviceName(deviceList.get(i).getDeviceName());
- if (deviceAnalogVariableList.size() > 0) {
- for (int j = 0; j < deviceAnalogVariableList.size(); j++) {
- if (deviceList.get(i).getId().equals(deviceAnalogVariableList.get(j).getCommunicationEquipment())) {
- deviceAnalogVariableListTwo.add(deviceAnalogVariableList.get(j));
- }
- }
- }
- deviceOneVoOne.setChildren(deviceAnalogVariableListTwo);
- deviceOneVo.add(deviceOneVoOne);
- }
- }
- 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<CorrespondDeviceVO> correspondDeviceList(String deviceName, int size, int current) {
- List<CorrespondDeviceVO> correspondDeviceListOne = baseMapper.CorrespondDeviceList(deviceName, null, null);
- int total = 0;
- if (correspondDeviceListOne.size() > 0) {
- total = correspondDeviceListOne.size();
- }
- int startCurrent = (current - 1) * size;
- List<CorrespondDeviceVO> correspondDeviceList = baseMapper.CorrespondDeviceList(deviceName, startCurrent, size);
- return new CommonPage<>(correspondDeviceList, total, size, current);
- }
- @Override
- public List<CorrespondDeviceListVO> correspondDeviceListEcho(int id) {
- LambdaQueryWrapper<Device> queryWrapper = Wrappers.lambdaQuery();
- queryWrapper.eq(Device::getEnable, 1).eq(Device::getId, id);
- List<Device> deviceList = this.list(queryWrapper);
- LambdaQueryWrapper<DeviceAnalogVariableList> queryWrapperOne = Wrappers.lambdaQuery();
- queryWrapperOne.eq(DeviceAnalogVariableList::getStatus, 1).eq(DeviceAnalogVariableList::getCommunicationEquipment, deviceList.get(0).getId());
- List<DeviceAnalogVariableList> deviceAnalogVariableList = deviceAnalogVariableListService.list(queryWrapperOne);
- List<CorrespondDeviceListVO> 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<Device> videoMonitoringDeviceList(String deviceName, Integer deviceType, Integer siteId, Integer size, Integer current) {
- IPage<Device> page = new Page<Device>(size, current);
- LambdaQueryWrapper<Device> 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.getCurrent(), page.getSize());
- }
- @Override
- public void variableCloning(Integer type, String newDeviceCode, String oldDeviceCode, String deviceName) {
- SysUser user = SecurityUtils.getLoginUser().getUser();
- //查询出旧设备配置
- LambdaQueryWrapper<Device> queryWrapper = Wrappers.lambdaQuery();
- queryWrapper.eq(Device::getEnable, 1).eq(Device::getDeviceCode, oldDeviceCode);
- List<Device> deviceList = this.list(queryWrapper);
- if (type == 1) {
- //设备表新增
- 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<DeviceAnalogVariableList> queryWrapperOne = Wrappers.lambdaQuery();
- queryWrapperOne.eq(DeviceAnalogVariableList::getStatus, 1).eq(DeviceAnalogVariableList::getCommunicationEquipment, deviceList.get(0).getId());
- List<DeviceAnalogVariableList> 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(deviceAnalogVariableList.get(i).getVariableCoding());
- 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<DeviceAnalogVariableList> queryWrapperOneA = Wrappers.lambdaQuery();
- queryWrapperOneA.eq(DeviceAnalogVariableList::getStatus, 1).eq(DeviceAnalogVariableList::getCommunicationEquipment, deviceList.get(0).getId());
- List<DeviceAnalogVariableList> deviceAnalogVariableListOne = deviceAnalogVariableListService.list(queryWrapperOneA);
- //克隆设备查询是否存在
- LambdaQueryWrapper<Device> queryWrapperTwo = Wrappers.lambdaQuery();
- queryWrapperTwo.eq(Device::getEnable, 1).eq(Device::getDeviceCode, newDeviceCode);
- List<Device> deviceListTwo = this.list(queryWrapperTwo);
- LambdaQueryWrapper<DeviceAnalogVariableList> queryWrapperTwoA = Wrappers.lambdaQuery();
- queryWrapperTwoA.eq(DeviceAnalogVariableList::getStatus, 1).eq(DeviceAnalogVariableList::getCommunicationEquipment, deviceListTwo.get(0).getId());
- List<DeviceAnalogVariableList> 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(deviceAnalogVariableListOne.get(i).getVariableCoding());
- 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<Device> deviceListOne(Integer siteId, Integer deviceType) {
- LambdaQueryWrapper<Device> queryWrapperTwo = Wrappers.lambdaQuery();
- queryWrapperTwo.eq(Device::getEnable, 1).eq(Device::getSiteId, siteId).eq(Device::getDeviceType, deviceType);
- List<Device> deviceListTwo = this.list(queryWrapperTwo);
- return deviceListTwo;
- }
- @Override
- public String deviceExport(String deviceName, Integer deviceType, Integer siteId) {
- Workbook workbook = null;
- File file = null;
- try {
- ExportParams params = new ExportParams(null, "通信设备列表");
- workbook = ExcelExportUtil.exportBigExcel(params, DeviceExportVO.class,
- (o, i) -> {
- Page<Device> page = new Page<>(i, 30);
- LambdaQueryWrapper<Device> queryWrapper = Wrappers.lambdaQuery();
- queryWrapper.eq(Device::getSiteId, siteId).eq(Device::getEnable, 1);
- if (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 ArrayList<>(BeanMapperUtils.mapList(page.getRecords(), Device.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();
- }
- }
|