|
|
@@ -0,0 +1,126 @@
|
|
|
+package com.izouma.nineth.service;
|
|
|
+
|
|
|
+import com.izouma.nineth.domain.*;
|
|
|
+import com.izouma.nineth.dto.PageQuery;
|
|
|
+import com.izouma.nineth.enums.*;
|
|
|
+import com.izouma.nineth.exception.BusinessException;
|
|
|
+import com.izouma.nineth.repo.TradeAuctionOrderRepo;
|
|
|
+import com.izouma.nineth.repo.TradeAuctionRepo;
|
|
|
+import com.izouma.nineth.repo.UserRepo;
|
|
|
+import com.izouma.nineth.utils.JpaUtils;
|
|
|
+import lombok.AllArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.data.domain.Page;
|
|
|
+import org.springframework.scheduling.annotation.Scheduled;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+
|
|
|
+import javax.transaction.Transactional;
|
|
|
+import java.math.BigDecimal;
|
|
|
+import java.time.LocalDateTime;
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+@Service
|
|
|
+@AllArgsConstructor
|
|
|
+@Slf4j
|
|
|
+public class TradeAuctionOrderService {
|
|
|
+
|
|
|
+ private TradeAuctionOrderRepo tradeAuctionOrderRepo;
|
|
|
+ private TradeAuctionService tradeAuctionService;
|
|
|
+ private TradeAuctionRepo tradeAuctionRepo;
|
|
|
+ private UserRepo userRepo;
|
|
|
+
|
|
|
+ private static int orderCancelInterval = 210;
|
|
|
+
|
|
|
+ public Page<TradeAuctionOrder> all(PageQuery pageQuery) {
|
|
|
+ return tradeAuctionOrderRepo.findAll(JpaUtils.toSpecification(pageQuery, TradeAuctionOrder.class), JpaUtils
|
|
|
+ .toPageRequest(pageQuery));
|
|
|
+ }
|
|
|
+
|
|
|
+ public TradeAuctionOrder create(Long auctionId, Long userId, BigDecimal price) {
|
|
|
+ TradeAuction tradeAuction = tradeAuctionRepo.findById(auctionId).orElseThrow(new BusinessException("未找到易拍活动"));
|
|
|
+ User user = userRepo.findById(userId).orElseThrow(new BusinessException("暂无用户"));
|
|
|
+ if (tradeAuction.getCurrentOwnerId().equals(userId)) {
|
|
|
+ throw new BusinessException("不可竞价持有的易拍产品");
|
|
|
+ }
|
|
|
+ if (LocalDateTime.now().compareTo(tradeAuction.getCurrentEndTime()) < 0) {
|
|
|
+ throw new BusinessException("未到竞价时间");
|
|
|
+ }
|
|
|
+ if (!tradeAuction.getStatus().equals(TradeAuctionStatus.ONGOING)) {
|
|
|
+ throw new BusinessException("易拍产品未处在竞价状态");
|
|
|
+ }
|
|
|
+ BigDecimal serviceCharge = (BigDecimal.valueOf(0.03).multiply(tradeAuction.getCurrentPrice()))
|
|
|
+ .add(tradeAuction.getCurrentPrice().subtract(tradeAuction.getPrice()));
|
|
|
+ TradeAuctionOrder tradeAuctionOrder = TradeAuctionOrder.builder()
|
|
|
+ .tradeAuctionId(tradeAuction.getId())
|
|
|
+ .earnedPrice(price.subtract(tradeAuction.getPrice()))
|
|
|
+ .serviceCharge(serviceCharge)
|
|
|
+ .name(tradeAuction.getName())
|
|
|
+ .nickname(user.getNickname())
|
|
|
+ .userId(user.getId())
|
|
|
+ .originPrice(tradeAuction.getPrice())
|
|
|
+ .currentPrice(tradeAuction.getCurrentPrice())
|
|
|
+ .price(price)
|
|
|
+ .paybackStatus(PaybackStatus.NOPASSED)
|
|
|
+ .pic(tradeAuction.getPic())
|
|
|
+ .source(AuctionSource.OFFICIAL)
|
|
|
+ .status(AuctionOrderStatus.NOT_PAID)
|
|
|
+ .build();
|
|
|
+ tradeAuctionOrder = tradeAuctionOrderRepo.save(tradeAuctionOrder);
|
|
|
+ //下订单减库存
|
|
|
+ tradeAuctionService.decreaseStock(tradeAuction.getStock(), 1);
|
|
|
+ return tradeAuctionOrder;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Transactional
|
|
|
+ public void notify(Long orderId, String transactionId, PayMethod payMethod) {
|
|
|
+ TradeAuctionOrder tradeAuctionOrder = tradeAuctionOrderRepo.findById(orderId)
|
|
|
+ .orElseThrow(new BusinessException("未找到订单"));
|
|
|
+ if (!tradeAuctionOrder.getStatus().equals(AuctionOrderStatus.NOT_PAID)) {
|
|
|
+ throw new BusinessException("订单已经处理");
|
|
|
+ }
|
|
|
+ tradeAuctionOrder.setPayTime(LocalDateTime.now());
|
|
|
+ tradeAuctionOrder.setPayMethod(payMethod);
|
|
|
+ tradeAuctionOrder.setTransactionId(transactionId);
|
|
|
+ tradeAuctionOrder.setStatus(AuctionOrderStatus.FINISH);
|
|
|
+ tradeAuctionOrderRepo.save(tradeAuctionOrder);
|
|
|
+
|
|
|
+ TradeAuction tradeAuction = tradeAuctionRepo.findById(tradeAuctionOrder.getTradeAuctionId())
|
|
|
+ .orElseThrow(new BusinessException("未找到该易拍活动"));
|
|
|
+ tradeAuction.setStatus(TradeAuctionStatus.WAITING);
|
|
|
+ tradeAuction.setCurrentPrice(tradeAuctionOrder.getCurrentPrice());
|
|
|
+ tradeAuction.setCurrentOwner(tradeAuctionOrder.getNickname());
|
|
|
+ tradeAuction.setCurrentOwnerId(tradeAuctionOrder.getUserId());
|
|
|
+ tradeAuction.setCurrentEndTime(tradeAuction.getCurrentEndTime().plusDays(1));
|
|
|
+ tradeAuctionRepo.save(tradeAuction);
|
|
|
+
|
|
|
+ tradeAuctionService.increaseSale(tradeAuction.getId(), 1);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Scheduled(fixedRate = 30000)
|
|
|
+ public void batchCancelledAuctionOrder() {
|
|
|
+ List<TradeAuctionOrder> orders = tradeAuctionOrderRepo
|
|
|
+ .findByStatusAndCreatedAtBeforeAndDelFalse(AuctionOrderStatus.NOT_PAID,
|
|
|
+ LocalDateTime.now().minusSeconds(orderCancelInterval));
|
|
|
+ orders.parallelStream().forEach(o -> {
|
|
|
+ try {
|
|
|
+ TradeAuctionOrder order = tradeAuctionOrderRepo.findById(o.getId())
|
|
|
+ .orElseThrow(new BusinessException("订单不存在"));
|
|
|
+ if (order.getStatus() == AuctionOrderStatus.NOT_PAID) {
|
|
|
+ cancel(order);
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.error("取消易拍订单错误 " + o.getId(), e);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ public void cancel(TradeAuctionOrder order) {
|
|
|
+ if (order.getStatus() != AuctionOrderStatus.NOT_PAID) {
|
|
|
+ throw new BusinessException("当前订单状态无法取消[" + order.getStatus().name() + "]");
|
|
|
+ }
|
|
|
+ order.setStatus(AuctionOrderStatus.CANCELLED);
|
|
|
+ order.setCancelTime(LocalDateTime.now());
|
|
|
+ tradeAuctionService.increaseStock(order.getTradeAuctionId(), 1);
|
|
|
+ tradeAuctionOrderRepo.save(order);
|
|
|
+ }
|
|
|
+}
|