| | |
| | | updateReqVO.setOwnerUserId(null).setStatusTypeId(null); // 不允许更新的字段 |
| | | // 1.1 校验存在 |
| | | CrmBusinessDO oldBusiness = validateBusinessExists(updateReqVO.getId()); |
| | | // 1.2 校验产品项的有效性 |
| | | List<CrmBusinessProductDO> businessProducts = validateBusinessProducts(updateReqVO.getProducts()); |
| | | // 1.2 校验产品项的有效性(传入旧产品列表用于合并 null 字段) |
| | | List<CrmBusinessProductDO> oldProducts = businessProductMapper.selectListByBusinessId(updateReqVO.getId()); |
| | | List<CrmBusinessProductDO> businessProducts = validateBusinessProducts(updateReqVO.getProducts(), oldProducts); |
| | | // 1.3 校验关联字段 |
| | | validateRelationDataExists(updateReqVO); |
| | | |
| | |
| | | updateBusinessProduct(updateObj.getId(), businessProducts); |
| | | |
| | | // 3. 记录操作日志上下文 |
| | | updateReqVO.setOwnerUserId(oldBusiness.getOwnerUserId()); // 避免操作日志出现“删除负责人”的情况 |
| | | updateReqVO.setOwnerUserId(oldBusiness.getOwnerUserId()); // 避免操作日志出现"删除负责人"的情况 |
| | | LogRecordContext.putVariable(DiffParseFunction.OLD_OBJECT, BeanUtils.toBean(oldBusiness, CrmBusinessSaveReqVO.class)); |
| | | LogRecordContext.putVariable("businessName", oldBusiness.getName()); |
| | | } |
| | |
| | | } |
| | | |
| | | private List<CrmBusinessProductDO> validateBusinessProducts(List<CrmBusinessSaveReqVO.BusinessProduct> list) { |
| | | return validateBusinessProducts(list, Collections.emptyList()); |
| | | } |
| | | |
| | | /** |
| | | * 校验产品项的有效性(更新场景合并旧数据) |
| | | * |
| | | * @param list 前端传入的产品列表 |
| | | * @param oldProducts 已有产品列表(用于合并 null 字段) |
| | | * @return 处理后的产品 DO 列表 |
| | | */ |
| | | private List<CrmBusinessProductDO> validateBusinessProducts(List<CrmBusinessSaveReqVO.BusinessProduct> list, |
| | | List<CrmBusinessProductDO> oldProducts) { |
| | | // 1. 校验物料存在 |
| | | Set<Long> itemIds = convertSet(list, CrmBusinessSaveReqVO.BusinessProduct::getItemId); |
| | | Map<Long, MdmItemRespDTO> itemMap = mdmItemApi.getItemMap(itemIds); |
| | |
| | | throw exception(PRODUCT_NOT_EXISTS); |
| | | } |
| | | } |
| | | // 2. 转化为 CrmBusinessProductDO 列表 |
| | | return convertList(list, o -> BeanUtils.toBean(o, CrmBusinessProductDO.class, |
| | | item -> item.setTotalPrice(MoneyUtils.priceMultiply(item.getBusinessPrice(), item.getCount())))); |
| | | // 2. 构建旧产品 Map(按 id 索引) |
| | | Map<Long, CrmBusinessProductDO> oldProductMap = convertMap(oldProducts, CrmBusinessProductDO::getId); |
| | | // 3. 转化为 CrmBusinessProductDO 列表(合并旧数据) |
| | | return convertList(list, o -> { |
| | | CrmBusinessProductDO product = BeanUtils.toBean(o, CrmBusinessProductDO.class); |
| | | // 更新场景:如果产品已有 id 且有旧数据,则合并 null 字段 |
| | | if (o.getId() != null && oldProductMap.containsKey(o.getId())) { |
| | | CrmBusinessProductDO old = oldProductMap.get(o.getId()); |
| | | if (product.getBusinessPrice() == null) { |
| | | product.setBusinessPrice(old.getBusinessPrice()); |
| | | } |
| | | if (product.getItemPrice() == null) { |
| | | product.setItemPrice(old.getItemPrice()); |
| | | } |
| | | if (product.getCount() == null) { |
| | | product.setCount(old.getCount()); |
| | | } |
| | | if (product.getItemUnitId() == null) { |
| | | product.setItemUnitId(old.getItemUnitId()); |
| | | } |
| | | } |
| | | // 优先使用前端传入的 totalPrice,否则通过 businessPrice * count 计算 |
| | | if (o.getTotalPrice() != null) { |
| | | product.setTotalPrice(o.getTotalPrice()); |
| | | } else { |
| | | product.setTotalPrice(MoneyUtils.priceMultiply(product.getBusinessPrice(), product.getCount())); |
| | | } |
| | | return product; |
| | | }); |
| | | } |
| | | |
| | | private void calculateTotalPrice(CrmBusinessDO business, List<CrmBusinessProductDO> businessProducts) { |
| | | business.setTotalProductPrice(getSumValue(businessProducts, CrmBusinessProductDO::getTotalPrice, BigDecimal::add, BigDecimal.ZERO)); |
| | | BigDecimal discountPrice = MoneyUtils.priceMultiplyPercent(business.getTotalProductPrice(), business.getDiscountPercent()); |
| | | business.setTotalPrice(business.getTotalProductPrice().subtract(discountPrice)); |
| | | // totalProductPrice:优先使用前端传入值,否则从产品汇总 |
| | | if (business.getTotalProductPrice() == null) { |
| | | business.setTotalProductPrice(getSumValue(businessProducts, CrmBusinessProductDO::getTotalPrice, BigDecimal::add, BigDecimal.ZERO)); |
| | | } |
| | | // totalPrice:优先使用前端传入值,否则按折扣计算 |
| | | if (business.getTotalPrice() == null) { |
| | | BigDecimal discountPrice = MoneyUtils.priceMultiplyPercent(business.getTotalProductPrice(), business.getDiscountPercent()); |
| | | business.setTotalPrice(business.getTotalProductPrice().subtract(discountPrice)); |
| | | } |
| | | } |
| | | |
| | | @Override |