You've already forked FrameTour-BE
refactor(PrinterServiceImpl):重构价格计算逻辑以使用统一的价格计算服务
在 `PrinterServiceImpl` 类中,移除了对 `PriceRepository` 的依赖,并引入了 `IPriceCalculationService` 接口来处理价格计算。主要改动包括:- 添加了新的导入语句,如 `PriceCalculationRequest`, `PriceCalculationResult`, `ProductItem`, `ProductType` 和 `IPriceCalculationService`。 - 在 `queryPrice` 方法中,通过 `IPriceCalculationService` 计算照片打印的总价格,替代了原有的直接从 `PriceRepository` 获取价格配置的方式。 - 更新了 `createOrder` 方法中的价格计算逻辑,同样采用 `IPriceCalculationService` 来确定订单的价格信息。 - 对于没有可打印照片的情况,现在会返回一个零价格的对象或抛出异常,具体取决于调用上下文。 这些更改旨在提高代码的模块化程度和可维护性,同时确保价格计算的一致性和准确性。
This commit is contained in:
@@ -14,6 +14,11 @@ import com.ycwl.basic.model.pc.member.resp.MemberRespVO;
|
||||
import com.ycwl.basic.model.pc.order.entity.OrderEntity;
|
||||
import com.ycwl.basic.model.pc.order.entity.OrderItemEntity;
|
||||
import com.ycwl.basic.model.pc.price.entity.PriceConfigEntity;
|
||||
import com.ycwl.basic.pricing.dto.PriceCalculationRequest;
|
||||
import com.ycwl.basic.pricing.dto.PriceCalculationResult;
|
||||
import com.ycwl.basic.pricing.dto.ProductItem;
|
||||
import com.ycwl.basic.pricing.enums.ProductType;
|
||||
import com.ycwl.basic.pricing.service.IPriceCalculationService;
|
||||
import com.ycwl.basic.model.pc.printer.entity.PrintTaskEntity;
|
||||
import com.ycwl.basic.model.pc.printer.entity.PrinterEntity;
|
||||
import com.ycwl.basic.model.pc.printer.resp.MemberPrintResp;
|
||||
@@ -52,8 +57,6 @@ public class PrinterServiceImpl implements PrinterService {
|
||||
@Autowired
|
||||
private PrinterMapper printerMapper;
|
||||
@Autowired
|
||||
private PriceRepository priceRepository;
|
||||
@Autowired
|
||||
private SourceMapper sourceMapper;
|
||||
@Autowired
|
||||
private MemberMapper memberMapper;
|
||||
@@ -66,6 +69,8 @@ public class PrinterServiceImpl implements PrinterService {
|
||||
private WxPayService wxPayService;
|
||||
@Autowired
|
||||
private PrintTaskMapper printTaskMapper;
|
||||
@Autowired
|
||||
private IPriceCalculationService priceCalculationService;
|
||||
|
||||
@Override
|
||||
public List<PrinterResp> listByScenicId(Long scenicId) {
|
||||
@@ -205,15 +210,49 @@ public class PrinterServiceImpl implements PrinterService {
|
||||
@Override
|
||||
public PriceObj queryPrice(Long memberId, Long scenicId) {
|
||||
List<MemberPrintResp> userPhotoList = getUserPhotoList(memberId, scenicId);
|
||||
// 判断几张
|
||||
PriceConfigEntity priceConfig = priceRepository.getPriceConfigByScenicTypeGoods(scenicId, 3, null);
|
||||
|
||||
// 计算照片总数量
|
||||
long count = userPhotoList.stream()
|
||||
.filter(item -> Objects.nonNull(item.getQuantity()))
|
||||
.mapToInt(MemberPrintResp::getQuantity)
|
||||
.sum();
|
||||
|
||||
PriceObj obj = new PriceObj();
|
||||
long count = userPhotoList.stream().filter(item -> Objects.nonNull(item.getQuantity())).mapToInt(MemberPrintResp::getQuantity).sum();
|
||||
obj.setPrice(priceConfig.getPrice().multiply(BigDecimal.valueOf(count)));
|
||||
obj.setSlashPrice(priceConfig.getSlashPrice().multiply(BigDecimal.valueOf(count)));
|
||||
if (count == 0) {
|
||||
// 如果没有照片,返回零价格
|
||||
obj.setPrice(BigDecimal.ZERO);
|
||||
obj.setSlashPrice(BigDecimal.ZERO);
|
||||
obj.setGoodsType(3);
|
||||
obj.setFree(false);
|
||||
obj.setScenicId(scenicId);
|
||||
return obj;
|
||||
}
|
||||
|
||||
// 构建价格计算请求
|
||||
PriceCalculationRequest request = new PriceCalculationRequest();
|
||||
request.setUserId(memberId);
|
||||
request.setScenicId(scenicId);
|
||||
|
||||
// 创建照片打印商品项
|
||||
ProductItem photoItem = new ProductItem();
|
||||
photoItem.setProductType(ProductType.PHOTO_PRINT);
|
||||
photoItem.setProductId(scenicId.toString());
|
||||
photoItem.setQuantity(Long.valueOf(count).intValue());
|
||||
photoItem.setPurchaseCount(1);
|
||||
photoItem.setScenicId(scenicId.toString());
|
||||
|
||||
request.setProducts(Collections.singletonList(photoItem));
|
||||
|
||||
// 使用统一价格计算服务
|
||||
PriceCalculationResult result = priceCalculationService.calculatePrice(request);
|
||||
|
||||
// 转换为原有的 PriceObj 格式
|
||||
obj.setPrice(result.getFinalAmount());
|
||||
obj.setSlashPrice(result.getOriginalAmount());
|
||||
obj.setGoodsType(3);
|
||||
obj.setFree(false);
|
||||
obj.setFree(result.getFinalAmount().compareTo(BigDecimal.ZERO) == 0);
|
||||
obj.setScenicId(scenicId);
|
||||
|
||||
return obj;
|
||||
}
|
||||
|
||||
@@ -250,11 +289,13 @@ public class PrinterServiceImpl implements PrinterService {
|
||||
throw new BaseException("打印机不属于该景区");
|
||||
}
|
||||
}
|
||||
PriceConfigEntity priceConfig = priceRepository.getPriceConfigByScenicTypeGoods(scenicId, 3, null);
|
||||
if (priceConfig == null) {
|
||||
throw new BaseException("该套餐暂未开放购买");
|
||||
// 验证照片数量
|
||||
List<MemberPrintResp> userPhotoList = getUserPhotoList(memberId, scenicId);
|
||||
long count = userPhotoList.stream().filter(item -> Objects.nonNull(item.getQuantity())).mapToInt(MemberPrintResp::getQuantity).sum();
|
||||
if (count == 0) {
|
||||
throw new BaseException("没有可打印的照片");
|
||||
}
|
||||
log.info("创建打印订单,价格配置:{}", priceConfig);
|
||||
|
||||
OrderEntity order = new OrderEntity();
|
||||
Long orderId = SnowFlakeUtil.getLongId();
|
||||
order.setId(orderId);
|
||||
@@ -262,9 +303,10 @@ public class PrinterServiceImpl implements PrinterService {
|
||||
MemberRespVO member = memberMapper.getById(memberId);
|
||||
order.setOpenId(member.getOpenId());
|
||||
order.setScenicId(scenicId);
|
||||
order.setType(priceConfig.getType());
|
||||
order.setType(3); // 照片打印类型
|
||||
batchSetUserPhotoListToPrinter(memberId, scenicId, printerId);
|
||||
List<MemberPrintResp> userPhotoList = getUserPhotoList(memberId, scenicId);
|
||||
// 重新获取照片列表(包含打印机信息)
|
||||
userPhotoList = getUserPhotoList(memberId, scenicId);
|
||||
List<OrderItemEntity> orderItems = userPhotoList.stream().map(goods -> {
|
||||
OrderItemEntity orderItem = new OrderItemEntity();
|
||||
orderItem.setOrderId(orderId);
|
||||
@@ -273,10 +315,28 @@ public class PrinterServiceImpl implements PrinterService {
|
||||
orderItem.setGoodsType(3);
|
||||
return orderItem;
|
||||
}).collect(Collectors.toList());
|
||||
long count = userPhotoList.stream().filter(item -> Objects.nonNull(item.getQuantity())).mapToInt(MemberPrintResp::getQuantity).sum();
|
||||
order.setPrice(priceConfig.getPrice().multiply(BigDecimal.valueOf(count)));
|
||||
order.setSlashPrice(priceConfig.getSlashPrice().multiply(BigDecimal.valueOf(count)));
|
||||
order.setPayPrice(priceConfig.getPrice().multiply(BigDecimal.valueOf(count)));
|
||||
|
||||
// 使用统一价格计算服务计算最终价格
|
||||
|
||||
PriceCalculationRequest request = new PriceCalculationRequest();
|
||||
request.setUserId(memberId);
|
||||
request.setScenicId(scenicId);
|
||||
|
||||
// 创建照片打印商品项
|
||||
ProductItem photoItem = new ProductItem();
|
||||
photoItem.setProductType(ProductType.PHOTO_PRINT);
|
||||
photoItem.setProductId(scenicId.toString());
|
||||
photoItem.setQuantity(Long.valueOf(count).intValue());
|
||||
photoItem.setPurchaseCount(1);
|
||||
photoItem.setScenicId(scenicId.toString());
|
||||
|
||||
request.setProducts(Collections.singletonList(photoItem));
|
||||
|
||||
PriceCalculationResult priceResult = priceCalculationService.calculatePrice(request);
|
||||
|
||||
order.setPrice(priceResult.getFinalAmount());
|
||||
order.setSlashPrice(priceResult.getOriginalAmount());
|
||||
order.setPayPrice(priceResult.getFinalAmount());
|
||||
// order.setFaceId();
|
||||
if (order.getPayPrice().equals(BigDecimal.ZERO)) {
|
||||
order.setStatus(OrderStateEnum.PAID.getState());
|
||||
|
Reference in New Issue
Block a user