Pu Zhibing
2025-02-27 f0a9a41697a8568e8b3bd3436c450e68b3298916
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
package com.panzhihua.service_community.service.impl;
 
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.panzhihua.common.model.vos.LoginUserInfoVO;
import com.panzhihua.common.model.vos.R;
import com.panzhihua.common.model.vos.community.VolunteerCreditsExchangeVO;
import com.panzhihua.common.model.vos.community.VolunteerIntegralMerchantVO;
import com.panzhihua.common.model.vos.community.VolunteerIntegralRecordVO;
import com.panzhihua.common.model.vos.community.VolunteerMerchantVO;
import com.panzhihua.common.service.user.UserService;
import com.panzhihua.common.utlis.Snowflake;
import com.panzhihua.common.utlis.StringUtils;
import com.panzhihua.service_community.dao.VolunteerCreditsExchangeDao;
import com.panzhihua.service_community.entity.VolunteerCreditsExchange;
import com.panzhihua.service_community.entity.VolunteerIntegralMerchant;
import com.panzhihua.service_community.entity.VolunteerMerchant;
import com.panzhihua.service_community.service.VolunteerCreditsExchangeService;
import com.panzhihua.service_community.service.VolunteerIntegralMerchantService;
import com.panzhihua.service_community.service.VolunteerIntegralRecordService;
import com.panzhihua.service_community.service.VolunteerMerchantService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
 
