dongyukun
2024-11-05 e8ad669f7c97d45cd23630dc101180a130d6c17e
提交 | 用户 | 时间
a6de49 1 package com.iailab.module.data.channel.opcua.service.impl;
H 2
cfbd83 3 import com.baomidou.dynamic.datasource.annotation.DSTransactional;
a6de49 4 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
H 5 import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
6 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
aecc49 7 import com.iailab.framework.common.pojo.PageResult;
a6de49 8 import com.iailab.framework.common.util.object.ConvertUtils;
H 9 import com.iailab.module.data.channel.opcua.dao.ChannelOPCUATagDao;
10 import com.iailab.module.data.channel.opcua.dto.ChannelOPCUATagDTO;
11 import com.iailab.module.data.channel.opcua.entity.ChannelOPCUATagEntity;
12 import com.iailab.module.data.channel.opcua.service.ChannelOPCUATagService;
aecc49 13 import com.iailab.module.data.channel.opcua.vo.OpcUaTagPageReqVO;
a6de49 14 import lombok.extern.slf4j.Slf4j;
H 15 import org.apache.poi.ss.usermodel.CellType;
16 import org.apache.poi.xssf.usermodel.XSSFRow;
17 import org.apache.poi.xssf.usermodel.XSSFSheet;
18 import org.apache.poi.xssf.usermodel.XSSFWorkbook;
19 import org.springframework.beans.factory.annotation.Value;
20 import org.springframework.stereotype.Service;
21 import org.springframework.web.multipart.MultipartFile;
22
23 import javax.annotation.Resource;
24 import java.io.File;
25 import java.io.FileInputStream;
26 import java.util.ArrayList;
27 import java.util.List;
28 import java.util.UUID;
29
30 /**
aecc49 31  * @author lirm
a6de49 32  * @Description
aecc49 33  * @createTime 2024年08月26日
a6de49 34  */
H 35 @Slf4j
36 @Service
37 public class ChannelOPCUATagServiceImpl extends ServiceImpl<ChannelOPCUATagDao, ChannelOPCUATagEntity> implements ChannelOPCUATagService {
38     @Resource
39     private ChannelOPCUATagDao channelOPCUATagDao;
40
41     @Value("${iems.upload-dir}")
42     private String uploadDir;
43
44     @Override
aecc49 45     public PageResult<ChannelOPCUATagEntity> queryPage(OpcUaTagPageReqVO reqVO) {
L 46         return channelOPCUATagDao.selectPage(reqVO);
a6de49 47     }
H 48
49     @Override
50     public ChannelOPCUATagEntity info(String id) {
51         return channelOPCUATagDao.selectById(id);
52     }
53
54     @Override
55     public void add(ChannelOPCUATagEntity channelOPCUATagEntity) {
56         channelOPCUATagDao.insert(channelOPCUATagEntity);
57     }
58
59     @Override
60     public void update(ChannelOPCUATagEntity channelOPCUATagEntity) {
61         channelOPCUATagDao.updateById(channelOPCUATagEntity);
62     }
63
64     @Override
65     public void delete(String id) {
66         channelOPCUATagDao.deleteById(id);
67     }
68
aecc49 69
L 70     @Override
71     public List<ChannelOPCUATagEntity> getByDevice(String device) {
72         QueryWrapper<ChannelOPCUATagEntity> queryWrapper = new QueryWrapper<>();
73         queryWrapper.eq("device", device).orderByDesc ("create_time");
74         return channelOPCUATagDao.selectList(queryWrapper);
75     }
76
a6de49 77     @Override
H 78     public List<ChannelOPCUATagDTO> selectAll() {
79         List<ChannelOPCUATagEntity> entityList = baseMapper.selectList(
80                 null
81         );
82         return ConvertUtils.sourceToTarget(entityList, ChannelOPCUATagDTO.class);
83     }
84
85     @Override
86     public List<ChannelOPCUATagEntity> listByIds(List<String> ids) {
87         return baseMapper.selectList(new QueryWrapper<ChannelOPCUATagEntity>().in("id", ids));
88     }
89
90     @Override
91     public void deleteByDeviceName(String name) {
92         baseMapper.delete(new QueryWrapper<ChannelOPCUATagEntity>().eq("device",name));
93     }
94
95     @Override
cfbd83 96     @DSTransactional(rollbackFor = Exception.class)
a6de49 97     public void importTag(String device, MultipartFile file) throws Exception {
H 98         try {
99             String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
100             String fileName = UUID.randomUUID().toString() + suffix;
101             String path = uploadDir + fileName;
102             file.transferTo(new File(path));
103
104             XSSFWorkbook hssfWorkbook = new XSSFWorkbook(new FileInputStream(path));
105             XSSFSheet sheet = hssfWorkbook.getSheetAt(0);
106             int lastRowNum = sheet.getLastRowNum();
107             log.info("最后一行:" + lastRowNum);
108             int lastCellNum = 4;
109             List<ChannelOPCUATagEntity> dangerList = new ArrayList<>();
110             for (int i = 2; i <= lastRowNum; i++) {
111                 XSSFRow row = sheet.getRow(i);
112                 for (int j = row.getFirstCellNum(); j < lastCellNum; j++) {
113                     row.getCell(j).setCellType(CellType.STRING);
114                 }
115                 ChannelOPCUATagEntity tagEntity = new ChannelOPCUATagEntity();
116                 tagEntity.setId(UUID.randomUUID().toString());
117                 tagEntity.setTagName(row.getCell(1).getStringCellValue());
118                 tagEntity.setDataType(row.getCell(2).getStringCellValue());
119                 row.getCell(4).setCellType(CellType.STRING);
120                 if(row.getCell(3).getStringCellValue().equals("1")){
121                     tagEntity.setAddress(String.format("1%04d",Integer.parseInt(row.getCell(4).getStringCellValue())));
122                 }else if(row.getCell(3).getStringCellValue().equals("3")){
123                     tagEntity.setAddress(String.format("4%04d",Integer.parseInt(row.getCell(4).getStringCellValue())));
124                 }
125                 tagEntity.setEnabled(true);
126                 tagEntity.setDevice(device);
127                 tagEntity.setSamplingRate(1);
128                 dangerList.add(tagEntity);
129             }
130             if (CollectionUtils.isEmpty(dangerList)) {
131                 return;
132             }
133             //getBaseMapper().insertList(dangerList);
134             dangerList.forEach(item -> {
135                 try {
136                     getBaseMapper().insert(item);
137                 } catch (Exception ex) {
138                     log.warn("插入异常:" + item.getTagName());
139                 }
140             });
141         } catch (Exception ex) {
142             ex.printStackTrace();
143             log.warn("导入失败!");
144             throw ex;
145         }
146     }
147 }