rentaiming
2024-07-26 8d738d847973e1184a704b5ca3d4f492bffdb03f
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
package com.ruoyi.goods.controller.inner;
 
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.ruoyi.common.core.constant.SecurityConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.security.annotation.InnerAuth;
import com.ruoyi.goods.mapper.GoodsSkuMapper;
import com.ruoyi.goods.service.IGoodsSkuService;
import com.ruoyi.system.api.domain.GoodsGroupPurchaseInfo;
import com.ruoyi.goods.service.impl.GoodsGroupPurchaseInfoServiceImpl;
import com.ruoyi.system.api.domain.GoodsGroupPurchase;
import com.ruoyi.goods.service.IGoodsGroupPurchaseService;
import com.ruoyi.system.api.domain.GoodsSku;
import com.ruoyi.system.api.feignClient.OrderClient;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
 
/**
 * <p>
 * 商品团购表 前端控制器
 * </p>
 *
 * @author mitao
 * @since 2024-05-16
 */
@Slf4j
@RestController
@RequiredArgsConstructor
@RequestMapping("/goods-group-purchase")
public class GoodsGroupPurchaseController {
 
    private final IGoodsGroupPurchaseService goodsGroupPurchaseService;
 
    private final GoodsGroupPurchaseInfoServiceImpl goodsGroupPurchaseInfoServiceImpl;
 
    private final OrderClient orderClient;
 
    private final GoodsSkuMapper goodsSkuMapper;
 
    @InnerAuth
    @PostMapping("/getGoodsSeckiGoodsGroupPurchaseOne")
    public R<GoodsGroupPurchase> getGoodsSeckiGoodsGroupPurchaseOne(@RequestBody Long goodsSkuId) {
        GoodsGroupPurchase GoodsSeckillOne = goodsGroupPurchaseService.getById(goodsSkuId);
        return R.ok(GoodsSeckillOne);
    }
 
 
    @InnerAuth
    @PostMapping("/getGoodsSeckiGoodsGroupPurchaseInfo")
    public R<GoodsGroupPurchaseInfo> getGoodsSeckiGoodsGroupPurchaseInfo(@RequestBody Long goodsSkuId) {
        LambdaQueryWrapper<GoodsGroupPurchaseInfo> wrapper6= Wrappers.lambdaQuery();
        wrapper6.eq(GoodsGroupPurchaseInfo::getDelFlag,0);
        wrapper6.eq(GoodsGroupPurchaseInfo::getGroupStatus,0);
        wrapper6.eq(GoodsGroupPurchaseInfo::getGroupPurchaseId,goodsSkuId);
        GoodsGroupPurchaseInfo one1 = goodsGroupPurchaseInfoServiceImpl.getOne(wrapper6);
 
        return R.ok(one1);
    }
 
    @InnerAuth
    @PostMapping("/getGoodsSeckiGoodsGroupPurchaseInfo2")
    public R<GoodsGroupPurchaseInfo> getGoodsSeckiGoodsGroupPurchaseInfo2(@RequestBody String goodsSkuId) {
        LambdaQueryWrapper<GoodsGroupPurchaseInfo> wrapper6= Wrappers.lambdaQuery();
        wrapper6.eq(GoodsGroupPurchaseInfo::getDelFlag,0);
        wrapper6.eq(GoodsGroupPurchaseInfo::getMubre,goodsSkuId);
        GoodsGroupPurchaseInfo one1 = goodsGroupPurchaseInfoServiceImpl.getOne(wrapper6);
 
        return R.ok(one1);
    }
 
    @InnerAuth
    @PostMapping("/updgoodsGroupPurchaseInfo1")
    public R<GoodsGroupPurchaseInfo> updgoodsGroupPurchaseInfo1(@RequestBody GoodsGroupPurchaseInfo goodsGroupPurchaseInfo) {
        LambdaQueryWrapper<GoodsGroupPurchaseInfo> wrapper6= Wrappers.lambdaQuery();
        wrapper6.eq(GoodsGroupPurchaseInfo::getDelFlag,0);
        wrapper6.eq(GoodsGroupPurchaseInfo::getMubre,goodsGroupPurchaseInfo.getMubre());
        GoodsGroupPurchaseInfo one1 = goodsGroupPurchaseInfoServiceImpl.getOne(wrapper6);
       int i=one1.getCurrentNumber()-1;
       if (i==0){
           GoodsGroupPurchase byId = goodsGroupPurchaseService.getById(one1.getGroupPurchaseId());
           int i1 = byId.getLimitNumber() * byId.getGroupSize();
 
           GoodsSku goodsSku = goodsSkuMapper.selectById(byId.getGoodsSkuId());
           int i2 = i1 + goodsSku.getStock();
           goodsSku.setStock(i2);
           goodsSkuMapper.updateById(goodsSku);
           goodsGroupPurchaseInfoServiceImpl.removeById(one1);
       }else{
           one1.setCurrentNumber(i);
           if (one1.getGroupSize()>one1.getCurrentNumber()){
               one1.setGroupStatus(0);
           }
           goodsGroupPurchaseInfoServiceImpl.updateById(one1);
       }
 
        return R.ok(one1);
    }
 
