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.OrderEntity;
|
||||||
import com.ycwl.basic.model.pc.order.entity.OrderItemEntity;
|
import com.ycwl.basic.model.pc.order.entity.OrderItemEntity;
|
||||||
import com.ycwl.basic.model.pc.price.entity.PriceConfigEntity;
|
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.PrintTaskEntity;
|
||||||
import com.ycwl.basic.model.pc.printer.entity.PrinterEntity;
|
import com.ycwl.basic.model.pc.printer.entity.PrinterEntity;
|
||||||
import com.ycwl.basic.model.pc.printer.resp.MemberPrintResp;
|
import com.ycwl.basic.model.pc.printer.resp.MemberPrintResp;
|
||||||
@@ -52,8 +57,6 @@ public class PrinterServiceImpl implements PrinterService {
|
|||||||
@Autowired
|
@Autowired
|
||||||
private PrinterMapper printerMapper;
|
private PrinterMapper printerMapper;
|
||||||
@Autowired
|
@Autowired
|
||||||
private PriceRepository priceRepository;
|
|
||||||
@Autowired
|
|
||||||
private SourceMapper sourceMapper;
|
private SourceMapper sourceMapper;
|
||||||
@Autowired
|
@Autowired
|
||||||
private MemberMapper memberMapper;
|
private MemberMapper memberMapper;
|
||||||
@@ -66,6 +69,8 @@ public class PrinterServiceImpl implements PrinterService {
|
|||||||
private WxPayService wxPayService;
|
private WxPayService wxPayService;
|
||||||
@Autowired
|
@Autowired
|
||||||
private PrintTaskMapper printTaskMapper;
|
private PrintTaskMapper printTaskMapper;
|
||||||
|
@Autowired
|
||||||
|
private IPriceCalculationService priceCalculationService;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public List<PrinterResp> listByScenicId(Long scenicId) {
|
public List<PrinterResp> listByScenicId(Long scenicId) {
|
||||||
@@ -205,15 +210,49 @@ public class PrinterServiceImpl implements PrinterService {
|
|||||||
@Override
|
@Override
|
||||||
public PriceObj queryPrice(Long memberId, Long scenicId) {
|
public PriceObj queryPrice(Long memberId, Long scenicId) {
|
||||||
List<MemberPrintResp> userPhotoList = getUserPhotoList(memberId, 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();
|
PriceObj obj = new PriceObj();
|
||||||
long count = userPhotoList.stream().filter(item -> Objects.nonNull(item.getQuantity())).mapToInt(MemberPrintResp::getQuantity).sum();
|
if (count == 0) {
|
||||||
obj.setPrice(priceConfig.getPrice().multiply(BigDecimal.valueOf(count)));
|
// 如果没有照片,返回零价格
|
||||||
obj.setSlashPrice(priceConfig.getSlashPrice().multiply(BigDecimal.valueOf(count)));
|
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.setGoodsType(3);
|
||||||
obj.setFree(false);
|
obj.setFree(result.getFinalAmount().compareTo(BigDecimal.ZERO) == 0);
|
||||||
obj.setScenicId(scenicId);
|
obj.setScenicId(scenicId);
|
||||||
|
|
||||||
return obj;
|
return obj;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -250,11 +289,13 @@ public class PrinterServiceImpl implements PrinterService {
|
|||||||
throw new BaseException("打印机不属于该景区");
|
throw new BaseException("打印机不属于该景区");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
PriceConfigEntity priceConfig = priceRepository.getPriceConfigByScenicTypeGoods(scenicId, 3, null);
|
// 验证照片数量
|
||||||
if (priceConfig == null) {
|
List<MemberPrintResp> userPhotoList = getUserPhotoList(memberId, scenicId);
|
||||||
throw new BaseException("该套餐暂未开放购买");
|
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();
|
OrderEntity order = new OrderEntity();
|
||||||
Long orderId = SnowFlakeUtil.getLongId();
|
Long orderId = SnowFlakeUtil.getLongId();
|
||||||
order.setId(orderId);
|
order.setId(orderId);
|
||||||
@@ -262,9 +303,10 @@ public class PrinterServiceImpl implements PrinterService {
|
|||||||
MemberRespVO member = memberMapper.getById(memberId);
|
MemberRespVO member = memberMapper.getById(memberId);
|
||||||
order.setOpenId(member.getOpenId());
|
order.setOpenId(member.getOpenId());
|
||||||
order.setScenicId(scenicId);
|
order.setScenicId(scenicId);
|
||||||
order.setType(priceConfig.getType());
|
order.setType(3); // 照片打印类型
|
||||||
batchSetUserPhotoListToPrinter(memberId, scenicId, printerId);
|
batchSetUserPhotoListToPrinter(memberId, scenicId, printerId);
|
||||||
List<MemberPrintResp> userPhotoList = getUserPhotoList(memberId, scenicId);
|
// 重新获取照片列表(包含打印机信息)
|
||||||
|
userPhotoList = getUserPhotoList(memberId, scenicId);
|
||||||
List<OrderItemEntity> orderItems = userPhotoList.stream().map(goods -> {
|
List<OrderItemEntity> orderItems = userPhotoList.stream().map(goods -> {
|
||||||
OrderItemEntity orderItem = new OrderItemEntity();
|
OrderItemEntity orderItem = new OrderItemEntity();
|
||||||
orderItem.setOrderId(orderId);
|
orderItem.setOrderId(orderId);
|
||||||
@@ -273,10 +315,28 @@ public class PrinterServiceImpl implements PrinterService {
|
|||||||
orderItem.setGoodsType(3);
|
orderItem.setGoodsType(3);
|
||||||
return orderItem;
|
return orderItem;
|
||||||
}).collect(Collectors.toList());
|
}).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();
|
// order.setFaceId();
|
||||||
if (order.getPayPrice().equals(BigDecimal.ZERO)) {
|
if (order.getPayPrice().equals(BigDecimal.ZERO)) {
|
||||||
order.setStatus(OrderStateEnum.PAID.getState());
|
order.setStatus(OrderStateEnum.PAID.getState());
|
||||||
|
|||||||
Reference in New Issue
Block a user