From ef7458a6eb8ad72f45b65a4e417a9d04f5015246 Mon Sep 17 00:00:00 2001
From: gongchunyi <deslre0381@gmail.com>
Date: 星期六, 14 三月 2026 11:47:08 +0800
Subject: [PATCH] feat: 根据宜搭表单 ID 获取数据工具类
---
src/main/java/com/ruoyi/production/service/impl/ProductMaterialServiceImpl.java | 457 ++++++++++++++++++++++++++++++++++++---------------------
1 files changed, 288 insertions(+), 169 deletions(-)
diff --git a/src/main/java/com/ruoyi/production/service/impl/ProductMaterialServiceImpl.java b/src/main/java/com/ruoyi/production/service/impl/ProductMaterialServiceImpl.java
index 7367dc7..678cfd2 100644
--- a/src/main/java/com/ruoyi/production/service/impl/ProductMaterialServiceImpl.java
+++ b/src/main/java/com/ruoyi/production/service/impl/ProductMaterialServiceImpl.java
@@ -1,33 +1,36 @@
package com.ruoyi.production.service.impl;
-import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
+import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
-import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.framework.config.AliDingConfig;
+import com.ruoyi.framework.util.AliDingUtils;
+import com.ruoyi.production.dto.ProductMaterialDto;
+import com.ruoyi.production.dto.ProductMaterialGroupDto;
import com.ruoyi.production.enums.MaterialConfigTypeEnum;
import com.ruoyi.production.mapper.ProductMaterialMapper;
import com.ruoyi.production.pojo.ProductMaterial;
import com.ruoyi.production.pojo.ProductMaterialConfig;
+import com.ruoyi.production.pojo.ProductMaterialSku;
import com.ruoyi.production.service.ProductMaterialConfigService;
import com.ruoyi.production.service.ProductMaterialService;
+import com.ruoyi.production.service.ProductMaterialSkuService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
-import java.nio.charset.StandardCharsets;
import java.time.LocalDateTime;
-import java.time.OffsetDateTime;
-import java.time.ZoneId;
-import java.time.format.DateTimeFormatter;
-import java.time.format.DateTimeParseException;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
import java.util.concurrent.locks.ReentrantLock;
+import java.util.stream.Collectors;
/**
* <br>
@@ -48,17 +51,22 @@
@Autowired
private ProductMaterialConfigService productMaterialConfigService;
+ @Autowired
+ private ProductMaterialSkuService productMaterialSkuService;
+
/**
* 鍚屾閿侊紝闃叉鎵嬪姩鍜屽畾鏃朵换鍔″悓鏃舵墽琛�
*/
private final ReentrantLock syncLock = new ReentrantLock();
@Override
+ @Transactional(rollbackFor = Exception.class)
public void loadProductMaterialData() {
syncProductMaterialData(1);
}
@Override
+ @Transactional(rollbackFor = Exception.class)
public void syncProductMaterialJob() {
syncProductMaterialData(2);
}
@@ -74,64 +82,46 @@
}
try {
- // 鑾峰彇 AccessToken
- String accessToken = getAccessToken();
- if (StringUtils.isEmpty(accessToken)) {
+
+
+ JSONArray searchConditions = new JSONArray();
+ JSONObject statusCondition = new JSONObject();
+ statusCondition.put("key", "processInstanceStatus");
+ JSONArray statusValueArray = new JSONArray();
+ statusValueArray.add("COMPLETED");
+ statusCondition.put("value", statusValueArray);
+ statusCondition.put("type", "ARRAY");
+ statusCondition.put("operator", "in");
+ statusCondition.put("componentName", "SelectField");
+ searchConditions.add(statusCondition);
+
+ JSONObject resultCondition = new JSONObject();
+ resultCondition.put("key", "processApprovedResult");
+ JSONArray resultValueArray = new JSONArray();
+ resultValueArray.add("agree");
+ resultCondition.put("value", resultValueArray);
+ resultCondition.put("type", "ARRAY");
+ resultCondition.put("operator", "in");
+ resultCondition.put("componentName", "SelectField");
+ searchConditions.add(resultCondition);
+
+ String searchFieldJson = searchConditions.toJSONString();
+
+ JSONArray dataArr = AliDingUtils.getFormDataList(aliDingConfig, aliDingConfig.getMaterialCodeFormUuid(), searchFieldJson, productMaterialSkuService, ProductMaterialSku::getFormModifiedTime);
+
+ if (dataArr.isEmpty()) {
+ log.info("娌℃湁鏇村鏂版暟鎹渶瑕佸悓姝�");
return;
}
- // 鑾峰彇鏈湴鏈�鍚庡悓姝ユ椂闂�
- LocalDateTime lastSyncTime = getLastSyncTime();
- log.info("寮�濮嬬墿鏂欑紪鐮佸閲忓悓姝ワ紝鏈湴鏈�鍚庝慨鏀规椂闂�: {}", lastSyncTime);
-
- int pageNumber = 1;
- int pageSize = 50;
- boolean hasMore = true;
- int totalSynced = 0;
-
- while (hasMore) {
- // 鏌ヨ鍙傛暟
- JSONObject searchParam = buildSearchParam(lastSyncTime, pageNumber, pageSize);
-
- // 璋冪敤瀹滄惌鎺ュ彛鎷夊彇鏁版嵁
- String dataRes = HttpUtils.sendPostJson(
- aliDingConfig.getSearchFormDataUrl(),
- searchParam.toJSONString(),
- StandardCharsets.UTF_8.name(),
- null,
- accessToken
- );
-
- if (StringUtils.isEmpty(dataRes)) {
- log.warn("绗� {} 椤垫媺鍙栨暟鎹负绌�", pageNumber);
- break;
- }
-
- JSONObject resultObj = JSON.parseObject(dataRes);
- JSONArray dataArr = resultObj.getJSONArray("data");
- Integer totalCount = resultObj.getInteger("totalCount");
-
- if (dataArr == null || dataArr.isEmpty()) {
- log.info("娌℃湁鏇村鏂版暟鎹渶瑕佸悓姝�");
- break;
- }
-
- // 瑙f瀽骞朵繚瀛樻暟鎹�
- List<ProductMaterial> list = parseProductMaterials(dataArr, totalCount);
- if (!list.isEmpty()) {
- // 澶勭悊鏇存柊鎴栨柊澧�
- int affected = processSaveOrUpdate(list);
- totalSynced += affected;
- }
-
- // 鍒ゆ柇鏄惁杩樻湁涓嬩竴椤�
- hasMore = (pageNumber * pageSize) < totalCount;
- pageNumber++;
-
- log.info("姝e湪鍚屾绗� {} 椤碉紝褰撳墠宸插悓姝� {}/{}", pageNumber - 1, totalSynced, totalCount);
+ // 瑙f瀽骞朵繚瀛樻暟鎹�
+ List<ProductMaterialSku> list = parseProductMaterials(dataArr, dataArr.size());
+ if (!list.isEmpty()) {
+ // 澶勭悊鏇存柊鎴栨柊澧�
+ int affected = processSaveOrUpdate(list);
+ log.info("鐗╂枡鏁版嵁鍚屾瀹屾垚锛屽叡鍚屾 {} 鏉℃暟鎹�", affected);
}
- log.info("鐗╂枡鏁版嵁鍚屾瀹屾垚锛屽叡鍚屾 {} 鏉℃暟鎹�", totalSynced);
} catch (Exception e) {
log.error("鍚屾鐗╂枡缂栫爜寮傚父", e);
} finally {
@@ -140,73 +130,8 @@
}
}
- private String getAccessToken() {
- String params = "appkey=" + aliDingConfig.getAppKey()
- + "&appsecret=" + aliDingConfig.getAppSecret();
- String tokenRes = HttpUtils.sendGet(aliDingConfig.getAccessTokenUrl(), params);
- JSONObject tokenObj = JSON.parseObject(tokenRes);
- String accessToken = tokenObj.getString("access_token");
- if (StringUtils.isEmpty(accessToken)) {
- log.error("鑾峰彇閽夐拤AccessToken澶辫触: {}", tokenRes);
- }
- return accessToken;
- }
-
- private LocalDateTime getLastSyncTime() {
- LambdaQueryWrapper<ProductMaterial> queryWrapper = new LambdaQueryWrapper<>();
- queryWrapper.orderByDesc(ProductMaterial::getFormModifiedTime).last("LIMIT 1");
- ProductMaterial lastRecord = this.getOne(queryWrapper);
- return lastRecord != null ? lastRecord.getFormModifiedTime() : null;
- }
-
- private JSONObject buildSearchParam(LocalDateTime lastSyncTime, int pageNumber, int pageSize) {
- JSONObject searchParam = new JSONObject();
- searchParam.put("appType", aliDingConfig.getAppType());
- searchParam.put("systemToken", aliDingConfig.getSystemToken());
- searchParam.put("userId", aliDingConfig.getUserId());
- searchParam.put("formUuid", aliDingConfig.getMaterialCodeFormUuid());
- searchParam.put("currentPage", pageNumber);
- searchParam.put("pageSize", pageSize);
-
- JSONArray searchConditions = new JSONArray();
- JSONObject statusCondition = new JSONObject();
- statusCondition.put("key", "processInstanceStatus");
- JSONArray statusValueArray = new JSONArray();
- statusValueArray.add("COMPLETED");
- statusCondition.put("value", statusValueArray);
- statusCondition.put("type", "ARRAY");
- statusCondition.put("operator", "in");
- statusCondition.put("componentName", "SelectField");
- searchConditions.add(statusCondition);
-
- JSONObject resultCondition = new JSONObject();
- resultCondition.put("key", "processApprovedResult");
- JSONArray resultValueArray = new JSONArray();
- resultValueArray.add("agree");
- resultCondition.put("value", resultValueArray);
- resultCondition.put("type", "ARRAY");
- resultCondition.put("operator", "in");
- resultCondition.put("componentName", "SelectField");
- searchConditions.add(resultCondition);
-
- searchParam.put("searchFieldJson", searchConditions.toJSONString());
- searchParam.put("orderConfigJson", "{\"gmt_modified\":\"+\"}");
-
- if (lastSyncTime != null) {
- String startTime = lastSyncTime.plusSeconds(1).atZone(ZoneId.systemDefault())
- .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
- searchParam.put("modifiedFromTimeGMT", startTime);
- }
-
- String endTime = LocalDateTime.now().atZone(ZoneId.systemDefault())
- .format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
- searchParam.put("modifiedToTimeGMT", endTime);
-
- return searchParam;
- }
-
- private List<ProductMaterial> parseProductMaterials(JSONArray dataArr, Integer totalCount) {
- List<ProductMaterial> list = new ArrayList<>();
+ private List<ProductMaterialSku> parseProductMaterials(JSONArray dataArr, Integer totalCount) {
+ List<ProductMaterialSku> list = new ArrayList<>();
LocalDateTime now = LocalDateTime.now();
for (int i = 0; i < dataArr.size(); i++) {
@@ -214,47 +139,78 @@
String formInstanceId = item.getString("formInstanceId");
JSONObject originator = item.getJSONObject("originator");
- String originatorName = originator != null && originator.containsKey("userName")
- ? originator.getJSONObject("userName").getString("nameInChinese") : "鏈煡";
-
+ String originatorName = originator != null && originator.containsKey("userName") ? originator.getJSONObject("userName").getString("nameInChinese") : "鏈煡";
JSONObject formData = item.getJSONObject("formData");
+ // 澶勭悊鐗╂枡涓昏〃鏁版嵁
ProductMaterial material = new ProductMaterial();
-
- material.setFormInstanceId(formInstanceId);
- material.setIdentifierCode(formData.getString("textField_l92h77ju"));
- material.setMaterialCode(formData.getString("textField_l92f36f2"));
material.setMaterialName(formData.getString("textField_l92f36f5"));
- material.setSpecification(formData.getString("textField_l92f36f6"));
material.setBaseUnit(formData.getString("textField_la147lnw"));
- material.setMaterialAttribute(formData.getString("selectField_la14k51j"));
- material.setFinishedProductName(formData.getString("radioField_lbkk2nn2"));
material.setRemark(formData.getString("textareaField_l92f36f9"));
- // 澶勭悊鐗╂枡绫诲瀷鍜屽瓨璐х被鍒�
String materialType = formData.getString("selectField_l92f36fb");
String inventoryCat = formData.getString("selectField_la154noy");
material.setMaterialTypeId(getOrCreateConfigId(materialType, MaterialConfigTypeEnum.MATERIAL_TYPE.name()));
material.setInventoryCategoryId(getOrCreateConfigId(inventoryCat, MaterialConfigTypeEnum.INVENTORY_CAT.name()));
- material.setOriginatorName(originatorName);
- material.setOriginatorOrg("瀹佸涓垱缁胯兘瀹炰笟闆嗗洟鏈夐檺鍏徃");
+ Long materialId = getOrCreateMaterial(material);
- material.setFormModifiedTime(parseUtcTime(item.getString("modifiedTimeGMT")));
- material.setCreateTime(now);
- material.setUpdateTime(now);
+ // 澶勭悊鐗╂枡瑙勬牸鏁版嵁
+ ProductMaterialSku sku = new ProductMaterialSku();
+ sku.setMaterialId(materialId);
+ sku.setFormInstanceId(formInstanceId);
+ sku.setIdentifierCode(formData.getString("textField_l92h77ju"));
+ sku.setMaterialCode(formData.getString("textField_l92f36f2"));
+ sku.setSpecification(formData.getString("textField_l92f36f6"));
+ sku.setSupplyType(formData.getString("selectField_la14k51j"));
+ sku.setOriginatorName(originatorName);
+ sku.setOriginatorOrg("瀹佸涓垱缁胯兘瀹炰笟闆嗗洟鏈夐檺鍏徃");
+ sku.setFormModifiedTime(AliDingUtils.parseUtcTime(item.getString("modifiedTimeGMT")));
+ sku.setCreateTime(now);
+ sku.setUpdateTime(now);
- list.add(material);
+ list.add(sku);
}
return list;
+ }
+
+ private Long getOrCreateMaterial(ProductMaterial material) {
+ LambdaQueryWrapper<ProductMaterial> queryWrapper = new LambdaQueryWrapper<>();
+ queryWrapper.eq(ProductMaterial::getMaterialName, material.getMaterialName());
+
+ ProductMaterial exist = this.getOne(queryWrapper);
+ if (exist == null) {
+ material.setCreateTime(LocalDateTime.now());
+ material.setUpdateTime(LocalDateTime.now());
+ this.save(material);
+ return material.getId();
+ } else {
+ // 濡傛灉宸插瓨鍦紝浣嗗叧閿睘鎬у彂鐢熷彉鍖栵紝鍒欒繘琛屾洿鏂帮紙浠ュ疁鎼暟鎹负鍑嗭級
+ boolean needUpdate = false;
+ if (material.getMaterialTypeId() != null && !material.getMaterialTypeId().equals(exist.getMaterialTypeId())) {
+ exist.setMaterialTypeId(material.getMaterialTypeId());
+ needUpdate = true;
+ }
+ if (material.getInventoryCategoryId() != null && !material.getInventoryCategoryId().equals(exist.getInventoryCategoryId())) {
+ exist.setInventoryCategoryId(material.getInventoryCategoryId());
+ needUpdate = true;
+ }
+ if (StringUtils.isNotEmpty(material.getBaseUnit()) && !material.getBaseUnit().equals(exist.getBaseUnit())) {
+ exist.setBaseUnit(material.getBaseUnit());
+ needUpdate = true;
+ }
+ if (needUpdate) {
+ exist.setUpdateTime(LocalDateTime.now());
+ this.updateById(exist);
+ }
+ return exist.getId();
+ }
}
private Integer getOrCreateConfigId(String name, String type) {
if (StringUtils.isEmpty(name)) {
return null;
}
- ProductMaterialConfig config = productMaterialConfigService.getOne(new LambdaQueryWrapper<ProductMaterialConfig>()
- .eq(ProductMaterialConfig::getConfigName, name)
- .eq(ProductMaterialConfig::getConfigType, type));
+ ProductMaterialConfig config = productMaterialConfigService.getOne(new LambdaQueryWrapper<ProductMaterialConfig>().eq(ProductMaterialConfig::getConfigName, name).eq(ProductMaterialConfig::getConfigType, type));
if (config == null) {
config = new ProductMaterialConfig();
config.setConfigName(name);
@@ -264,43 +220,206 @@
return config.getId();
}
- private int processSaveOrUpdate(List<ProductMaterial> list) {
+ private int processSaveOrUpdate(List<ProductMaterialSku> list) {
if (list == null || list.isEmpty()) {
return 0;
}
int affected = 0;
- for (ProductMaterial material : list) {
- ProductMaterial exist = this.getOne(new LambdaQueryWrapper<ProductMaterial>()
- .eq(ProductMaterial::getFormInstanceId, material.getFormInstanceId()));
+ for (ProductMaterialSku sku : list) {
- if (exist == null) {
- this.save(material);
- affected++;
- log.info("鏂板鐗╂枡鏁版嵁 formInstanceId={}", material.getFormInstanceId());
+ LambdaQueryWrapper<ProductMaterialSku> wrapper = new LambdaQueryWrapper<>();
+ wrapper.eq(ProductMaterialSku::getMaterialId, sku.getMaterialId())
+ .eq(ProductMaterialSku::getSpecification, sku.getSpecification());
+
+ if (StringUtils.isNotEmpty(sku.getMaterialCode())) {
+ wrapper.eq(ProductMaterialSku::getMaterialCode, sku.getMaterialCode());
} else {
- if (exist.getFormModifiedTime() == null || !exist.getFormModifiedTime().equals(material.getFormModifiedTime())) {
- material.setId(exist.getId());
- material.setCreateTime(exist.getCreateTime());
- this.updateById(material);
+ wrapper.isNull(ProductMaterialSku::getMaterialCode);
+ }
+
+ ProductMaterialSku exist = productMaterialSkuService.getOne(wrapper);
+ if (exist == null) {
+ productMaterialSkuService.save(sku);
+ affected++;
+ log.info("鏂板鐗╂枡瑙勬牸 {}", sku.getSpecification());
+ } else {
+ if (exist.getFormModifiedTime() == null || !exist.getFormModifiedTime().equals(sku.getFormModifiedTime())) {
+ sku.setId(exist.getId());
+ sku.setCreateTime(exist.getCreateTime());
+ productMaterialSkuService.updateById(sku);
+
affected++;
- log.info("鏇存柊鐗╂枡鏁版嵁 formInstanceId={}", material.getFormInstanceId());
+ log.info("鏇存柊鐗╂枡瑙勬牸 {}", sku.getSpecification());
}
}
}
return affected;
}
- private LocalDateTime parseUtcTime(String utcString) {
- if (StringUtils.isEmpty(utcString)) {
- return null;
+ @Override
+ public List<ProductMaterialGroupDto> ProductMaterialList(Integer type) {
+ List<ProductMaterialConfig> configList = productMaterialConfigService.list(new LambdaQueryWrapper<ProductMaterialConfig>()
+ .eq(ProductMaterialConfig::getConfigType, MaterialConfigTypeEnum.MATERIAL_TYPE.name())
+ );
+ if (CollectionUtils.isEmpty(configList)) {
+ return new ArrayList<>();
}
- try {
- OffsetDateTime odt = OffsetDateTime.parse(utcString);
- return odt.toLocalDateTime();
- } catch (DateTimeParseException ex) {
- log.warn("瑙f瀽鏃堕棿 {} 澶辫触: {}", utcString, ex.getMessage());
- return null;
+ List<ProductMaterialGroupDto> result = new ArrayList<>();
+ Map<Integer, List<ProductMaterialDto>> materialMap = new HashMap<>();
+ if (type != null && type == 2) {
+ List<ProductMaterial> materialList = this.list(new LambdaQueryWrapper<ProductMaterial>()
+ .select(
+ ProductMaterial::getId,
+ ProductMaterial::getMaterialTypeId,
+ ProductMaterial::getInventoryCategoryId,
+ ProductMaterial::getMaterialName
+ )
+ );
+ materialMap = materialList.stream()
+ .map(this::convert)
+ .collect(Collectors.groupingBy(ProductMaterialDto::getMaterialTypeId));
}
+
+ for (ProductMaterialConfig config : configList) {
+ ProductMaterialGroupDto dto = new ProductMaterialGroupDto();
+ dto.setConfigId(config.getId());
+ dto.setConfigName(config.getConfigName());
+ if (type != null && type == 2) {
+ dto.setMaterialList(materialMap.getOrDefault(config.getId(), new ArrayList<>()));
+ }
+ result.add(dto);
+ }
+ return result;
+ }
+
+
+ @Override
+ public List<ProductMaterialGroupDto> productMaterialListByQuery(String materialName, Integer materialTypeId) {
+
+ if (StringUtils.isEmpty(materialName) && materialTypeId == null) {
+ return new ArrayList<>();
+ }
+
+ LambdaQueryWrapper<ProductMaterial> wrapper = new LambdaQueryWrapper<>();
+ // 鍙煡璇㈤渶瑕佺殑瀛楁鏁版嵁
+ wrapper.select(
+ ProductMaterial::getId,
+ ProductMaterial::getMaterialTypeId,
+ ProductMaterial::getInventoryCategoryId,
+ ProductMaterial::getMaterialName
+ );
+ if (StringUtils.isNotEmpty(materialName)) {
+ wrapper.like(ProductMaterial::getMaterialName, materialName);
+ }
+ if (materialTypeId != null) {
+ wrapper.eq(ProductMaterial::getMaterialTypeId, materialTypeId);
+ }
+ List<ProductMaterial> materials = this.list(wrapper);
+ if (CollectionUtils.isEmpty(materials)) {
+ return new ArrayList<>();
+ }
+ Map<Integer, List<ProductMaterialDto>> map = materials.stream()
+ .map(this::convert)
+ .collect(Collectors.groupingBy(ProductMaterialDto::getMaterialTypeId));
+
+ List<ProductMaterialConfig> configList = productMaterialConfigService.list(new LambdaQueryWrapper<ProductMaterialConfig>()
+ .eq(ProductMaterialConfig::getConfigType, MaterialConfigTypeEnum.MATERIAL_TYPE.name()));
+
+ List<ProductMaterialGroupDto> result = new ArrayList<>();
+ for (ProductMaterialConfig config : configList) {
+ List<ProductMaterialDto> dtoList = map.get(config.getId());
+ if (CollectionUtils.isEmpty(dtoList)) {
+ continue;
+ }
+ ProductMaterialGroupDto dto = new ProductMaterialGroupDto();
+ dto.setConfigId(config.getId());
+ dto.setConfigName(config.getConfigName());
+ dto.setMaterialList(dtoList);
+ result.add(dto);
+ }
+
+ return result;
+ }
+
+ private ProductMaterialDto convert(ProductMaterial m) {
+ ProductMaterialDto dto = new ProductMaterialDto();
+ dto.setId(m.getId());
+ dto.setMaterialName(m.getMaterialName());
+ dto.setMaterialTypeId(m.getMaterialTypeId());
+ dto.setInventoryCategoryId(m.getInventoryCategoryId());
+ return dto;
+ }
+
+ @Override
+ @Transactional(rollbackFor = Exception.class)
+ public void addProductMaterial(ProductMaterial productMaterial) {
+ validateProductMaterial(productMaterial, false);
+ if (existsMaterialName(productMaterial.getMaterialName(), null)) {
+ throw new ServiceException("鐗╂枡鍚嶇О宸插瓨鍦�");
+ }
+ LocalDateTime now = LocalDateTime.now();
+ if (productMaterial.getCreateTime() == null) {
+ productMaterial.setCreateTime(now);
+ }
+ productMaterial.setUpdateTime(now);
+ if (!this.save(productMaterial)) {
+ throw new ServiceException("鏂板鐗╂枡澶辫触");
+ }
+ log.info("鏂板鐗╂枡鎴愬姛 materialName={}", productMaterial.getMaterialName());
+ }
+
+ @Override
+ @Transactional(rollbackFor = Exception.class)
+ public void updateProductMaterial(ProductMaterial productMaterial) {
+ validateProductMaterial(productMaterial, true);
+ ProductMaterial exist = this.getById(productMaterial.getId());
+ if (exist == null) {
+ throw new ServiceException("鐗╂枡涓嶅瓨鍦�");
+ }
+ if (existsMaterialName(productMaterial.getMaterialName(), productMaterial.getId())) {
+ throw new ServiceException("鐗╂枡鍚嶇О宸插瓨鍦�");
+ }
+ productMaterial.setUpdateTime(LocalDateTime.now());
+ if (!this.updateById(productMaterial)) {
+ throw new ServiceException("淇敼鐗╂枡澶辫触");
+ }
+ log.info("淇敼鐗╂枡鎴愬姛 id={}", productMaterial.getId());
+ }
+
+ @Override
+ @Transactional(rollbackFor = Exception.class)
+ public void deleteProductMaterial(List<Long> ids) {
+ if (ids == null || ids.isEmpty()) {
+ throw new ServiceException("璇烽�夋嫨鑷冲皯涓�鏉℃暟鎹�");
+ }
+ if (!this.removeByIds(ids)) {
+ throw new ServiceException("鍒犻櫎鐗╂枡澶辫触");
+ }
+ log.info("鍒犻櫎鐗╂枡鎴愬姛 ids={}", ids);
+ }
+
+ private void validateProductMaterial(ProductMaterial productMaterial, boolean requireId) {
+ if (productMaterial == null) {
+ throw new ServiceException("鍙傛暟涓嶈兘涓虹┖");
+ }
+ if (requireId && productMaterial.getId() == null) {
+ throw new ServiceException("涓婚敭ID涓嶈兘涓虹┖");
+ }
+ if (StringUtils.isEmpty(productMaterial.getMaterialName())) {
+ throw new ServiceException("鐗╂枡鍚嶇О涓嶈兘涓虹┖");
+ }
+ }
+
+ private boolean existsMaterialName(String materialName, Long excludeId) {
+ if (StringUtils.isEmpty(materialName)) {
+ return false;
+ }
+ LambdaQueryWrapper<ProductMaterial> queryWrapper = new LambdaQueryWrapper<>();
+ queryWrapper.eq(ProductMaterial::getMaterialName, materialName);
+ if (excludeId != null) {
+ queryWrapper.ne(ProductMaterial::getId, excludeId);
+ }
+ return this.count(queryWrapper) > 0;
}
}
--
Gitblit v1.9.3