    @InnerAuth
    @PostMapping("/updgoodsGroupPurchaseInfo")
    public R getGoodsSeckiGoodsGroupPurchaseInfo(@RequestBody GoodsGroupPurchaseInfo goodsGroupPurchaseInfo) {
             if (goodsGroupPurchaseInfo.getId()!=null){
                 GoodsGroupPurchaseInfo byId = goodsGroupPurchaseInfoServiceImpl.getById(goodsGroupPurchaseInfo.getId());
                 Integer num= byId.getCurrentNumber()+1;
                 byId.setCurrentNumber(num);
                 if (num==byId.getGroupSize()){
                     byId.setGroupStatus(1);
                     orderClient.updateOrderStatusByMubres1(byId.getMubre(), SecurityConstants.INNER);
                 }
                 goodsGroupPurchaseInfoServiceImpl.updateById(byId);
             }else{
                 if (goodsGroupPurchaseInfo.getCurrentNumber()==goodsGroupPurchaseInfo.getGroupSize()){
                     goodsGroupPurchaseInfo.setGroupStatus(1);
                     orderClient.updateOrderStatusByMubres1(goodsGroupPurchaseInfo.getMubre(), SecurityConstants.INNER);
                 }
                 goodsGroupPurchaseInfoServiceImpl.save(goodsGroupPurchaseInfo);
             }
 
        return R.ok();
    }
 
 
    /**
     * 团购商品开始团购
     *
     * @param groupPurchaseId 团购商品id
     */
    @InnerAuth
    @GetMapping("/start/{groupPurchaseId}")
    R<?> startGroupPurchase(@PathVariable("groupPurchaseId") Long groupPurchaseId) {
        try {
            goodsGroupPurchaseService.startGroupPurchase(groupPurchaseId);
        } catch (JsonProcessingException e) {
            log.error("团购商品开始团购失败", e);
            return R.fail("团购商品开始团购失败");
        }
        return R.ok();
    }
 
    @InnerAuth
    @GetMapping("/num/{groupPurchaseId}")
    R<?> GroupPurchaseNum(@PathVariable("groupPurchaseId") Long groupPurchaseId) {
        try {
            GoodsGroupPurchase byId = goodsGroupPurchaseService.getById(groupPurchaseId);
           Integer m= byId.getCurrentNumber()+1;
            byId.setCurrentNumber(m);
            goodsGroupPurchaseService.updateById(byId);
        } catch (Exception e) {
            log.error("团购商品开始团购失败", e);
            return R.fail("团购商品开始团购失败");
        }
        return R.ok();
    }
 
    @InnerAuth
    @GetMapping("/num1/{groupPurchaseId}")
    R<?> GroupPurchaseNum1(@PathVariable("groupPurchaseId") Long groupPurchaseId) {
        try {
            GoodsGroupPurchase byId = goodsGroupPurchaseService.getById(groupPurchaseId);
            Integer m= byId.getCurrentNumber()-1;
            byId.setCurrentNumber(m);
            goodsGroupPurchaseService.updateById(byId);
        } catch (Exception e) {
            log.error("团购商品开始团购失败", e);
            return R.fail("团购商品开始团购失败");
        }
        return R.ok();
    }
 
 
    /**
     * 团购商品结束团购
     *
     * @param groupPurchaseId 团购商品id
     */
    @InnerAuth
    @GetMapping("/end/{groupPurchaseId}")
    R<?> endGroupPurchase(@PathVariable("groupPurchaseId") Long groupPurchaseId) {
        try {
            goodsGroupPurchaseService.endGroupPurchase(groupPurchaseId);
        } catch (JsonProcessingException e) {
            log.error("团购商品开始团购失败", e);
            return R.fail("团购商品结束团购失败");
        }
        return R.ok();
    }
}