import javax.annotation.Resource;
 
 
@Slf4j
@Service
public class VolunteerCreditsExchangeServiceImpl extends ServiceImpl<VolunteerCreditsExchangeDao,
        VolunteerCreditsExchange> implements VolunteerCreditsExchangeService {
 
    @Resource
    private VolunteerIntegralMerchantService goodsService;
 
    @Resource
    private UserService userService;
 
    @Resource
    private VolunteerIntegralRecordService virService;
 
    @Resource
    private VolunteerMerchantService vmService;
 
 
    @Override
    public R getList(int pageNum, int pageSize, String goodsId, String orderNumber,
                     String goodsName, String condition, String userName,String communityId,
                     String userId,String orderType,String merchantId) {
 
        Page page=new Page<VolunteerCreditsExchange>(pageNum,pageSize);
        return R.ok(baseMapper.getList(page,goodsId,orderNumber,goodsName,condition,
                userName,communityId,userId,orderType,merchantId));
    }
 
    @Override
    public R getData(String id) {
        return R.ok(baseMapper.getData(id));
    }
 
    @Override
    public R insertData(VolunteerCreditsExchangeVO item)
    {
        if (item==null)
        {
            return R.fail("参数不能为空");
        }
 
        if(StringUtils.isEmpty(item.getGoodsId()))
        {
            return R.fail("商品id不能为空");
        }
 
        VolunteerIntegralMerchant goods=goodsService.queryById(item.getGoodsId());
 
        LoginUserInfoVO loginUserInfoVOR=userService.getUserInfoByUserId(item.getUserId()).getData();
 
        if(goods==null)
        {
            return R.fail("商品不存在");
        }
 
        int integral=0;
        if(!StringUtils.isEmpty(loginUserInfoVOR.getLoveIntegral()))
        {
            integral=Integer.valueOf(loginUserInfoVOR.getLoveIntegral());
        }
 
        if(integral<Integer.valueOf(goods.getIntegral()))
        {
            return R.fail("剩余积分不足!无法下单兑换");
        }
 
        item.setNeedScore(goods.getIntegral());
 
        if(StringUtils.isEmpty(item.getUserId()))
        {
            return R.fail("兑换用户id不能为空");
        }
 
//        if(StringUtils.isEmpty(goods.getGoodNum()))
//        {
//            return R.fail("商品库存不足");
//        }
//        else
//        {
//            int goodNum=Integer.valueOf(goods.getGoodNum());
//            if(goodNum<=0)
//            {
//                return R.fail("商品库存不足");
//            }
//
//            goodNum--;
//
//            VolunteerIntegralMerchantVO vo=new VolunteerIntegralMerchantVO();
//            vo.setId(goods.getId());
//            vo.setGoodNum(goodNum+"");
//            goodsService.updateById(vo);
//        }
 
        int num1=integral-Integer.valueOf(goods.getIntegral());
        loginUserInfoVOR.setLoveIntegral(num1+"");
        userService.putUser(loginUserInfoVOR);
 
        item.setPalyIntegral(goods.getIntegral());
 
 
        //增加积分明细记录
        VolunteerIntegralRecordVO virvo=new VolunteerIntegralRecordVO();
        virvo.setUserId(loginUserInfoVOR.getUserId()+"");
        virvo.setIType("2");
        virvo.setIntegral(item.getPalyIntegral());
        virvo.setCommunityId(item.getCommunityId());
        virService.insertVolunteer(virvo);
 
 
        //设置订单号
        item.setOrderNumber(Snowflake.getId()+"");
        int num= baseMapper.insertData(item);
        if(num>0)
        {
            return R.ok();
        }
        return R.fail("操作失败");
    }
 
    @Override
    public R insertMerchantData(VolunteerCreditsExchangeVO item)
    {
        if (item==null)
        {
            return R.fail("参数不能为空");
        }
 
        if(StringUtils.isEmpty(item.getMerchantId()))
        {
            return R.fail("商家id不能为空");
        }
 
        LoginUserInfoVO loginUserInfoVOR=userService.getUserInfoByUserId(item.getUserId()).getData();
 
        if(StringUtils.isEmpty(item.getPalyIntegral()))
        {
            return R.fail("支付积分不能为空");
        }
 
        int integral=0;
        if(!StringUtils.isEmpty(loginUserInfoVOR.getLoveIntegral()))
        {
            integral=Integer.valueOf(loginUserInfoVOR.getLoveIntegral());
        }
 
        if(integral<Integer.valueOf(item.getPalyIntegral()))
        {
            return R.fail("剩余积分不足!无法支付");
        }
 
        if(StringUtils.isEmpty(item.getUserId()))
        {
            return R.fail("兑换用户id不能为空");
        }
 
        //增加积分明细记录
        VolunteerIntegralRecordVO virvo=new VolunteerIntegralRecordVO();
        virvo.setUserId(loginUserInfoVOR.getUserId()+"");
        virvo.setIType("3");
        virvo.setIntegral(item.getPalyIntegral());
        virvo.setCommunityId(item.getCommunityId());
        virService.insertVolunteer(virvo);
 
        //修改个人积分
        int num1=integral-Integer.valueOf(item.getPalyIntegral());
        loginUserInfoVOR.setLoveIntegral(num1+"");
        userService.putUser(loginUserInfoVOR);
 
 
        //修改商家积分
        R<VolunteerMerchant> vm= vmService.getById(item.getMerchantId());
 
        int vmNum=0;
        if(!StringUtils.isEmpty(vm.getData().getMerchantIntegral()))
        {
            vmNum=Integer.valueOf(vm.getData().getMerchantIntegral());
        }
 
        vmNum+=Integer.valueOf(item.getPalyIntegral());
 
        VolunteerMerchantVO vmvo=new VolunteerMerchantVO();
        vmvo.setId(item.getMerchantId());
        vmvo.setMerchantIntegral(vmNum+"");
        vmService.updateVolunteerMerchant(vmvo);
 
        item.setCondition("3");
        //设置订单号
        String orderNum=Snowflake.getId()+"";
        item.setOrderNumber(orderNum);
        int num= baseMapper.insertData(item);
        if(num>0)
        {
            return R.ok(orderNum);
        }
        return R.fail("操作失败");
    }
 
    @Override
    public R update(VolunteerCreditsExchangeVO item)
    {
        if (item==null)
        {
            return R.fail("参数不能为空");
        }
 
        int num= baseMapper.update(item);
        if(num>0)
        {
            return R.ok();
        }
        return R.fail("操作失败");
    }
 
    @Override
    public R delete(String id) {
        if(StringUtils.isEmpty(id))
        {
            return R.fail("删除订单id不能为空");
        }
 
 
        int num= baseMapper.delete(id);
        if(num>0)
        {
            return R.ok();
        }
        return R.fail("操作失败");
    }
 
    @Override
    public R conditionData(String id)
    {
        if(StringUtils.isEmpty(id))
        {
            return R.fail("核销订单id不能为空");
        }
 
        int num= baseMapper.conditionData(id);
        if(num>0)
        {
            return R.ok();
        }
        return R.fail("操作失败");
    }
 
    /**
     * 社区订单取消
     * @param id  订单iD
     * @return
     */
    @Override
    public R communityCancellation(String id)
    {
        if(StringUtils.isEmpty(id))
        {
            return R.fail("订单id不能为空");
        }
        //查询订单
        VolunteerCreditsExchange vce=baseMapper.getData(id);
        if(StringUtils.equals("0",vce.getCondition()))
        {
            //修改订单状态为取消
            VolunteerCreditsExchangeVO volunteerCreditsExchangeVO=new VolunteerCreditsExchangeVO();
            volunteerCreditsExchangeVO.setId(id);
            volunteerCreditsExchangeVO.setCondition("2");
            baseMapper.update(volunteerCreditsExchangeVO);
 
            //查询用户详情
            LoginUserInfoVO loginUserInfoVOR=userService.getUserInfoByUserId(vce.getUserId()).getData();
            int integral=0;
            if(!StringUtils.isEmpty(loginUserInfoVOR.getLoveIntegral()))
            {
                integral=Integer.valueOf(loginUserInfoVOR.getLoveIntegral());
            }
            integral=integral+Integer.valueOf(vce.getPalyIntegral());
            loginUserInfoVOR.setLoveIntegral(integral+"");
            userService.putUser(loginUserInfoVOR);
 
            //更新商品库存
            VolunteerIntegralMerchant goods=goodsService.queryById(vce.getGoodsId());
            VolunteerIntegralMerchantVO vimvo=new VolunteerIntegralMerchantVO();
            int goodNum=Integer.valueOf(goods.getGoodNum());
            goodNum++;
            vimvo.setId(goods.getId());
            vimvo.setGoodNum(goodNum+"");
            goodsService.updateById(vimvo);
 
            //增加积分明细记录
            VolunteerIntegralRecordVO virvo=new VolunteerIntegralRecordVO();
            virvo.setUserId(loginUserInfoVOR.getUserId()+"");
            virvo.setIType("4");
            virvo.setIntegral(vce.getPalyIntegral());
            virvo.setCommunityId(vce.getCommunityId());
            virService.insertVolunteer(virvo);
        }
        return R.fail("订单不是未核销订单");
    }
 
 
}