diff --git a/aucma-base/src/main/java/com/aucma/base/controller/BaseDeviceParamController.java b/aucma-base/src/main/java/com/aucma/base/controller/BaseDeviceParamController.java index ba9d52e..1b59e4f 100644 --- a/aucma-base/src/main/java/com/aucma/base/controller/BaseDeviceParamController.java +++ b/aucma-base/src/main/java/com/aucma/base/controller/BaseDeviceParamController.java @@ -14,6 +14,7 @@ import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.multipart.MultipartFile; import com.aucma.common.annotation.Log; import com.aucma.common.core.controller.BaseController; import com.aucma.common.core.domain.AjaxResult; @@ -58,6 +59,29 @@ public class BaseDeviceParamController extends BaseController { util.exportExcel(response, list, "设备参数数据" ); } + /** + * 导入设备参数列表 + */ + @PreAuthorize("@ss.hasPermi('base:deviceParam:import')" ) + @Log(title = "设备参数" , businessType = BusinessType.IMPORT) + @PostMapping("/importData" ) + public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception { + ExcelUtil util = new ExcelUtil(BaseDeviceParam.class); + List deviceParamList = util.importExcel(file.getInputStream()); + String operName = getUsername(); + String message = baseDeviceParamService.importDeviceParam(deviceParamList, updateSupport, operName); + return success(message); + } + + /** + * 下载设备参数导入模板 + */ + @PostMapping("/importTemplate" ) + public void importTemplate(HttpServletResponse response) { + ExcelUtil util = new ExcelUtil(BaseDeviceParam.class); + util.importTemplateExcel(response, "设备参数数据" ); + } + /** * 获取设备参数详细信息 */ diff --git a/aucma-base/src/main/java/com/aucma/base/domain/BaseDeviceParam.java b/aucma-base/src/main/java/com/aucma/base/domain/BaseDeviceParam.java index 8a92eef..a17373c 100644 --- a/aucma-base/src/main/java/com/aucma/base/domain/BaseDeviceParam.java +++ b/aucma-base/src/main/java/com/aucma/base/domain/BaseDeviceParam.java @@ -25,7 +25,7 @@ public class BaseDeviceParam extends BaseEntity { /** * 产线名称 */ - @Excel(name = "产线名称") + @Excel(name = "产线名称", type = Excel.Type.EXPORT) private String productLineName; /** @@ -67,45 +67,45 @@ public class BaseDeviceParam extends BaseEntity { /** * 设备名称 */ - @Excel(name = "设备名称") + @Excel(name = "设备名称", type = Excel.Type.EXPORT) private String deviceName; /** * 读取频率(毫秒) */ - @Excel(name = "读取频率", readConverterExp = "毫=秒") + @Excel(name = "读取频率") private Long readFrequency; /** * 启用标识 */ - @Excel(name = "启用标识") + @Excel(name = "启用标识", readConverterExp = "1=有效/0无效") private Long isFlag; /** * 创建人 */ - @Excel(name = "创建人") + @Excel(name = "创建人", type = Excel.Type.EXPORT) private String createdBy; /** * 创建时间 */ @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") - @Excel(name = "创建时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss") + @Excel(name = "创建时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss", type = Excel.Type.EXPORT) private Date createdTime; /** * 更新人 */ - @Excel(name = "更新人") + @Excel(name = "更新人", type = Excel.Type.EXPORT) private String updatedBy; /** * 更新时间 */ @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") - @Excel(name = "更新时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss") + @Excel(name = "更新时间", width = 30, dateFormat = "yyyy-MM-dd HH:mm:ss", type = Excel.Type.EXPORT) private Date updatedTime; public String getProductLineName() { diff --git a/aucma-base/src/main/java/com/aucma/base/mapper/BaseDeviceParamMapper.java b/aucma-base/src/main/java/com/aucma/base/mapper/BaseDeviceParamMapper.java index a98ec25..1698e39 100644 --- a/aucma-base/src/main/java/com/aucma/base/mapper/BaseDeviceParamMapper.java +++ b/aucma-base/src/main/java/com/aucma/base/mapper/BaseDeviceParamMapper.java @@ -58,4 +58,28 @@ public interface BaseDeviceParamMapper * @return 结果 */ public int deleteBaseDeviceParamByObjIds(Long[] objIds); + + /** + * 根据参数编号列表查询已存在的参数 + * + * @param paramCodes 参数编号列表 + * @return 设备参数集合 + */ + public List selectByParamCodes(List paramCodes); + + /** + * 批量新增设备参数 + * + * @param list 设备参数列表 + * @return 结果 + */ + public int batchInsertDeviceParam(List list); + + /** + * 批量更新设备参数 + * + * @param list 设备参数列表 + * @return 结果 + */ + public int batchUpdateDeviceParam(List list); } diff --git a/aucma-base/src/main/java/com/aucma/base/service/IBaseDeviceParamService.java b/aucma-base/src/main/java/com/aucma/base/service/IBaseDeviceParamService.java index accfbee..7c7b2b7 100644 --- a/aucma-base/src/main/java/com/aucma/base/service/IBaseDeviceParamService.java +++ b/aucma-base/src/main/java/com/aucma/base/service/IBaseDeviceParamService.java @@ -58,4 +58,14 @@ public interface IBaseDeviceParamService * @return 结果 */ public int deleteBaseDeviceParamByObjId(Long objId); + + /** + * 导入设备参数数据 + * + * @param baseDeviceParamList 设备参数数据列表 + * @param isUpdateSupport 是否更新支持,如果已存在,则进行更新数据 + * @param operName 操作用户 + * @return 结果 + */ + public String importDeviceParam(List baseDeviceParamList, Boolean isUpdateSupport, String operName); } diff --git a/aucma-base/src/main/java/com/aucma/base/service/impl/BaseDeviceParamServiceImpl.java b/aucma-base/src/main/java/com/aucma/base/service/impl/BaseDeviceParamServiceImpl.java index 78b410c..8a78d0e 100644 --- a/aucma-base/src/main/java/com/aucma/base/service/impl/BaseDeviceParamServiceImpl.java +++ b/aucma-base/src/main/java/com/aucma/base/service/impl/BaseDeviceParamServiceImpl.java @@ -1,11 +1,18 @@ package com.aucma.base.service.impl; +import java.util.ArrayList; +import java.util.Date; import java.util.List; +import java.util.Map; +import java.util.stream.Collectors; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; +import org.springframework.transaction.annotation.Transactional; import com.aucma.base.mapper.BaseDeviceParamMapper; import com.aucma.base.domain.BaseDeviceParam; import com.aucma.base.service.IBaseDeviceParamService; +import com.aucma.common.exception.ServiceException; +import com.aucma.common.utils.StringUtils; /** * 设备参数Service业务层处理 @@ -90,4 +97,119 @@ public class BaseDeviceParamServiceImpl implements IBaseDeviceParamService { return baseDeviceParamMapper.deleteBaseDeviceParamByObjId(objId); } + + /** + * 导入设备参数数据(批量优化版) + * + * @param baseDeviceParamList 设备参数数据列表 + * @param isUpdateSupport 是否更新支持,如果已存在,则进行更新数据 + * @param operName 操作用户 + * @return 结果 + */ + @Override + @Transactional(rollbackFor = Exception.class) + public String importDeviceParam(List baseDeviceParamList, Boolean isUpdateSupport, String operName) + { + if (StringUtils.isNull(baseDeviceParamList) || baseDeviceParamList.size() == 0) + { + throw new ServiceException("导入设备参数数据不能为空!"); + } + + // 1. 提取所有参数编号,一次性查询已存在的记录 + List paramCodes = baseDeviceParamList.stream() + .map(BaseDeviceParam::getParamCode) + .filter(StringUtils::isNotEmpty) + .collect(Collectors.toList()); + + // 2. 批量查询已存在的参数(paramCode -> 实体 映射) + // Oracle IN 子句限制1000个元素,分批查询(每批900个) + Map existMap = new java.util.HashMap<>(); + if (!paramCodes.isEmpty()) + { + int queryBatchSize = 900; + for (int i = 0; i < paramCodes.size(); i += queryBatchSize) + { + int end = Math.min(i + queryBatchSize, paramCodes.size()); + List batchCodes = paramCodes.subList(i, end); + List existList = baseDeviceParamMapper.selectByParamCodes(batchCodes); + for (BaseDeviceParam p : existList) + { + existMap.put(p.getParamCode(), p); + } + } + } + + // 3. 分类:新增列表、更新列表、跳过列表 + List insertList = new ArrayList<>(); + List updateList = new ArrayList<>(); + int skipNum = 0; + Date now = new Date(); + + for (BaseDeviceParam deviceParam : baseDeviceParamList) + { + if (StringUtils.isEmpty(deviceParam.getParamCode())) + { + continue; + } + BaseDeviceParam exist = existMap.get(deviceParam.getParamCode()); + if (exist == null) + { + // 新增 + deviceParam.setCreatedBy(operName); + deviceParam.setCreatedTime(now); + insertList.add(deviceParam); + } + else if (isUpdateSupport) + { + // 更新 + deviceParam.setObjId(exist.getObjId()); + deviceParam.setUpdatedBy(operName); + deviceParam.setUpdatedTime(now); + updateList.add(deviceParam); + } + else + { + skipNum++; + } + } + + // 4. 批量插入(分批处理,每批100条) + int insertCount = 0; + if (!insertList.isEmpty()) + { + int batchSize = 100; + for (int i = 0; i < insertList.size(); i += batchSize) + { + int end = Math.min(i + batchSize, insertList.size()); + List batch = insertList.subList(i, end); + baseDeviceParamMapper.batchInsertDeviceParam(batch); + insertCount += batch.size(); + } + } + + // 5. 批量更新(分批处理,每批100条) + int updateCount = 0; + if (!updateList.isEmpty()) + { + int batchSize = 100; + for (int i = 0; i < updateList.size(); i += batchSize) + { + int end = Math.min(i + batchSize, updateList.size()); + List batch = updateList.subList(i, end); + baseDeviceParamMapper.batchUpdateDeviceParam(batch); + updateCount += batch.size(); + } + } + + // 6. 返回结果 + StringBuilder resultMsg = new StringBuilder(); + resultMsg.append("导入完成!"); + resultMsg.append("
新增:" + insertCount + " 条"); + resultMsg.append("
更新:" + updateCount + " 条"); + if (skipNum > 0) + { + resultMsg.append("
跳过(已存在):" + skipNum + " 条"); + } + return resultMsg.toString(); + } } diff --git a/aucma-base/src/main/resources/mapper/base/BaseDeviceParamMapper.xml b/aucma-base/src/main/resources/mapper/base/BaseDeviceParamMapper.xml index 6faa578..4211b42 100644 --- a/aucma-base/src/main/resources/mapper/base/BaseDeviceParamMapper.xml +++ b/aucma-base/src/main/resources/mapper/base/BaseDeviceParamMapper.xml @@ -134,4 +134,49 @@ #{objId} + + + + + + INSERT INTO base_deviceparam (obj_id, param_code, param_name, param_network, param_address, + param_type, device_code, read_frequency, is_flag, created_by, created_time) + VALUES (seq_base_deviceparam.NEXTVAL, + #{item.paramCode,jdbcType=VARCHAR}, + #{item.paramName,jdbcType=VARCHAR}, + #{item.paramNetwork,jdbcType=VARCHAR}, + #{item.paramAddress,jdbcType=VARCHAR}, + #{item.paramType,jdbcType=VARCHAR}, + #{item.deviceCode,jdbcType=VARCHAR}, + #{item.readFrequency,jdbcType=NUMERIC}, + #{item.isFlag,jdbcType=NUMERIC}, + #{item.createdBy,jdbcType=VARCHAR}, + #{item.createdTime,jdbcType=TIMESTAMP}); + + + + + + UPDATE base_deviceparam + SET param_name = #{item.paramName,jdbcType=VARCHAR}, + param_network = #{item.paramNetwork,jdbcType=VARCHAR}, + param_address = #{item.paramAddress,jdbcType=VARCHAR}, + param_type = #{item.paramType,jdbcType=VARCHAR}, + device_code = #{item.deviceCode,jdbcType=VARCHAR}, + read_frequency = #{item.readFrequency,jdbcType=NUMERIC}, + is_flag = #{item.isFlag,jdbcType=NUMERIC}, + updated_by = #{item.updatedBy,jdbcType=VARCHAR}, + updated_time = #{item.updatedTime,jdbcType=TIMESTAMP} + WHERE param_code = #{item.paramCode,jdbcType=VARCHAR}; + + \ No newline at end of file diff --git a/pom.xml b/pom.xml index edb7581..b3d4d0e 100644 --- a/pom.xml +++ b/pom.xml @@ -94,6 +94,11 @@ + + org.apache.poi + poi + ${poi.version} + org.apache.poi poi-ooxml