Showing
2 changed files
with
63 additions
and
0 deletions
| ... | @@ -7,6 +7,8 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; | ... | @@ -7,6 +7,8 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; |
| 7 | import com.baomidou.mybatisplus.core.metadata.IPage; | 7 | import com.baomidou.mybatisplus.core.metadata.IPage; |
| 8 | import com.baomidou.mybatisplus.core.toolkit.Wrappers; | 8 | import com.baomidou.mybatisplus.core.toolkit.Wrappers; |
| 9 | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; | 9 | import com.baomidou.mybatisplus.extension.plugins.pagination.Page; |
| 10 | +import com.infoloop.tianting.clientcustomerorderservice.AddSkusSellQuantitiesRpcRequest; | ||
| 11 | +import com.infoloop.tianting.clientcustomerorderservice.AddSkusSellQuantitiesRpcResponse; | ||
| 10 | import com.infoloop.tianting.clientcustomerorderservice.BatchCreateClientCustomerOrderDetailsRpcRequest; | 12 | import com.infoloop.tianting.clientcustomerorderservice.BatchCreateClientCustomerOrderDetailsRpcRequest; |
| 11 | import com.infoloop.tianting.clientcustomerorderservice.BatchCreateClientCustomerOrderDetailsRpcResponse; | 13 | import com.infoloop.tianting.clientcustomerorderservice.BatchCreateClientCustomerOrderDetailsRpcResponse; |
| 12 | import com.infoloop.tianting.clientcustomerorderservice.BatchCreateClientCustomerOrdersRpcRequest; | 14 | import com.infoloop.tianting.clientcustomerorderservice.BatchCreateClientCustomerOrdersRpcRequest; |
| ... | @@ -79,6 +81,7 @@ import org.springframework.stereotype.Service; | ... | @@ -79,6 +81,7 @@ import org.springframework.stereotype.Service; |
| 79 | 81 | ||
| 80 | import java.util.ArrayList; | 82 | import java.util.ArrayList; |
| 81 | import java.util.List; | 83 | import java.util.List; |
| 84 | +import java.util.function.Function; | ||
| 82 | import java.util.stream.Collectors; | 85 | import java.util.stream.Collectors; |
| 83 | 86 | ||
| 84 | import static com.tianting.infoloop.constants.ConfigConstants.DATA_SOURCE_MASTER; | 87 | import static com.tianting.infoloop.constants.ConfigConstants.DATA_SOURCE_MASTER; |
| ... | @@ -723,4 +726,48 @@ public class OrderGrpcService extends ClientCustomerOrderServiceRpcGrpc.ClientCu | ... | @@ -723,4 +726,48 @@ public class OrderGrpcService extends ClientCustomerOrderServiceRpcGrpc.ClientCu |
| 723 | responseStreamObserver.onCompleted(); | 726 | responseStreamObserver.onCompleted(); |
| 724 | } | 727 | } |
| 725 | 728 | ||
| 729 | + @Override | ||
| 730 | + @DS(DATA_SOURCE_MASTER) | ||
| 731 | + public void addSkusSellQuantities(final AddSkusSellQuantitiesRpcRequest request, | ||
| 732 | + final StreamObserver<AddSkusSellQuantitiesRpcResponse> responseStreamObserver) { | ||
| 733 | + final var builder = AddSkusSellQuantitiesRpcResponse.newBuilder(); | ||
| 734 | + try { | ||
| 735 | + LambdaQueryWrapper<SkuSellQuantity> queryWrapper = new LambdaQueryWrapper<>(); | ||
| 736 | + queryWrapper.eq(SkuSellQuantity::getEnterpriseId, request.getEnterpriseId()); | ||
| 737 | + queryWrapper.eq(SkuSellQuantity::getStallId, request.getStallId()); | ||
| 738 | + queryWrapper.in(SkuSellQuantity::getSkuId, request.getSkuSellQuantitiesList().stream() | ||
| 739 | + .map(com.infoloop.tianting.clientcustomerorderservice.SkuSellQuantity::getSkuId) | ||
| 740 | + .collect(Collectors.toList())); | ||
| 741 | + final var list = skuSellQuantityService.list(queryWrapper.last("FOR UPDATE")); | ||
| 742 | + final var skuSellQuantityMap = list.stream().collect(Collectors.toMap(SkuSellQuantity::getSkuId, Function.identity())); | ||
| 743 | + final var updates = new ArrayList<SkuSellQuantity>(); | ||
| 744 | + for (var sellQuantity : request.getSkuSellQuantitiesList()) { | ||
| 745 | + final var skuSellQuantity = skuSellQuantityMap.get(sellQuantity.getSkuId()); | ||
| 746 | + if (skuSellQuantity == null) { | ||
| 747 | + throw new IllegalArgumentException("addSkusSellQuantities skuId not exists; " + sellQuantity.getSkuId()); | ||
| 748 | + } | ||
| 749 | + final int todaySellQuantity = skuSellQuantity.getTodaySellQuantity() + sellQuantity.getQuantity(); | ||
| 750 | + if (todaySellQuantity > skuSellQuantity.getMaxSellQuantity()) { | ||
| 751 | + log.error("addSkusSellQuantities quantity exceeds maxSellQuantity; skuId={}, quantity={}, maxSellQuantity={}", sellQuantity.getSkuId(), sellQuantity.getQuantity(), skuSellQuantity.getMaxSellQuantity()); | ||
| 752 | + builder.setIsAdded(false); | ||
| 753 | + responseStreamObserver.onNext(builder.build()); | ||
| 754 | + responseStreamObserver.onCompleted(); | ||
| 755 | + return; | ||
| 756 | + } | ||
| 757 | + updates.add(new SkuSellQuantity() | ||
| 758 | + .setId(skuSellQuantity.getId()) | ||
| 759 | + .setTodaySellQuantity(todaySellQuantity)); | ||
| 760 | + } | ||
| 761 | + if (!updates.isEmpty()) { | ||
| 762 | + builder.setIsAdded(skuSellQuantityService.updateBatchById(updates)); | ||
| 763 | + } | ||
| 764 | + } catch (final Exception e) { | ||
| 765 | + log.error("addSkusSellQuantities error;", e); | ||
| 766 | + responseStreamObserver.onError(Status.INTERNAL.withDescription(e.getMessage()).asException()); | ||
| 767 | + return; | ||
| 768 | + } | ||
| 769 | + responseStreamObserver.onNext(builder.build()); | ||
| 770 | + responseStreamObserver.onCompleted(); | ||
| 771 | + } | ||
| 772 | + | ||
| 726 | } | 773 | } | ... | ... |
| ... | @@ -39,6 +39,7 @@ service ClientCustomerOrderServiceRpc { | ... | @@ -39,6 +39,7 @@ service ClientCustomerOrderServiceRpc { |
| 39 | rpc QuerySkuSellQuantitiesByStall(QuerySkuSellQuantitiesByStallRpcRequest) returns (QuerySkuSellQuantitiesByStallRpcResponse) {} | 39 | rpc QuerySkuSellQuantitiesByStall(QuerySkuSellQuantitiesByStallRpcRequest) returns (QuerySkuSellQuantitiesByStallRpcResponse) {} |
| 40 | rpc QuerySkuSellQuantitiesByStallAndSkuIds(QuerySkuSellQuantitiesByStallAndSkuIdsRpcRequest) returns (QuerySkuSellQuantitiesByStallAndSkuIdsRpcResponse) {} | 40 | rpc QuerySkuSellQuantitiesByStallAndSkuIds(QuerySkuSellQuantitiesByStallAndSkuIdsRpcRequest) returns (QuerySkuSellQuantitiesByStallAndSkuIdsRpcResponse) {} |
| 41 | rpc BatchSaveSkuSellQuantities(BatchSaveSkuSellQuantitiesRpcRequest) returns (BatchSaveSkuSellQuantitiesRpcResponse) {} | 41 | rpc BatchSaveSkuSellQuantities(BatchSaveSkuSellQuantitiesRpcRequest) returns (BatchSaveSkuSellQuantitiesRpcResponse) {} |
| 42 | + rpc AddSkusSellQuantities(AddSkusSellQuantitiesRpcRequest) returns (AddSkusSellQuantitiesRpcResponse) {} | ||
| 42 | 43 | ||
| 43 | } | 44 | } |
| 44 | 45 | ||
| ... | @@ -668,3 +669,18 @@ message SkuSellQuantityModification { | ... | @@ -668,3 +669,18 @@ message SkuSellQuantityModification { |
| 668 | message BatchSaveSkuSellQuantitiesRpcResponse { | 669 | message BatchSaveSkuSellQuantitiesRpcResponse { |
| 669 | bool isSaved = 1; | 670 | bool isSaved = 1; |
| 670 | } | 671 | } |
| 672 | + | ||
| 673 | +message AddSkusSellQuantitiesRpcRequest { | ||
| 674 | + int32 enterpriseId = 1; | ||
| 675 | + int32 stallId = 2; | ||
| 676 | + repeated SkuSellQuantity skuSellQuantities = 3; | ||
| 677 | +} | ||
| 678 | + | ||
| 679 | +message SkuSellQuantity { | ||
| 680 | + int32 skuId = 1; | ||
| 681 | + int32 quantity = 2; | ||
| 682 | +} | ||
| 683 | + | ||
| 684 | +message AddSkusSellQuantitiesRpcResponse { | ||
| 685 | + bool isAdded = 1; | ||
| 686 | +} | ... | ... |
-
Please register or login to post a comment