huliguo
2025-04-18 34f741f39e22bf48df33321230380b40c23110c3
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
package com.ruoyi.other.controller;
 
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.page.PageInfo;
import com.ruoyi.common.redis.annotation.DistributedLock;
import com.ruoyi.other.api.domain.Goods;
import com.ruoyi.other.dto.AddGoodsDTO;
import com.ruoyi.other.enums.GoodsStatus;
import com.ruoyi.other.service.GoodsService;
import com.ruoyi.other.vo.GoodsVO;
import com.ruoyi.system.api.domain.SysConfig;
import com.ruoyi.system.api.feignClient.SysConfigClient;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*;
 
import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.List;
 
/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author luodangjia
 * @since 2024-11-20
 */
@RestController
@RequestMapping("/goods")
@Api("商品")
public class GoodsController extends BaseController {
    @Resource
    private GoodsService goodsService;
    @Resource
    private SysConfigClient sysConfigClient;
 
 
 
 
 
    /**
     * 添加商品
     */
    @PostMapping("/addGoods")
    @ApiOperation(value = "发布商品", tags = {"管理后台-商品管理"})
    public R<Void> addGoods(@RequestBody Goods goods) {
 
        if(goods.getPurchaseLimit()==null){
            goods.setPurchaseLimit(-1);
        }
        goods.setSaleNum(0);
        goods.setStatus(GoodsStatus.DOWN.getCode());
        goods.setIntegral(getPoint(goods.getSellingPrice()));
        goods.setDelFlag(0);
        goods.setCreateTime(LocalDateTime.now());
        goodsService.addGoods(goods);
        return R.ok();
    }
    /**
     * 获取现金对应积分
     */
    public Integer getPoint(BigDecimal cash){
        if (cash == null || cash.compareTo(BigDecimal.ZERO) < 0) {
            throw new IllegalArgumentException("金额不能为null或负数");
        }
        // 获取积分兑换比例配置
        R<SysConfig> info = sysConfigClient.getInfo(6L);
        if (info == null || info.getData() == null) {
            throw new RuntimeException("获取积分兑换比例配置失败");
        }
        String configValue = info.getData().getConfigValue();
        if (StringUtils.isBlank(configValue)) {
            throw new RuntimeException("积分兑换比例配置值为空");
        }
        try {
            // 使用BigDecimal处理比例,避免精度问题
            BigDecimal ratio = new BigDecimal(configValue.trim());
            if (ratio.compareTo(BigDecimal.ZERO) <= 0) {
                throw new RuntimeException("积分兑换比例必须大于0");
            }
 
            // 计算积分并四舍五入取整
            return cash.multiply(ratio).intValue();
 
        } catch (NumberFormatException e) {
            throw new RuntimeException("积分兑换比例配置值格式错误", e);
        } catch (ArithmeticException e) {
            throw new RuntimeException("积分计算结果溢出", e);
        }
 
    }
 
    /**
     * 商品克隆
     */
    @GetMapping("/cloneGoods")
    @ApiOperation(value = "商品克隆", tags = {"管理后台-商品管理"})
    public R<Void> cloneGoods(@RequestParam Long goodsId,@RequestParam String goodsName) {
        Goods goods = goodsService.getManageGoodsDetail(goodsId);
        goods.setId(null);
        goods.setName(goodsName);
        goodsService.addGoods(goods);
        return R.ok();
    }
 
    /**
     * 管理后台商品列表
     */
    @GetMapping("/manageGoodsList")
    @ApiOperation(value = "商品列表", tags = {"管理后台-商品管理"})
    public R<IPage<Goods>> manageGoodsList(@ApiParam("页码") @RequestParam Integer pageNum,
                                           @ApiParam("每一页数据大小") Integer pageSize,
                                           Goods goods){
        return R.ok(goodsService.getManageGoodsList(new Page<>(pageNum, pageSize), goods));
    }
 
    /**
     * 后台商品详情
     */
    @GetMapping("/manageGoodsDetail/{goodsId}")
    @ApiOperation(value = "商品详情", tags = {"管理后台-商品管理"})
    public R<Goods> manageGoodsDetail(@PathVariable("goodsId") Long goodsId){
        return R.ok(goodsService.getManageGoodsDetail(goodsId));
    }
 
