From bab43330bf6f48bdb7bfb258611f51bb05ef56fe Mon Sep 17 00:00:00 2001 From: 潘志宝 <979469083@qq.com> Date: 星期一, 06 一月 2025 09:02:21 +0800 Subject: [PATCH] 动态数据长度 --- iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/channel/modbus/service/impl/ChannelModbusTagServiceImpl.java | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++-- 1 files changed, 51 insertions(+), 2 deletions(-) diff --git a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/channel/modbus/service/impl/ChannelModbusTagServiceImpl.java b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/channel/modbus/service/impl/ChannelModbusTagServiceImpl.java index 7e80908..64d3084 100644 --- a/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/channel/modbus/service/impl/ChannelModbusTagServiceImpl.java +++ b/iailab-module-data/iailab-module-data-biz/src/main/java/com/iailab/module/data/channel/modbus/service/impl/ChannelModbusTagServiceImpl.java @@ -1,21 +1,28 @@ package com.iailab.module.data.channel.modbus.service.impl; +import cn.hutool.core.collection.CollUtil; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.iailab.framework.common.pojo.PageResult; +import com.iailab.framework.common.util.object.BeanUtils; import com.iailab.framework.common.util.object.ConvertUtils; import com.iailab.module.data.channel.modbus.dao.ChannelModBusTagDao; import com.iailab.module.data.channel.modbus.dto.ChannelModbusTagDTO; import com.iailab.module.data.channel.modbus.entity.ChannelModBusTagEntity; import com.iailab.module.data.channel.modbus.service.ChannelModbusTagService; +import com.iailab.module.data.channel.modbus.vo.ModBusTagImportExcelVO; import com.iailab.module.data.channel.modbus.vo.ModBusTagPageReqVO; +import com.iailab.module.data.channel.tag.vo.TagImportRespVO; +import com.iailab.module.data.common.enums.CommonConstant; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.stereotype.Service; import javax.annotation.Resource; -import java.util.Date; -import java.util.List; +import java.util.*; + +import static com.iailab.framework.common.exception.util.ServiceExceptionUtil.exception; +import static com.iailab.module.data.enums.ErrorCodeConstants.*; /** * @author lirm @@ -82,4 +89,46 @@ baseMapper.delete(new QueryWrapper<ChannelModBusTagEntity>().eq("device", name)); } + @Override + public TagImportRespVO importModBusTagList(List<ModBusTagImportExcelVO> importTags, boolean isUpdateSupport, String device) { + // 1.1 参数校验 + if (CollUtil.isEmpty(importTags)) { + throw exception(TAG_IMPORT_LIST_IS_EMPTY); + } + + // 2. 遍历,逐个创建 or 更新 + TagImportRespVO respVO = TagImportRespVO.builder().createTagNames(new ArrayList<>()) + .updateTagNames(new ArrayList<>()).failureTagNames(new LinkedHashMap<>()).build(); + importTags.forEach(importTag -> { + + // 判断如果不存在,再进行插入 + ChannelModBusTagEntity existTag = channelModBusTagDao.selectOne(new QueryWrapper<ChannelModBusTagEntity>() + .eq("device", device) + .eq("tag_name",importTag.getTagName())); + if (existTag == null) { + ChannelModBusTagEntity channelModBusTagEntity = ConvertUtils.sourceToTarget(importTag, ChannelModBusTagEntity.class); + channelModBusTagEntity.setId(UUID.randomUUID().toString()); + channelModBusTagEntity.setEnabled(CommonConstant.IS_ENABLE); + channelModBusTagEntity.setDevice(device); + channelModBusTagEntity.setCreateTime(new Date()); + channelModBusTagDao.insert(channelModBusTagEntity); + + respVO.getCreateTagNames().add(channelModBusTagEntity.getTagName()); + return; + } + + // 如果存在,判断是否允许更新 + if (!isUpdateSupport) { + respVO.getFailureTagNames().put(importTag.getTagName(), TAG_EXISTS.getMsg()); + return; + } + + ChannelModBusTagEntity updateTag = BeanUtils.toBean(importTag, ChannelModBusTagEntity.class); + updateTag.setId(existTag.getId()); + baseMapper.updateById(updateTag); + respVO.getUpdateTagNames().add(importTag.getTagName()); + }); + return respVO; + } + } -- Gitblit v1.9.3