puzhibing
2025-02-05 74b0e0814e37d640596f44ec86d20fa9ecce9ed6
ruoyi-service/ruoyi-other/src/main/java/com/ruoyi/other/controller/TGoodsController.java
@@ -19,6 +19,9 @@
import com.ruoyi.common.core.web.page.PageInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.redis.service.RedisService;
import com.ruoyi.common.security.annotation.Logical;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.order.api.feignClient.OrderClient;
import com.ruoyi.order.api.model.TShoppingOrder;
@@ -30,6 +33,7 @@
import com.ruoyi.other.service.TActivityService;
import com.ruoyi.other.service.TCouponService;
import com.ruoyi.other.service.TGoodsService;
import com.ruoyi.other.util.RedisLock;
import com.ruoyi.payment.api.feignClient.AliPaymentClient;
import com.ruoyi.payment.api.feignClient.WxPaymentClient;
import com.ruoyi.payment.api.vo.AliPaymentReq;
@@ -37,6 +41,7 @@
import com.ruoyi.payment.api.vo.PaymentOrder;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
@@ -44,6 +49,8 @@
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;
/**
@@ -61,7 +68,6 @@
    private TGoodsService goodsService;
    @Autowired
    private TActivityService activityService;
    @Resource
    private TokenService tokenService;
    @Resource
@@ -74,6 +80,11 @@
    private TCouponService couponService;
    @Resource
    private WxPaymentClient wxPaymentClient;
    @Resource
    private RedisService redisService;
    @Autowired
    public RedisTemplate redisTemplate;
    /**
@@ -88,6 +99,8 @@
        return R.ok(collect);
    }
    @RequiresPermissions(value = {"/goods/add"}, logical = Logical.OR)
    @PostMapping("/saveGoods")
    @ApiOperation(tags = {"管理后台-商品管理"},value = "商品添加")
    @Log(title = "【商品管理】添加商品", businessType = BusinessType.INSERT)
@@ -98,6 +111,7 @@
    @RequiresPermissions(value = {"/goods/del"}, logical = Logical.OR)
    @GetMapping("/delete")
    @ApiOperation(tags = {"管理后台-商品管理"},value = "商品删除")
    @Log(title = "【商品管理】删除商品", businessType = BusinessType.DELETE)
@@ -108,6 +122,7 @@
    @RequiresPermissions(value = {"/goods/update"}, logical = Logical.OR)
    @PostMapping("/updateVip")
    @ApiOperation(tags = {"管理后台-商品管理"},value = "商品修改")
    @Log(title = "【商品管理】修改商品", businessType = BusinessType.UPDATE)
@@ -117,7 +132,7 @@
    }
    @RequiresPermissions(value = {"/goods/select"}, logical = Logical.OR)
    @GetMapping("/getInfo")
    @ApiOperation(tags = {"管理后台-商品管理"},value = "商品查看详情")
    public AjaxResult<TGoods> getInfo(Integer id) {
@@ -156,6 +171,7 @@
    @RequiresPermissions(value = {"/goods"}, logical = Logical.OR)
    @ApiOperation(tags = {"管理后台-商品管理"},value = "商品列表分页查询")
    @PostMapping(value = "/pageList")
    public AjaxResult<PageInfo<TGoods>> pageList(@RequestBody GoodsDTO dto) {
@@ -177,7 +193,6 @@
        //检查当前用户积分是否够
        Long userId = tokenService.getLoginUserApplet().getUserId();
        Integer point = 0;
        if (exchangeDto.getGoodType()==1) {
            //查询当前商品信息
            TGoods good = goodsService.getById(exchangeDto.getGoodId());
@@ -203,18 +218,26 @@
        return R.ok();
    }
    @ApiOperation(tags = {"小程序-兑换商城"},value = "积分兑换商品")
    @PostMapping(value = "/app/shop")
    public AjaxResult<PageInfo<TGoods>> shop(@RequestBody ExchangeDto exchangeDto) {
        //检查当前用户积分是否够
        //redis锁 和支付使用同一个锁
        RedisLock redisLock = new RedisLock(redisTemplate, "POINT_EXCHANGE_LOCK", 5, 30000);
        try {
            redisLock.lock();
        Long userId = tokenService.getLoginUserApplet().getUserId();
        Integer point = 0;
        TAppUser user = appUserClient.getUserById(userId).getData();
        if (exchangeDto.getGoodType()==1) {
            //查询当前商品信息
            TGoods good = goodsService.getById(exchangeDto.getGoodId());
            point = good.getRedeemPoints();
                if (user.getPoints().compareTo(good.getRedeemPoints()) < 0){
                    return AjaxResult.error("当前用户积分不足");
                }
            //检查当前用户是否到达兑换上限
            Long count = orderClient.getExchangeById(exchangeDto.getGoodId(), userId,exchangeDto.getGoodType()).getData();
            if (good.getLimitExchangeTimes() != -1 && count >= good.getLimitExchangeTimes()) {
@@ -226,17 +249,13 @@
                    goodsService.updateById(good);
                }
            }
        if (user.getPoints()<good.getRedeemPoints()){
            return AjaxResult.error("当前用户积分不足");
             }
        }else {
            TCoupon coupon = couponService.getById(exchangeDto.getGoodId());
            point = coupon.getRedeemPoints();
            Long count = orderClient.getExchangeById(exchangeDto.getGoodId(), userId,exchangeDto.getGoodType()).getData();
            if (user.getPoints()<point){
                if (user.getPoints().compareTo(coupon.getRedeemPoints()) < 0){
                return AjaxResult.error("当前用户积分不足");
            }
                Long count = orderClient.getExchangeById(exchangeDto.getGoodId(), userId,exchangeDto.getGoodType()).getData();
            if (coupon.getInventoryQuantity() != -1 && count >= coupon.getInventoryQuantity()) {
                return AjaxResult.error("当前用户已到达兑换"+coupon+"次");
            }else {
@@ -245,8 +264,6 @@
                    couponService.updateById(coupon);
                }
            }
        }
        exchangeDto.setPoint(point);
        exchangeDto.setUserId(userId);
@@ -281,7 +298,13 @@
        pointChangeDto.setCode(longR.getData().getCode());
        appUserClient.changeDown(pointChangeDto);
        return AjaxResult.success(longR.getData().getId());
        }catch (Exception e){
            e.printStackTrace();
        }finally {
            //解锁
            redisLock.unlock();
        }
        return AjaxResult.success();
    }