package com.iailab.module.data.channel.opcua.service.impl;
|
|
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
|
import com.baomidou.mybatisplus.core.metadata.IPage;
|
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
|
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
|
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
import com.iailab.framework.common.util.object.ConvertUtils;
|
import com.iailab.module.data.common.utils.PageUtils;
|
import com.iailab.module.data.common.utils.Query;
|
import com.iailab.module.data.channel.opcua.dao.ChannelOPCUATagDao;
|
import com.iailab.module.data.channel.opcua.dto.ChannelOPCUATagDTO;
|
import com.iailab.module.data.channel.opcua.entity.ChannelOPCUATagEntity;
|
import com.iailab.module.data.channel.opcua.service.ChannelOPCUATagService;
|
import lombok.extern.slf4j.Slf4j;
|
import org.apache.poi.ss.usermodel.CellType;
|
import org.apache.poi.xssf.usermodel.XSSFRow;
|
import org.apache.poi.xssf.usermodel.XSSFSheet;
|
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
|
import org.springframework.beans.factory.annotation.Value;
|
import org.springframework.stereotype.Service;
|
import org.springframework.transaction.annotation.Transactional;
|
import org.springframework.web.multipart.MultipartFile;
|
|
import javax.annotation.Resource;
|
import java.io.File;
|
import java.io.FileInputStream;
|
import java.util.ArrayList;
|
import java.util.List;
|
import java.util.Map;
|
import java.util.UUID;
|
|
/**
|
* @author DongYukun
|
* @Description
|
* @createTime 2023年05月08日 15:04:00
|
*/
|
@Slf4j
|
@Service
|
public class ChannelOPCUATagServiceImpl extends ServiceImpl<ChannelOPCUATagDao, ChannelOPCUATagEntity> implements ChannelOPCUATagService {
|
@Resource
|
private ChannelOPCUATagDao channelOPCUATagDao;
|
|
@Value("${iems.upload-dir}")
|
private String uploadDir;
|
|
/**
|
* 分页查询tag
|
*
|
* @param params
|
*/
|
@Override
|
public PageUtils queryPage(Map<String, Object> params) {
|
String tagName = (String) params.get("tagName");
|
String device = (String) params.get("device");
|
|
IPage<ChannelOPCUATagEntity> page = this.page(
|
new Query<ChannelOPCUATagEntity>().getPage(params),
|
new QueryWrapper<ChannelOPCUATagEntity>()
|
.like(StringUtils.isNotBlank(tagName), "tag_name", tagName)
|
.eq(StringUtils.isNotBlank(device), "device", device)
|
.orderByDesc("create_time")
|
);
|
return new PageUtils(page);
|
}
|
|
/**
|
* 查询tag详情
|
*
|
* @param id
|
*/
|
@Override
|
public ChannelOPCUATagEntity info(String id) {
|
return channelOPCUATagDao.selectById(id);
|
}
|
|
@Override
|
public List<ChannelOPCUATagEntity> getByDevice(String device) {
|
QueryWrapper<ChannelOPCUATagEntity> queryWrapper = new QueryWrapper<>();
|
queryWrapper.eq("device", device).orderByDesc ("create_time");
|
return channelOPCUATagDao.selectList(queryWrapper);
|
|
}
|
|
/**
|
* 添加tag
|
*
|
* @param channelOPCUATagEntity
|
*/
|
@Override
|
public void add(ChannelOPCUATagEntity channelOPCUATagEntity) {
|
channelOPCUATagDao.insert(channelOPCUATagEntity);
|
}
|
|
/**
|
* 修改tag
|
*
|
* @param channelOPCUATagEntity
|
*/
|
@Override
|
public void update(ChannelOPCUATagEntity channelOPCUATagEntity) {
|
channelOPCUATagDao.updateById(channelOPCUATagEntity);
|
}
|
|
/**
|
* 删除tag
|
*
|
* @param id
|
*/
|
@Override
|
public void delete(String id) {
|
channelOPCUATagDao.deleteById(id);
|
}
|
|
@Override
|
public List<ChannelOPCUATagDTO> selectAll() {
|
|
List<ChannelOPCUATagEntity> entityList = baseMapper.selectList(
|
null
|
);
|
return ConvertUtils.sourceToTarget(entityList, ChannelOPCUATagDTO.class);
|
}
|
|
@Override
|
public List<ChannelOPCUATagEntity> listByIds(List<String> ids) {
|
return baseMapper.selectList(new QueryWrapper<ChannelOPCUATagEntity>().in("id", ids));
|
}
|
|
@Override
|
public void deleteByDeviceName(String name) {
|
baseMapper.delete(new QueryWrapper<ChannelOPCUATagEntity>().eq("device",name));
|
}
|
|
/**
|
* 导入Tag
|
*
|
* @param device
|
* @param file
|
* @throws Exception
|
*/
|
@Override
|
@Transactional(rollbackFor = Exception.class)
|
public void importTag(String device, MultipartFile file) throws Exception {
|
try {
|
String suffix = file.getOriginalFilename().substring(file.getOriginalFilename().lastIndexOf("."));
|
String fileName = UUID.randomUUID().toString() + suffix;
|
String path = uploadDir + fileName;
|
file.transferTo(new File(path));
|
|
XSSFWorkbook hssfWorkbook = new XSSFWorkbook(new FileInputStream(path));
|
XSSFSheet sheet = hssfWorkbook.getSheetAt(0);
|
int lastRowNum = sheet.getLastRowNum();
|
log.info("最后一行:" + lastRowNum);
|
int lastCellNum = 4;
|
List<ChannelOPCUATagEntity> dangerList = new ArrayList<>();
|
for (int i = 2; i <= lastRowNum; i++) {
|
XSSFRow row = sheet.getRow(i);
|
for (int j = row.getFirstCellNum(); j < lastCellNum; j++) {
|
row.getCell(j).setCellType(CellType.STRING);
|
}
|
ChannelOPCUATagEntity tagEntity = new ChannelOPCUATagEntity();
|
tagEntity.setId(UUID.randomUUID().toString());
|
tagEntity.setTagName(row.getCell(1).getStringCellValue());
|
tagEntity.setDataType(row.getCell(2).getStringCellValue());
|
row.getCell(4).setCellType(CellType.STRING);
|
if(row.getCell(3).getStringCellValue().equals("1")){
|
tagEntity.setAddress(String.format("1%04d",Integer.parseInt(row.getCell(4).getStringCellValue())));
|
}else if(row.getCell(3).getStringCellValue().equals("3")){
|
tagEntity.setAddress(String.format("4%04d",Integer.parseInt(row.getCell(4).getStringCellValue())));
|
}
|
tagEntity.setEnabled(true);
|
tagEntity.setDevice(device);
|
tagEntity.setSamplingRate(1);
|
dangerList.add(tagEntity);
|
}
|
if (CollectionUtils.isEmpty(dangerList)) {
|
return;
|
}
|
//getBaseMapper().insertList(dangerList);
|
dangerList.forEach(item -> {
|
try {
|
getBaseMapper().insert(item);
|
} catch (Exception ex) {
|
log.warn("插入异常:" + item.getTagName());
|
}
|
});
|
} catch (Exception ex) {
|
ex.printStackTrace();
|
log.warn("导入失败!");
|
throw ex;
|
}
|
}
|
}
|