zhuyifan

feat(ClientCustomerOrderService): 添加增加 SKU 销售数量的功能

......@@ -7,6 +7,8 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.infoloop.tianting.clientcustomerorderservice.AddSkusSellQuantitiesRpcRequest;
import com.infoloop.tianting.clientcustomerorderservice.AddSkusSellQuantitiesRpcResponse;
import com.infoloop.tianting.clientcustomerorderservice.BatchCreateClientCustomerOrderDetailsRpcRequest;
import com.infoloop.tianting.clientcustomerorderservice.BatchCreateClientCustomerOrderDetailsRpcResponse;
import com.infoloop.tianting.clientcustomerorderservice.BatchCreateClientCustomerOrdersRpcRequest;
......@@ -79,6 +81,7 @@ import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import static com.tianting.infoloop.constants.ConfigConstants.DATA_SOURCE_MASTER;
......@@ -723,4 +726,48 @@ public class OrderGrpcService extends ClientCustomerOrderServiceRpcGrpc.ClientCu
responseStreamObserver.onCompleted();
}
@Override
@DS(DATA_SOURCE_MASTER)
public void addSkusSellQuantities(final AddSkusSellQuantitiesRpcRequest request,
final StreamObserver<AddSkusSellQuantitiesRpcResponse> responseStreamObserver) {
final var builder = AddSkusSellQuantitiesRpcResponse.newBuilder();
try {
LambdaQueryWrapper<SkuSellQuantity> queryWrapper = new LambdaQueryWrapper<>();
queryWrapper.eq(SkuSellQuantity::getEnterpriseId, request.getEnterpriseId());
queryWrapper.eq(SkuSellQuantity::getStallId, request.getStallId());
queryWrapper.in(SkuSellQuantity::getSkuId, request.getSkuSellQuantitiesList().stream()
.map(com.infoloop.tianting.clientcustomerorderservice.SkuSellQuantity::getSkuId)
.collect(Collectors.toList()));
final var list = skuSellQuantityService.list(queryWrapper.last("FOR UPDATE"));
final var skuSellQuantityMap = list.stream().collect(Collectors.toMap(SkuSellQuantity::getSkuId, Function.identity()));
final var updates = new ArrayList<SkuSellQuantity>();
for (var sellQuantity : request.getSkuSellQuantitiesList()) {
final var skuSellQuantity = skuSellQuantityMap.get(sellQuantity.getSkuId());
if (skuSellQuantity == null) {
throw new IllegalArgumentException("addSkusSellQuantities skuId not exists; " + sellQuantity.getSkuId());
}
final int todaySellQuantity = skuSellQuantity.getTodaySellQuantity() + sellQuantity.getQuantity();
if (todaySellQuantity > skuSellQuantity.getMaxSellQuantity()) {
log.error("addSkusSellQuantities quantity exceeds maxSellQuantity; skuId={}, quantity={}, maxSellQuantity={}", sellQuantity.getSkuId(), sellQuantity.getQuantity(), skuSellQuantity.getMaxSellQuantity());
builder.setIsAdded(false);
responseStreamObserver.onNext(builder.build());
responseStreamObserver.onCompleted();
return;
}
updates.add(new SkuSellQuantity()
.setId(skuSellQuantity.getId())
.setTodaySellQuantity(todaySellQuantity));
}
if (!updates.isEmpty()) {
builder.setIsAdded(skuSellQuantityService.updateBatchById(updates));
}
} catch (final Exception e) {
log.error("addSkusSellQuantities error;", e);
responseStreamObserver.onError(Status.INTERNAL.withDescription(e.getMessage()).asException());
return;
}
responseStreamObserver.onNext(builder.build());
responseStreamObserver.onCompleted();
}
}
......
......@@ -39,6 +39,7 @@ service ClientCustomerOrderServiceRpc {
rpc QuerySkuSellQuantitiesByStall(QuerySkuSellQuantitiesByStallRpcRequest) returns (QuerySkuSellQuantitiesByStallRpcResponse) {}
rpc QuerySkuSellQuantitiesByStallAndSkuIds(QuerySkuSellQuantitiesByStallAndSkuIdsRpcRequest) returns (QuerySkuSellQuantitiesByStallAndSkuIdsRpcResponse) {}
rpc BatchSaveSkuSellQuantities(BatchSaveSkuSellQuantitiesRpcRequest) returns (BatchSaveSkuSellQuantitiesRpcResponse) {}
rpc AddSkusSellQuantities(AddSkusSellQuantitiesRpcRequest) returns (AddSkusSellQuantitiesRpcResponse) {}
}
......@@ -668,3 +669,18 @@ message SkuSellQuantityModification {
message BatchSaveSkuSellQuantitiesRpcResponse {
bool isSaved = 1;
}
message AddSkusSellQuantitiesRpcRequest {
int32 enterpriseId = 1;
int32 stallId = 2;
repeated SkuSellQuantity skuSellQuantities = 3;
}
message SkuSellQuantity {
int32 skuId = 1;
int32 quantity = 2;
}
message AddSkusSellQuantitiesRpcResponse {
bool isAdded = 1;
}
......