提交 | 用户 | 时间
|
a6de49
|
1 |
package com.iailab.module.data.channel.opcua.service.impl; |
H |
2 |
|
03e8ac
|
3 |
import cn.hutool.core.collection.CollUtil; |
cfbd83
|
4 |
import com.baomidou.dynamic.datasource.annotation.DSTransactional; |
a6de49
|
5 |
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; |
H |
6 |
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; |
|
7 |
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; |
aecc49
|
8 |
import com.iailab.framework.common.pojo.PageResult; |
03e8ac
|
9 |
import com.iailab.framework.common.util.object.BeanUtils; |
a6de49
|
10 |
import com.iailab.framework.common.util.object.ConvertUtils; |
H |
11 |
import com.iailab.module.data.channel.opcua.dao.ChannelOPCUATagDao; |
|
12 |
import com.iailab.module.data.channel.opcua.dto.ChannelOPCUATagDTO; |
|
13 |
import com.iailab.module.data.channel.opcua.entity.ChannelOPCUATagEntity; |
|
14 |
import com.iailab.module.data.channel.opcua.service.ChannelOPCUATagService; |
03e8ac
|
15 |
import com.iailab.module.data.channel.opcua.vo.OpcUaTagImportExcelVO; |
aecc49
|
16 |
import com.iailab.module.data.channel.opcua.vo.OpcUaTagPageReqVO; |
03e8ac
|
17 |
import com.iailab.module.data.channel.tag.vo.TagImportRespVO; |
J |
18 |
import com.iailab.module.data.common.enums.CommonConstant; |
a6de49
|
19 |
import lombok.extern.slf4j.Slf4j; |
H |
20 |
import org.apache.poi.ss.usermodel.CellType; |
|
21 |
import org.apache.poi.xssf.usermodel.XSSFRow; |
|
22 |
import org.apache.poi.xssf.usermodel.XSSFSheet; |
|
23 |
import org.apache.poi.xssf.usermodel.XSSFWorkbook; |
|
24 |
import org.springframework.beans.factory.annotation.Value; |
|
25 |
import org.springframework.stereotype.Service; |
|
26 |
import org.springframework.web.multipart.MultipartFile; |
|
27 |
|
|
28 |
import javax.annotation.Resource; |
|
29 |
import java.io.File; |
|
30 |
import java.io.FileInputStream; |
03e8ac
|
31 |
import java.util.*; |
J |
32 |
|
|
33 |
import static com.iailab.framework.common.exception.util.ServiceExceptionUtil.exception; |
|
34 |
import static com.iailab.module.data.enums.ErrorCodeConstants.*; |
a6de49
|
35 |
|
H |
36 |
/** |
aecc49
|
37 |
* @author lirm |
a6de49
|
38 |
* @Description |
aecc49
|
39 |
* @createTime 2024年08月26日 |
a6de49
|
40 |
*/ |
H |
41 |
@Slf4j |
|
42 |
@Service |
|
43 |
public class ChannelOPCUATagServiceImpl extends ServiceImpl<ChannelOPCUATagDao, ChannelOPCUATagEntity> implements ChannelOPCUATagService { |
|
44 |
@Resource |
|
45 |
private ChannelOPCUATagDao channelOPCUATagDao; |
|
46 |
|
|
47 |
@Value("${iems.upload-dir}") |
|
48 |
private String uploadDir; |
|
49 |
|
|
50 |
@Override |
aecc49
|
51 |
public PageResult<ChannelOPCUATagEntity> queryPage(OpcUaTagPageReqVO reqVO) { |
L |
52 |
return channelOPCUATagDao.selectPage(reqVO); |
a6de49
|
53 |
} |
H |
54 |
|
|
55 |
@Override |
|
56 |
public ChannelOPCUATagEntity info(String id) { |
|
57 |
return channelOPCUATagDao.selectById(id); |
|
58 |
} |
|
59 |
|
|
60 |
@Override |
|
61 |
public void add(ChannelOPCUATagEntity channelOPCUATagEntity) { |
|
62 |
channelOPCUATagDao.insert(channelOPCUATagEntity); |
|
63 |
} |
|
64 |
|
|
65 |
@Override |
|
66 |
public void update(ChannelOPCUATagEntity channelOPCUATagEntity) { |
|
67 |
channelOPCUATagDao.updateById(channelOPCUATagEntity); |
|
68 |
} |
|
69 |
|
|
70 |
@Override |
|
71 |
public void delete(String id) { |
|
72 |
channelOPCUATagDao.deleteById(id); |
|
73 |
} |
|
74 |
|
aecc49
|
75 |
|
L |
76 |
@Override |
|
77 |
public List<ChannelOPCUATagEntity> getByDevice(String device) { |
|
78 |
QueryWrapper<ChannelOPCUATagEntity> queryWrapper = new QueryWrapper<>(); |
|
79 |
queryWrapper.eq("device", device).orderByDesc ("create_time"); |
|
80 |
return channelOPCUATagDao.selectList(queryWrapper); |
|
81 |
} |
|
82 |
|
a6de49
|
83 |
@Override |
H |
84 |
public List<ChannelOPCUATagDTO> selectAll() { |
|
85 |
List<ChannelOPCUATagEntity> entityList = baseMapper.selectList( |
|
86 |
null |
|
87 |
); |
|
88 |
return ConvertUtils.sourceToTarget(entityList, ChannelOPCUATagDTO.class); |
|
89 |
} |
|
90 |
|
|
91 |
@Override |
|
92 |
public List<ChannelOPCUATagEntity> listByIds(List<String> ids) { |
|
93 |
return baseMapper.selectList(new QueryWrapper<ChannelOPCUATagEntity>().in("id", ids)); |
|
94 |
} |
|
95 |
|
|
96 |
@Override |
|
97 |
public void deleteByDeviceName(String name) { |
|
98 |
baseMapper.delete(new QueryWrapper<ChannelOPCUATagEntity>().eq("device",name)); |
|
99 |
} |
|
100 |
|
|
101 |
@Override |
cfbd83
|
102 |
@DSTransactional(rollbackFor = Exception.class) |
a6de49
|
103 |
public void importTag(String device, MultipartFile file) throws Exception { |
H |
104 |
try { |
|
105 |
String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf(".")); |
|
106 |
String fileName = UUID.randomUUID().toString() + suffix; |
|
107 |
String path = uploadDir + fileName; |
|
108 |
file.transferTo(new File(path)); |
|
109 |
|
|
110 |
XSSFWorkbook hssfWorkbook = new XSSFWorkbook(new FileInputStream(path)); |
|
111 |
XSSFSheet sheet = hssfWorkbook.getSheetAt(0); |
|
112 |
int lastRowNum = sheet.getLastRowNum(); |
|
113 |
log.info("最后一行:" + lastRowNum); |
|
114 |
int lastCellNum = 4; |
|
115 |
List<ChannelOPCUATagEntity> dangerList = new ArrayList<>(); |
|
116 |
for (int i = 2; i <= lastRowNum; i++) { |
|
117 |
XSSFRow row = sheet.getRow(i); |
|
118 |
for (int j = row.getFirstCellNum(); j < lastCellNum; j++) { |
|
119 |
row.getCell(j).setCellType(CellType.STRING); |
|
120 |
} |
|
121 |
ChannelOPCUATagEntity tagEntity = new ChannelOPCUATagEntity(); |
|
122 |
tagEntity.setId(UUID.randomUUID().toString()); |
|
123 |
tagEntity.setTagName(row.getCell(1).getStringCellValue()); |
|
124 |
tagEntity.setDataType(row.getCell(2).getStringCellValue()); |
|
125 |
row.getCell(4).setCellType(CellType.STRING); |
|
126 |
if(row.getCell(3).getStringCellValue().equals("1")){ |
|
127 |
tagEntity.setAddress(String.format("1%04d",Integer.parseInt(row.getCell(4).getStringCellValue()))); |
|
128 |
}else if(row.getCell(3).getStringCellValue().equals("3")){ |
|
129 |
tagEntity.setAddress(String.format("4%04d",Integer.parseInt(row.getCell(4).getStringCellValue()))); |
|
130 |
} |
03e8ac
|
131 |
tagEntity.setEnabled(1); |
a6de49
|
132 |
tagEntity.setDevice(device); |
H |
133 |
tagEntity.setSamplingRate(1); |
|
134 |
dangerList.add(tagEntity); |
|
135 |
} |
|
136 |
if (CollectionUtils.isEmpty(dangerList)) { |
|
137 |
return; |
|
138 |
} |
|
139 |
//getBaseMapper().insertList(dangerList); |
|
140 |
dangerList.forEach(item -> { |
|
141 |
try { |
|
142 |
getBaseMapper().insert(item); |
|
143 |
} catch (Exception ex) { |
|
144 |
log.warn("插入异常:" + item.getTagName()); |
|
145 |
} |
|
146 |
}); |
|
147 |
} catch (Exception ex) { |
|
148 |
ex.printStackTrace(); |
|
149 |
log.warn("导入失败!"); |
|
150 |
throw ex; |
|
151 |
} |
|
152 |
} |
03e8ac
|
153 |
|
J |
154 |
@Override |
|
155 |
public TagImportRespVO importOpcUaTagList(List<OpcUaTagImportExcelVO> importTags, boolean isUpdateSupport, String device) { |
|
156 |
// 1.1 参数校验 |
|
157 |
if (CollUtil.isEmpty(importTags)) { |
|
158 |
throw exception(TAG_IMPORT_LIST_IS_EMPTY); |
|
159 |
} |
|
160 |
// 2. 遍历,逐个创建 or 更新 |
|
161 |
TagImportRespVO respVO = TagImportRespVO.builder().createTagNames(new ArrayList<>()) |
|
162 |
.updateTagNames(new ArrayList<>()).failureTagNames(new LinkedHashMap<>()).build(); |
|
163 |
importTags.forEach(importTag -> { |
|
164 |
// 判断如果不存在,再进行插入 |
|
165 |
ChannelOPCUATagEntity existTag = channelOPCUATagDao.selectOne(new QueryWrapper<ChannelOPCUATagEntity>() |
|
166 |
.eq("device", device) |
|
167 |
.eq("tag_name",importTag.getTagName())); |
|
168 |
if (existTag == null) { |
|
169 |
ChannelOPCUATagEntity channelOpCuaTagEntity = ConvertUtils.sourceToTarget(importTag, ChannelOPCUATagEntity.class); |
|
170 |
channelOpCuaTagEntity.setId(UUID.randomUUID().toString()); |
|
171 |
channelOpCuaTagEntity.setEnabled(CommonConstant.IS_ENABLE); |
|
172 |
channelOpCuaTagEntity.setDevice(device); |
|
173 |
channelOpCuaTagEntity.setCreateTime(new Date()); |
|
174 |
channelOPCUATagDao.insert(channelOpCuaTagEntity); |
|
175 |
|
|
176 |
respVO.getCreateTagNames().add(channelOpCuaTagEntity.getTagName()); |
|
177 |
return; |
|
178 |
} |
|
179 |
|
|
180 |
// 如果存在,判断是否允许更新 |
|
181 |
if (!isUpdateSupport) { |
|
182 |
respVO.getFailureTagNames().put(importTag.getTagName(), TAG_EXISTS.getMsg()); |
|
183 |
return; |
|
184 |
} |
|
185 |
|
|
186 |
ChannelOPCUATagEntity updateTag = BeanUtils.toBean(importTag, ChannelOPCUATagEntity.class); |
|
187 |
updateTag.setId(existTag.getId()); |
|
188 |
baseMapper.updateById(updateTag); |
|
189 |
respVO.getUpdateTagNames().add(importTag.getTagName()); |
|
190 |
}); |
|
191 |
return respVO; |
|
192 |
} |
a6de49
|
193 |
} |