From 8db855719710fc5e33a51d1f30050c007b38b65d Mon Sep 17 00:00:00 2001
From: 无关风月 <443237572@qq.com>
Date: 星期一, 30 九月 2024 14:15:35 +0800
Subject: [PATCH] bug修改
---
ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/TShoppingOrderServiceImpl.java | 162 +++++++++++++++++++++++++++++++++++++++++++++--------
1 files changed, 137 insertions(+), 25 deletions(-)
diff --git a/ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/TShoppingOrderServiceImpl.java b/ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/TShoppingOrderServiceImpl.java
index c64f6ab..30d8358 100644
--- a/ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/TShoppingOrderServiceImpl.java
+++ b/ruoyi-service/ruoyi-order/src/main/java/com/ruoyi/order/service/impl/TShoppingOrderServiceImpl.java
@@ -6,6 +6,8 @@
import com.ruoyi.account.api.dto.GrantCouponDto;
import com.ruoyi.account.api.feignClient.AppCouponClient;
import com.ruoyi.account.api.feignClient.AppUserAddressClient;
+import com.ruoyi.account.api.feignClient.AppUserClient;
+import com.ruoyi.account.api.model.TAppUser;
import com.ruoyi.account.api.model.TAppUserAddress;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.web.domain.AjaxResult;
@@ -23,6 +25,7 @@
import com.ruoyi.order.mapper.TShoppingOrderMapper;
import com.ruoyi.order.service.TShoppingOrderRefundService;
import com.ruoyi.order.service.TShoppingOrderService;
+import com.ruoyi.order.util.RedisLock;
import com.ruoyi.other.api.domain.TCoupon;
import com.ruoyi.other.api.domain.TGoods;
import com.ruoyi.other.api.feignClient.CouponClient;
@@ -35,6 +38,8 @@
import com.ruoyi.payment.api.vo.AliQueryOrder;
import com.ruoyi.payment.api.vo.NotifyV3PayDecodeRespBody;
import io.seata.spring.annotation.GlobalTransactional;
+import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
@@ -42,11 +47,10 @@
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.SimpleDateFormat;
+import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
-import java.util.ArrayList;
-import java.util.Date;
-import java.util.List;
+import java.util.*;
import java.util.stream.Collectors;
/**
@@ -68,6 +72,8 @@
@Resource
private CouponClient couponClient;
+ @Resource
+ private AppUserClient appUserClient;
@Resource
private AppUserAddressClient appUserAddressClient;
@@ -82,6 +88,9 @@
@Resource
private TShoppingOrderRefundService shoppingOrderRefundService;
+
+ @Autowired
+ public RedisTemplate redisTemplate;
@@ -190,14 +199,16 @@
* @return
*/
@Override
- public List<MyShoppingOrderList> getMyShoppingOrderList(GetMyShoppingOrderList query) {
+ public Map<String, Object> getMyShoppingOrderList(GetMyShoppingOrderList query) {
Long userId = tokenService.getLoginUserApplet().getUserId();
LambdaQueryWrapper<TShoppingOrder> wrapper = new LambdaQueryWrapper<TShoppingOrder>().eq(TShoppingOrder::getDelFlag, 0)
.eq(TShoppingOrder::getAppUserId, userId);
if(query.getStatus() != 0){
wrapper.eq(TShoppingOrder::getStatus, query.getStatus());
}
- List<TShoppingOrder> list = this.list(wrapper.orderByDesc(TShoppingOrder::getCreateTime).last(" limit " + query.getPageCurr() + ", " + query.getPageSize()));
+ long count = this.count(wrapper);
+ Integer pageCurr = (query.getPageCurr() - 1) * query.getPageSize();
+ List<TShoppingOrder> list = this.list(wrapper.orderByDesc(TShoppingOrder::getCreateTime).last(" limit " + pageCurr + ", " + query.getPageSize()));
List<MyShoppingOrderList> pageList = new ArrayList<>();
for (TShoppingOrder tShoppingOrder : list) {
MyShoppingOrderList myShoppingOrderList = new MyShoppingOrderList();
@@ -223,7 +234,10 @@
myShoppingOrderList.setOrderType(tShoppingOrder.getOrderType());
pageList.add(myShoppingOrderList);
}
- return pageList;
+ Map<String, Object> map = new HashMap<>();
+ map.put("list", pageList);
+ map.put("total", count);
+ return map;
}
@@ -301,7 +315,7 @@
//退款金额
BigDecimal refundAmount = shoppingOrder.getPaymentAmount().subtract(bigDecimal);
-
+
//先查询第三方订单状态订单是否退款
//支付方式(1=微信,2=支付宝)
Integer paymentType = shoppingOrder.getPaymentType();
@@ -340,7 +354,7 @@
shoppingOrderRefund.setPayType(shoppingOrder.getPaymentType());
shoppingOrderRefund.setShoppingOrderId(shoppingOrder.getId());
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssSSS");
- shoppingOrderRefund.setRefundCode("GDF" + sdf.format(new Date()) + (Math.random() * 1000));
+ shoppingOrderRefund.setRefundCode("GDF" + sdf.format(new Date()) + Double.valueOf((Math.random() * 1000)).intValue());
shoppingOrderRefund.setRefundAmount(refundAmount);
shoppingOrderRefund.setRefundStatus(1);
shoppingOrderRefund.setCode(shoppingOrder.getCode());
@@ -353,9 +367,10 @@
if(1 == paymentType){
WxPaymentRefundModel model = new WxPaymentRefundModel();
model.setOut_trade_no(shoppingOrder.getCode());
+ model.setTransaction_id(shoppingOrder.getSerialNumber());
model.setOut_refund_no(shoppingOrderRefund.getRefundCode());
model.setReason("取消订单");
- model.setNotify_url("http://127.0.0.1:9000/order/t-shopping-order/cancelShoppingOrderWxRefund");
+ model.setNotify_url("/order/t-shopping-order/cancelShoppingOrderWxRefund");
WxPaymentRefundModel.RefundAmount amount = new WxPaymentRefundModel.RefundAmount();
amount.setRefund(refundAmount.multiply(new BigDecimal(100)).intValue());
amount.setTotal(shoppingOrder.getPaymentAmount().multiply(new BigDecimal(100)).intValue());
@@ -378,6 +393,44 @@
AjaxResult success = cancelShoppingOrderWxRefund(resp.getOutTradeNo(), resp.getTradeNo(), "SUCCESS", sdf1.format(new Date()));
if(success.isSuccess()){
shoppingOrderRefundService.save(shoppingOrderRefund);
+ //商品
+ if(shoppingOrder.getOrderType() == 1){
+ //redis锁 和支付使用同一个锁
+ RedisLock redisLock = new RedisLock(redisTemplate, "SHOPPING_GOODS_LOCK", 5, 30000);
+ try {
+ redisLock.lock();
+ TGoods goods = goodsClient.getGoodsById(shoppingOrder.getGoodsId()).getData();
+ Integer inventory = goods.getInventory();
+ if(-1 != inventory){
+ goods.setInventory(inventory + shoppingOrder.getPurchaseQuantity());
+ goodsClient.updateGoods(goods);
+ }
+ }catch (Exception e){
+ e.printStackTrace();
+ }finally {
+ //解锁
+ redisLock.unlock();
+ }
+ }
+ //优惠券
+ if(shoppingOrder.getOrderType() == 2){
+ //redis锁 和支付使用同一个锁
+ RedisLock redisLock = new RedisLock(redisTemplate, "SHOPPING_COUPON_LOCK", 5, 30000);
+ try {
+ redisLock.lock();
+ TCoupon coupon = couponClient.getCouponById1(shoppingOrder.getCouponId()).getData();
+ Integer inventory = coupon.getInventoryQuantity();
+ if(-1 != inventory){
+ coupon.setInventoryQuantity(inventory + shoppingOrder.getPurchaseQuantity());
+ couponClient.updateCoupon(coupon);
+ }
+ }catch (Exception e){
+ e.printStackTrace();
+ }finally {
+ //解锁
+ redisLock.unlock();
+ }
+ }
}
}
}
@@ -398,6 +451,7 @@
public AjaxResult cancelShoppingOrderWxRefund(String out_refund_no, String refund_id, String refund_status, String success_time) {
if("SUCCESS".equals(refund_status)){
TShoppingOrderRefund one = shoppingOrderRefundService.getOne(new LambdaQueryWrapper<TShoppingOrderRefund>().eq(TShoppingOrderRefund::getRefundCode, out_refund_no));
+ one.setRefundStatus(2);
one.setRefundSerialNumber(refund_id);
one.setRefundTime(LocalDateTime.parse(success_time, DateTimeFormatter.ofPattern("yyyy-MM-DDTHH:mm:ss+TIMEZONE")));
shoppingOrderRefundService.updateById(one);
@@ -405,26 +459,41 @@
TShoppingOrder shoppingOrder = this.getById(one.getShoppingOrderId());
//商品
if(shoppingOrder.getOrderType() == 1){
- // todo 需完善redis锁
//redis锁 和支付使用同一个锁
- TGoods goods = goodsClient.getGoodsById(shoppingOrder.getGoodsId()).getData();
- Integer inventory = goods.getInventory();
- if(-1 != inventory){
- goods.setInventory(inventory + shoppingOrder.getPurchaseQuantity());
- goodsClient.updateGoods(goods);
+ RedisLock redisLock = new RedisLock(redisTemplate, "SHOPPING_GOODS_LOCK", 5, 30000);
+ try {
+ redisLock.lock();
+ TGoods goods = goodsClient.getGoodsById(shoppingOrder.getGoodsId()).getData();
+ Integer inventory = goods.getInventory();
+ if(-1 != inventory){
+ goods.setInventory(inventory + shoppingOrder.getPurchaseQuantity());
+ goodsClient.updateGoods(goods);
+ }
+ }catch (Exception e){
+ e.printStackTrace();
+ }finally {
+ //解锁
+ redisLock.unlock();
}
- //解锁
}
//优惠券
if(shoppingOrder.getOrderType() == 2){
- //redis锁
- TCoupon coupon = couponClient.getCouponById1(shoppingOrder.getCouponId()).getData();
- Integer inventory = coupon.getInventoryQuantity();
- if(-1 != inventory){
- coupon.setInventoryQuantity(inventory + shoppingOrder.getPurchaseQuantity());
- couponClient.updateCoupon(coupon);
+ //redis锁 和支付使用同一个锁
+ RedisLock redisLock = new RedisLock(redisTemplate, "SHOPPING_COUPON_LOCK", 5, 30000);
+ try {
+ redisLock.lock();
+ TCoupon coupon = couponClient.getCouponById1(shoppingOrder.getCouponId()).getData();
+ Integer inventory = coupon.getInventoryQuantity();
+ if(-1 != inventory){
+ coupon.setInventoryQuantity(inventory + shoppingOrder.getPurchaseQuantity());
+ couponClient.updateCoupon(coupon);
+ }
+ }catch (Exception e){
+ e.printStackTrace();
+ }finally {
+ //解锁
+ redisLock.unlock();
}
- //解锁
}
}
return AjaxResult.success();
@@ -438,7 +507,8 @@
@Override
public List<MyShoppingOrderList> getNoInvoicedOrder(GetNoInvoicedOrder query) {
Long userId = tokenService.getLoginUserApplet().getUserId();
- List<TShoppingOrder> list = this.baseMapper.getNoInvoicedOrder(userId, query.getMonth(), query.getPageCurr(), query.getPageSize());
+ Integer pageCurr = (query.getPageCurr() - 1) * query.getPageSize();
+ List<TShoppingOrder> list = this.baseMapper.getNoInvoicedOrder(userId, query.getMonth(), pageCurr, query.getPageSize());
List<MyShoppingOrderList> pageList = new ArrayList<>();
for (TShoppingOrder tShoppingOrder : list) {
MyShoppingOrderList myShoppingOrderList = new MyShoppingOrderList();
@@ -450,7 +520,7 @@
name = goods.getName();
imgUrl = goods.getCoverPicture();
}else{
- TCoupon coupon = couponClient.getCouponById1(tShoppingOrder.getGoodsId()).getData();
+ TCoupon coupon = couponClient.getCouponById1(tShoppingOrder.getCouponId()).getData();
name = coupon.getName();
imgUrl = coupon.getCoverPicture();
}
@@ -473,6 +543,7 @@
TShoppingOrder shoppingOrder = this.baseMapper.selectOne(Wrappers.lambdaQuery(TShoppingOrder.class).eq(TShoppingOrder::getCode, code));
shoppingOrder.setPaymentStatus(2);
shoppingOrder.setSerialNumber(outTradeNo);
+ shoppingOrder.setPayTime(LocalDateTime.now());
this.updateById(shoppingOrder);
//如果是优惠券,赠送优惠券
if (shoppingOrder.getOrderType()==2){
@@ -499,8 +570,49 @@
}
PageInfo<TShoppingOrder> pageInfo = new PageInfo<>(query.getPageCurr(),query.getPageSize());
List<TShoppingOrder> list = this.baseMapper.pageList(pageInfo,query,startTime1,startTime2);
+ for (TShoppingOrder tShoppingOrder : list) {
+ tShoppingOrder.setUid(tShoppingOrder.getId().toString());
+ switch (tShoppingOrder.getOrderType()){
+ case 1:
+ TGoods data = goodsClient.getGoodsById(tShoppingOrder.getGoodsId()).getData();
+ if (data!=null){
+ tShoppingOrder.setName(data.getName());
+ }
+ break;
+ case 2:
+ TCoupon data1 = couponClient.getCouponById1(tShoppingOrder.getCouponId()).getData();
+ if (data1!=null){
+ tShoppingOrder.setName(data1.getName());
+ }
+ tShoppingOrder.setStatus(3);
+ break;
+ }
+ if (tShoppingOrder.getAppUserId() != null){
+ TAppUser data = appUserClient.getUserById(tShoppingOrder.getAppUserId()).getData();
+ if (data!=null){
+ tShoppingOrder.setPhone(data.getPhone());
+ }
+ }
+ }
pageInfo.setRecords(list);
return pageInfo;
}
+
+ @Override
+ public List<SixShopDto> sixBefore(LocalDate sixBefore, Integer status) {
+ return this.baseMapper.sixBefore(sixBefore,status);
+ }
+
+ @Override
+ public BigDecimal getSumAmount(LocalDate sixBefore) {
+ return this.baseMapper.getSumAmount(sixBefore);
+ }
+
+ @Override
+ public List<Map<String, Object>> getData(ChargingStatisticsQueryDto statisticsQueryDto) {
+ return this.baseMapper.getData(statisticsQueryDto);
+ }
+
+
}
--
Gitblit v1.7.1