交易改造

pull/4/head
zhuyang 2021-10-09 22:10:39 +08:00
parent b5480816ef
commit 001892824c
12 changed files with 259 additions and 193 deletions

View File

@ -7,7 +7,8 @@ import org.springframework.cloud.openfeign.EnableFeignClients;
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients(basePackages = {"cn.iocoder.mall.productservice.rpc","cn.iocoder.mall.searchservice.rpc"})
@EnableFeignClients(basePackages = {"cn.iocoder.mall.productservice.rpc","cn.iocoder.mall.searchservice.rpc",
"cn.iocoder.mall.tradeservice.rpc"})
public class ShopWebApplication {
public static void main(String[] args) {

View File

@ -2,11 +2,11 @@ package cn.iocoder.mall.shopweb.client.trade;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.common.framework.vo.PageResult;
import cn.iocoder.mall.tradeservice.rpc.order.TradeOrderRpc;
import cn.iocoder.mall.tradeservice.rpc.order.TradeOrderFeign;
import cn.iocoder.mall.tradeservice.rpc.order.dto.TradeOrderCreateReqDTO;
import cn.iocoder.mall.tradeservice.rpc.order.dto.TradeOrderPageReqDTO;
import cn.iocoder.mall.tradeservice.rpc.order.dto.TradeOrderRespDTO;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.Arrays;
@ -14,23 +14,23 @@ import java.util.Arrays;
@Service
public class TradeOrderClient {
@DubboReference(version = "${dubbo.consumer.TradeOrderRpc.version}")
private TradeOrderRpc tradeOrderRpc;
@Autowired
private TradeOrderFeign tradeOrderFeign;
public Integer createTradeOrder(TradeOrderCreateReqDTO createReqDTO) {
CommonResult<Integer> createTradeOrderResult = tradeOrderRpc.createTradeOrder(createReqDTO);
CommonResult<Integer> createTradeOrderResult = tradeOrderFeign.createTradeOrder(createReqDTO);
createTradeOrderResult.checkError();
return createTradeOrderResult.getData();
}
public PageResult<TradeOrderRespDTO> pageTradeOrder(TradeOrderPageReqDTO pageReqDTO) {
CommonResult<PageResult<TradeOrderRespDTO>> pageTradeOrderResult = tradeOrderRpc.pageTradeOrder(pageReqDTO);
CommonResult<PageResult<TradeOrderRespDTO>> pageTradeOrderResult = tradeOrderFeign.pageTradeOrder(pageReqDTO);
pageTradeOrderResult.checkError();
return pageTradeOrderResult.getData();
}
public TradeOrderRespDTO getTradeOrder(Integer tradeOrderId, String... fields) {
CommonResult<TradeOrderRespDTO> getTradeOrderResult = tradeOrderRpc.getTradeOrder(tradeOrderId, Arrays.asList(fields));
CommonResult<TradeOrderRespDTO> getTradeOrderResult = tradeOrderFeign.getTradeOrder(tradeOrderId, Arrays.asList(fields));
getTradeOrderResult.checkError();
return getTradeOrderResult.getData();
}

View File

@ -14,7 +14,7 @@ import cn.iocoder.mall.promotion.api.rpc.price.dto.PriceProductCalcReqDTO;
import cn.iocoder.mall.promotion.api.rpc.price.dto.PriceProductCalcRespDTO;
import cn.iocoder.mall.shopweb.controller.trade.vo.cart.CartDetailVO;
import cn.iocoder.mall.shopweb.convert.trade.CartConvert;
import cn.iocoder.mall.tradeservice.rpc.cart.CartRpc;
import cn.iocoder.mall.tradeservice.rpc.cart.CartFeign;
import cn.iocoder.mall.tradeservice.rpc.cart.dto.*;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
@ -29,8 +29,8 @@ import java.util.stream.Collectors;
@Service
public class CartManager {
@DubboReference(version = "${dubbo.consumer.ProductCategoryRpc.version}")
private CartRpc cartRpc;
@Autowired
private CartFeign cartFeign;
@DubboReference(version = "${dubbo.consumer.PriceRpc.version}")
private PriceRpc priceRpc;
@DubboReference(version = "${dubbo.consumer.PromotionActivityRpc.version}")
@ -47,7 +47,7 @@ public class CartManager {
* @param quantity
*/
public void addCartItem(Integer userId, Integer skuId, Integer quantity) {
CommonResult<Boolean> addCartItemResult = cartRpc.addCartItem(new CartItemAddReqDTO().setUserId(userId)
CommonResult<Boolean> addCartItemResult = cartFeign.addCartItem(new CartItemAddReqDTO().setUserId(userId)
.setSkuId(skuId).setQuantity(quantity));
addCartItemResult.checkError();
}
@ -59,7 +59,7 @@ public class CartManager {
* @return
*/
public Integer sumCartItemQuantity(Integer userId) {
CommonResult<Integer> sumCartItemQuantityResult = cartRpc.sumCartItemQuantity(userId);
CommonResult<Integer> sumCartItemQuantityResult = cartFeign.sumCartItemQuantity(userId);
sumCartItemQuantityResult.checkError();
return sumCartItemQuantityResult.getData();
}
@ -72,7 +72,7 @@ public class CartManager {
* @param quantity
*/
public void updateCartItemQuantity(Integer userId, Integer skuId, Integer quantity) {
CommonResult<Boolean> updateCartItemQuantityResult = cartRpc.updateCartItemQuantity(new CartItemUpdateQuantityReqDTO()
CommonResult<Boolean> updateCartItemQuantityResult = cartFeign.updateCartItemQuantity(new CartItemUpdateQuantityReqDTO()
.setUserId(userId).setSkuId(skuId).setQuantity(quantity));
updateCartItemQuantityResult.checkError();
}
@ -85,7 +85,7 @@ public class CartManager {
* @param selected
*/
public void updateCartItemSelected(Integer userId, Set<Integer> skuIds, Boolean selected) {
CommonResult<Boolean> updateCartItemSelectedResult = cartRpc.updateCartItemSelected(new CartItemUpdateSelectedReqDTO()
CommonResult<Boolean> updateCartItemSelectedResult = cartFeign.updateCartItemSelected(new CartItemUpdateSelectedReqDTO()
.setUserId(userId).setSkuIds(skuIds).setSelected(selected));
updateCartItemSelectedResult.checkError();
}
@ -97,7 +97,7 @@ public class CartManager {
*/
public CartDetailVO getCartDetail(Integer userId) {
// 获得购物车的商品
CommonResult<List<CartItemRespDTO>> listCartItemsResult = cartRpc.listCartItems(new CartItemListReqDTO().setUserId(userId));
CommonResult<List<CartItemRespDTO>> listCartItemsResult = cartFeign.listCartItems(new CartItemListReqDTO().setUserId(userId));
listCartItemsResult.checkError();
// 购物车为空时,构造空的 UsersOrderConfirmCreateVO 返回
if (CollectionUtils.isEmpty(listCartItemsResult.getData())) {

View File

@ -26,7 +26,7 @@ import cn.iocoder.mall.shopweb.controller.trade.vo.order.TradeOrderRespVO;
import cn.iocoder.mall.shopweb.convert.trade.CartConvert;
import cn.iocoder.mall.shopweb.convert.trade.TradeOrderConvert;
import cn.iocoder.mall.tradeservice.enums.order.TradeOrderDetailFieldEnum;
import cn.iocoder.mall.tradeservice.rpc.cart.CartRpc;
import cn.iocoder.mall.tradeservice.rpc.cart.CartFeign;
import cn.iocoder.mall.tradeservice.rpc.cart.dto.CartItemListReqDTO;
import cn.iocoder.mall.tradeservice.rpc.cart.dto.CartItemRespDTO;
import cn.iocoder.mall.tradeservice.rpc.order.dto.TradeOrderPageReqDTO;
@ -53,9 +53,9 @@ public class TradeOrderService {
private PriceRpc priceRpc;
@DubboReference(version = "${dubbo.consumer.PromotionActivityRpc.version}")
private PromotionActivityRpc promotionActivityRpc;
@DubboReference(version = "${dubbo.consumer.ProductCategoryRpc.version}")
private CartRpc cartRpc;
@DubboReference(version = "${dubbo.consumer.CouponCardRpc.version}")
@Autowired
private CartFeign cartFeign;
private CouponCardRpc couponCardRpc;
@Autowired
@ -72,7 +72,7 @@ public class TradeOrderService {
public TradeOrderConfirmCreateInfoRespVO getOrderConfirmCreateInfoFromCart(Integer userId, Integer couponCardId) {
// 获得购物车的商品
CommonResult<List<CartItemRespDTO>> listCartItemsResult = cartRpc.listCartItems(
CommonResult<List<CartItemRespDTO>> listCartItemsResult = cartFeign.listCartItems(
new CartItemListReqDTO().setUserId(userId).setSelected(true));
listCartItemsResult.checkError();
// 购物车为空时,构造空的 OrderConfirmCreateInfoRespVO 返回

View File

@ -26,6 +26,10 @@
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-openfeign-core</artifactId>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,61 @@
package cn.iocoder.mall.tradeservice.rpc.cart;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.tradeservice.rpc.cart.dto.*;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.List;
/**
* Title:
* Description:
*
* @author zhuyang
* @version 1.0 2021/10/9
*/
@FeignClient(value = "trade-service")
public interface CartFeign {
/**
*
*
* @param addReqDTO
* @return
*/
@PostMapping("addCartItem")
CommonResult<Boolean> addCartItem(@RequestBody CartItemAddReqDTO addReqDTO);
/**
*
*
* @param updateQuantityReqDTO DTO
* @return
*/
@PostMapping("updateCartItemQuantity")
CommonResult<Boolean> updateCartItemQuantity(@RequestBody CartItemUpdateQuantityReqDTO updateQuantityReqDTO);
/**
*
*
* @param updateSelectedReqDTO DTO
* @return
*/
@PostMapping("updateCartItemSelected")
CommonResult<Boolean> updateCartItemSelected(@RequestBody CartItemUpdateSelectedReqDTO updateSelectedReqDTO);
/**
*
*
* @param deleteListReqDTO DTO
* @return
*/
@PostMapping("deleteCartItems")
CommonResult<Boolean> deleteCartItems(@RequestBody CartItemDeleteListReqDTO deleteListReqDTO);
@GetMapping("/sumCartItemQuantity")
public CommonResult<Integer> sumCartItemQuantity(@RequestParam("userId") Integer userId) ;
@PostMapping("/listCartItems")
public CommonResult<List<CartItemRespDTO>> listCartItems(@RequestBody CartItemListReqDTO listReqDTO) ;
}

View File

@ -1,61 +0,0 @@
package cn.iocoder.mall.tradeservice.rpc.cart;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.tradeservice.rpc.cart.dto.*;
import java.util.List;
/**
* Rpc
*/
public interface CartRpc {
/**
*
*
* @param addReqDTO
* @return
*/
CommonResult<Boolean> addCartItem(CartItemAddReqDTO addReqDTO);
/**
*
*
* @param updateQuantityReqDTO DTO
* @return
*/
CommonResult<Boolean> updateCartItemQuantity(CartItemUpdateQuantityReqDTO updateQuantityReqDTO);
/**
*
*
* @param updateSelectedReqDTO DTO
* @return
*/
CommonResult<Boolean> updateCartItemSelected(CartItemUpdateSelectedReqDTO updateSelectedReqDTO);
/**
*
*
* @param deleteListReqDTO DTO
* @return
*/
CommonResult<Boolean> deleteCartItems(CartItemDeleteListReqDTO deleteListReqDTO);
/**
*
*
* @param userId
* @return
*/
CommonResult<Integer> sumCartItemQuantity(Integer userId);
/**
*
*
* @param listReqDTO DTO
* @return
*/
CommonResult<List<CartItemRespDTO>> listCartItems(CartItemListReqDTO listReqDTO);
}

View File

@ -5,13 +5,23 @@ import cn.iocoder.common.framework.vo.PageResult;
import cn.iocoder.mall.tradeservice.rpc.order.dto.TradeOrderCreateReqDTO;
import cn.iocoder.mall.tradeservice.rpc.order.dto.TradeOrderPageReqDTO;
import cn.iocoder.mall.tradeservice.rpc.order.dto.TradeOrderRespDTO;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import java.util.Collection;
/**
* Rpc
* Title:
* Description:
*
* @author zhuyang
* @version 1.0 2021/10/9
*/
public interface TradeOrderRpc {
@FeignClient(value = "trade-service")
public interface TradeOrderFeign {
/**
*
@ -19,7 +29,8 @@ public interface TradeOrderRpc {
* @param createReqDTO
* @return
*/
CommonResult<Integer> createTradeOrder(TradeOrderCreateReqDTO createReqDTO);
@PostMapping("createTradeOrder")
CommonResult<Integer> createTradeOrder(@RequestBody TradeOrderCreateReqDTO createReqDTO);
/**
*
@ -28,16 +39,16 @@ public interface TradeOrderRpc {
* @param fields {@link cn.iocoder.mall.tradeservice.enums.order.TradeOrderDetailFieldEnum}
* @return
*/
CommonResult<TradeOrderRespDTO> getTradeOrder(Integer tradeOrderId, Collection<String> fields);
@GetMapping("getTradeOrder")
CommonResult<TradeOrderRespDTO> getTradeOrder(@RequestParam("tradeOrderId")Integer tradeOrderId, @RequestParam("fields") Collection<String> fields);
/**
*
*
* @param pageDTO
* @return
*/
CommonResult<PageResult<TradeOrderRespDTO>> pageTradeOrder(TradeOrderPageReqDTO pageDTO);
@PostMapping("pageTradeOrder")
CommonResult<PageResult<TradeOrderRespDTO>> pageTradeOrder(@RequestBody TradeOrderPageReqDTO pageDTO);
// TODO 芋艿:需要重构成入参是 DTO方便后续升级返回是 CommonResult用于返回失败的原因
/**
@ -49,6 +60,6 @@ public interface TradeOrderRpc {
* @param payAmount
* @return
*/
CommonResult<Boolean> updateTradeOrderPaySuccess(String tradeOrderId, Integer payAmount);
@PostMapping("updateTradeOrderPaySuccess")
CommonResult<Boolean> updateTradeOrderPaySuccess(@RequestParam("tradeOrderId") String tradeOrderId, @RequestParam("payAmount")Integer payAmount);
}

View File

@ -0,0 +1,74 @@
package cn.iocoder.mall.tradeservice.controller;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.tradeservice.rpc.cart.dto.*;
import cn.iocoder.mall.tradeservice.service.cart.CartManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;
import static cn.iocoder.common.framework.vo.CommonResult.success;
@RestController
@RequestMapping("/trade/cart")
public class CartController {
@Autowired
private CartManager cartManager;
/**
*
*
* @param addReqDTO
* @return
*/
@PostMapping("addCartItem")
CommonResult<Boolean> addCartItem(@RequestBody CartItemAddReqDTO addReqDTO){
cartManager.addCartItem(addReqDTO);
return success(true);
}
/**
*
*
* @param updateQuantityReqDTO DTO
* @return
*/
@PostMapping("updateCartItemQuantity")
CommonResult<Boolean> updateCartItemQuantity(@RequestBody CartItemUpdateQuantityReqDTO updateQuantityReqDTO){
cartManager.updateCartItemSelected(updateQuantityReqDTO);
return success(true);
}
/**
*
*
* @param updateSelectedReqDTO DTO
* @return
*/
@PostMapping("updateCartItemSelected")
CommonResult<Boolean> updateCartItemSelected(@RequestBody CartItemUpdateSelectedReqDTO updateSelectedReqDTO){
cartManager.updateCartItemSelected(updateSelectedReqDTO);
return success(true);
}
/**
*
*
* @param deleteListReqDTO DTO
* @return
*/
@PostMapping("deleteCartItems")
CommonResult<Boolean> deleteCartItems(@RequestBody CartItemDeleteListReqDTO deleteListReqDTO){
cartManager.deleteCartItems(deleteListReqDTO);
return success(true);
}
@GetMapping("/sumCartItemQuantity")
public CommonResult<Integer> sumCartItemQuantity(@RequestParam("userId") Integer userId) {
return success(cartManager.sumCartItemQuantity(userId));
}
@PostMapping("/listCartItems")
public CommonResult<List<CartItemRespDTO>> listCartItems(@RequestBody CartItemListReqDTO listReqDTO) {
return success(cartManager.listCartItems(listReqDTO));
}
}

View File

@ -0,0 +1,78 @@
package cn.iocoder.mall.tradeservice.controller;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.common.framework.vo.PageResult;
import cn.iocoder.mall.tradeservice.rpc.order.dto.TradeOrderCreateReqDTO;
import cn.iocoder.mall.tradeservice.rpc.order.dto.TradeOrderPageReqDTO;
import cn.iocoder.mall.tradeservice.rpc.order.dto.TradeOrderRespDTO;
import cn.iocoder.mall.tradeservice.service.order.TradeOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.Collection;
import static cn.iocoder.common.framework.vo.CommonResult.success;
/**
* Title:
* Description:
*
* @author zhuyang
* @version 1.0 2021/10/9
*/
@RestController
@RequestMapping("/trade/order")
public class TradeOrderController {
@Autowired
private TradeOrderService tradeOrderService;
/**
*
*
* @param createReqDTO
* @return
*/
@PostMapping("createTradeOrder")
CommonResult<Integer> createTradeOrder(@RequestBody TradeOrderCreateReqDTO createReqDTO){
return success(tradeOrderService.createTradeOrder(createReqDTO));
}
/**
*
*
* @param tradeOrderId
* @param fields {@link cn.iocoder.mall.tradeservice.enums.order.TradeOrderDetailFieldEnum}
* @return
*/
@GetMapping("getTradeOrder")
CommonResult<TradeOrderRespDTO> getTradeOrder( @RequestParam("tradeOrderId")Integer tradeOrderId, @RequestParam("fields") Collection<String> fields){
return success(tradeOrderService.getTradeOrder(tradeOrderId, fields));
}
/**
*
*
* @param pageDTO
* @return
*/
@PostMapping("pageTradeOrder")
CommonResult<PageResult<TradeOrderRespDTO>> pageTradeOrder(@RequestBody TradeOrderPageReqDTO pageDTO){
return success(tradeOrderService.pageTradeOrder(pageDTO));
}
// TODO 芋艿:需要重构成入参是 DTO方便后续升级返回是 CommonResult用于返回失败的原因
/**
*
*
* pay-service
*
* @param tradeOrderId
* @param payAmount
* @return
*/
@PostMapping("updateTradeOrderPaySuccess")
CommonResult<Boolean> updateTradeOrderPaySuccess(@RequestParam("tradeOrderId") String tradeOrderId, @RequestParam("payAmount")Integer payAmount){
tradeOrderService.updateTradeOrderPaySuccess(Integer.valueOf(tradeOrderId), payAmount);
return success(true);
}
}

View File

@ -1,56 +0,0 @@
package cn.iocoder.mall.tradeservice.rpc.cart;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.mall.tradeservice.service.cart.CartManager;
import cn.iocoder.mall.tradeservice.rpc.cart.dto.*;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.List;
import static cn.iocoder.common.framework.vo.CommonResult.success;
/**
* Rpc
*/
@DubboService
public class CartRpcImpl implements CartRpc {
@Autowired
private CartManager cartManager;
@Override
public CommonResult<Boolean> addCartItem(CartItemAddReqDTO addItemReqDTO) {
cartManager.addCartItem(addItemReqDTO);
return success(true);
}
@Override
public CommonResult<Boolean> updateCartItemQuantity(CartItemUpdateQuantityReqDTO updateQuantityReqDTO) {
cartManager.updateCartItemSelected(updateQuantityReqDTO);
return success(true);
}
@Override
public CommonResult<Boolean> updateCartItemSelected(CartItemUpdateSelectedReqDTO updateSelectedReqDTO) {
cartManager.updateCartItemSelected(updateSelectedReqDTO);
return success(true);
}
@Override
public CommonResult<Boolean> deleteCartItems(CartItemDeleteListReqDTO deleteListReqDTO) {
cartManager.deleteCartItems(deleteListReqDTO);
return success(true);
}
@Override
public CommonResult<Integer> sumCartItemQuantity(Integer userId) {
return success(cartManager.sumCartItemQuantity(userId));
}
@Override
public CommonResult<List<CartItemRespDTO>> listCartItems(CartItemListReqDTO listReqDTO) {
return success(cartManager.listCartItems(listReqDTO));
}
}

View File

@ -1,46 +0,0 @@
package cn.iocoder.mall.tradeservice.rpc.order;
import cn.iocoder.common.framework.vo.CommonResult;
import cn.iocoder.common.framework.vo.PageResult;
import cn.iocoder.mall.tradeservice.rpc.order.dto.TradeOrderCreateReqDTO;
import cn.iocoder.mall.tradeservice.rpc.order.dto.TradeOrderPageReqDTO;
import cn.iocoder.mall.tradeservice.rpc.order.dto.TradeOrderRespDTO;
import cn.iocoder.mall.tradeservice.service.order.TradeOrderService;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import java.util.Collection;
import static cn.iocoder.common.framework.vo.CommonResult.success;
/**
* Rpc
*/
@DubboService
public class TradeOrderRpcImpl implements TradeOrderRpc {
@Autowired
private TradeOrderService tradeOrderService;
@Override
public CommonResult<Integer> createTradeOrder(TradeOrderCreateReqDTO createReqDTO) {
return success(tradeOrderService.createTradeOrder(createReqDTO));
}
@Override
public CommonResult<TradeOrderRespDTO> getTradeOrder(Integer tradeOrderId, Collection<String> fields) {
return success(tradeOrderService.getTradeOrder(tradeOrderId, fields));
}
@Override
public CommonResult<PageResult<TradeOrderRespDTO>> pageTradeOrder(TradeOrderPageReqDTO pageDTO) {
return success(tradeOrderService.pageTradeOrder(pageDTO));
}
@Override
public CommonResult<Boolean> updateTradeOrderPaySuccess(String tradeOrderId, Integer payAmount) {
tradeOrderService.updateTradeOrderPaySuccess(Integer.valueOf(tradeOrderId), payAmount);
return success(true);
}
}