feat(deviceParam): 支持设备参数批量导入与导入模板下载

- 在BaseDeviceParam实体Excel注解中添加导出类型限制
- 在BaseDeviceParamController新增导入设备参数列表接口及导入模板下载接口
- 在BaseDeviceParamMapper中新增批量查询、批量新增和批量更新方法
- 实现BaseDeviceParamServiceImpl中设备参数批量导入逻辑,支持分批处理和更新支持
- 定义IBaseDeviceParamService接口中导入设备参数方法声明
- pom.xml中添加Apache POI依赖支持Excel处理功能
master
zangch@mesnac.com 2 weeks ago
parent 79c23c0874
commit 5ebc36ed5e

@ -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<BaseDeviceParam> util = new ExcelUtil<BaseDeviceParam>(BaseDeviceParam.class);
List<BaseDeviceParam> 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<BaseDeviceParam> util = new ExcelUtil<BaseDeviceParam>(BaseDeviceParam.class);
util.importTemplateExcel(response, "设备参数数据" );
}
/**
*
*/

@ -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() {

@ -58,4 +58,28 @@ public interface BaseDeviceParamMapper
* @return
*/
public int deleteBaseDeviceParamByObjIds(Long[] objIds);
/**
*
*
* @param paramCodes
* @return
*/
public List<BaseDeviceParam> selectByParamCodes(List<String> paramCodes);
/**
*
*
* @param list
* @return
*/
public int batchInsertDeviceParam(List<BaseDeviceParam> list);
/**
*
*
* @param list
* @return
*/
public int batchUpdateDeviceParam(List<BaseDeviceParam> list);
}

@ -58,4 +58,14 @@ public interface IBaseDeviceParamService
* @return
*/
public int deleteBaseDeviceParamByObjId(Long objId);
/**
*
*
* @param baseDeviceParamList
* @param isUpdateSupport
* @param operName
* @return
*/
public String importDeviceParam(List<BaseDeviceParam> baseDeviceParamList, Boolean isUpdateSupport, String operName);
}

@ -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<BaseDeviceParam> baseDeviceParamList, Boolean isUpdateSupport, String operName)
{
if (StringUtils.isNull(baseDeviceParamList) || baseDeviceParamList.size() == 0)
{
throw new ServiceException("导入设备参数数据不能为空!");
}
// 1. 提取所有参数编号,一次性查询已存在的记录
List<String> paramCodes = baseDeviceParamList.stream()
.map(BaseDeviceParam::getParamCode)
.filter(StringUtils::isNotEmpty)
.collect(Collectors.toList());
// 2. 批量查询已存在的参数paramCode -> 实体 映射)
// Oracle IN 子句限制1000个元素分批查询每批900个
Map<String, BaseDeviceParam> 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<String> batchCodes = paramCodes.subList(i, end);
List<BaseDeviceParam> existList = baseDeviceParamMapper.selectByParamCodes(batchCodes);
for (BaseDeviceParam p : existList)
{
existMap.put(p.getParamCode(), p);
}
}
}
// 3. 分类:新增列表、更新列表、跳过列表
List<BaseDeviceParam> insertList = new ArrayList<>();
List<BaseDeviceParam> 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<BaseDeviceParam> 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<BaseDeviceParam> batch = updateList.subList(i, end);
baseDeviceParamMapper.batchUpdateDeviceParam(batch);
updateCount += batch.size();
}
}
// 6. 返回结果
StringBuilder resultMsg = new StringBuilder();
resultMsg.append("导入完成!");
resultMsg.append("<br/>新增:" + insertCount + " 条");
resultMsg.append("<br/>更新:" + updateCount + " 条");
if (skipNum > 0)
{
resultMsg.append("<br/>跳过(已存在):" + skipNum + " 条");
}
return resultMsg.toString();
}
}

@ -134,4 +134,49 @@
#{objId}
</foreach>
</delete>
<select id="selectByParamCodes" resultMap="BaseDeviceParamResult">
select obj_id, param_code, param_name, param_network, param_address,
param_type, device_code, read_frequency, is_flag,
created_by, created_time, updated_by, updated_time
from base_deviceparam
where param_code in
<foreach item="paramCode" collection="list" open="(" separator="," close=")">
#{paramCode}
</foreach>
</select>
<insert id="batchInsertDeviceParam" parameterType="java.util.List">
<foreach item="item" collection="list" open="BEGIN" close="END;" separator="">
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});
</foreach>
</insert>
<update id="batchUpdateDeviceParam" parameterType="java.util.List">
<foreach item="item" collection="list" open="BEGIN " close=" END;" separator=" ">
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};
</foreach>
</update>
</mapper>

@ -94,6 +94,11 @@
</dependency>
<!-- excel工具 -->
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi</artifactId>
<version>${poi.version}</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>poi-ooxml</artifactId>

Loading…
Cancel
Save