From 6733a32d4bcd7ad3ec3f109da0f3d2524766f7bb Mon Sep 17 00:00:00 2001 From: liding <756868258@qq.com> Date: 星期二, 15 七月 2025 15:54:33 +0800 Subject: [PATCH] 1.数据优化 2.配煤计算器入库优化 --- main-business/src/main/java/com/ruoyi/business/service/impl/PendingInventoryServiceImpl.java | 218 +++++++++++++++++++++++++++++++++++++---------------- 1 files changed, 151 insertions(+), 67 deletions(-) diff --git a/main-business/src/main/java/com/ruoyi/business/service/impl/PendingInventoryServiceImpl.java b/main-business/src/main/java/com/ruoyi/business/service/impl/PendingInventoryServiceImpl.java index cacd7f6..1e6c5ad 100644 --- a/main-business/src/main/java/com/ruoyi/business/service/impl/PendingInventoryServiceImpl.java +++ b/main-business/src/main/java/com/ruoyi/business/service/impl/PendingInventoryServiceImpl.java @@ -37,6 +37,7 @@ import com.ruoyi.common.utils.bean.BeanUtils; import com.ruoyi.system.mapper.SysUserMapper; import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.interceptor.TransactionAspectSupport; @@ -60,6 +61,7 @@ * @since 2025-06-04 */ @Service +@Slf4j @RequiredArgsConstructor public class PendingInventoryServiceImpl extends ServiceImpl<PendingInventoryMapper, PendingInventory> implements PendingInventoryService { @@ -303,113 +305,195 @@ @Transactional public boolean addPending(PendingInventoryDto pendingInventoryDto) { try { - CoalPlan coalPlan = coalPlanMapper.selectOne(new LambdaQueryWrapper<CoalPlan>().eq(CoalPlan::getPlan, "閰嶇叅璁$畻鍣ㄦ柟妗�")); + // 1. 鑾峰彇閰嶇叅璁$畻鍣ㄦ柟妗� + CoalPlan coalPlan = coalPlanMapper.selectOne( + new LambdaQueryWrapper<CoalPlan>().eq(CoalPlan::getPlan, "閰嶇叅璁$畻鍣ㄦ柟妗�")); if (coalPlan == null) { + log.error("閰嶇叅璁$畻鍣ㄦ柟妗堜笉瀛樺湪"); return false; } - // 娣诲姞鍒板緟鍏ュ簱 + // 2. 鍑嗗寰呭叆搴撹褰� Long userId = SecurityUtils.getUserId(); PendingInventory pendingInventory = new PendingInventory(); pendingInventory.setUnit("t"); pendingInventory.setRegistrantId(userId); pendingInventory.setRegistrationDate(LocalDate.now()); + pendingInventory.setSupplierName("閰嶇叅璁$畻鍣ㄦ柟妗堝叆搴�"); + pendingInventory.setCoalPlanId(coalPlan.getId()); - for (Map<String, Object> map : pendingInventoryDto.getFieldsResultList()) { - pendingInventory.setSupplierName("閰嶇叅璁$畻鍣ㄦ柟妗堝叆搴�"); + // 3. 瀹夊叏鑾峰彇瀛楁缁撴灉 + Map<String, Object> map = Optional.ofNullable(pendingInventoryDto.getFieldsResultList()) + .orElseThrow(() -> new BaseException("瀛楁缁撴灉鍒楄〃涓嶈兘涓虹┖")); - // 澶勭悊鐓ょ偔ID - if (map.get("coalId") == null) { - CoalInfo coalInfo = new CoalInfo(); - coalInfo.setCoal((String) map.get("createCoal")); - coalInfo.setMaintainerId(userId); - coalInfo.setMaintenanceDate(LocalDate.now()); - if (coalInfoMapper.insert(coalInfo) <= 0) { - return false; - } - pendingInventory.setCoalId(coalInfo.getId()); - } else { - pendingInventory.setCoalId((Long) map.get("coalId")); + // 4. 澶勭悊鐓ょ偔ID + if (map.get("coalId") == null) { + String coalName = safeGetString(map, "createCoal"); + if (coalName == null || coalName.isEmpty()) { + throw new BaseException("鐓ょ偔鍚嶇О涓嶈兘涓虹┖"); } - // 璁剧疆浠锋牸鍜屾暟閲� - BigDecimal cost = BigDecimal.valueOf((Double) map.get("cost")); - BigDecimal tonnage = BigDecimal.valueOf((Double) map.get("totalTonnage")); + CoalInfo coalInfo = new CoalInfo(); + coalInfo.setCoal(coalName); + coalInfo.setMaintainerId(userId); + coalInfo.setMaintenanceDate(LocalDate.now()); + if (coalInfoMapper.insert(coalInfo) <= 0) { + log.error("鍒涘缓鐓ょ偔淇℃伅澶辫触"); + return false; + } + pendingInventory.setCoalId(coalInfo.getId()); + } else { + Long coalId = safeGetLong(map, "coalId"); + if (coalId == null) { + throw new BaseException("鐓ょ偔ID鏍煎紡閿欒"); + } + pendingInventory.setCoalId(coalId); + } - pendingInventory.setPriceIncludingTax(cost); - pendingInventory.setInventoryQuantity(tonnage); - pendingInventory.setTotalPriceIncludingTax(cost.multiply(tonnage)); + // 5. 璁剧疆浠锋牸鍜屾暟閲� + BigDecimal cost = safeGetBigDecimal(map, "cost"); + BigDecimal tonnage = safeGetBigDecimal(map, "totalTonnage"); - BigDecimal costExcludingTax = cost.divide(BigDecimal.valueOf(1.13), 2, RoundingMode.HALF_UP); - pendingInventory.setPriceExcludingTax(costExcludingTax); - pendingInventory.setTotalPriceExcludingTax(costExcludingTax.multiply(tonnage)); - pendingInventory.setCoalPlanId(coalPlan.getId()); + if (cost == null || tonnage == null) { + throw new BaseException("鎴愭湰鎴栧惃浣嶄笉鑳戒负绌�"); + } - // 鐓よ川瀛楁鍊� - String coalFields = coalPlan.getCoalFields(); - List<String> coalFieldList = Arrays.asList(coalFields.split(",")); + pendingInventory.setPriceIncludingTax(cost); + pendingInventory.setInventoryQuantity(tonnage); + pendingInventory.setTotalPriceIncludingTax(cost.multiply(tonnage)); - for (String field : coalFieldList) { - CoalField coalField = coalFieldMapper.selectOne( - new LambdaQueryWrapper<CoalField>().eq(CoalField::getFields, field)); - if (coalField == null) { + BigDecimal costExcludingTax = cost.divide(BigDecimal.valueOf(1.13), 2, RoundingMode.HALF_UP); + pendingInventory.setPriceExcludingTax(costExcludingTax); + pendingInventory.setTotalPriceExcludingTax(costExcludingTax.multiply(tonnage)); + + // 6. 澶勭悊鐓よ川瀛楁鍊� + String coalFields = coalPlan.getCoalFields(); + if (coalFields == null || coalFields.isEmpty()) { + throw new BaseException("鐓よ川瀛楁閰嶇疆涓嶈兘涓虹┖"); + } + + String[] coalFieldList = coalFields.split(","); + for (String field : coalFieldList) { + CoalField coalField = coalFieldMapper.selectOne( + new LambdaQueryWrapper<CoalField>().eq(CoalField::getFields, field)); + if (coalField == null) { + log.warn("鏈壘鍒扮叅璐ㄥ瓧娈甸厤缃�: {}", field); + continue; + } + + CoalValue coalValue = new CoalValue(); + coalValue.setPlanId(coalPlan.getId()); + coalValue.setFields(field); + coalValue.setFieldName(coalField.getFieldName()); + coalValue.setType("1"); + + // 瀹夊叏璁剧疆鐓よ川鍊� + switch (coalField.getFieldName()) { + case "鍙戠儹閲�": + coalValue.setCoalValue(safeGetString(map, "cv")); + break; + case "纭垎": + coalValue.setCoalValue(safeGetString(map, "sulfur")); + break; + case "鐏板垎": + coalValue.setCoalValue(safeGetString(map, "ash")); + break; + case "姘村垎": + coalValue.setCoalValue(safeGetString(map, "moisture")); + break; + default: + log.warn("鏈煡鐓よ川瀛楁: {}", coalField.getFieldName()); continue; - } + } - CoalValue coalValue = new CoalValue(); - coalValue.setPlanId(coalPlan.getId()); - coalValue.setFields(field); - coalValue.setFieldName(coalField.getFieldName()); - coalValue.setType("1"); - - switch (coalField.getFieldName()) { - case "鍙戠儹閲�": - coalValue.setCoalValue((String) map.get("cv")); - break; - case "纭垎": - coalValue.setCoalValue((String) map.get("sulfur")); - break; - case "鐏板垎": - coalValue.setCoalValue((String) map.get("ash")); - break; - case "姘村垎": - coalValue.setCoalValue((String) map.get("moisture")); - break; - } - - // 淇濆瓨鐓よ川鍊� - if (coalValueMapper.insert(coalValue) <= 0) { - return false; - } + if (coalValueMapper.insert(coalValue) <= 0) { + log.error("淇濆瓨鐓よ川鍊煎け璐�: {}", field); + return false; } } - // 鎻掑叆寰呭叆搴撹褰� + // 7. 鎻掑叆寰呭叆搴撹褰� if (pendingInventoryMapper.insert(pendingInventory) <= 0) { + log.error("鎻掑叆寰呭叆搴撹褰曞け璐�"); return false; } - // 鏇存柊姝e紡搴� - for (Map<String, Object> coalResult : pendingInventoryDto.getCoalResultList()) { - Long officialId = (Long) coalResult.get("officialId"); - BigDecimal quantity = (BigDecimal) coalResult.get("quantity"); - OfficialInventory officialInventory = officialInventoryMapper.selectById(officialId); + // 8. 鏇存柊姝e紡搴� + List<Map<String, Object>> coalResults = Optional.ofNullable(pendingInventoryDto.getCoalResultList()) + .orElseThrow(() -> new BaseException("鐓ょ偔缁撴灉鍒楄〃涓嶈兘涓虹┖")); - if (officialInventory == null || officialInventory.getInventoryQuantity().compareTo(quantity) < 0) { + for (Map<String, Object> coalResult : coalResults) { + Long officialId = safeGetLong(coalResult, "officialId"); + BigDecimal quantity = safeGetBigDecimal(coalResult, "quantity"); + + if (officialId == null || quantity == null) { + throw new BaseException("姝e紡搴撳瓨ID鎴栨暟閲忎笉鑳戒负绌�"); + } + + OfficialInventory officialInventory = officialInventoryMapper.selectById(officialId); + if (officialInventory == null) { + throw new BaseException("鎵句笉鍒版寮忓簱瀛樿褰�: " + officialId); + } + + if (officialInventory.getInventoryQuantity().compareTo(quantity) < 0) { throw new BaseException("搴撳瓨鏁伴噺涓嶈冻锛屾坊鍔犺嚦寰呭叆搴撳け璐�"); } officialInventory.setInventoryQuantity(officialInventory.getInventoryQuantity().subtract(quantity)); if (officialInventoryMapper.updateById(officialInventory) <= 0) { + log.error("鏇存柊姝e紡搴撳瓨澶辫触: {}", officialId); return false; } } return true; } catch (Exception e) { - // 璁板綍鏃ュ織 log.error("娣诲姞寰呭叆搴撳け璐�", e); throw new BaseException("娣诲姞寰呭叆搴撳け璐�: " + e.getMessage()); } } + + // 瀹夊叏绫诲瀷杞崲杈呭姪鏂规硶 + private String safeGetString(Map<String, Object> map, String key) { + Object value = map.get(key); + if (value == null) { + return null; + } + return value.toString(); + } + + private Long safeGetLong(Map<String, Object> map, String key) { + Object value = map.get(key); + if (value == null) { + return null; + } + if (value instanceof Number) { + return ((Number) value).longValue(); + } + try { + return Long.parseLong(value.toString()); + } catch (NumberFormatException e) { + log.warn("鏃犳硶杞崲涓篖ong: key={}, value={}", key, value); + return null; + } + } + + private BigDecimal safeGetBigDecimal(Map<String, Object> map, String key) { + Object value = map.get(key); + if (value == null) { + return null; + } + if (value instanceof BigDecimal) { + return (BigDecimal) value; + } + if (value instanceof Number) { + return BigDecimal.valueOf(((Number) value).doubleValue()); + } + try { + return new BigDecimal(value.toString()); + } catch (NumberFormatException e) { + log.warn("鏃犳硶杞崲涓築igDecimal: key={}, value={}", key, value); + return null; + } + } } -- Gitblit v1.9.3