|
|
@@ -20,6 +20,7 @@ import com.izouma.awesomeAdmin.exception.BusinessException;
|
|
|
import com.izouma.awesomeAdmin.repo.*;
|
|
|
import com.izouma.awesomeAdmin.security.Authority;
|
|
|
import com.izouma.awesomeAdmin.utils.SecurityUtils;
|
|
|
+import com.izouma.awesomeAdmin.utils.Translator;
|
|
|
import lombok.AllArgsConstructor;
|
|
|
import org.apache.commons.beanutils.BeanUtils;
|
|
|
import org.apache.commons.collections.MapUtils;
|
|
|
@@ -75,6 +76,7 @@ public class OrderService {
|
|
|
private final DelegationRepo delegationRepo;
|
|
|
private final CommissionRecordRepo commissionRecordRepo;
|
|
|
private final Environment env;
|
|
|
+ private final UserBalanceService userBalanceService;
|
|
|
|
|
|
public Page<Order> all(@RequestBody PageQuery pageQuery) {
|
|
|
Page<Order> page = orderRepo.findAll((Specification<Order>) (root, criteriaQuery, criteriaBuilder) -> {
|
|
|
@@ -114,7 +116,7 @@ public class OrderService {
|
|
|
}
|
|
|
|
|
|
public Order get(Long id) {
|
|
|
- Order order = orderRepo.findById(id).orElseThrow(new BusinessException("无记录"));
|
|
|
+ Order order = orderRepo.findById(id).orElseThrow(new BusinessException(Translator.toLocale("record.not_found")));
|
|
|
order.setUser(userRepo.findById(order.getUserId()).orElse(null));
|
|
|
order.setFromUser(order.getFromUserId() == null ? null : userRepo.findById(order.getFromUserId())
|
|
|
.orElse(null));
|
|
|
@@ -134,7 +136,7 @@ public class OrderService {
|
|
|
if (!category.isOpen()) throw new BusinessException("画苑已关闭,不可下单");
|
|
|
category.checkTime();
|
|
|
|
|
|
- User user = userRepo.findById(userId).orElseThrow(new BusinessException("用户不存在"));
|
|
|
+ User user = userRepo.findById(userId).orElseThrow(new BusinessException(Translator.toLocale("user.not_found")));
|
|
|
long count = orderRepo.count((Specification<Order>) (root, criteriaQuery, criteriaBuilder) ->
|
|
|
criteriaBuilder.and(
|
|
|
criteriaBuilder.equal(root.get("userId"), userId),
|
|
|
@@ -144,9 +146,9 @@ public class OrderService {
|
|
|
throw new BusinessException("最多同时购买" + user.getMaxOrderCount() + "幅");
|
|
|
}
|
|
|
Delegation delegation = delegationRepo.findById(product.getDelegationId())
|
|
|
- .orElseThrow(new BusinessException("无记录"));
|
|
|
+ .orElseThrow(new BusinessException(Translator.toLocale("record.not_found")));
|
|
|
Order sellerOrder = orderRepo.findById(delegation.getSellerOrderId())
|
|
|
- .orElseThrow(new BusinessException("订单不存在"));
|
|
|
+ .orElseThrow(new BusinessException(Translator.toLocale("order.not_found")));
|
|
|
if (product.getStatus() == ProductStatus.SOLD_OUT) {
|
|
|
throw new BusinessException("该商品已售罄");
|
|
|
}
|
|
|
@@ -192,7 +194,7 @@ public class OrderService {
|
|
|
|
|
|
public void cancelOrder(Long orderId, boolean checkRole, boolean checkStatus) {
|
|
|
SecurityUtils.checkBanned();
|
|
|
- Order buyerOrder = orderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
|
|
|
+ Order buyerOrder = orderRepo.findById(orderId).orElseThrow(new BusinessException(Translator.toLocale("order.not_found")));
|
|
|
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
|
|
|
boolean hasAdminRole = false;
|
|
|
if (authentication != null) {
|
|
|
@@ -201,23 +203,23 @@ public class OrderService {
|
|
|
}
|
|
|
if (checkRole && !hasAdminRole &&
|
|
|
!buyerOrder.getUserId().equals(SecurityUtils.getAuthenticatedUser().getId())) {
|
|
|
- throw new BusinessException("无权操作");
|
|
|
+ throw new BusinessException(Translator.toLocale("permission.denied"));
|
|
|
}
|
|
|
Delegation delegation = delegationRepo.findByBuyerOrderId(orderId)
|
|
|
- .orElseThrow(new BusinessException("无委托记录"));
|
|
|
+ .orElseThrow(new BusinessException(Translator.toLocale("delegation.not_found")));
|
|
|
Order sellerOrder = orderRepo.findById(delegation.getSellerOrderId())
|
|
|
- .orElseThrow(new BusinessException("订单不存在"));
|
|
|
- Product product = productRepo.findById(buyerOrder.getProductId()).orElseThrow(new BusinessException("商品不存在"));
|
|
|
+ .orElseThrow(new BusinessException(Translator.toLocale("order.not_found")));
|
|
|
+ Product product = productRepo.findById(buyerOrder.getProductId()).orElseThrow(new BusinessException(Translator.toLocale("product.not_found")));
|
|
|
if (checkStatus) {
|
|
|
if (buyerOrder.getStatus() != OrderStatus.NOT_PAID) {
|
|
|
- throw new BusinessException("状态错误");
|
|
|
+ throw new BusinessException(Translator.toLocale("status.error"));
|
|
|
}
|
|
|
} else {
|
|
|
if (!EnumSet.of(OrderStatus.NOT_PAID,
|
|
|
- OrderStatus.NOT_CONFIRMED,
|
|
|
- OrderStatus.CONFIRMED)
|
|
|
+ OrderStatus.NOT_CONFIRMED,
|
|
|
+ OrderStatus.CONFIRMED)
|
|
|
.contains(buyerOrder.getStatus())) {
|
|
|
- throw new BusinessException("状态错误");
|
|
|
+ throw new BusinessException(Translator.toLocale("status.error"));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -238,15 +240,15 @@ public class OrderService {
|
|
|
|
|
|
public void confirmPayment(Long userId, Long orderId, boolean checkUser) {
|
|
|
SecurityUtils.checkBanned();
|
|
|
- Delegation delegation = delegationRepo.findByBuyerOrderId(orderId).orElseThrow(new BusinessException("无委托记录"));
|
|
|
- Order buyerOrder = orderRepo.findById(orderId).orElseThrow(new BusinessException("买家订单不存在"));
|
|
|
+ Delegation delegation = delegationRepo.findByBuyerOrderId(orderId).orElseThrow(new BusinessException(Translator.toLocale("delegation.not_found")));
|
|
|
+ Order buyerOrder = orderRepo.findById(orderId).orElseThrow(new BusinessException(Translator.toLocale("order.buyer.not_found")));
|
|
|
Order sellerOrder = orderRepo.findById(delegation.getSellerOrderId())
|
|
|
- .orElseThrow(new BusinessException("卖家订不存在"));
|
|
|
+ .orElseThrow(new BusinessException(Translator.toLocale("order.seller.not_found")));
|
|
|
if (checkUser && !buyerOrder.getUserId().equals(userId)) {
|
|
|
- throw new BusinessException("无操作权限");
|
|
|
+ throw new BusinessException(Translator.toLocale("permission.denied"));
|
|
|
}
|
|
|
if (buyerOrder.getStatus() != OrderStatus.NOT_PAID) {
|
|
|
- throw new BusinessException("状态错误,请刷新后重试");
|
|
|
+ throw new BusinessException(Translator.toLocale("status.error"));
|
|
|
}
|
|
|
sellerOrder.setStatus(OrderStatus.SOLD_NOT_CONFIRMED);
|
|
|
|
|
|
@@ -257,22 +259,50 @@ public class OrderService {
|
|
|
|
|
|
public void confirmReceipt(Long userId, Long orderId, boolean checkUser) {
|
|
|
SecurityUtils.checkBanned();
|
|
|
- Order sellerOrder = orderRepo.findById(orderId).orElseThrow(new BusinessException("卖家订单不存在"));
|
|
|
+ Order sellerOrder = orderRepo.findById(orderId).orElseThrow(new BusinessException(Translator.toLocale("order.seller.not_found")));
|
|
|
if (checkUser && !sellerOrder.getUserId().equals(userId)) {
|
|
|
- throw new BusinessException("无操作权限");
|
|
|
+ throw new BusinessException(Translator.toLocale("permission.denied"));
|
|
|
}
|
|
|
if (sellerOrder.getStatus() != OrderStatus.SOLD_NOT_CONFIRMED) {
|
|
|
- throw new BusinessException("状态错误,请刷新后重试");
|
|
|
+ throw new BusinessException(Translator.toLocale("status.error"));
|
|
|
}
|
|
|
- Product product = productRepo.findById(sellerOrder.getProductId()).orElseThrow(new BusinessException("商品不存在"));
|
|
|
- Delegation delegation = delegationRepo.findBySellerOrderId(orderId).orElseThrow(new BusinessException("无委托记录"));
|
|
|
+ Product product = productRepo.findById(sellerOrder.getProductId()).orElseThrow(new BusinessException(Translator.toLocale("product.not_found")));
|
|
|
+ Delegation delegation = delegationRepo.findBySellerOrderId(orderId).orElseThrow(new BusinessException(Translator.toLocale("delegation.not_found")));
|
|
|
Order buyerOrder = orderRepo.findById(delegation.getBuyerOrderId())
|
|
|
- .orElseThrow(new BusinessException("买家订单不存在"));
|
|
|
+ .orElseThrow(new BusinessException(Translator.toLocale("order.buyer.not_found")));
|
|
|
if (buyerOrder.getStatus() != OrderStatus.NOT_CONFIRMED) {
|
|
|
throw new BusinessException("买家还没有确认付款");
|
|
|
}
|
|
|
- User seller = userRepo.findById(sellerOrder.getUserId()).orElseThrow(new BusinessException("用不存在"));
|
|
|
- User buyer = userRepo.findById(buyerOrder.getUserId()).orElseThrow(new BusinessException("用户不存在"));
|
|
|
+ User seller = userRepo.findById(sellerOrder.getUserId()).orElseThrow(new BusinessException(Translator.toLocale("user.not_found")));
|
|
|
+ User buyer = userRepo.findById(buyerOrder.getUserId()).orElseThrow(new BusinessException(Translator.toLocale("user.not_found")));
|
|
|
+
|
|
|
+ sellerOrder.setStatus(OrderStatus.SOLD);
|
|
|
+ sellerOrder.setSoldTime(LocalDateTime.now());
|
|
|
+ orderRepo.save(sellerOrder);
|
|
|
+ buyerOrder.setStatus(OrderStatus.CONFIRMED);
|
|
|
+ buyerOrder.setConfirmTime(LocalDateTime.now());
|
|
|
+ orderRepo.save(buyerOrder);
|
|
|
+
|
|
|
+ product.setUserId(buyerOrder.getUserId());
|
|
|
+ product.setSales(Optional.ofNullable(product.getSales()).orElse(0) + 1);
|
|
|
+ productRepo.save(product);
|
|
|
+ }
|
|
|
+
|
|
|
+ public void balancePay(Long userId, Long orderId) {
|
|
|
+ SecurityUtils.checkBanned();
|
|
|
+ Delegation delegation = delegationRepo.findByBuyerOrderId(orderId).orElseThrow(new BusinessException(Translator.toLocale("delegation.not_found")));
|
|
|
+ Order buyerOrder = orderRepo.findById(orderId).orElseThrow(new BusinessException(Translator.toLocale("order.seller.not_found")));
|
|
|
+ Order sellerOrder = orderRepo.findById(delegation.getSellerOrderId())
|
|
|
+ .orElseThrow(new BusinessException(Translator.toLocale("order.buyer.not_found")));
|
|
|
+ Product product = productRepo.findById(sellerOrder.getProductId()).orElseThrow(new BusinessException(Translator.toLocale("product.not_found")));
|
|
|
+ if (buyerOrder.getStatus() != OrderStatus.NOT_PAID) {
|
|
|
+ throw new BusinessException(Translator.toLocale("status.error"));
|
|
|
+ }
|
|
|
+ if (sellerOrder.getStatus() != OrderStatus.SOLD_NOT_CONFIRMED) {
|
|
|
+ throw new BusinessException(Translator.toLocale("status.error"));
|
|
|
+ }
|
|
|
+ userBalanceService.modify(userId, buyerOrder.getInterest().negate(), Constants.BalanceRemark.PAY);
|
|
|
+ userBalanceService.modify(sellerOrder.getUserId(), buyerOrder.getInterest(), Constants.BalanceRemark.RECEIPT);
|
|
|
|
|
|
sellerOrder.setStatus(OrderStatus.SOLD);
|
|
|
sellerOrder.setSoldTime(LocalDateTime.now());
|
|
|
@@ -288,13 +318,13 @@ public class OrderService {
|
|
|
|
|
|
public void applyShip(Long userId, Long orderId) {
|
|
|
SecurityUtils.checkBanned();
|
|
|
- Order order = orderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
|
|
|
- Product product = productRepo.findById(order.getProductId()).orElseThrow(new BusinessException("商品不存在"));
|
|
|
+ Order order = orderRepo.findById(orderId).orElseThrow(new BusinessException(Translator.toLocale("order.not_found")));
|
|
|
+ Product product = productRepo.findById(order.getProductId()).orElseThrow(new BusinessException(Translator.toLocale("product.not_found")));
|
|
|
if (!order.getUserId().equals(userId)) {
|
|
|
- throw new BusinessException("无操作权限");
|
|
|
+ throw new BusinessException(Translator.toLocale("permission.denied"));
|
|
|
}
|
|
|
if (!(order.getStatus() == OrderStatus.CONFIRMED || order.getStatus() == SELLING)) {
|
|
|
- throw new BusinessException("状态错误,请刷新后重试");
|
|
|
+ throw new BusinessException(Translator.toLocale("status.error"));
|
|
|
}
|
|
|
if (order.getStatus() == SELLING) {
|
|
|
Delegation delegation = delegationRepo.findBySellerOrderId(orderId).orElse(null);
|
|
|
@@ -311,8 +341,8 @@ public class OrderService {
|
|
|
}
|
|
|
|
|
|
public void cancelShip(Long orderId) {
|
|
|
- Order order = orderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
|
|
|
- Product product = productRepo.findById(order.getProductId()).orElseThrow(new BusinessException("商品不存在"));
|
|
|
+ Order order = orderRepo.findById(orderId).orElseThrow(new BusinessException(Translator.toLocale("order.not_found")));
|
|
|
+ Product product = productRepo.findById(order.getProductId()).orElseThrow(new BusinessException(Translator.toLocale("product.not_found")));
|
|
|
if (order.getStatus() == NOT_SHIPPED || order.getStatus() == SHIPPED) {
|
|
|
product.setLocked(order.isLocked());
|
|
|
product.setStatus(ProductStatus.IN_STOCK);
|
|
|
@@ -325,12 +355,12 @@ public class OrderService {
|
|
|
|
|
|
public void shipOrder(Long userId, Long orderId, String logisticsType, String logisticsNo) {
|
|
|
SecurityUtils.checkBanned();
|
|
|
- Order order = orderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
|
|
|
+ Order order = orderRepo.findById(orderId).orElseThrow(new BusinessException(Translator.toLocale("order.not_found")));
|
|
|
// if (!order.getUserId().equals(userId)) {
|
|
|
-// throw new BusinessException("无操作权限");
|
|
|
+// throw new BusinessException(Translator.toLocale("permission.denied"));
|
|
|
// }
|
|
|
if (order.getStatus() != OrderStatus.NOT_SHIPPED) {
|
|
|
- throw new BusinessException("状态错误,请刷新后重试");
|
|
|
+ throw new BusinessException(Translator.toLocale("status.error"));
|
|
|
}
|
|
|
order.setStatus(OrderStatus.SHIPPED);
|
|
|
order.setLogisticsType(logisticsType);
|
|
|
@@ -340,12 +370,12 @@ public class OrderService {
|
|
|
|
|
|
public void receiveOrder(Long userId, Long orderId) {
|
|
|
SecurityUtils.checkBanned();
|
|
|
- Order order = orderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
|
|
|
+ Order order = orderRepo.findById(orderId).orElseThrow(new BusinessException(Translator.toLocale("order.not_found")));
|
|
|
if (!order.getUserId().equals(userId)) {
|
|
|
- throw new BusinessException("无操作权限");
|
|
|
+ throw new BusinessException(Translator.toLocale("permission.denied"));
|
|
|
}
|
|
|
if (order.getStatus() != OrderStatus.SHIPPED) {
|
|
|
- throw new BusinessException("状态错误,请刷新后重试");
|
|
|
+ throw new BusinessException(Translator.toLocale("status.error"));
|
|
|
}
|
|
|
order.setStatus(OrderStatus.RECEIVED);
|
|
|
order.setReceiveTime(LocalDateTime.now());
|
|
|
@@ -433,8 +463,8 @@ public class OrderService {
|
|
|
}
|
|
|
|
|
|
public ModelAndView payOrder(Long orderId) throws WriterException, IOException {
|
|
|
- Order order = orderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
|
|
|
- User fromUser = userRepo.findById(order.getFromUserId()).orElseThrow(new BusinessException("用户不存在"));
|
|
|
+ Order order = orderRepo.findById(orderId).orElseThrow(new BusinessException(Translator.toLocale("order.not_found")));
|
|
|
+ User fromUser = userRepo.findById(order.getFromUserId()).orElseThrow(new BusinessException(Translator.toLocale("user.not_found")));
|
|
|
ModelAndView modelAndView = new ModelAndView("PayOrder");
|
|
|
modelAndView.addObject("orderId", orderId);
|
|
|
modelAndView.addObject("name", fromUser.getNickname());
|
|
|
@@ -500,25 +530,25 @@ public class OrderService {
|
|
|
}
|
|
|
|
|
|
public void setLocked(Long orderId, boolean locked) {
|
|
|
- Order order = orderRepo.findById(orderId).orElseThrow(new BusinessException("订单不存在"));
|
|
|
+ Order order = orderRepo.findById(orderId).orElseThrow(new BusinessException(Translator.toLocale("order.not_found")));
|
|
|
if (order.getStatus() != SELLING) {
|
|
|
throw new BusinessException("出售中的订单才能进行此操作");
|
|
|
}
|
|
|
order.setLocked(locked);
|
|
|
orderRepo.save(order);
|
|
|
- Product product = productRepo.findById(order.getProductId()).orElseThrow(new BusinessException("商品不存在"));
|
|
|
+ Product product = productRepo.findById(order.getProductId()).orElseThrow(new BusinessException(Translator.toLocale("product.not_found")));
|
|
|
product.setLocked(locked);
|
|
|
productRepo.save(product);
|
|
|
}
|
|
|
|
|
|
public void del(Long id) {
|
|
|
- Order order = orderRepo.findById(id).orElseThrow(new BusinessException("订单不存在"));
|
|
|
+ Order order = orderRepo.findById(id).orElseThrow(new BusinessException(Translator.toLocale("order.not_found")));
|
|
|
order.setDel(true);
|
|
|
orderRepo.save(order);
|
|
|
}
|
|
|
|
|
|
public void restore(Long id) {
|
|
|
- Order order = orderRepo.findById(id).orElseThrow(new BusinessException("订单不存在"));
|
|
|
+ Order order = orderRepo.findById(id).orElseThrow(new BusinessException(Translator.toLocale("order.not_found")));
|
|
|
order.setDel(false);
|
|
|
orderRepo.save(order);
|
|
|
}
|