refactor(technology): 优化 BOM 成本计算逻辑

- 更新缓存键生成逻辑,增加月份参数
- 优化子 BOM 查询效率,使用 Set 替代 List
- 改进 E50 类型 BOM 的成本计算处理
- 修复部分辅助生产费用的计算逻辑
-调整成本配置备份逻辑,备份上月数据
This commit is contained in:
曹鹏飞 2025-04-16 11:28:18 +08:00
parent 0fef01e0c8
commit 34ccf46e7f
2 changed files with 20 additions and 15 deletions

View File

@ -1,6 +1,7 @@
package com.nflg.product.technology.service; package com.nflg.product.technology.service;
import cn.hutool.core.collection.CollectionUtil; import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil; import cn.hutool.core.util.StrUtil;
import com.nflg.product.technology.constant.BomConstant; import com.nflg.product.technology.constant.BomConstant;
import com.nflg.product.technology.constant.TechnologyConfigEnum; import com.nflg.product.technology.constant.TechnologyConfigEnum;
@ -23,6 +24,7 @@ import javax.annotation.Resource;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.RoundingMode; import java.math.RoundingMode;
import java.time.Duration; import java.time.Duration;
import java.time.LocalDateTime;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
@ -97,7 +99,7 @@ public class BomCostCalculateService {
} }
private EBomCostCacheDTO calculateBom(String rootMaterialNo, String month, boolean isRoot, EBomDTO dto, List<EBomDTO> datas, BomCostCalculateConfig config, List<EBomCostCacheDTO> result) { private EBomCostCacheDTO calculateBom(String rootMaterialNo, String month, boolean isRoot, EBomDTO dto, List<EBomDTO> datas, BomCostCalculateConfig config, List<EBomCostCacheDTO> result) {
String cdata = redisTemplate.opsForValue().get(buildKey(dto)); String cdata = redisTemplate.opsForValue().get(buildKey(dto, month));
boolean purchaseTypeIsF = materialMainAttrService.purchaseTypeIsF(dto.getMaterialNo()); boolean purchaseTypeIsF = materialMainAttrService.purchaseTypeIsF(dto.getMaterialNo());
boolean purchaseTypeIsE50 = materialMainAttrService.purchaseTypeIsE50(dto.getMaterialNo()); boolean purchaseTypeIsE50 = materialMainAttrService.purchaseTypeIsE50(dto.getMaterialNo());
if ((isRoot && purchaseTypeIsE50) || StrUtil.isBlank(cdata)) { if ((isRoot && purchaseTypeIsE50) || StrUtil.isBlank(cdata)) {
@ -125,9 +127,9 @@ public class BomCostCalculateService {
cdto.setAuxiliaryDepartmentLaborCost(calculateAuxiliaryDepartmentLaborCost(dto, config, workingHours)); cdto.setAuxiliaryDepartmentLaborCost(calculateAuxiliaryDepartmentLaborCost(dto, config, workingHours));
cdto.setAuxiliaryDepartmentExpenses(calculateAuxiliaryDepartmentExpenses(dto, config, workingHours)); cdto.setAuxiliaryDepartmentExpenses(calculateAuxiliaryDepartmentExpenses(dto, config, workingHours));
cdto.setProductionCosts(calculateProductionCosts(dto, config, workingHours)); cdto.setProductionCosts(calculateProductionCosts(dto, config, workingHours));
List<EBomDTO> children = datas.stream() Set<EBomDTO> children = datas.stream()
.filter(d -> StrUtil.equals(d.getParentMaterialNo(), dto.getMaterialNo())) .filter(d -> StrUtil.equals(d.getParentMaterialNo(), dto.getMaterialNo()))
.collect(Collectors.toList()); .collect(Collectors.toSet());
if (CollectionUtil.isEmpty(children)) { if (CollectionUtil.isEmpty(children)) {
cdto.setHasChildren(false); cdto.setHasChildren(false);
// 计算自身 // 计算自身
@ -197,7 +199,7 @@ public class BomCostCalculateService {
if (!(isRoot && purchaseTypeIsE50)) { if (!(isRoot && purchaseTypeIsE50)) {
cdata = JsonUtil.toJson(cdto); cdata = JsonUtil.toJson(cdto);
//log.debug(cdata); //log.debug(cdata);
redisTemplate.opsForValue().set(buildKey(dto), cdata, CACHE_DURATION); redisTemplate.opsForValue().set(buildKey(dto, month), cdata, CACHE_DURATION);
} }
return cdto; return cdto;
} else { } else {
@ -403,9 +405,10 @@ public class BomCostCalculateService {
return price; return price;
} }
private String buildKey(EBomDTO dto) { private String buildKey(EBomDTO dto, String month) {
return StrUtil.format("technology:cost:{}:{}", dto.getMaterialNo() return StrUtil.format("technology:cost:{}:{}:{}", dto.getMaterialNo()
, StrUtil.isBlank(dto.getCurrentVersion()) ? "A00" : dto.getCurrentVersion()); , StrUtil.isBlank(dto.getCurrentVersion()) ? "A00" : dto.getCurrentVersion()
, StrUtil.isBlank(month) ? DateUtil.format(LocalDateTime.now(), "yyyyMM") : month);
} }
private void getChildren(String materialNo, List<EBomDTO> datas, String month) { private void getChildren(String materialNo, List<EBomDTO> datas, String month) {
@ -467,14 +470,16 @@ public class BomCostCalculateService {
EBomParentEntity parent = ebomService.getParent(materialNo); EBomParentEntity parent = ebomService.getParent(materialNo);
VUtils.isTure(Objects.isNull(parent)).throwMessage("无有效bom"); VUtils.isTure(Objects.isNull(parent)).throwMessage("无有效bom");
EBomDTO dto = convert(parent, month); EBomDTO dto = convert(parent, month);
String cdata = redisTemplate.opsForValue().get(buildKey(dto)); String cdata = redisTemplate.opsForValue().get(buildKey(dto, month));
if (StrUtil.isNotBlank(cdata)) { if (StrUtil.isNotBlank(cdata)) {
return JsonUtil.fromJson(cdata, EBomCostCacheDTO.class); EBomCostCacheDTO dto1 = JsonUtil.fromJson(cdata, EBomCostCacheDTO.class);
} else { if (!dto1.isPurchaseTypeIsE50()) {
return calculate(materialNo, month) return dto1;
.stream().filter(f -> StrUtil.equals(f.getMaterialNo(), materialNo)).findFirst() }
.orElse(null);
} }
return calculate(materialNo, month)
.stream().filter(f -> StrUtil.equals(f.getMaterialNo(), materialNo)).findFirst()
.orElse(null);
} }
private BigDecimal getTechnologyConfig(TechnologyConfigEnum type, String month) { private BigDecimal getTechnologyConfig(TechnologyConfigEnum type, String month) {

View File

@ -150,7 +150,7 @@ public class CostConfigService {
} }
public void backupSteelsCostConfig() { public void backupSteelsCostConfig() {
LocalDateTime now = LocalDateTime.now(); LocalDateTime now = LocalDateTime.now().minusMonths(1);
String month = now.format(DateTimeFormatter.ofPattern("yyyyMM")); String month = now.format(DateTimeFormatter.ofPattern("yyyyMM"));
if (steelsCostConfigHistoryService.lambdaQuery() if (steelsCostConfigHistoryService.lambdaQuery()
.eq(SteelsCostConfigHistoryEntity::getMonth, month) .eq(SteelsCostConfigHistoryEntity::getMonth, month)
@ -173,7 +173,7 @@ public class CostConfigService {
} }
public void backupPaintCostConfig() { public void backupPaintCostConfig() {
LocalDateTime now = LocalDateTime.now(); LocalDateTime now = LocalDateTime.now().minusMonths(1);
String month = now.format(DateTimeFormatter.ofPattern("yyyyMM")); String month = now.format(DateTimeFormatter.ofPattern("yyyyMM"));
if (paintCostConfigHistoryService.lambdaQuery() if (paintCostConfigHistoryService.lambdaQuery()
.eq(PaintCostConfigHistoryEntity::getMonth, month) .eq(PaintCostConfigHistoryEntity::getMonth, month)