支付、订单查询模块

This commit is contained in:
yovinchen 2023-10-13 16:41:19 +08:00
parent 1d6ed7f564
commit b98a4954ad
34 changed files with 1112 additions and 29 deletions

View File

@ -13,6 +13,7 @@
<module name="service-gateway" /> <module name="service-gateway" />
<module name="rabbit_util" /> <module name="rabbit_util" />
<module name="service-home" /> <module name="service-home" />
<module name="service-order-client" />
<module name="service-sys" /> <module name="service-sys" />
<module name="service-user-client" /> <module name="service-user-client" />
<module name="service-util" /> <module name="service-util" />
@ -26,6 +27,7 @@
<module name="model" /> <module name="model" />
<module name="service-cart-client" /> <module name="service-cart-client" />
<module name="service-activity" /> <module name="service-activity" />
<module name="service-payment" />
</profile> </profile>
</annotationProcessing> </annotationProcessing>
</component> </component>
@ -44,6 +46,8 @@
<module name="service-gateway" options="-parameters" /> <module name="service-gateway" options="-parameters" />
<module name="service-home" options="-parameters" /> <module name="service-home" options="-parameters" />
<module name="service-order" options="-parameters" /> <module name="service-order" options="-parameters" />
<module name="service-order-client" options="-parameters" />
<module name="service-payment" options="-parameters" />
<module name="service-product" options="-parameters" /> <module name="service-product" options="-parameters" />
<module name="service-product-client" options="-parameters" /> <module name="service-product-client" options="-parameters" />
<module name="service-search" options="-parameters" /> <module name="service-search" options="-parameters" />

View File

@ -9,6 +9,7 @@
<file url="file://$PROJECT_DIR$/guigu-ssyx-parent/model/src/main/java" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/guigu-ssyx-parent/model/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/guigu-ssyx-parent/service-client/service-activity-client/src/main/java" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/guigu-ssyx-parent/service-client/service-activity-client/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/guigu-ssyx-parent/service-client/service-cart-client/src/main/java" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/guigu-ssyx-parent/service-client/service-cart-client/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/guigu-ssyx-parent/service-client/service-order-client/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/guigu-ssyx-parent/service-client/service-product-client/src/main/java" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/guigu-ssyx-parent/service-client/service-product-client/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/guigu-ssyx-parent/service-client/service-search-client/src/main/java" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/guigu-ssyx-parent/service-client/service-search-client/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/guigu-ssyx-parent/service-client/service-user-client/src/main/java" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/guigu-ssyx-parent/service-client/service-user-client/src/main/java" charset="UTF-8" />
@ -20,6 +21,7 @@
<file url="file://$PROJECT_DIR$/guigu-ssyx-parent/service/service-cart/src/main/java" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/guigu-ssyx-parent/service/service-cart/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/guigu-ssyx-parent/service/service-home/src/main/java" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/guigu-ssyx-parent/service/service-home/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/guigu-ssyx-parent/service/service-order/src/main/java" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/guigu-ssyx-parent/service/service-order/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/guigu-ssyx-parent/service/service-payment/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/guigu-ssyx-parent/service/service-product/src/main/java" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/guigu-ssyx-parent/service/service-product/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/guigu-ssyx-parent/service/service-search/src/main/java" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/guigu-ssyx-parent/service/service-search/src/main/java" charset="UTF-8" />
<file url="file://$PROJECT_DIR$/guigu-ssyx-parent/service/service-sys/src/main/java" charset="UTF-8" /> <file url="file://$PROJECT_DIR$/guigu-ssyx-parent/service/service-sys/src/main/java" charset="UTF-8" />

View File

@ -24,7 +24,7 @@ const post_submit_order = '/order/auth/submitOrder' // 生成订单
const get_order_info = '/order/auth/getOrderInfoById' // 订单详情 const get_order_info = '/order/auth/getOrderInfoById' // 订单详情
const get_wx_login = '/user/weixin/wxLogin' // 微信用户登陆 const get_wx_login = '/user/weixin/wxLogin' // 微信用户登陆
const post_update_user = '/user/weixin/auth/updateUser' // 更新用户信息 const post_update_user = '/user/weixin/auth/updateUser' // 更新用户信息
const get_weixin_payment = '/payment/weixin/createJsapi/' // 获取微信支付信息 const get_weixin_payment = '/payment/weixin/createJsapi' // 获取微信支付信息
const get_find_user_order = '/order/auth/findUserOrderPage' // 获取用户订单信息 const get_find_user_order = '/order/auth/findUserOrderPage' // 获取用户订单信息
const get_order_status = '/payment/weixin/queryPayStatus' // 获取订单状态 const get_order_status = '/payment/weixin/queryPayStatus' // 获取订单状态

View File

@ -25932,7 +25932,7 @@
var get_order_info = '/order/auth/getOrderInfoById'; // 订单详情 var get_order_info = '/order/auth/getOrderInfoById'; // 订单详情
var get_wx_login = '/user/weixin/wxLogin'; // 微信用户登陆 var get_wx_login = '/user/weixin/wxLogin'; // 微信用户登陆
var post_update_user = '/user/weixin/auth/updateUser'; // 更新用户信息 var post_update_user = '/user/weixin/auth/updateUser'; // 更新用户信息
var get_weixin_payment = '/payment/weixin/createJsapi/'; // 获取微信支付信息 var get_weixin_payment = '/payment/weixin/createJsapi'; // 获取微信支付信息
var get_find_user_order = '/order/auth/findUserOrderPage'; // 获取用户订单信息 var get_find_user_order = '/order/auth/findUserOrderPage'; // 获取用户订单信息
var get_order_status = '/payment/weixin/queryPayStatus'; // 获取订单状态 var get_order_status = '/payment/weixin/queryPayStatus'; // 获取订单状态

View File

