hjl
2024-05-21 ffb13ddfb98ddc0f360caa313a93b5dc8d6483f5
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package com.ruoyi.goods.service.impl;
 
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.constant.Constants;
import com.ruoyi.common.core.constant.RedisConstants;
import com.ruoyi.common.core.exception.GlobalException;
import com.ruoyi.common.security.service.TokenService;
import com.ruoyi.goods.domain.Recipient;
import com.ruoyi.goods.domain.TGoods;
import com.ruoyi.goods.domain.TOrder;
import com.ruoyi.goods.dto.GoodExchangeDTO;
import com.ruoyi.goods.mapper.TGoodsMapper;
import com.ruoyi.goods.service.ITGoodsService;
import com.ruoyi.goods.service.ITOrderService;
import com.ruoyi.goods.vo.TGoodsVO;
import com.ruoyi.study.api.domain.TUser;
import com.ruoyi.study.api.feignClient.StudyClient;
import org.redisson.api.RSemaphore;
import org.redisson.api.RedissonClient;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
 
/**
 * <p>
 * 商品表 服务实现类
 * </p>
 *
 * @author 无关风月
 * @since 2024-04-26
 */
@Service
public class TGoodsServiceImpl extends ServiceImpl<TGoodsMapper, TGoods> implements ITGoodsService {
 
    @Resource
    private RedissonClient redissonClient;
    @Resource
    private StudyClient studyClient;
    @Resource
    private ITOrderService orderService;
    @Resource
    private TokenService tokenService;
 
    @Override
    public List<TGoodsVO> goodRecommend(Integer userId) {
        return baseMapper.goodRecommend(userId);
    }
 
    @Override
    public Map<String, Object> redeemNow(String goodId, Recipient recipient) {
        // 商品详情
        TGoods goods = lambdaQuery().eq(TGoods::getId, goodId).one();
        Map<String, Object> result = new HashMap<>(8);
        result.put("goodDetail", goods);
        // 用户收货地址
        result.put("address", recipient);
        // 库存预热,redisson分布式锁
        String key = String.format(RedisConstants.GOOD_STOCK, goods.getId());
        RSemaphore semaphore = redissonClient.getSemaphore(key);
        semaphore.trySetPermits(goods.getSurplus());
        return result;
    }
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Object goodExchange(GoodExchangeDTO goodExchange, Recipient recipient) {
        Integer number = goodExchange.getNumber();
        Integer goodId = goodExchange.getGoodId();
        TGoods good = this.getById(goodId);
        if (null == good) {
            throw new GlobalException("商品不存在,请稍后重试!");
        }
        // 校验用户积分是否足够兑换
        TUser user = studyClient.userInfo().getData();
        int needIntegral = good.getIntegral() * number;
        if (user.getIntegral() < needIntegral) {
            throw new GlobalException("兑换失败,当前剩余积分不足!");
        }
        // redisson分布式锁,防止超卖
        String key = String.format(RedisConstants.GOOD_STOCK, good.getId());
        RSemaphore semaphore = redissonClient.getSemaphore(key);
        boolean tried = semaphore.tryAcquire(number);
        // 兑换失败,库存不足
        if (!tried) {
            throw new GlobalException("当前商品库存不足!");
        }
        // 兑换成功,生成订单信息、生成积分明细(积分明细需要远程调用rouyi-study服务)
        TOrder order = orderInfo(goodExchange, recipient, number, goodId, needIntegral);
        if (!orderService.save(order)) {
            throw new GlobalException("订单生成失败!");
        }
        // 远程调用,生成积分明细
        Boolean boo = studyClient.addIntegralDetail(Constants.BURDEN + needIntegral, Constants.SHOPPING_CONSUME).getData();
        if (!boo) {
            throw new GlobalException("生成积分明细失败,请稍后重试!");
        }
        return true;
    }
 
    private TOrder orderInfo(GoodExchangeDTO goodExchange, Recipient recipient, Integer number, Integer goodId, int needIntegral) {
        TOrder order = new TOrder();
        order.setOrderNumber(goodExchange.getOrderNumber());
        order.setUserId(tokenService.getLoginUserStudy().getUserid());
        order.setInsertTime(new Date());
        order.setGoodsId(goodId);
        order.setCount(number);
        order.setState(1);
        order.setIntegral(needIntegral);
        order.setConsigneeName(recipient.getRecipient());
        order.setConsigneePhone(recipient.getRecipientPhone());
        order.setConsigneeAddress(recipient.getAddress());
        order.setDisabled(Boolean.FALSE);
        return order;
    }
}