diff --git a/.idea/encodings.xml b/.idea/encodings.xml index ec240c8..656f22f 100644 --- a/.idea/encodings.xml +++ b/.idea/encodings.xml @@ -15,6 +15,7 @@ + diff --git a/.idea/misc.xml b/.idea/misc.xml index fb0bd54..9b83232 100644 --- a/.idea/misc.xml +++ b/.idea/misc.xml @@ -14,7 +14,7 @@ - + \ No newline at end of file diff --git a/.idea/prettier.xml b/.idea/prettier.xml deleted file mode 100644 index b0ab31a..0000000 --- a/.idea/prettier.xml +++ /dev/null @@ -1,6 +0,0 @@ - - - - - \ No newline at end of file diff --git a/common/service-util/src/main/java/com/atguigu/ssyx/common/CodeGet.java b/common/service-util/src/main/java/com/atguigu/ssyx/common/CodeGet.java index 2a2d09a..fc5db9b 100644 --- a/common/service-util/src/main/java/com/atguigu/ssyx/common/CodeGet.java +++ b/common/service-util/src/main/java/com/atguigu/ssyx/common/CodeGet.java @@ -19,7 +19,7 @@ public class CodeGet { // 全局配置 GlobalConfig gc = new GlobalConfig(); // TODO 需要修改路径名称 - gc.setOutputDir("G:\\File\\Java\\ssyx\\ssyx-parent\\service\\service-order" + "/src/main/java"); + gc.setOutputDir("F:\\File\\Java\\ssyx\\ssyx-parent\\service\\service-order" + "/src/main/java"); gc.setServiceName("%sService"); // 去掉Service接口的首字母I gc.setAuthor("bunny"); diff --git a/common/service-util/src/main/java/com/atguigu/ssyx/common/constant/MessageConstant.java b/common/service-util/src/main/java/com/atguigu/ssyx/common/constant/MessageConstant.java index eb39b05..e4bc860 100644 --- a/common/service-util/src/main/java/com/atguigu/ssyx/common/constant/MessageConstant.java +++ b/common/service-util/src/main/java/com/atguigu/ssyx/common/constant/MessageConstant.java @@ -34,4 +34,5 @@ public class MessageConstant { public static final String DATA_IS_EMPTY = "数据为空"; public static final String STOCK_LESS = "库存不足"; public static final String SKU_NUM_CANNOT_BE_LESS = "商品数量不能再少了"; + public static final String CART_IS_EMPTY = "购物车为空"; } diff --git a/pom.xml b/pom.xml index a7d1c5d..b5e3df5 100644 --- a/pom.xml +++ b/pom.xml @@ -44,6 +44,11 @@ + + org.projectlombok + lombok + 1.18.30 + org.springframework.cloud spring-cloud-dependencies diff --git a/service-client/pom.xml b/service-client/pom.xml index 9031e4c..9ffdecf 100644 --- a/service-client/pom.xml +++ b/service-client/pom.xml @@ -18,6 +18,7 @@ service-user-client service-search-client service-activity-client + service-cart-client diff --git a/service-client/service-cart-client/pom.xml b/service-client/service-cart-client/pom.xml new file mode 100644 index 0000000..ac1094d --- /dev/null +++ b/service-client/service-cart-client/pom.xml @@ -0,0 +1,23 @@ + + 4.0.0 + + com.atguigu + service-client + 1.0-SNAPSHOT + + + service-cart-client + jar + + service-cart-client + https://maven.apache.org + + + UTF-8 + + + + + + diff --git a/service-client/service-cart-client/src/main/java/com/atguigu/ssyx/client/cart/CartFeignClient.java b/service-client/service-cart-client/src/main/java/com/atguigu/ssyx/client/cart/CartFeignClient.java new file mode 100644 index 0000000..12214cf --- /dev/null +++ b/service-client/service-cart-client/src/main/java/com/atguigu/ssyx/client/cart/CartFeignClient.java @@ -0,0 +1,15 @@ +package com.atguigu.ssyx.client.cart; + +import com.atguigu.ssyx.model.order.CartInfo; +import org.springframework.cloud.openfeign.FeignClient; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; + +import java.util.List; + +@FeignClient(value = "service-cart", path = "/api/cart") +public interface CartFeignClient { + // 根据用户Id查询购物车列表 + @GetMapping("inner/getCartCheckedList/{userId}") + List getCartCheckedList(@PathVariable Long userId); +} diff --git a/service/service-cart/src/main/java/com/atguigu/ssyx/cart/api/CartApiController.java b/service/service-cart/src/main/java/com/atguigu/ssyx/cart/api/CartApiController.java new file mode 100644 index 0000000..899e412 --- /dev/null +++ b/service/service-cart/src/main/java/com/atguigu/ssyx/cart/api/CartApiController.java @@ -0,0 +1,27 @@ +package com.atguigu.ssyx.cart.api; + +import com.atguigu.ssyx.cart.service.CartInfoService; +import com.atguigu.ssyx.model.order.CartInfo; +import io.swagger.annotations.Api; +import io.swagger.annotations.ApiOperation; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +import java.util.List; + +@Api(tags = "购物车相关接口") +@RestController +@RequestMapping("/api/cart") +public class CartApiController { + @Autowired + private CartInfoService cartInfoService; + + @ApiOperation(value = "根据用户Id查询购物车列表") + @GetMapping("inner/getCartCheckedList/{userId}") + public List getCartCheckedList(@PathVariable Long userId) { + return cartInfoService.getCartCheckedList(userId); + } +} diff --git a/service/service-cart/src/main/java/com/atguigu/ssyx/cart/controller/CartApiController.java b/service/service-cart/src/main/java/com/atguigu/ssyx/cart/controller/CartInfoController.java similarity index 99% rename from service/service-cart/src/main/java/com/atguigu/ssyx/cart/controller/CartApiController.java rename to service/service-cart/src/main/java/com/atguigu/ssyx/cart/controller/CartInfoController.java index df5277d..574ea06 100644 --- a/service/service-cart/src/main/java/com/atguigu/ssyx/cart/controller/CartApiController.java +++ b/service/service-cart/src/main/java/com/atguigu/ssyx/cart/controller/CartInfoController.java @@ -1,5 +1,6 @@ package com.atguigu.ssyx.cart.controller; + import com.atguigu.ssyx.cart.service.CartInfoService; import com.atguigu.ssyx.client.ActivityFeignClient; import com.atguigu.ssyx.common.context.BaseContext; @@ -13,10 +14,11 @@ import org.springframework.web.bind.annotation.*; import java.util.List; + @Api(tags = "购物车相关接口") @RestController @RequestMapping("/api/cart") -public class CartApiController { +public class CartInfoController { @Autowired private CartInfoService cartInfoService; @Autowired diff --git a/service/service-cart/src/main/java/com/atguigu/ssyx/cart/service/CartInfoService.java b/service/service-cart/src/main/java/com/atguigu/ssyx/cart/service/CartInfoService.java index 969895c..87a15f9 100644 --- a/service/service-cart/src/main/java/com/atguigu/ssyx/cart/service/CartInfoService.java +++ b/service/service-cart/src/main/java/com/atguigu/ssyx/cart/service/CartInfoService.java @@ -71,4 +71,12 @@ public interface CartInfoService { * @param isChecked 是否选中 */ void batchCheckCart(List skuIdList, Long userId, Integer isChecked); + + /** + * * 根据用户Id查询购物车列表 + * @param userId 用户ID + * @return 购物车列表 + */ + List getCartCheckedList(Long userId); } + diff --git a/service/service-cart/src/main/java/com/atguigu/ssyx/cart/service/impl/CartInfoServiceImpl.java b/service/service-cart/src/main/java/com/atguigu/ssyx/cart/service/impl/CartInfoServiceImpl.java index 34fb9ee..ea15949 100644 --- a/service/service-cart/src/main/java/com/atguigu/ssyx/cart/service/impl/CartInfoServiceImpl.java +++ b/service/service-cart/src/main/java/com/atguigu/ssyx/cart/service/impl/CartInfoServiceImpl.java @@ -16,10 +16,8 @@ import org.springframework.data.redis.core.BoundHashOperations; import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; -import java.util.ArrayList; -import java.util.Comparator; -import java.util.Date; -import java.util.List; +import java.util.*; +import java.util.stream.Collectors; @Service public class CartInfoServiceImpl implements CartInfoService { @@ -223,4 +221,24 @@ public class CartInfoServiceImpl implements CartInfoService { } }); } + + /** + * * 根据用户Id查询购物车列表 + * + * @param userId 用户ID + * @return 购物车列表 + */ + @Override + public List getCartCheckedList(Long userId) { + String cartKey = module.getCartKey(userId); + BoundHashOperations hashOperations = redisTemplate.boundHashOps(cartKey); + List cartInfoList = hashOperations.values(); + // 当购物车为空时抛出异常 + if (cartInfoList==null) { + throw new BunnyException(MessageConstant.CART_IS_EMPTY); + } + + cartInfoList = cartInfoList.stream().filter(cartInfo -> cartInfo.getIsChecked() == 1).collect(Collectors.toList()); + return cartInfoList; + } } diff --git a/service/service-order/pom.xml b/service/service-order/pom.xml index 0d9e8cc..14e19af 100644 --- a/service/service-order/pom.xml +++ b/service/service-order/pom.xml @@ -20,8 +20,24 @@ com.atguigu - service-product-client + service-user-client 1.0-SNAPSHOT + + com.atguigu + service-cart-client + 1.0-SNAPSHOT + + + com.atguigu + service-activity-client + 1.0-SNAPSHOT + + + javax.validation + validation-api + 2.0.1.Final + compile + diff --git a/service/service-order/src/main/java/com/atguigu/ssyx/order/controller/OrderInfoController.java b/service/service-order/src/main/java/com/atguigu/ssyx/order/controller/OrderInfoController.java index 7e7190e..bf6bbd3 100644 --- a/service/service-order/src/main/java/com/atguigu/ssyx/order/controller/OrderInfoController.java +++ b/service/service-order/src/main/java/com/atguigu/ssyx/order/controller/OrderInfoController.java @@ -1,15 +1,19 @@ package com.atguigu.ssyx.order.controller; +import com.atguigu.ssyx.common.context.BaseContext; import com.atguigu.ssyx.common.result.Result; +import com.atguigu.ssyx.model.order.CartInfo; +import com.atguigu.ssyx.model.order.OrderInfo; import com.atguigu.ssyx.order.service.OrderInfoService; import com.atguigu.ssyx.vo.order.OrderConfirmVo; +import com.atguigu.ssyx.vo.order.OrderSubmitVo; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; + +import java.util.List; @Api(value = "Order管理", tags = "Order管理") @RestController @@ -24,5 +28,20 @@ public class OrderInfoController { OrderConfirmVo vo = orderInfoService.confirmOrder(); return Result.success(vo); } + + @ApiOperation("获取订单详情") + @GetMapping("auth/getOrderInfoById/{orderId}") + public Result getOrderInfoById(@PathVariable Long orderId) { + OrderInfo orderInfo = orderInfoService.getOrderInfoById(orderId); + return Result.success(orderInfo); + } + + @ApiOperation("生成订单") + @PostMapping("auth/submitOrder") + public Result submitOrder(@RequestBody OrderSubmitVo vo) { + Long userId = BaseContext.getUserId(); + Long orderId = orderInfoService.submitOrder(vo, userId); + return Result.success(orderId); + } } diff --git a/service/service-order/src/main/java/com/atguigu/ssyx/order/service/OrderInfoService.java b/service/service-order/src/main/java/com/atguigu/ssyx/order/service/OrderInfoService.java index 465df07..498b3a9 100644 --- a/service/service-order/src/main/java/com/atguigu/ssyx/order/service/OrderInfoService.java +++ b/service/service-order/src/main/java/com/atguigu/ssyx/order/service/OrderInfoService.java @@ -2,6 +2,7 @@ package com.atguigu.ssyx.order.service; import com.atguigu.ssyx.model.order.OrderInfo; import com.atguigu.ssyx.vo.order.OrderConfirmVo; +import com.atguigu.ssyx.vo.order.OrderSubmitVo; import com.baomidou.mybatisplus.extension.service.IService; public interface OrderInfoService extends IService { @@ -12,4 +13,19 @@ public interface OrderInfoService extends IService { * @return 订单确认内容 */ OrderConfirmVo confirmOrder(); + + /** + * * 获取订单详情 + * @param orderId 订单ID + * @return 订单详情 + */ + OrderInfo getOrderInfoById(Long orderId); + + /** + * * 生成订单 + * @param vo 订单提交信息 + * @param userId 用户ID + * @return 订单编号 + */ + Long submitOrder(OrderSubmitVo vo, Long userId); } diff --git a/service/service-order/src/main/java/com/atguigu/ssyx/order/service/impl/OrderInfoServiceImpl.java b/service/service-order/src/main/java/com/atguigu/ssyx/order/service/impl/OrderInfoServiceImpl.java index 1b8ae32..e851c4a 100644 --- a/service/service-order/src/main/java/com/atguigu/ssyx/order/service/impl/OrderInfoServiceImpl.java +++ b/service/service-order/src/main/java/com/atguigu/ssyx/order/service/impl/OrderInfoServiceImpl.java @@ -1,14 +1,36 @@ package com.atguigu.ssyx.order.service.impl; +import com.atguigu.ssyx.client.ActivityFeignClient; +import com.atguigu.ssyx.client.cart.CartFeignClient; +import com.atguigu.ssyx.client.user.UserFeignClient; +import com.atguigu.ssyx.common.constant.RedisConst; +import com.atguigu.ssyx.common.context.BaseContext; +import com.atguigu.ssyx.model.order.CartInfo; import com.atguigu.ssyx.model.order.OrderInfo; import com.atguigu.ssyx.order.mapper.OrderInfoMapper; import com.atguigu.ssyx.order.service.OrderInfoService; import com.atguigu.ssyx.vo.order.OrderConfirmVo; +import com.atguigu.ssyx.vo.order.OrderSubmitVo; +import com.atguigu.ssyx.vo.user.LeaderAddressVo; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.data.redis.core.RedisTemplate; import org.springframework.stereotype.Service; +import java.util.List; +import java.util.UUID; +import java.util.concurrent.TimeUnit; + @Service public class OrderInfoServiceImpl extends ServiceImpl implements OrderInfoService { + @Autowired + private UserFeignClient userFeignClient; + @Autowired + private CartFeignClient cartFeignClient; + @Autowired + private ActivityFeignClient activityFeignClient; + @Autowired + private RedisTemplate redisTemplate; /** * * 确认订单 @@ -17,6 +39,42 @@ public class OrderInfoServiceImpl extends ServiceImpl cartInfoList = cartFeignClient.getCartCheckedList(userId); + + // 生成订单Id + String orderNo = System.currentTimeMillis() + UUID.randomUUID().toString(); + // 设置过期时间一天 + redisTemplate.opsForValue().set(RedisConst.ORDER_REPEAT + orderNo, orderNo, 24, TimeUnit.HOURS); + // 获取购物车满足条件的促销与优惠券信息 + OrderConfirmVo orderTradeVo = activityFeignClient.findCartActivityAndCoupon(cartInfoList, userId); + orderTradeVo.setLeaderAddressVo(leaderAddressVo); + orderTradeVo.setOrderNo(orderNo); + return orderTradeVo; + } + + /** + * * 获取订单详情 + * + * @param orderId 订单ID + * @return 订单详情 + */ + @Override + public OrderInfo getOrderInfoById(Long orderId) { + return null; + } + + /** + * * 生成订单 + * + * @param vo 订单提交信息 + * @param userId 用户ID + * @return 订单编号 + */ + @Override + public Long submitOrder(OrderSubmitVo vo, Long userId) { return null; } }