@ -26,7 +26,7 @@ public class Result<T> {
//设置数据,返回对象的方法 //设置数据,返回对象的方法
public static <T> Result<T> build(T data, Integer code, String message) { public static <T> Result<T> build(T data, Integer code, String message) {
//创建Result对象设置值返回对象 //创建Resullt对象设置值返回对象
Result<T> result = new Result<>(); Result<T> result = new Result<>();
//判断返回结果中是否需要数据 //判断返回结果中是否需要数据
if (data != null) { if (data != null) {
@ -40,9 +40,10 @@ public class Result<T> {
return result; return result;
} }
//设置数据,返回对象的方法 //设置数据,返回对象的方法
public static <T> Result<T> build(T data, ResultCodeEnum resultCodeEnum) { public static <T> Result<T> build(T data, ResultCodeEnum resultCodeEnum) {
//创建Result对象设置值返回对象 //创建Resullt对象设置值返回对象
Result<T> result = new Result<>(); Result<T> result = new Result<>();
//判断返回结果中是否需要数据 //判断返回结果中是否需要数据
if (data != null) { if (data != null) {
@ -58,11 +59,13 @@ public class Result<T> {
//成功的方法 //成功的方法
public static <T> Result<T> ok(T data) { public static <T> Result<T> ok(T data) {
return build(data, ResultCodeEnum.SUCCESS); Result<T> result = build(data, ResultCodeEnum.SUCCESS);
return result;
} }
//失败的方法 //失败的方法
public static <T> Result<T> fail(T data) { public static <T> Result<T> fail(T data) {
return build(data, ResultCodeEnum.FAIL); return build(data, ResultCodeEnum.FAIL);
} }
} }

View File

@ -13,18 +13,36 @@ import lombok.Getter;
@Getter @Getter
public enum ResultCodeEnum { public enum ResultCodeEnum {
SUCCESS(200, "成功"), FAIL(201, "失败"), SERVICE_ERROR(2012, "服务异常"), DATA_ERROR(204, "数据异常"), ILLEGAL_REQUEST(205, "非法请求"), REPEAT_SUBMIT(206, "重复提交"), SUCCESS(200, "成功"),
FAIL(201, "失败"),
LOGIN_AUTH(208, "未登陆"), PERMISSION(209, "没有权限"), SERVICE_ERROR(2012, "服务异常"),
DATA_ERROR(204, "数据异常"),
ILLEGAL_REQUEST(205, "非法请求"),
REPEAT_SUBMIT(206, "重复提交"),
ORDER_PRICE_ERROR(210, "订单商品价格变化"), ORDER_STOCK_FALL(204, "订单库存锁定失败"), CREATE_ORDER_FAIL(210, "创建订单失败"), LOGIN_AUTH(208, "未登陆"),
PERMISSION(209, "没有权限"),
COUPON_GET(220, "优惠券已经领取"), COUPON_LIMIT_GET(221, "优惠券已发放完毕"), ORDER_PRICE_ERROR(210, "订单商品价格变化"),
ORDER_STOCK_FALL(204, "订单库存锁定失败"),
CREATE_ORDER_FAIL(210, "创建订单失败"),
URL_ENCODE_ERROR(216, "URL编码失败"), ILLEGAL_CALLBACK_REQUEST_ERROR(217, "非法回调请求"), FETCH_ACCESSTOKEN_FAILD(218, "获取accessToken失败"), FETCH_USERINFO_ERROR(219, "获取用户信息失败"), COUPON_GET(220, "优惠券已经领取"),
COUPON_LIMIT_GET(221, "优惠券已发放完毕"),
URL_ENCODE_ERROR(216, "URL编码失败"),
ILLEGAL_CALLBACK_REQUEST_ERROR(217, "非法回调请求"),
FETCH_ACCESSTOKEN_FAILD(218, "获取accessToken失败"),
FETCH_USERINFO_ERROR(219, "获取用户信息失败"),
SKU_LIMIT_ERROR(230, "购买个数不能大于限购个数"), REGION_OPEN(240, "该区域已开通"), REGION_NO_OPEN(240, "该区域未开通"), SKU_LIMIT_ERROR(230, "购买个数不能大于限购个数"),
REGION_OPEN(240, "该区域已开通"),
REGION_NO_OPEN(240, "该区域未开通"),
PAYMENT_WAITING(242, "订单支付中"),
PAYMENT_SUCCESS(241, "订单支付成功"),
PAYMENT_FAIL(243, "订单支付失败"),
; ;
private final Integer code; private final Integer code;

View File

@ -17,6 +17,7 @@
<module>service-search-client</module> <module>service-search-client</module>
<module>service-activity-client</module> <module>service-activity-client</module>
<module>service-cart-client</module> <module>service-cart-client</module>
<module>service-order-client</module>
</modules> </modules>
<dependencies> <dependencies>
<dependency> <dependency>

View File

@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.atguigu</groupId>
<artifactId>service-client</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>service-order-client</artifactId>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

View File

@ -0,0 +1,22 @@
package com.atguigu.ssyx.client.order;
import com.atguigu.ssyx.model.order.OrderInfo;
import io.swagger.annotations.ApiOperation;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
/**
* ClassName: OrderFeignClient
* Package: com.atguigu.ssyx.client.order
*
* @author yovinchen
* @Create 2023/10/13 11:03
*/
@FeignClient(value = "service-order")
public interface OrderFeignClient {
@ApiOperation(value = "根据orderNo查询订单信息")
@GetMapping("/api/order/inner/getOrderInfo/{orderNo}")
OrderInfo getOrderInfo(@PathVariable("orderNo") String orderNo);
}

View File

@ -21,6 +21,7 @@
<module>service-home</module> <module>service-home</module>
<module>service-cart</module> <module>service-cart</module>
<module>service-order</module> <module>service-order</module>
<module>service-payment</module>
</modules> </modules>
<dependencies> <dependencies>

View File

@ -12,6 +12,11 @@ feign:
spring: spring:
main: main:
allow-bean-definition-overriding: true #当遇到同样名字的时候,是否允许覆盖注册 allow-bean-definition-overriding: true #当遇到同样名字的时候,是否允许覆盖注册
rabbitmq:
host: 43.143.164.194
port: 5672
username: guest
password: guest
redis: redis:
host: 82.157.68.223 host: 82.157.68.223
port: 6379 port: 6379

View File

@ -1,12 +1,17 @@
package com.atguigu.ssyx.order.controller; package com.atguigu.ssyx.order.controller;
import com.atguigu.ssyx.common.auth.AuthContextHolder;
import com.atguigu.ssyx.common.result.Result; import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.model.order.OrderInfo; import com.atguigu.ssyx.model.order.OrderInfo;
import com.atguigu.ssyx.order.service.OrderInfoService; import com.atguigu.ssyx.order.service.OrderInfoService;
import com.atguigu.ssyx.vo.order.OrderConfirmVo; import com.atguigu.ssyx.vo.order.OrderConfirmVo;
import com.atguigu.ssyx.vo.order.OrderSubmitVo; import com.atguigu.ssyx.vo.order.OrderSubmitVo;
import com.atguigu.ssyx.vo.order.OrderUserQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
@ -22,10 +27,28 @@ import org.springframework.web.bind.annotation.*;
@RequestMapping("/api/order") @RequestMapping("/api/order")
public class OrderInfoController { public class OrderInfoController {
@Autowired @Autowired
private OrderInfoService orderInfoService; private OrderInfoService orderInfoService;
@ApiOperation(value = "订单查询")
@GetMapping("auth/findUserOrderPage/{page}/{limit}")
public Result findUserOrderPage(
@ApiParam(name = "page", value = "当前页码", required = true)
@PathVariable Long page,
@ApiParam(name = "limit", value = "每页记录数", required = true)
@PathVariable Long limit,
@ApiParam(name = "orderVo", value = "查询对象", required = false)
OrderUserQueryVo orderUserQueryVo) {
//获取userId
Long userId = AuthContextHolder.getUserId();
orderUserQueryVo.setUserId(userId);
//分页查询条件
Page<OrderInfo> pageParam = new Page<>(page, limit);
IPage<OrderInfo> pageModel = orderInfoService.getOrderInfoByUserIdPage(pageParam, orderUserQueryVo);
return Result.ok(pageModel);
}
@ApiOperation("确认订单") @ApiOperation("确认订单")
@GetMapping("auth/confirmOrder") @GetMapping("auth/confirmOrder")
public Result confirm() { public Result confirm() {

View File

@ -0,0 +1,44 @@
package com.atguigu.ssyx.order.receiver;
import com.atguigu.ssyx.mq.constant.MqConst;
import com.atguigu.ssyx.order.service.OrderInfoService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.io.IOException;
/**
* ClassName: OrderReceiver
* Package: com.atguigu.ssyx.order.receiver
*
* @author yovinchen
* @Create 2023/10/13 14:15
*/
@Component
public class OrderReceiver {
@Autowired
private OrderInfoService orderInfoService;
//订单支付成功更新订单状态扣减库存
@RabbitListener(bindings = @QueueBinding(
value = @Queue(value = MqConst.QUEUE_ORDER_PAY, durable = "true"),
exchange = @Exchange(value = MqConst.EXCHANGE_PAY_DIRECT),
key = {MqConst.ROUTING_PAY_SUCCESS}
))
public void orderPay(String orderNo,
Message message,
Channel channel) throws IOException {
if (!StringUtils.isEmpty(orderNo)) {
orderInfoService.orderPay(orderNo);
}
channel.basicAck(message.getMessageProperties().getDeliveryTag(),
false);
}
}

View File

@ -3,6 +3,9 @@ package com.atguigu.ssyx.order.service;
import com.atguigu.ssyx.model.order.OrderInfo; import com.atguigu.ssyx.model.order.OrderInfo;
import com.atguigu.ssyx.vo.order.OrderConfirmVo; import com.atguigu.ssyx.vo.order.OrderConfirmVo;
import com.atguigu.ssyx.vo.order.OrderSubmitVo; import com.atguigu.ssyx.vo.order.OrderSubmitVo;
import com.atguigu.ssyx.vo.order.OrderUserQueryVo;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
/** /**
@ -45,4 +48,20 @@ public interface OrderInfoService extends IService<OrderInfo> {
* @return * @return
*/ */
OrderInfo getOrderInfoByOrderNo(String orderNo); OrderInfo getOrderInfoByOrderNo(String orderNo);
/**
* 订单支付成功更新订单状态扣减库存
*
* @param orderNo
*/
void orderPay(String orderNo);
/**
* 订单查询
*
* @param pageParam
* @param orderUserQueryVo
* @return
*/
IPage<OrderInfo> getOrderInfoByUserIdPage(Page<OrderInfo> pageParam, OrderUserQueryVo orderUserQueryVo);
} }

View File

@ -24,9 +24,12 @@ import com.atguigu.ssyx.order.service.OrderItemService;
import com.atguigu.ssyx.vo.order.CartInfoVo; import com.atguigu.ssyx.vo.order.CartInfoVo;
import com.atguigu.ssyx.vo.order.OrderConfirmVo; import com.atguigu.ssyx.vo.order.OrderConfirmVo;
import com.atguigu.ssyx.vo.order.OrderSubmitVo; import com.atguigu.ssyx.vo.order.OrderSubmitVo;
import com.atguigu.ssyx.vo.order.OrderUserQueryVo;
import com.atguigu.ssyx.vo.product.SkuStockLockVo; import com.atguigu.ssyx.vo.product.SkuStockLockVo;
import com.atguigu.ssyx.vo.user.LeaderAddressVo; import com.atguigu.ssyx.vo.user.LeaderAddressVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.BoundHashOperations; import org.springframework.data.redis.core.BoundHashOperations;
@ -328,7 +331,61 @@ public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo
*/ */
@Override @Override
public OrderInfo getOrderInfoByOrderNo(String orderNo) { public OrderInfo getOrderInfoByOrderNo(String orderNo) {
return null; return baseMapper.selectOne(new LambdaQueryWrapper<OrderInfo>().eq(OrderInfo::getOrderNo, orderNo));
}
/**
* 订单支付成功更新订单状态扣减库存
*
* @param orderNo
*/
@Override
public void orderPay(String orderNo) {
//查询订单状态是否已经修改完成了支付状态
OrderInfo orderInfo = this.getOrderInfoByOrderNo(orderNo);
if (orderInfo == null || orderInfo.getOrderStatus() != OrderStatus.UNPAID) {
return;
}
//更新状态
this.updateOrderStatus(orderInfo.getId());
//扣减库存
rabbitService.sendMessage(MqConst.EXCHANGE_ORDER_DIRECT, MqConst.ROUTING_MINUS_STOCK, orderNo);
}
/**
* 订单查询
*
* @param pageParam
* @param orderUserQueryVo
* @return
*/
@Override
public IPage<OrderInfo> getOrderInfoByUserIdPage(Page<OrderInfo> pageParam, OrderUserQueryVo orderUserQueryVo) {
IPage<OrderInfo> pageModel = baseMapper.selectPage(pageParam, new LambdaQueryWrapper<OrderInfo>()
.eq(OrderInfo::getUserId, orderUserQueryVo.getUserId())
.eq(OrderInfo::getOrderStatus, orderUserQueryVo.getOrderStatus()));
//获取每个订单把每个订单里面订单项查询封装
List<OrderInfo> orderInfoList = pageModel.getRecords();
for (OrderInfo orderInfo : orderInfoList) {
//根据订单id查询里面所有订单项列表
List<OrderItem> orderItemList = orderItemMapper.selectList(new LambdaQueryWrapper<OrderItem>()
.eq(OrderItem::getOrderId, orderInfo.getId()));
//把订单项集合封装到每个订单里面
orderInfo.setOrderItemList(orderItemList);
//封装订单状态名称
orderInfo.getParam().put("orderStatusName", orderInfo.getOrderStatus().getComment());
}
return pageModel;
}
//更新状态
private void updateOrderStatus(Long id) {
OrderInfo orderInfo = baseMapper.selectById(id);
orderInfo.setOrderStatus(OrderStatus.WAITING_DELEVER);
orderInfo.setProcessStatus(ProcessStatus.WAITING_DELEVER);
baseMapper.updateById(orderInfo);
} }
//计算总金额 //计算总金额

View File

@ -0,0 +1,5 @@
FROM openjdk:8-jdk-alpine
LABEL authors="yovinchen"
VOLUME /tmp
ADD ./target/service-payment.jar service-order.jar
ENTRYPOINT ["java","-jar","/service-payment.jar", "&"]

View File

@ -0,0 +1,39 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://maven.apache.org/POM/4.0.0"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.atguigu</groupId>
<artifactId>service</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>service-payment</artifactId>
<dependencies>
<!--导入微信支付sdk-->
<dependency>
<groupId>com.github.wxpay</groupId>
<artifactId>wxpay-sdk</artifactId>
<version>0.0.3</version>
</dependency>
<dependency>
<groupId>com.atguigu</groupId>
<artifactId>service-order-client</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>com.atguigu</groupId>
<artifactId>rabbit_util</artifactId>
<version>1.0-SNAPSHOT</version>
<scope>compile</scope>
</dependency>
</dependencies>
<properties>
<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>
</project>

View File

@ -0,0 +1,22 @@
package com.atguigu.ssyx;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
/**
* ClassName: ServicePaymentApplication
* Package: vom.atguigu.ssyx
*
* @author yovinchen
* @Create 2023/10/13 10:29
*/
@SpringBootApplication
@EnableDiscoveryClient
@EnableFeignClients
public class ServicePaymentApplication {
public static void main(String[] args) {
SpringApplication.run(ServicePaymentApplication.class, args);
}
}

View File

@ -0,0 +1,66 @@
package com.atguigu.ssyx.payment.controller;
import com.atguigu.ssyx.common.result.Result;
import com.atguigu.ssyx.common.result.ResultCodeEnum;
import com.atguigu.ssyx.payment.service.PaymentInfoService;
import com.atguigu.ssyx.payment.service.WeixinService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
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.Map;
/**
* ClassName: WeiXinController
* Package: com.atguigu.ssyx.payment.controller
* 微信支付API
*
* @author yovinchen
* @Create 2023/10/13 10:32
*/
@Api(tags = "微信支付接口")
@RestController
@RequestMapping("/api/payment/weixin")
@Slf4j
public class WeiXinController {
@Autowired
private WeixinService weixinService;
@Autowired
private PaymentInfoService paymentInfoService;
@ApiOperation(value = "调用微信支付系统生成预付单")
@GetMapping("/createJsapi/{orderNo}")
public Result createJsapi(@PathVariable("orderNo") String orderNo) {
Map<String, String> map = weixinService.createJsapi(orderNo);
return Result.ok(map);
}
@ApiOperation(value = "查询订单支付状态")
@GetMapping("/queryPayStatus/{orderNo}")
public Result queryPayStatus(@PathVariable("orderNo") String orderNo) {
//1 调用微信支付系统接口查询订单支付状态
Map<String, String> resultMap = weixinService.queryPayStatus(orderNo);
//2 微信支付系统返回值为null支付失败
if (resultMap == null) {
return Result.build(null, ResultCodeEnum.PAYMENT_FAIL);
}
//3 如果微信支付系统返回值判断支付成功
if ("SUCCESS".equals(resultMap.get("trade_state"))) {
String out_trade_no = resultMap.get("out_trade_no");
paymentInfoService.paySuccess(out_trade_no, resultMap);
return Result.ok(null);
}
//4 支付中等待
return Result.build(null, ResultCodeEnum.PAYMENT_WAITING);
}
}

View File

@ -0,0 +1,16 @@
package com.atguigu.ssyx.payment.mapper;
import com.atguigu.ssyx.model.order.PaymentInfo;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
/**
* <p>
* 支付信息表 Mapper 接口
* </p>
*
* @author atguigu
* @since 2023-10-12
*/
public interface PaymentInfoMapper extends BaseMapper<PaymentInfo> {
}

View File

@ -0,0 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.atguigu.ssyx.order.mapper.PaymentInfoMapper">
</mapper>

View File

@ -0,0 +1,40 @@
package com.atguigu.ssyx.payment.service;
import com.atguigu.ssyx.model.order.PaymentInfo;
import com.baomidou.mybatisplus.extension.service.IService;
import java.util.Map;
/**
* ClassName: PaymentInfoService
* Package: com.atguigu.ssyx.payment.service
*
* @author yovinchen
* @Create 2023/10/13 10:33
*/
public interface PaymentInfoService extends IService<PaymentInfo> {
/**
* 根据orderNo查询支付订单
*
* @param orderNo
* @return
*/
PaymentInfo getPaymentInfoByOrderNo(String orderNo);
/**
* 保存支付订单
*
* @param orderNo
* @return
*/
PaymentInfo savePaymentInfo(String orderNo);
/**
* 支付成功
*
* @param orderNo
* @param resultMap
*/
void paySuccess(String orderNo, Map<String, String> resultMap);
}

View File

@ -0,0 +1,28 @@
package com.atguigu.ssyx.payment.service;
import java.util.Map;
/**
* ClassName: WeixinService
* Package: com.atguigu.ssyx.payment.controller.payment.service
*
* @author yovinchen
* @Create 2023/10/13 10:32
*/
public interface WeixinService {
/**
* 调用微信支付系统生成预付单
*
* @param orderNo
* @return
*/
Map<String, String> createJsapi(String orderNo);
/**
* 调用微信支付系统接口查询订单支付状态
*
* @param orderNo
* @return
*/
Map<String, String> queryPayStatus(String orderNo);
}

View File

@ -0,0 +1,111 @@
package com.atguigu.ssyx.payment.service.impl;
import com.atguigu.ssyx.client.order.OrderFeignClient;
import com.atguigu.ssyx.common.exception.SsyxException;
import com.atguigu.ssyx.common.result.ResultCodeEnum;
import com.atguigu.ssyx.enums.PaymentStatus;
import com.atguigu.ssyx.enums.PaymentType;
import com.atguigu.ssyx.model.order.OrderInfo;
import com.atguigu.ssyx.model.order.PaymentInfo;
import com.atguigu.ssyx.mq.constant.MqConst;
import com.atguigu.ssyx.mq.service.RabbitService;
import com.atguigu.ssyx.payment.mapper.PaymentInfoMapper;
import com.atguigu.ssyx.payment.service.PaymentInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.Date;
import java.util.Map;
/**
* <p>
* 支付信息表 服务实现类
* </p>
*
* @author atguigu
* @since 2023-10-12
*/
@Service
public class PaymentInfoServiceImpl extends ServiceImpl<PaymentInfoMapper, PaymentInfo> implements PaymentInfoService {
@Autowired
private OrderFeignClient orderFeignClient;
@Autowired
private RabbitService rabbitService;
/**
* 根据orderNo查询支付订单
*
* @param orderNo
* @return
*/
@Override
public PaymentInfo getPaymentInfoByOrderNo(String orderNo) {
return baseMapper.selectOne(new LambdaQueryWrapper<PaymentInfo>().eq(PaymentInfo::getOrderNo, orderNo));
}
/**
* 保存支付订单
*
* @param orderNo
* @return
*/
@Override
public PaymentInfo savePaymentInfo(String orderNo) {
//远程调用调用根据orderNo查询订单信息
OrderInfo orderInfo = orderFeignClient.getOrderInfo(orderNo);
if (orderInfo == null) {
throw new SsyxException(ResultCodeEnum.DATA_ERROR);
}
//封装到PaymentInfo对象
PaymentInfo paymentInfo = new PaymentInfo();
paymentInfo.setCreateTime(new Date());
paymentInfo.setOrderId(orderInfo.getId());
paymentInfo.setPaymentType(PaymentType.WEIXIN);
paymentInfo.setUserId(orderInfo.getUserId());
paymentInfo.setOrderNo(orderInfo.getOrderNo());
paymentInfo.setPaymentStatus(PaymentStatus.UNPAID);
String subject = "userID:" + orderInfo.getUserId() + "下订单";
paymentInfo.setSubject(subject);
//paymentInfo.setTotalAmount(orderInfo.getTotalAmount());
//TODO 为了测试
paymentInfo.setTotalAmount(new BigDecimal("0.01"));
//调用方法实现添加
baseMapper.insert(paymentInfo);
return paymentInfo;
}
/**
* 支付成功
*
* @param orderNo
* @param resultMap
*/
@Override
public void paySuccess(String orderNo, Map<String, String> resultMap) {
//1 查询当前订单支付记录表状态是否是已经支付
PaymentInfo paymentInfo = baseMapper.selectOne(
new LambdaQueryWrapper<PaymentInfo>()
.eq(PaymentInfo::getOrderNo, orderNo)
);
if (paymentInfo.getPaymentStatus() != PaymentStatus.UNPAID) {
return;
}
//2 如果支付记录表支付状态没有支付更新
paymentInfo.setPaymentStatus(PaymentStatus.PAID);
paymentInfo.setTradeNo(resultMap.get("transaction_id"));
paymentInfo.setCallbackContent(resultMap.toString());
baseMapper.updateById(paymentInfo);
//3 整合RabbitMQ实现 修改订单记录已经支付库存扣减
rabbitService.sendMessage(MqConst.EXCHANGE_PAY_DIRECT,
MqConst.ROUTING_PAY_SUCCESS, orderNo);
}
}

View File

@ -0,0 +1,140 @@
package com.atguigu.ssyx.payment.service.impl;
import com.atguigu.ssyx.common.constant.RedisConst;
import com.atguigu.ssyx.model.order.PaymentInfo;
import com.atguigu.ssyx.payment.service.PaymentInfoService;
import com.atguigu.ssyx.payment.service.WeixinService;
import com.atguigu.ssyx.payment.utils.ConstantPropertiesUtils;
import com.atguigu.ssyx.payment.utils.HttpClient;
import com.atguigu.ssyx.vo.user.UserLoginVo;
import com.github.wxpay.sdk.WXPayUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
/**
* ClassName: WeXinServiceImpl
* Package: com.atguigu.ssyx.payment.service.impl
*
* @author yovinchen
* @Create 2023/10/13 10:33
*/
@Service
public class WeXinServiceImpl implements WeixinService {
@Autowired
private PaymentInfoService paymentInfoService;
@Autowired
private RedisTemplate redisTemplate;
/**
* 调用微信支付系统生成预付单
*
* @param orderNo
* @return
*/
@Override
public Map<String, String> createJsapi(String orderNo) {
//1 向payment_info支付记录表添加记录目前支付状态正在支付中
PaymentInfo paymentInfo = paymentInfoService.getPaymentInfoByOrderNo(orderNo);
if (paymentInfo == null) {
paymentInfo = paymentInfoService.savePaymentInfo(orderNo);
}
//2 封装微信支付系统接口需要参数
Map<String, String> paramMap = new HashMap<>();
paramMap.put("appid", ConstantPropertiesUtils.APPID);
paramMap.put("mch_id", ConstantPropertiesUtils.PARTNER);
paramMap.put("nonce_str", WXPayUtil.generateNonceStr());
paramMap.put("body", paymentInfo.getSubject());
paramMap.put("out_trade_no", paymentInfo.getOrderNo());
int totalFee = paymentInfo.getTotalAmount().multiply(new BigDecimal(100)).intValue();
paramMap.put("total_fee", String.valueOf(totalFee));
paramMap.put("spbill_create_ip", "127.0.0.1");
paramMap.put("notify_url", ConstantPropertiesUtils.NOTIFYURL);
paramMap.put("trade_type", "JSAPI");
//openid
UserLoginVo userLoginVo = (UserLoginVo) redisTemplate.opsForValue().get(RedisConst.USER_LOGIN_KEY_PREFIX + paymentInfo.getUserId());
if (null != userLoginVo && !StringUtils.isEmpty(userLoginVo.getOpenId())) {
paramMap.put("openid", userLoginVo.getOpenId());
} else {
paramMap.put("openid", "odo3j4q2KskkbbW-krfE-cAxUnzU1");
}
//3 使用HttpClient调用微信支付系统接口
HttpClient client = new HttpClient("https://api.mch.weixin.qq.com/pay/unifiedorder");
//设置参数xml格式
try {
client.setXmlParam(WXPayUtil.generateSignedXml(paramMap, ConstantPropertiesUtils.PARTNERKEY));
client.setHttps(true);
client.post();
//4 调用微信支付系统接口之后返回结果 prepay_id
String xml = client.getContent();
Map<String, String> resultMap = WXPayUtil.xmlToMap(xml);
//5 封装需要数据-包含预付单标识 prepay_id
Map<String, String> parameterMap = new HashMap<>();
String prepayId = String.valueOf(resultMap.get("prepay_id"));
String packages = "prepay_id=" + prepayId;
parameterMap.put("appId", ConstantPropertiesUtils.APPID);
parameterMap.put("nonceStr", resultMap.get("nonce_str"));
parameterMap.put("package", packages);
parameterMap.put("signType", "MD5");
parameterMap.put("timeStamp", String.valueOf(new Date().getTime()));
String sign = WXPayUtil.generateSignature(parameterMap, ConstantPropertiesUtils.PARTNERKEY);
//返回结果
Map<String, String> result = new HashMap();
result.put("timeStamp", parameterMap.get("timeStamp"));
result.put("nonceStr", parameterMap.get("nonceStr"));
result.put("signType", "MD5");
result.put("paySign", sign);
result.put("package", packages);
//6 返回结果
return result;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/**
* 调用微信支付系统接口查询订单支付状态
*
* @param orderNo
* @return
*/
@Override
public Map<String, String> queryPayStatus(String orderNo) {
//封装数据
Map paramMap = new HashMap();
paramMap.put("appid", ConstantPropertiesUtils.APPID);
paramMap.put("mch_id", ConstantPropertiesUtils.PARTNER);
paramMap.put("out_trade_no", orderNo);
paramMap.put("nonce_str", WXPayUtil.generateNonceStr());
//2设置请求
HttpClient client = new HttpClient("https://api.mch.weixin.qq.com/pay/orderquery");
try {
client.setXmlParam(WXPayUtil.generateSignedXml(paramMap, ConstantPropertiesUtils.PARTNERKEY));
client.setHttps(true);
client.post();
//3 得到返回结果
String xml = client.getContent();
Map<String, String> stringMap = WXPayUtil.xmlToMap(xml);
return stringMap;
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}

View File

@ -0,0 +1,42 @@
package com.atguigu.ssyx.payment.utils;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
/**
* ClassName: ConstantPropertiesUtils
* Package: com.atguigu.ssyx.payment.utils
*
* @author yovinchen
* @Create 2023/10/13 10:48
*/
@Component
public class ConstantPropertiesUtils implements InitializingBean {
public static String APPID;
public static String PARTNER;
public static String PARTNERKEY;
public static String NOTIFYURL;
public static String CERT;
@Value("${weixin.appid}")
private String appid;
@Value("${weixin.partner}")
private String partner;
@Value("${weixin.partnerkey}")
private String partnerkey;
@Value("${weixin.notifyurl}")
private String notifyurl;
@Value("${weixin.cert}")
private String cert;
@Override
public void afterPropertiesSet() throws Exception {
APPID = appid;
PARTNER = partner;
PARTNERKEY = partnerkey;
NOTIFYURL = notifyurl;
CERT = cert;
}
}

View File

@ -0,0 +1,207 @@
package com.atguigu.ssyx.payment.utils;
import org.apache.http.Consts;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.*;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.SSLContextBuilder;
import org.apache.http.conn.ssl.TrustStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.ssl.SSLContexts;
import org.apache.http.util.EntityUtils;
import javax.net.ssl.SSLContext;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.security.KeyStore;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.text.ParseException;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
/**
* ClassName: HttpClient
* Package: com.atguigu.ssyx.payment.utils
*
* @author yovinchen
* @Create 2023/10/13 10:47
*/
public class HttpClient {
private String url;
private Map<String, String> param;
private int statusCode;
private String content;
private String xmlParam;
private boolean isHttps;
private boolean isCert = false;
//证书密码 微信商户号mch_id
private String certPassword;
public HttpClient(String url, Map<String, String> param) {
this.url = url;
this.param = param;
}
public HttpClient(String url) {
this.url = url;
}
public boolean isHttps() {
return isHttps;
}
public void setHttps(boolean isHttps) {
this.isHttps = isHttps;
}
public boolean isCert() {
return isCert;
}
public void setCert(boolean cert) {
isCert = cert;
}
public String getXmlParam() {
return xmlParam;
}
public void setXmlParam(String xmlParam) {
this.xmlParam = xmlParam;
}
public String getCertPassword() {
return certPassword;
}
public void setCertPassword(String certPassword) {
this.certPassword = certPassword;
}
public void setParameter(Map<String, String> map) {
param = map;
}
public void addParameter(String key, String value) {
if (param == null)
param = new HashMap<String, String>();
param.put(key, value);
}
public void post() throws IOException {
HttpPost http = new HttpPost(url);
setEntity(http);
execute(http);
}
public void put() throws IOException {
HttpPut http = new HttpPut(url);
setEntity(http);
execute(http);
}
public void get() throws IOException {
if (param != null) {
StringBuilder url = new StringBuilder(this.url);
boolean isFirst = true;
for (String key : param.keySet()) {
if (isFirst)
url.append("?");
else
url.append("&");
url.append(key).append("=").append(param.get(key));
}
this.url = url.toString();
}
HttpGet http = new HttpGet(url);
execute(http);
}
/**
* set http post,put param
*/
private void setEntity(HttpEntityEnclosingRequestBase http) {
if (param != null) {
List<NameValuePair> nvps = new LinkedList<NameValuePair>();
for (String key : param.keySet())
nvps.add(new BasicNameValuePair(key, param.get(key))); // 参数
http.setEntity(new UrlEncodedFormEntity(nvps, Consts.UTF_8)); // 设置参数
}
if (xmlParam != null) {
http.setEntity(new StringEntity(xmlParam, Consts.UTF_8));
}
}
private void execute(HttpUriRequest http) throws
IOException {
CloseableHttpClient httpClient = null;
try {
if (isHttps) {
if (isCert) {
FileInputStream inputStream = new FileInputStream(new File(ConstantPropertiesUtils.CERT));
KeyStore keystore = KeyStore.getInstance("PKCS12");
char[] partnerId2charArray = certPassword.toCharArray();
keystore.load(inputStream, partnerId2charArray);
SSLContext sslContext = SSLContexts.custom().loadKeyMaterial(keystore, partnerId2charArray).build();
SSLConnectionSocketFactory sslsf =
new SSLConnectionSocketFactory(sslContext,
new String[]{"TLSv1"},
null,
SSLConnectionSocketFactory.BROWSER_COMPATIBLE_HOSTNAME_VERIFIER);
httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
} else {
SSLContext sslContext = new SSLContextBuilder()
.loadTrustMaterial(null, new TrustStrategy() {
// 信任所有
public boolean isTrusted(X509Certificate[] chain,
String authType)
throws CertificateException {
return true;
}
}).build();
SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(
sslContext);
httpClient = HttpClients.custom().setSSLSocketFactory(sslsf)
.build();
}
} else {
httpClient = HttpClients.createDefault();
}
CloseableHttpResponse response = httpClient.execute(http);
try {
if (response != null) {
if (response.getStatusLine() != null)
statusCode = response.getStatusLine().getStatusCode();
HttpEntity entity = response.getEntity();
// 响应内容
content = EntityUtils.toString(entity, Consts.UTF_8);
}
} finally {
response.close();
}
} catch (Exception e) {
e.printStackTrace();
} finally {
httpClient.close();
}
}
public int getStatusCode() {
return statusCode;
}
public String getContent() throws ParseException, IOException {
return content;
}
}

View File

@ -0,0 +1,60 @@
server:
port: 8210
mybatis-plus:
type-enums-package: com.atguigu.ssyx.enums
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
mapper-locations: classpath:mapper/*.xml
feign:
sentinel:
enabled: true
client:
config:
default: #配置全局的feign的调用超时时间 如果 有指定的服务配置 默认的配置不会生效
connectTimeout: 30000 # 指定的是 消费者 连接服务提供者的连接超时时间 是否能连接 单位是毫秒
readTimeout: 50000 # 指定的是调用服务提供者的 服务 的超时时间() 单位是毫秒
spring:
main:
allow-bean-definition-overriding: true #当遇到同样名字的时候,是否允许覆盖注册
rabbitmq:
host: 43.143.164.194
port: 5672
username: guest
password: guest
redis:
host: 82.157.68.223
port: 6379
database: 0
timeout: 1800000
password:
lettuce:
pool:
max-active: 20 #最大连接数
max-wait: -1 #最大阻塞等待时间(负数表示没限制)
max-idle: 5 #最大空闲
min-idle: 0 #最小空闲
datasource:
type: com.zaxxer.hikari.HikariDataSource
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql://82.157.68.223:3306/shequ-order?characterEncoding=utf-8&useSSL=false
username: shequ-order
password: shequ-order
hikari:
connection-test-query: SELECT 1
connection-timeout: 60000
idle-timeout: 500000
max-lifetime: 540000
maximum-pool-size: 5
minimum-idle: 3
pool-name: GuliHikariPool
jackson:
date-format: yyyy-MM-dd HH:mm:ss
time-zone: GMT+8
# 微信
weixin:
#小程序微信公众平台appId
appid: wxcc651fcbab275e33
partner: 1481962542
partnerkey: MXb72b9RfshXZD4FRGV5KLqmv5bx9LT9
notifyurl: http://gmall-prod.atguigu.cn/api/payment/weixin/notify
cert: /Users/yovinchen/project/Java/ssyx/guigu-ssyx-parent/service/service-payment/src/main/resources/cert/apiclient_cert.p12

View File

@ -0,0 +1,11 @@
spring:
application:
name: service-payment
profiles:
active: dev
cloud:
nacos:
discovery:
server-addr: 82.157.68.223:8848
username: nacos
password: nacos

View File

@ -39,4 +39,12 @@ public interface SkuInfoMapper extends BaseMapper<SkuInfo> {
* @param skuNum * @param skuNum
*/ */
void unlockStock(@Param("skuId") Long skuId, @Param("skuNum") Integer skuNum); void unlockStock(@Param("skuId") Long skuId, @Param("skuNum") Integer skuNum);
/**
* 减库存
*
* @param skuId
* @param skuNum
*/
void minusStock(@Param("skuId") Long skuId, @Param("skuNum") Integer skuNum);
} }

View File

@ -0,0 +1,43 @@
package com.atguigu.ssyx.product.receiver;
import com.atguigu.ssyx.mq.constant.MqConst;
import com.atguigu.ssyx.product.service.SkuInfoService;
import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.util.StringUtils;
import java.io.IOException;
/**
* ClassName: StockReceiver
* Package: com.atguigu.ssyx.product.receiver
*
* @author yovinchen
* @Create 2023/10/13 15:05
*/
@Component
public class StockReceiver {
@Autowired
private SkuInfoService skuInfoService;
/**
* 扣减库存成功更新订单状态
*
* @param orderNo
* @throws IOException
*/
@RabbitListener(bindings = @QueueBinding(value = @Queue(value = MqConst.QUEUE_MINUS_STOCK, durable = "true"), exchange = @Exchange(value = MqConst.EXCHANGE_ORDER_DIRECT), key = {MqConst.ROUTING_MINUS_STOCK}))
public void minusStock(String orderNo, Message message, Channel channel) throws IOException {
if (!StringUtils.isEmpty(orderNo)) {
skuInfoService.minusStock(orderNo);
}
channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
}
}

View File

@ -115,4 +115,11 @@ public interface SkuInfoService extends IService<SkuInfo> {
* @return * @return
*/ */
Boolean checkAndLock(List<SkuStockLockVo> skuStockLockVoList, String orderNo); Boolean checkAndLock(List<SkuStockLockVo> skuStockLockVoList, String orderNo);
/**
* 扣减库存成功更新订单状态
*
* @param orderNo
*/
void minusStock(String orderNo);
} }

View File

@ -312,9 +312,7 @@ public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoMapper, SkuInfo> impl
//获取第一页数据每页显示三条记录 //获取第一页数据每页显示三条记录
Page<SkuInfo> pageParam = new Page<>(1, 3); Page<SkuInfo> pageParam = new Page<>(1, 3);
//调用方法查询 //调用方法查询
IPage<SkuInfo> skuInfoPage = baseMapper.selectPage(pageParam, new LambdaQueryWrapper<SkuInfo>().eq(SkuInfo::getIsNewPerson, 1) IPage<SkuInfo> skuInfoPage = baseMapper.selectPage(pageParam, new LambdaQueryWrapper<SkuInfo>().eq(SkuInfo::getIsNewPerson, 1).eq(SkuInfo::getPublishStatus, 1).orderByDesc(SkuInfo::getStock));
.eq(SkuInfo::getPublishStatus, 1)
.orderByDesc(SkuInfo::getStock));
return skuInfoPage.getRecords(); return skuInfoPage.getRecords();
} }
@ -333,31 +331,47 @@ public class SkuInfoServiceImpl extends ServiceImpl<SkuInfoMapper, SkuInfo> impl
} }
//2 遍历skuStockLockVoList得到每个商品验证库存并锁定库存具备原子性 //2 遍历skuStockLockVoList得到每个商品验证库存并锁定库存具备原子性
skuStockLockVoList.stream() skuStockLockVoList.stream().forEach(skuStockLockVo -> {
.forEach(skuStockLockVo -> { this.checkLock(skuStockLockVo);
this.checkLock(skuStockLockVo); });
});
//3 只要有一个商品锁定失败所有锁定成功的商品都解锁 //3 只要有一个商品锁定失败所有锁定成功的商品都解锁
boolean flag = skuStockLockVoList.stream() boolean flag = skuStockLockVoList.stream().anyMatch(skuStockLockVo -> !skuStockLockVo.getIsLock());
.anyMatch(skuStockLockVo -> !skuStockLockVo.getIsLock());
if (flag) { if (flag) {
//所有锁定成功的商品都解锁 //所有锁定成功的商品都解锁
skuStockLockVoList.stream() skuStockLockVoList.stream().filter(SkuStockLockVo::getIsLock).forEach(skuStockLockVo -> {
.filter(SkuStockLockVo::getIsLock) baseMapper.unlockStock(skuStockLockVo.getSkuId(), skuStockLockVo.getSkuNum());
.forEach(skuStockLockVo -> { });
baseMapper.unlockStock(skuStockLockVo.getSkuId(), skuStockLockVo.getSkuNum());
});
//返回失败的状态 //返回失败的状态
return false; return false;
} }
//4 如果所有商品都锁定成功了redis缓存相关数据为了方便后面解锁和减库存 //4 如果所有商品都锁定成功了redis缓存相关数据为了方便后面解锁和减库存
redisTemplate.opsForValue() redisTemplate.opsForValue().set(RedisConst.SROCK_INFO + orderNo, skuStockLockVoList);
.set(RedisConst.SROCK_INFO + orderNo, skuStockLockVoList);
return true; return true;
} }
/**
* 扣减库存成功更新订单状态
*
* @param orderNo
*/
@Override
public void minusStock(String orderNo) {
//从redis获取锁定库存信息
List<SkuStockLockVo> skuStockLockVoList = (List<SkuStockLockVo>) redisTemplate.opsForValue().get(RedisConst.SROCK_INFO + orderNo);
if (CollectionUtils.isEmpty(skuStockLockVoList)) {
return;
}
//遍历集合得到每个对象减库存
skuStockLockVoList.forEach(skuStockLockVo -> {
baseMapper.minusStock(skuStockLockVo.getSkuId(), skuStockLockVo.getSkuNum());
});
//删除redis数据
redisTemplate.delete(RedisConst.SROCK_INFO + orderNo);
}
//2 遍历skuStockLockVoList得到每个商品验证库存并锁定库存具备原子性 //2 遍历skuStockLockVoList得到每个商品验证库存并锁定库存具备原子性
private void checkLock(SkuStockLockVo skuStockLockVo) { private void checkLock(SkuStockLockVo skuStockLockVo) {
//获取锁 //获取锁