fix(device): 优化设备编号生成逻辑,兼容多种编号格式

- 获取设备编号时,支持格式如E00001和SJ_2等
- 增加对空或无数字编号的处理,返回默认编号E00001
- 通过正则提取数字部分,数字自增后格式化为5位编号
- 捕获解析异常,避免程序崩溃并返回默认编号
- 调整SQL查询条件,确保条件字段加上表别名dp,避免歧义
master
zangch@mesnac.com 2 weeks ago
parent 86fa02175b
commit 79c23c0874

@ -92,20 +92,34 @@ public class BaseDeviceLedgerServiceImpl implements IBaseDeviceLedgerService
}
/**
*
*
* E00001SJ_2
* @return
*/
@Override
public String getDeviceCode() {
String deviceCode = baseDeviceLedgerMapper.getDeviceCode();
String numericPart = deviceCode.substring(1);
// 将数字部分转换为整数并加1
int incrementedNumber = Integer.parseInt(numericPart) + 1;
// 格式化加1后的数字部分确保总长度为5位
String formattedNumber = String.format("%05d", incrementedNumber);
// 拼接 'E' 和格式化后的数字部分
String newDeviceCode = "E" + formattedNumber;
return newDeviceCode;
// 如果没有设备记录,返回默认编号
if (deviceCode == null || deviceCode.isEmpty()) {
return "E00001";
}
// 尝试提取数字部分
String numericPart = deviceCode.replaceAll("[^0-9]", "");
if (numericPart.isEmpty()) {
// 没有数字部分,返回默认编号
return "E00001";
}
try {
// 将数字部分转换为整数并加1
int incrementedNumber = Integer.parseInt(numericPart) + 1;
// 格式化加1后的数字部分确保总长度为5位
String formattedNumber = String.format("%05d", incrementedNumber);
// 拼接 'E' 和格式化后的数字部分
return "E" + formattedNumber;
} catch (NumberFormatException e) {
// 解析失败,返回默认编号
return "E00001";
}
}
}

@ -46,20 +46,18 @@
<select id="selectBaseDeviceParamList" parameterType="BaseDeviceParam" resultMap="BaseDeviceParamResult">
<include refid="selectBaseDeviceParamVo"/>
<where>
<if test="paramCode != null and paramCode != ''">and param_code = #{paramCode}</if>
<if test="paramName != null and paramName != ''">and param_name like concat(concat('%', #{paramName}),
'%')
</if>
<if test="paramNetwork != null and paramNetwork != ''">and param_network = #{paramNetwork}</if>
<if test="paramAddress != null and paramAddress != ''">and param_address = #{paramAddress}</if>
<if test="paramType != null and paramType != ''">and param_type = #{paramType}</if>
<if test="deviceCode != null and deviceCode != ''">and device_code = #{deviceCode}</if>
<if test="readFrequency != null ">and read_frequency = #{readFrequency}</if>
<if test="isFlag != null ">and is_flag = #{isFlag}</if>
<if test="createdBy != null and createdBy != ''">and created_by = #{createdBy}</if>
<if test="createdTime != null ">and created_time = #{createdTime}</if>
<if test="updatedBy != null and updatedBy != ''">and updated_by = #{updatedBy}</if>
<if test="updatedTime != null ">and updated_time = #{updatedTime}</if>
<if test="paramCode != null and paramCode != ''">and dp.param_code = #{paramCode}</if>
<if test="paramName != null and paramName != ''">and dp.param_name like concat(concat('%', #{paramName}), '%')</if>
<if test="paramNetwork != null and paramNetwork != ''">and dp.param_network = #{paramNetwork}</if>
<if test="paramAddress != null and paramAddress != ''">and dp.param_address = #{paramAddress}</if>
<if test="paramType != null and paramType != ''">and dp.param_type = #{paramType}</if>
<if test="deviceCode != null and deviceCode != ''">and dp.device_code = #{deviceCode}</if>
<if test="readFrequency != null ">and dp.read_frequency = #{readFrequency}</if>
<if test="isFlag != null ">and dp.is_flag = #{isFlag}</if>
<if test="createdBy != null and createdBy != ''">and dp.created_by = #{createdBy}</if>
<if test="createdTime != null ">and dp.created_time = #{createdTime}</if>
<if test="updatedBy != null and updatedBy != ''">and dp.updated_by = #{updatedBy}</if>
<if test="updatedTime != null ">and dp.updated_time = #{updatedTime}</if>
</where>
</select>

Loading…
Cancel
Save