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