    /**
     * 后台商品修改
     */
    @PutMapping("/manageGoodsUpdate")
    @ApiOperation(value = "商品修改", tags = {"管理后台-商品管理"})
    public R<Void> manageGoodsUpdate(@RequestBody Goods goods){
        if(goods.getPurchaseLimit()==null){
            goods.setPurchaseLimit(-1);
        }
        goods.setSaleNum(0);
        goods.setStatus(GoodsStatus.DOWN.getCode());
        goods.setIntegral(getPoint(goods.getSellingPrice()));
        goods.setDelFlag(0);
        goods.setCreateTime(LocalDateTime.now());
        goodsService.updateManageGoods(goods);
        return R.ok();
    }
 
 
    @PutMapping("/editGoodsSort")
    @ApiOperation(value = "商品修改排序", tags = {"管理后台-商品管理"})
    @ApiImplicitParams({
            @ApiImplicitParam(value = "商品id", name = "id", dataType = "int", required = true),
            @ApiImplicitParam(value = "排序", name = "sort", dataType = "int", required = true),
    })
    public R editGoodsSort(@RequestParam("id") Integer id, @RequestParam("sort") Integer sort){
        Goods goods = goodsService.getById(id);
        goods.setSort(sort);
        goodsService.updateById(goods);
        return R.ok();
    }
 
 
 
 
    /**
     * 更新上/下架状态
     */
    @PutMapping("/manageGoodsStatus/{goodsId}/{status}")
    @ApiOperation(value = "商品上/下架", tags = {"管理后台-商品管理"})
    public R<Void> manageGoodsStatus(@PathVariable("goodsId") Long goodsId,
                                     @PathVariable("status") Integer status){
        goodsService.lambdaUpdate().eq(Goods::getId, goodsId).set(Goods::getStatus, status).update();
        return R.ok();
    }
 
 
    /**
     * 后台商品删除
     */
    @DeleteMapping("/manageGoodsDelete/{goodsId}")
    @ApiOperation(value = "商品删除", tags = {"管理后台-商品管理"})
    public R<Void> manageGoodsDelete(@PathVariable("goodsId") Long goodsId){
        Goods goods = goodsService.getById(goodsId);
        goods.setDelFlag(1);
        goodsService.updateById(goods);
        return R.ok();
    }
 
   
 
 
 
    /**
     * 商品列表
     */
    @GetMapping("/goodsList")
    @ApiOperation(value = "热门商品列表", tags = {"小程序-商城-首页", "小程序-首页"})
    public R<PageInfo<GoodsVO>> goodsList(Goods goods){
        return R.ok(goodsService.goodsList(goods));
    }
 
 
    /**
     * 指定门店商品
     */
    @GetMapping("/getGoodsListByShopId")
    @ApiOperation(value = "商品购买列表", tags = {"小程序-首页-门店详情"})
    public R<PageInfo<GoodsVO>> getGoodsListByShopId(@ApiParam("门店id") Integer shopId, Integer pageCurr, Integer pageSize) {
        PageInfo<GoodsVO> pageInfo = new PageInfo(pageCurr, pageSize);
        List<GoodsVO> goodsList = goodsService.getGoodsListByShopId(pageInfo, shopId);
        return R.ok(pageInfo.setRecords(goodsList));
    }
 
 
    /**
     * 商品详情
     */
    @GetMapping("/goodsDetail/{goodsId}")
    @ApiOperation(value = "商品详情", tags = {"小程序-商城-首页"})
    public R<GoodsVO> goodsDetail(@PathVariable("goodsId") Long goodsId) {
        return R.ok(goodsService.goodsDetail(goodsId));
    }
 
 
    /**
     * 获取商品数据
     * @return
     */
    @ResponseBody
    @PostMapping("/getGoodsByType")
    public R<List<Goods>> getGoodsByType(){
        List<Goods> list = goodsService.list(new LambdaQueryWrapper<Goods>().eq(Goods::getDelFlag, 0).eq(Goods::getStatus, 2));
        return R.ok(list);
    }
 
 
    /**
     * 根据id获取商品信息
     * @param id
     * @return
     */
    @ResponseBody
    @PostMapping("/getGoodsById")
    public R<Goods> getGoodsById(@RequestParam("id") Integer id){
        Goods goods = goodsService.getById(id);
        return R.ok(goods);
    }
 
    @ResponseBody
    @PostMapping("/getGoodsByIds")
    public R<List<Goods>> getGoodsByIds(@RequestParam("ids") String [] ids){
        List<Goods> goods = goodsService.lambdaQuery().in(Goods::getId, ids).list();
        return R.ok(goods);
    }
 
 
    /**
     * 编辑商品
     * @param goods
     * @return
     */
    @PostMapping("/editGoodsList")
    public R editGoodsList(@RequestBody List<Goods> goods){
        goodsService.updateBatchById(goods);
        return R.ok();
    }
 
 
    /**
     * 商品销量增加
     */
    @PutMapping("/editGoodsNum")
    @DistributedLock(lockNamePre = "#goods_lock", lockNamePost = "#goodsId")
    public R<Void> editGoodsNum(@RequestParam("goodsId") Integer goodsId, @RequestParam("num") Integer num){
        Goods goods = goodsService.getById(goodsId);
        goods.setSaleNum(goods.getSaleNum() + num);
        goodsService.updateById(goods);
        return R.ok();
    }
 
 
    /**
     * 获取所有商品
     * @return
     */
    @PostMapping("/getAllGoods")
    public R<List<Goods>> getAllGoods(){
        List<Goods> list = goodsService.list(new LambdaQueryWrapper<Goods>().eq(Goods::getDelFlag, 0).eq(Goods::getStatus, 2));
        return R.ok(list);
    }
}