From b59406c5aff7272e74049e0addffdfa82c867882 Mon Sep 17 00:00:00 2001
From: zss <zss@example.com>
Date: 星期六, 22 八月 2026 18:17:11 +0800
Subject: [PATCH] refactor(purchase): 优化采购台账质检相关代码
---
src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java | 90 ++++++++++++++++++++++++++++++++++++++++++---
1 files changed, 84 insertions(+), 6 deletions(-)
diff --git a/src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java b/src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java
index 4f83c66..7d8373d 100644
--- a/src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java
+++ b/src/main/java/com/ruoyi/purchase/service/impl/PurchaseLedgerServiceImpl.java
@@ -22,10 +22,10 @@
import com.ruoyi.basic.pojo.ProductModel;
import com.ruoyi.basic.pojo.SupplierManage;
import com.ruoyi.basic.utils.FileUtil;
-import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.enums.ApprovalStatusEnum;
import com.ruoyi.common.enums.ReviewStatusEnum;
import com.ruoyi.common.enums.StockInQualifiedRecordTypeEnum;
+import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
@@ -129,6 +129,7 @@
if (purchaseLedger.getApprovalStatus() != null) {
queryWrapper.eq(PurchaseLedger::getApprovalStatus, purchaseLedger.getApprovalStatus());
}
+ queryWrapper.orderByDesc(PurchaseLedger::getEntryDate);
return purchaseLedgerMapper.selectList(queryWrapper);
}
@@ -164,6 +165,9 @@
purchaseLedger.setApprovalStatus(1);
// 3. 鏂板鎴栨洿鏂颁富琛�
if (purchaseLedger.getId() == null) {
+ if (!StringUtils.hasText(purchaseLedger.getPurchaseContractNumber())) {
+ purchaseLedger.setPurchaseContractNumber(generatePurchaseContractNo(purchaseLedger.getEntryDate()));
+ }
purchaseLedgerMapper.insert(purchaseLedger);
} else {
// 鍒犻櫎閲囪喘瀹℃牳锛岄噸鏂版彁浜�
@@ -979,11 +983,7 @@
salesLedgerProduct.setRegister(loginUser.getNickName());
salesLedgerProduct.setRegisterDate(LocalDateTime.now());
salesLedgerProduct.setApproveStatus(0);
- // 鏄惁璐ㄦ鍒ゆ柇
salesLedgerProduct.setIsChecked(salesLedgerProductImportDto.getIsChecked() == 1);
- if(salesLedgerProductImportDto.getIsChecked() == 1){
- addQualityInspect(salesLedger, salesLedgerProduct);
- }
salesLedgerProductMapper.insert(salesLedgerProduct);
}
// 閲囪喘瀹℃牳
@@ -993,8 +993,8 @@
return AjaxResult.success("瀵煎叆鎴愬姛");
} catch (Exception e) {
e.printStackTrace();
+ return AjaxResult.error("瀵煎叆澶辫触锛�" + e.getMessage());
}
- return AjaxResult.success("瀵煎叆澶辫触");
}
@Override
@@ -1073,4 +1073,82 @@
}
return sb.toString();
}
+
+ private static final String PURCHASE_LOCK_PREFIX = "purchase_contract_no:";
+ private static final long PURCHASE_LOCK_WAIT_TIMEOUT = 10;
+ private static final long PURCHASE_LOCK_EXPIRE_TIME = 30;
+
+ private String generatePurchaseContractNo(Date entryDate) {
+ LocalDate currentDate = entryDate != null ? DateUtils.toLocalDate(entryDate) : LocalDate.now();
+ String datePart = currentDate.format(DateTimeFormatter.BASIC_ISO_DATE);
+ String lockKey = PURCHASE_LOCK_PREFIX + datePart;
+ String lockValue = Thread.currentThread().getId() + "-" + System.nanoTime();
+
+ try {
+ long startWaitTime = System.currentTimeMillis();
+ while (System.currentTimeMillis() - startWaitTime < PURCHASE_LOCK_WAIT_TIMEOUT * 1000) {
+ Boolean locked = redisTemplate.opsForValue().setIfAbsent(lockKey, lockValue, PURCHASE_LOCK_EXPIRE_TIME, TimeUnit.SECONDS);
+ if (Boolean.TRUE.equals(locked)) {
+ break;
+ }
+ try {
+ Thread.sleep(100);
+ } catch (InterruptedException e) {
+ Thread.currentThread().interrupt();
+ throw new RuntimeException("鑾峰彇閿佹椂琚腑鏂�", e);
+ }
+ }
+
+ if (Boolean.FALSE.equals(redisTemplate.hasKey(lockKey))) {
+ throw new RuntimeException("鑾峰彇閲囪喘鍚堝悓缂栧彿鐢熸垚閿佸け璐ワ細瓒呮椂");
+ }
+
+ String prefix = "CG-" + datePart + "-";
+ List<PurchaseLedger> existingList = purchaseLedgerMapper.selectList(
+ new LambdaQueryWrapper<PurchaseLedger>()
+ .likeRight(PurchaseLedger::getPurchaseContractNumber, prefix)
+ .select(PurchaseLedger::getPurchaseContractNumber));
+ List<Integer> existingSequences = existingList.stream()
+ .map(PurchaseLedger::getPurchaseContractNumber)
+ .filter(Objects::nonNull)
+ .map(no -> {
+ int lastDash = no.lastIndexOf('-');
+ if (lastDash >= 0) {
+ try {
+ return Integer.parseInt(no.substring(lastDash + 1));
+ } catch (NumberFormatException e) {
+ return 0;
+ }
+ }
+ return 0;
+ })
+ .collect(Collectors.toList());
+ int nextSequence = findFirstMissingSequence(existingSequences);
+
+ return prefix + String.format("%03d", nextSequence);
+ } finally {
+ String luaScript = "if redis.call('GET', KEYS[1]) == ARGV[1] then return redis.call('DEL', KEYS[1]) else return 0 end";
+ redisTemplate.execute(
+ new org.springframework.data.redis.core.script.DefaultRedisScript<>(luaScript, Long.class),
+ Collections.singletonList(lockKey),
+ lockValue
+ );
+ }
+ }
+
+ private int findFirstMissingSequence(List<Integer> sequences) {
+ if (sequences.isEmpty()) {
+ return 1;
+ }
+ sequences.sort(Integer::compareTo);
+ int next = 1;
+ for (int seq : sequences) {
+ if (seq == next) {
+ next++;
+ } else if (seq > next) {
+ break;
+ }
+ }
+ return next;
+ }
}
--
Gitblit v1.9.3