package com.iailab.module.data.channel.opcda.service.impl; import com.baomidou.dynamic.datasource.annotation.DSTransactional; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.toolkit.CollectionUtils; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.iailab.framework.common.pojo.PageResult; import com.iailab.framework.common.util.object.ConvertUtils; import com.iailab.module.data.channel.opcda.dao.ChannelOPCDATagDao; import com.iailab.module.data.channel.opcda.dto.ChannelOPCDATagDTO; import com.iailab.module.data.channel.opcda.entity.ChannelOPCDATagEntity; import com.iailab.module.data.channel.opcda.service.ChannelOPCDATagService; import com.iailab.module.data.channel.opcda.vo.OpcDaTagPageReqVO; 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.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.UUID; /** * @author lirm * @Description * @createTime 2024年08月26日 */ @Slf4j @Service public class ChannelOPCDATagServiceImpl extends ServiceImpl implements ChannelOPCDATagService { @Resource private ChannelOPCDATagDao channelOPCDATagDao; @Value("${iems.upload-dir}") private String uploadDir; @Override public PageResult queryPage(OpcDaTagPageReqVO reqVO) { return channelOPCDATagDao.selectPage(reqVO); } @Override public ChannelOPCDATagEntity info(String id) { return channelOPCDATagDao.selectById(id); } @Override public void add(ChannelOPCDATagEntity channelOPCDATagEntity) { channelOPCDATagDao.insert(channelOPCDATagEntity); } @Override public void update(ChannelOPCDATagEntity channelOPCDATagEntity) { channelOPCDATagDao.updateById(channelOPCDATagEntity); } @Override public void delete(String id) { channelOPCDATagDao.deleteById(id); } @Override public List selectAll() { List entityList = channelOPCDATagDao.selectList(null); return ConvertUtils.sourceToTarget(entityList, ChannelOPCDATagDTO.class); } @Override public List listByIds(List ids) { return channelOPCDATagDao.selectList(new QueryWrapper().in("id", ids)); } @Override public void deleteByServerId(String serverId) { channelOPCDATagDao.delete(new QueryWrapper().eq("server_id",serverId)); } @Override public List getByserverId(String serverId) { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("server_id", serverId).orderByDesc ("create_time"); return channelOPCDATagDao.selectList(queryWrapper); } @Override @DSTransactional(rollbackFor = Exception.class) public void importTag(String serverId, 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 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); } ChannelOPCDATagEntity tagEntity = new ChannelOPCDATagEntity(); tagEntity.setId(UUID.randomUUID().toString()); tagEntity.setTagName(row.getCell(1).getStringCellValue()); tagEntity.setDataType(row.getCell(2).getStringCellValue()); tagEntity.setItemId(row.getCell(3).getStringCellValue()); tagEntity.setEnabled(true); tagEntity.setServerId(serverId); dangerList.add(tagEntity); } if (CollectionUtils.isEmpty(dangerList)) { return; } //getBaseMapper().insertList(dangerList); dangerList.forEach(item -> { try { channelOPCDATagDao.insert(item); } catch (Exception ex) { log.warn("插入异常:" + item.getTagName()); } }); } catch (Exception ex) { ex.printStackTrace(); log.warn("导入失败!"); throw ex; } } }