feat: add group buy secondhand and file storage
This commit is contained in:
@@ -0,0 +1,158 @@
|
||||
package com.linhelp.groupbuy;
|
||||
|
||||
import com.linhelp.common.enums.DeliveryMethod;
|
||||
import com.linhelp.common.enums.GroupBuyOrderStatus;
|
||||
import com.linhelp.common.enums.OfflinePayStatus;
|
||||
import com.linhelp.common.exception.BizException;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.time.LocalDateTime;
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
|
||||
@Service
|
||||
public class GroupBuyOrderService {
|
||||
private final GroupBuyService groupBuyService;
|
||||
private final AtomicLong ids = new AtomicLong(1L);
|
||||
private final Map<Long, GroupBuyOrderResponse> orders = new LinkedHashMap<Long, GroupBuyOrderResponse>();
|
||||
|
||||
public GroupBuyOrderService(GroupBuyService groupBuyService) {
|
||||
this.groupBuyService = groupBuyService;
|
||||
}
|
||||
|
||||
public synchronized GroupBuyOrderResponse create(Long userId, GroupBuyOrderRequest request) {
|
||||
if (request.getDeliveryMethod() != DeliveryMethod.SELF_PICKUP && request.getAddressId() == null) {
|
||||
throw new BizException("配送地址不能为空");
|
||||
}
|
||||
GroupBuyResponse groupBuy = groupBuyService.reserveForOrder(request.getGroupBuyId(), request.getQuantity());
|
||||
Long orderId = ids.getAndIncrement();
|
||||
GroupBuyOrderResponse order = new GroupBuyOrderResponse();
|
||||
order.setId(orderId);
|
||||
order.setOrderNo("GB" + String.format("%08d", orderId));
|
||||
order.setTenantId(request.getTenantId());
|
||||
order.setCommunityId(request.getCommunityId());
|
||||
order.setUserId(userId);
|
||||
order.setGroupBuyId(groupBuy.getId());
|
||||
order.setGroupBuyTitle(groupBuy.getTitle());
|
||||
order.setCoverUrl(groupBuy.getCoverUrl());
|
||||
order.setAddressId(request.getAddressId());
|
||||
order.setQuantity(request.getQuantity());
|
||||
order.setPriceCent(groupBuy.getPriceCent());
|
||||
order.setAmountCent(groupBuy.getPriceCent() * request.getQuantity());
|
||||
order.setDeliveryMethod(request.getDeliveryMethod());
|
||||
order.setStatus(GroupBuyOrderStatus.PENDING_CONFIRM);
|
||||
order.setOfflinePayStatus(OfflinePayStatus.UNPAID);
|
||||
order.setRemark(request.getRemark());
|
||||
order.setCreatedAt(LocalDateTime.now());
|
||||
order.setUpdatedAt(order.getCreatedAt());
|
||||
orders.put(order.getId(), order);
|
||||
return copy(order);
|
||||
}
|
||||
|
||||
public synchronized GroupBuyOrderResponse detail(Long id) {
|
||||
return copy(require(id));
|
||||
}
|
||||
|
||||
public synchronized List<GroupBuyOrderResponse> listMine(Long userId) {
|
||||
List<GroupBuyOrderResponse> result = new ArrayList<GroupBuyOrderResponse>();
|
||||
for (GroupBuyOrderResponse order : orders.values()) {
|
||||
if (userId.equals(order.getUserId())) {
|
||||
result.add(copy(order));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public synchronized List<GroupBuyOrderResponse> listByGroupBuy(Long groupBuyId) {
|
||||
List<GroupBuyOrderResponse> result = new ArrayList<GroupBuyOrderResponse>();
|
||||
for (GroupBuyOrderResponse order : orders.values()) {
|
||||
if (groupBuyId.equals(order.getGroupBuyId())) {
|
||||
result.add(copy(order));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public synchronized GroupBuyOrderResponse confirm(Long id) {
|
||||
GroupBuyOrderResponse order = require(id);
|
||||
move(order, GroupBuyOrderStatus.PENDING_CONFIRM, GroupBuyOrderStatus.CONFIRMED);
|
||||
return copy(order);
|
||||
}
|
||||
|
||||
public synchronized GroupBuyOrderResponse markReady(Long id) {
|
||||
GroupBuyOrderResponse order = require(id);
|
||||
GroupBuyOrderStatus target = DeliveryMethod.SELF_PICKUP.equals(order.getDeliveryMethod())
|
||||
? GroupBuyOrderStatus.PENDING_PICKUP
|
||||
: GroupBuyOrderStatus.PENDING_DELIVERY;
|
||||
move(order, GroupBuyOrderStatus.CONFIRMED, target);
|
||||
return copy(order);
|
||||
}
|
||||
|
||||
public synchronized GroupBuyOrderResponse markDelivering(Long id) {
|
||||
GroupBuyOrderResponse order = require(id);
|
||||
move(order, GroupBuyOrderStatus.PENDING_DELIVERY, GroupBuyOrderStatus.DELIVERING);
|
||||
return copy(order);
|
||||
}
|
||||
|
||||
public synchronized GroupBuyOrderResponse complete(Long id) {
|
||||
GroupBuyOrderResponse order = require(id);
|
||||
if (order.getStatus() != GroupBuyOrderStatus.PENDING_PICKUP && order.getStatus() != GroupBuyOrderStatus.DELIVERING) {
|
||||
throw new BizException("订单状态不允许操作");
|
||||
}
|
||||
order.setStatus(GroupBuyOrderStatus.COMPLETED);
|
||||
order.setUpdatedAt(LocalDateTime.now());
|
||||
return copy(order);
|
||||
}
|
||||
|
||||
public synchronized GroupBuyOrderResponse cancel(Long id) {
|
||||
GroupBuyOrderResponse order = require(id);
|
||||
if (order.getStatus() == GroupBuyOrderStatus.COMPLETED) {
|
||||
throw new BizException("订单状态不允许操作");
|
||||
}
|
||||
order.setStatus(GroupBuyOrderStatus.CANCELED);
|
||||
order.setUpdatedAt(LocalDateTime.now());
|
||||
return copy(order);
|
||||
}
|
||||
|
||||
private void move(GroupBuyOrderResponse order, GroupBuyOrderStatus expected, GroupBuyOrderStatus target) {
|
||||
if (order.getStatus() != expected) {
|
||||
throw new BizException("订单状态不允许操作");
|
||||
}
|
||||
order.setStatus(target);
|
||||
order.setUpdatedAt(LocalDateTime.now());
|
||||
}
|
||||
|
||||
private GroupBuyOrderResponse require(Long id) {
|
||||
GroupBuyOrderResponse order = orders.get(id);
|
||||
if (order == null) {
|
||||
throw new BizException(404, "团购订单不存在");
|
||||
}
|
||||
return order;
|
||||
}
|
||||
|
||||
private GroupBuyOrderResponse copy(GroupBuyOrderResponse source) {
|
||||
GroupBuyOrderResponse target = new GroupBuyOrderResponse();
|
||||
target.setId(source.getId());
|
||||
target.setOrderNo(source.getOrderNo());
|
||||
target.setTenantId(source.getTenantId());
|
||||
target.setCommunityId(source.getCommunityId());
|
||||
target.setUserId(source.getUserId());
|
||||
target.setGroupBuyId(source.getGroupBuyId());
|
||||
target.setGroupBuyTitle(source.getGroupBuyTitle());
|
||||
target.setCoverUrl(source.getCoverUrl());
|
||||
target.setAddressId(source.getAddressId());
|
||||
target.setQuantity(source.getQuantity());
|
||||
target.setPriceCent(source.getPriceCent());
|
||||
target.setAmountCent(source.getAmountCent());
|
||||
target.setDeliveryMethod(source.getDeliveryMethod());
|
||||
target.setStatus(source.getStatus());
|
||||
target.setOfflinePayStatus(source.getOfflinePayStatus());
|
||||
target.setRemark(source.getRemark());
|
||||
target.setCreatedAt(source.getCreatedAt());
|
||||
target.setUpdatedAt(source.getUpdatedAt());
|
||||
return target;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user