From 21aab4f17e8d2a645e4b42342457704ef3889ac4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B5=B5=E8=BE=89?= <2830717623@qq.com> Date: Tue, 23 Jun 2026 10:27:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=89=E6=96=B9=E4=BF=A1=E7=94=A8=E4=BF=AE?= =?UTF-8?q?=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../UserShipperApplicationService.java | 52 +++++++++++++------ 1 file changed, 36 insertions(+), 16 deletions(-) diff --git a/mhd-user-center/src/main/java/com/mhd/user/application/service/UserShipperApplicationService.java b/mhd-user-center/src/main/java/com/mhd/user/application/service/UserShipperApplicationService.java index 6182e5ceb..841504787 100644 --- a/mhd-user-center/src/main/java/com/mhd/user/application/service/UserShipperApplicationService.java +++ b/mhd-user-center/src/main/java/com/mhd/user/application/service/UserShipperApplicationService.java @@ -1629,10 +1629,12 @@ public class UserShipperApplicationService { } // 遍历每个组织code,分别调用接口 + int orgIndex = 0; // 组织循环计数器 for (Long orgCode : orgCodeList) { String orgcode = userShipperMapper.getOrgNcCode(orgCode); if (StringUtils.isEmpty(orgcode)) { log.warn("组织NC编码为空,跳过查询,custcode={},orgCode={}", userShipperEntity.getCustomerNcCode(), orgCode); + orgIndex++; continue; } @@ -1720,7 +1722,6 @@ public class UserShipperApplicationService { userShipperEntity1.setSettlementCurrency(firstLimitData.getString("currencycode")); userShipperEntity1.setCreditAmount(firstLimitData.getBigDecimal("nbalancemny")); - userShipperMapper.updateById(userShipperEntity1); log.info("更新货主额度同步成功,custcode={},orgcode={},shipperId={},orgCode组织数={}", userShipperEntity1.getCustomerNcCode(), orgcode, userShipperEntity1.getShipperId(), orgCodeArray.size()); } else { log.info("赊销额度查询无limitdatas返回,跳过额度更新,custcode={},orgcode={}", userShipperEntity1.getCustomerNcCode(), orgcode); @@ -1729,11 +1730,14 @@ public class UserShipperApplicationService { // 同步协议明细(agreedaydatas),与limitdatas独立 com.alibaba.fastjson.JSONArray agreedaydatas = dataJson.getJSONArray("agreedaydatas"); if (agreedaydatas != null && !agreedaydatas.isEmpty()) { - // 先删除该货主已有的协议明细,避免重复 - agreementDetailsMapper.delete(new LambdaQueryWrapper() - .eq(AgreementDetails::getShipperId, userShipperEntity1.getShipperId())); - int i; - for (i = 0; i < agreedaydatas.size(); i++) { + // 只在第一次循环时删除该货主的所有协议明细,避免重复 + if (orgIndex == 0) { + agreementDetailsMapper.delete(new LambdaQueryWrapper() + .eq(AgreementDetails::getShipperId, userShipperEntity1.getShipperId())); + log.info("第一次循环,删除货主所有协议明细,shipperId={}", userShipperEntity1.getShipperId()); + } + + for (int i = 0; i < agreedaydatas.size(); i++) { com.alibaba.fastjson.JSONObject agreeItem = agreedaydatas.getJSONObject(i); AgreementDetails agreementDetails = new AgreementDetails(); agreementDetails.setShipperId(userShipperEntity1.getShipperId()); @@ -1746,8 +1750,6 @@ public class UserShipperApplicationService { agreementDetails.setPtype(agreeItem.getString("ptype")); agreementDetailsMapper.insert(agreementDetails); } - userShipperEntity1.setAgreementCount(i); - userShipperMapper.updateById(userShipperEntity1); log.info("同步协议明细成功,custcode={},orgcode={},shipperId={},共{}条", userShipperEntity1.getCustomerNcCode(), orgcode, userShipperEntity1.getShipperId(), agreedaydatas.size()); } else { log.info("赊销额度查询无agreedaydatas返回,跳过协议明细同步,custcode={},orgcode={}", userShipperEntity1.getCustomerNcCode(), orgcode); @@ -1763,8 +1765,16 @@ public class UserShipperApplicationService { } } catch (Exception e) { log.error("查询赊销额度异常,custcode={},orgcode={}", userShipperEntity1.getCustomerNcCode(), orgcode, e); + } finally { + orgIndex++; // 计数器递增 } } + + // 所有组织循环完成后,统一更新数据库(只更新一次) + if (userShipperEntity1 != null) { + userShipperMapper.updateById(userShipperEntity1); + log.info("批量更新货主额度完成,custcode={},shipperId={}", userShipperEntity1.getCustomerNcCode(), userShipperEntity1.getShipperId()); + } } return AjaxResult.success(allResults); } catch (Exception e) { @@ -1859,10 +1869,12 @@ public class UserShipperApplicationService { } // 遍历每个组织code,分别调用接口 + int orgIndex = 0; // 组织循环计数器 for (Long orgCode : orgCodeList) { String orgcode = userShipperMapper.getOrgNcCode(orgCode); if (StringUtils.isEmpty(orgcode)) { log.warn("组织NC编码为空,跳过查询,custcode={},orgCode={}", userShipperEntity.getCustomerNcCode(), orgCode); + orgIndex++; continue; } @@ -1950,7 +1962,6 @@ public class UserShipperApplicationService { userShipperEntity1.setSettlementCurrency(firstLimitData.getString("currencycode")); userShipperEntity1.setCreditAmount(firstLimitData.getBigDecimal("nbalancemny")); - userShipperMapper.updateById(userShipperEntity1); log.info("更新货主额度同步成功,custcode={},orgcode={},shipperId={},orgCode组织数={}", userShipperEntity1.getCustomerNcCode(), orgcode, userShipperEntity1.getShipperId(), orgCodeArray.size()); } else { log.info("赊销额度查询无limitdatas返回,跳过额度更新,custcode={},orgcode={}", userShipperEntity1.getCustomerNcCode(), orgcode); @@ -1959,11 +1970,14 @@ public class UserShipperApplicationService { // 同步协议明细(agreedaydatas),与limitdatas独立 com.alibaba.fastjson.JSONArray agreedaydatas = dataJson.getJSONArray("agreedaydatas"); if (agreedaydatas != null && !agreedaydatas.isEmpty()) { - // 先删除该货主已有的协议明细,避免重复 - agreementDetailsMapper.delete(new LambdaQueryWrapper() - .eq(AgreementDetails::getShipperId, userShipperEntity1.getShipperId())); - int i; - for (i = 0; i < agreedaydatas.size(); i++) { + // 只在第一次循环时删除该货主的所有协议明细,避免重复 + if (orgIndex == 0) { + agreementDetailsMapper.delete(new LambdaQueryWrapper() + .eq(AgreementDetails::getShipperId, userShipperEntity1.getShipperId())); + log.info("第一次循环,删除货主所有协议明细,shipperId={}", userShipperEntity1.getShipperId()); + } + + for (int i = 0; i < agreedaydatas.size(); i++) { com.alibaba.fastjson.JSONObject agreeItem = agreedaydatas.getJSONObject(i); AgreementDetails agreementDetails = new AgreementDetails(); agreementDetails.setShipperId(userShipperEntity1.getShipperId()); @@ -1976,8 +1990,6 @@ public class UserShipperApplicationService { agreementDetails.setPtype(agreeItem.getString("ptype")); agreementDetailsMapper.insert(agreementDetails); } - userShipperEntity1.setAgreementCount(i); - userShipperMapper.updateById(userShipperEntity1); log.info("同步协议明细成功,custcode={},orgcode={},shipperId={},共{}条", userShipperEntity1.getCustomerNcCode(), orgcode, userShipperEntity1.getShipperId(), agreedaydatas.size()); } else { log.info("赊销额度查询无agreedaydatas返回,跳过协议明细同步,custcode={},orgcode={}", userShipperEntity1.getCustomerNcCode(), orgcode); @@ -1993,8 +2005,16 @@ public class UserShipperApplicationService { } } catch (Exception e) { log.error("查询赊销额度异常,custcode={},orgcode={}", userShipperEntity1.getCustomerNcCode(), orgcode, e); + } finally { + orgIndex++; // 计数器递增 } } + + // 所有组织循环完成后,统一更新数据库(只更新一次) + if (userShipperEntity1 != null) { + userShipperMapper.updateById(userShipperEntity1); + log.info("批量更新货主额度完成,custcode={},shipperId={}", userShipperEntity1.getCustomerNcCode(), userShipperEntity1.getShipperId()); + } } return AjaxResult.success(allResults); } catch (Exception e) {