6 changed files with 136 additions and 53 deletions
-
57src/main/java/com/gaotao/modules/base/service/Impl/ReportLabelListServiceImpl.java
-
16src/main/java/com/gaotao/modules/handlingunit/service/HandlingUnitIdGeneratorService.java
-
89src/main/java/com/gaotao/modules/handlingunit/service/impl/HandlingUnitIdGeneratorServiceImpl.java
-
4src/main/java/com/gaotao/modules/inspection/task/IfsInspectionHistoryTask.java
-
20src/main/java/com/gaotao/modules/po/service/impl/PoServiceImpl.java
-
3src/main/resources/mapper/base/BaseMapper.xml
@ -0,0 +1,16 @@ |
|||||
|
package com.gaotao.modules.handlingunit.service; |
||||
|
|
||||
|
/** |
||||
|
* 处理单元ID生成服务接口 |
||||
|
* 生成格式:P+site+YYYYMMDD+8位自增流水码 |
||||
|
* 流水码每天根据前缀从1开始计数 |
||||
|
*/ |
||||
|
public interface HandlingUnitIdGeneratorService { |
||||
|
|
||||
|
/** |
||||
|
* 生成新的处理单元ID |
||||
|
* @param site 工厂代码 |
||||
|
* @return 生成的处理单元ID,格式:P+site+YYYYMMDD+8位自增流水码 |
||||
|
*/ |
||||
|
String generateUnitId(String site); |
||||
|
} |
||||
@ -0,0 +1,89 @@ |
|||||
|
package com.gaotao.modules.handlingunit.service.impl; |
||||
|
|
||||
|
import com.gaotao.modules.handlingunit.service.HandlingUnitIdGeneratorService; |
||||
|
import lombok.extern.slf4j.Slf4j; |
||||
|
import org.springframework.beans.factory.annotation.Autowired; |
||||
|
import org.springframework.jdbc.core.JdbcTemplate; |
||||
|
import org.springframework.stereotype.Service; |
||||
|
import org.springframework.transaction.annotation.Propagation; |
||||
|
import org.springframework.transaction.annotation.Transactional; |
||||
|
|
||||
|
import java.time.LocalDate; |
||||
|
import java.time.format.DateTimeFormatter; |
||||
|
|
||||
|
/** |
||||
|
* 处理单元ID生成服务实现类 |
||||
|
* 生成格式:P+site+YYYYMMDD+8位自增流水码 |
||||
|
* 流水码每天根据前缀从1开始计数 |
||||
|
*/ |
||||
|
@Slf4j |
||||
|
@Service |
||||
|
public class HandlingUnitIdGeneratorServiceImpl implements HandlingUnitIdGeneratorService { |
||||
|
|
||||
|
@Autowired |
||||
|
private JdbcTemplate jdbcTemplate; |
||||
|
|
||||
|
/** |
||||
|
* 生成新的处理单元ID |
||||
|
* @param site 工厂代码 |
||||
|
* @return 生成的处理单元ID,格式:A+site+YYYYMMDD+8位自增流水码 |
||||
|
*/ |
||||
|
@Override |
||||
|
@Transactional(propagation = Propagation.REQUIRES_NEW) |
||||
|
public String generateUnitId(String site) { |
||||
|
// 生成当前日期字符串 YYYYMMDD |
||||
|
String dateStr = LocalDate.now().format(DateTimeFormatter.ofPattern("yyyyMMdd")); |
||||
|
|
||||
|
// 生成前缀:A+site+YYYYMMDD |
||||
|
String prefix = "A" + site + dateStr; |
||||
|
|
||||
|
// 获取下一个流水号(8位,从1开始每天重置) |
||||
|
int sequenceNo = getNextSequenceNumber(prefix); |
||||
|
|
||||
|
// 格式化为8位流水号(补齐前导零) |
||||
|
String formattedSequence = String.format("%08d", sequenceNo); |
||||
|
|
||||
|
// 组合完整的处理单元ID |
||||
|
String unitId = prefix + formattedSequence; |
||||
|
|
||||
|
log.debug("生成处理单元ID: site={}, prefix={}, sequence={}, unitId={}", |
||||
|
site, prefix, sequenceNo, unitId); |
||||
|
|
||||
|
return unitId; |
||||
|
} |
||||
|
|
||||
|
/** |
||||
|
* 获取下一个流水号 |
||||
|
* 使用数据库保证并发安全,每天重置计数 |
||||
|
* @param prefix 日期前缀 (P+site+YYYYMMDD) |
||||
|
* @return 下一个流水号 |
||||
|
*/ |
||||
|
private int getNextSequenceNumber(String prefix) { |
||||
|
try { |
||||
|
// 检查是否已存在当天的记录 |
||||
|
String checkSql = "SELECT COUNT(*) FROM handling_unit_sequence WHERE prefix = ?"; |
||||
|
Integer count = jdbcTemplate.queryForObject(checkSql, Integer.class, prefix); |
||||
|
|
||||
|
if (count == null || count == 0) { |
||||
|
// 不存在记录,插入新记录,流水号从1开始 |
||||
|
String insertSql = "INSERT INTO handling_unit_sequence (prefix, next_sequence, created_date) VALUES (?, 1, GETDATE())"; |
||||
|
jdbcTemplate.update(insertSql, prefix); |
||||
|
return 1; |
||||
|
} else { |
||||
|
// 存在记录,更新并返回下一个流水号 |
||||
|
String updateSql = "UPDATE handling_unit_sequence SET next_sequence = next_sequence + 1, modified_date = GETDATE() " + |
||||
|
"WHERE prefix = ?"; |
||||
|
jdbcTemplate.update(updateSql, prefix); |
||||
|
|
||||
|
// 获取更新后的流水号 |
||||
|
String selectSql = "SELECT next_sequence FROM handling_unit_sequence WHERE prefix = ?"; |
||||
|
Integer nextSequence = jdbcTemplate.queryForObject(selectSql, Integer.class, prefix); |
||||
|
return nextSequence != null ? nextSequence : 1; |
||||
|
} |
||||
|
} catch (Exception e) { |
||||
|
log.error("获取流水号失败,prefix={}, error={}", prefix, e.getMessage(), e); |
||||
|
// 发生错误时,尝试使用系统时间作为备用方案 |
||||
|
return (int) (System.currentTimeMillis() % 100000000); // 取后8位作为备用 |
||||
|
} |
||||
|
} |
||||
|
} |
||||
Write
Preview
Loading…
Cancel
Save
Reference in new issue