liujie
3 天以前 ce531d3bb32f5221b35f9e24d027ef83450ebbc6
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package com.ruoyi.web.controller.api;
 
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.basic.PageInfo;
import com.ruoyi.common.core.domain.BaseModel;
import com.ruoyi.common.core.domain.BasePage;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.dto.ExchangeGoodsDto;
import com.ruoyi.system.dto.TErpGoodsUpdateStatusDto;
import com.ruoyi.system.model.*;
import com.ruoyi.system.query.ExchangeGoodsRecordQuery;
import com.ruoyi.system.query.TSysGoodsExchangeQuery;
import com.ruoyi.system.query.TSysGoodsQuery;
import com.ruoyi.system.service.*;
import com.ruoyi.system.vo.ExchangeGoodsRecordDetailVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
 
import javax.validation.Valid;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 积分商品信息 前端控制器
 * </p>
 *
 * @author xiaochen
 * @since 2025-08-20
 */
@RestController
@RequestMapping("/t-sys-goods")
@Api(tags = "积分商品管理")
public class TSysGoodsController {
 
    private final TSysGoodsService sysGoodsService;
    private final TokenService tokenService;
    private final TCrmSupplierService crmSupplierService;
    private final TSysGoodsExchangeService sysGoodsExchangeService;
    private final TCrmClinicService crmClinicService;
    private final TCrmClinicPointsService crmClinicPointsService;
    private final TCrmChangePointsService crmChangePointsService;
 
 
    @Autowired
    public TSysGoodsController(TokenService tokenService, TSysGoodsService sysGoodsService, TCrmSupplierService crmSupplierService, TSysGoodsExchangeService sysGoodsExchangeService, TCrmClinicService crmClinicService, TCrmClinicPointsService crmClinicPointsService, TCrmChangePointsService crmChangePointsService) {
        this.sysGoodsService = sysGoodsService;
        this.tokenService = tokenService;
        this.crmSupplierService = crmSupplierService;
        this.sysGoodsExchangeService = sysGoodsExchangeService;
        this.crmClinicService = crmClinicService;
        this.crmClinicPointsService = crmClinicPointsService;
 
        this.crmChangePointsService = crmChangePointsService;
 
    }
 
    /**
     * 获取积分商品列表
     */
    @ApiOperation(value = "获取积分商品列表--供应商")
    @PostMapping(value = "/pageList")
    public R<PageInfo<TSysGoods>> pageList(@RequestBody TSysGoodsQuery query) {
 
        SysUser user = tokenService.getLoginUser().getUser();
        Integer roleType = user.getRoleType();
        PageInfo<TSysGoods> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
        if (roleType == 4) {
            // 供应商
            TCrmSupplier crmSupplier = crmSupplierService.getOne(Wrappers.lambdaQuery(TCrmSupplier.class)
                    .eq(TCrmSupplier::getUserId, user.getUserId())
                    .last("LIMIT 1"));
            LambdaQueryWrapper<TSysGoods> tSysGoodsLambdaQueryWrapper = new LambdaQueryWrapper<>();
            tSysGoodsLambdaQueryWrapper.eq(TSysGoods::getSupplierId, crmSupplier.getId());
            if (query.getGoodsName() != null && !query.getGoodsName().isEmpty()) {
                tSysGoodsLambdaQueryWrapper.like(TSysGoods::getGoodsName, query.getGoodsName());
            }
            if (query.getStatus() != null) {
                tSysGoodsLambdaQueryWrapper.eq(TSysGoods::getStatus, query.getStatus());
            }
            tSysGoodsLambdaQueryWrapper.orderByDesc(BaseModel::getCreateTime);
 
            Page<TSysGoods> page = sysGoodsService.page(pageInfo, tSysGoodsLambdaQueryWrapper);
            List<TSysGoods> records = page.getRecords();
            if (!records.isEmpty()) {
                List<TSysGoodsExchange> list = sysGoodsExchangeService.list(new LambdaQueryWrapper<TSysGoodsExchange>().in(TSysGoodsExchange::getGoodsId, records.stream().map(TSysGoods::getId).collect(Collectors.toList())));
                records.forEach(item -> {
                    item.setExchangeQuantity(list.stream().filter(item1 -> item1.getGoodsId().equals(item.getId())).mapToInt(TSysGoodsExchange::getGoodsCount).sum());
                    if(item.getGoodsTotal()!=null){
                        item.setSurplusQuantity(item.getGoodsTotal() - item.getExchangeQuantity());
                    }
                });
            }
            pageInfo.setRecords(records);
            return R.ok(pageInfo);
        }
        return R.ok(pageInfo);
    }
 
 
    @ApiOperation(value = "添加积分商品--供应商")
    @PostMapping(value = "/add")
    public R<String> add(@RequestBody TSysGoods goods) {
        SysUser user = tokenService.getLoginUser().getUser();
        Integer roleType = user.getRoleType();
        if (roleType == 4) {
            // 供应商
            TCrmSupplier crmSupplier = crmSupplierService.getOne(Wrappers.lambdaQuery(TCrmSupplier.class)
                    .eq(TCrmSupplier::getUserId, user.getUserId())
                    .last("LIMIT 1"));
            goods.setSupplierId(crmSupplier.getId());
            sysGoodsService.save(goods);
            return R.ok(goods.getId());
        }
        return R.ok();
    }
 
    @ApiOperation(value = "编辑积分商品--供应商")
    @PostMapping(value = "/update")
    public R<String> update(@RequestBody TSysGoods goods) {
        SysUser user = tokenService.getLoginUser().getUser();
        Integer roleType = user.getRoleType();
 
        List<TSysGoodsExchange> list = sysGoodsExchangeService.list(new LambdaQueryWrapper<TSysGoodsExchange>().eq(TSysGoodsExchange::getGoodsId, goods.getId()));
        int sum = list.stream().mapToInt(TSysGoodsExchange::getGoodsCount).sum();
        if (goods.getGoodsTotal() < sum) {
            return R.fail("商品总数不能小于已兑换数量");
        }
        if (roleType == 4) {
            // 供应商
            TCrmSupplier crmSupplier = crmSupplierService.getOne(Wrappers.lambdaQuery(TCrmSupplier.class)
                    .eq(TCrmSupplier::getUserId, user.getUserId())
                    .last("LIMIT 1"));
            goods.setSupplierId(crmSupplier.getId());
            sysGoodsService.updateById(goods);
            return R.ok(goods.getId());
        }
        return R.ok();
    }
 
 
    @ApiOperation(value = "删除积分商品--供应商")
    @DeleteMapping(value = "/delete/{id}")
    public R<String> delete(@PathVariable String id) {
        SysUser user = tokenService.getLoginUser().getUser();
        Integer roleType = user.getRoleType();
        if (roleType == 4) {
            // 供应商
            TCrmSupplier crmSupplier = crmSupplierService.getOne(Wrappers.lambdaQuery(TCrmSupplier.class)
                    .eq(TCrmSupplier::getUserId, user.getUserId())
                    .last("LIMIT 1"));
            TSysGoods goods = sysGoodsService.getById(id);
            if (goods.getSupplierId().equals(crmSupplier.getId())) {
                sysGoodsService.removeById(id);
            } else {
                return R.fail("没有权限");
            }
        }
        return R.ok();
    }
 
 
    @ApiOperation(value = "积分商品 上下架--供应商")
    @PostMapping(value = "/updateStatus")
    public R<String> updateStatus(@RequestBody @Valid TErpGoodsUpdateStatusDto dto) {
        SysUser user = tokenService.getLoginUser().getUser();
        Integer roleType = user.getRoleType();
        if (roleType == 4) {
            // 供应商
            TCrmSupplier crmSupplier = crmSupplierService.getOne(Wrappers.lambdaQuery(TCrmSupplier.class)
                    .eq(TCrmSupplier::getUserId, user.getUserId())
                    .last("LIMIT 1"));
            TSysGoods goods = sysGoodsService.getById(dto.getId());
            if (goods.getSupplierId().equals(crmSupplier.getId())) {
                goods.setStatus(dto.getState());
                sysGoodsService.updateById(goods);
            } else {
                return R.fail("没有权限");
            }
        }
        return R.ok();
    }
 
 
    // 获取兑换记录
    @ApiOperation(value = "获取兑换记录--供应商")
    @PostMapping(value = "/getExchangeRecord")
    public R<PageInfo<TSysGoodsExchange>> getExchangeRecord(@RequestBody @Valid TSysGoodsExchangeQuery query) {
        PageInfo<TSysGoodsExchange> page = new PageInfo<>(query.getPageNum(), query.getPageSize());
        LambdaQueryWrapper<TSysGoodsExchange> wrapper = new LambdaQueryWrapper<TSysGoodsExchange>();
        if(query.getId()!=null && !query.getId().isEmpty()){
            wrapper.eq(TSysGoodsExchange::getGoodsId, query.getId());
        }
        if (query.getClinicName() != null && !query.getClinicName().isEmpty()) {
            wrapper.like(TSysGoodsExchange::getClinicName, query.getClinicName());
        }
        if (query.getTime() != null && !query.getTime().isEmpty()) {
            wrapper.between(TSysGoodsExchange::getCreateTime, query.getTime().split(" - ")[0] + " 00:00:00", query.getTime().split(" - ")[1] + " 23:59:59");
        }
        if (query.getStatus() != null) {
            wrapper.eq(TSysGoodsExchange::getStatus, query.getStatus());
        }
        wrapper.orderByDesc(BaseModel::getCreateTime);
        PageInfo<TSysGoodsExchange> page1 = sysGoodsExchangeService.page(page, wrapper);
        return R.ok(page1);
    }
 
    // 获取兑换记录
    @ApiOperation(value = "发货--供应商")
    @PostMapping(value = "/sendGoods")
    public R<?> sendGoods(@RequestBody @Valid TSysGoodsExchange exchange) {
        TSysGoodsExchange exchange1 = sysGoodsExchangeService.getById(exchange.getId());
        if (exchange1.getStatus() == 2) {
            return R.fail("该订单已发货");
        }
        exchange1.setStatus(2);
        exchange1.setLogisticsNumber(exchange1.getLogisticsNumber());
        sysGoodsExchangeService.updateById(exchange1);
        return R.ok();
    }
 
 
    /**
     * 获取积分商品列表
     */
    @ApiOperation(value = "获取积分商品列表--诊所")
    @PostMapping(value = "/pageListClinic")
    public R<HashMap<String, Object>> pageListClinic(@RequestBody TSysGoodsQuery query) {
        PageInfo<TSysGoods> pageInfo = new PageInfo<>(query.getPageNum(), query.getPageSize());
        LambdaQueryWrapper<TSysGoods> tSysGoodsLambdaQueryWrapper = new LambdaQueryWrapper<>();
        tSysGoodsLambdaQueryWrapper.eq(TSysGoods::getStatus, 1);
        if (query.getGoodsName() != null && !query.getGoodsName().isEmpty()) {
            tSysGoodsLambdaQueryWrapper.like(TSysGoods::getGoodsName, query.getGoodsName());
        }
        tSysGoodsLambdaQueryWrapper.orderByDesc(BaseModel::getCreateTime);
 
        Page<TSysGoods> page = sysGoodsService.page(pageInfo, tSysGoodsLambdaQueryWrapper);
        List<TSysGoods> records = page.getRecords();
        if (!records.isEmpty()) {
            List<TSysGoodsExchange> list = sysGoodsExchangeService.list(new LambdaQueryWrapper<TSysGoodsExchange>().in(TSysGoodsExchange::getGoodsId, records.stream().map(TSysGoods::getId).collect(Collectors.toList())));
            records.forEach(item -> {
                item.setExchangeQuantity(list.stream().filter(item1 -> item1.getGoodsId().equals(item.getId())).mapToInt(TSysGoodsExchange::getGoodsCount).sum());
                item.setSurplusQuantity(item.getGoodsTotal() - item.getExchangeQuantity());
            });
        }
        pageInfo.setRecords(records);
        HashMap<String, Object> map = new HashMap<>();
        map.put("data", pageInfo);
 
        // 获取我的可用积分
        map.put("total", sysGoodsService.count(tSysGoodsLambdaQueryWrapper));
        SysUser user = tokenService.getLoginUser().getUser();
        Integer roleType = user.getRoleType();
        if (roleType == 5) {
            // 诊所
            TCrmClinic crmClinic = crmClinicService.getOne(Wrappers.lambdaQuery(TCrmClinic.class)
                    .eq(TCrmClinic::getUserId, user.getUserId())
                    .last("LIMIT 1"));
            List<TCrmClinicPoints> list = crmClinicPointsService.list(new LambdaQueryWrapper<TCrmClinicPoints>().eq(TCrmClinicPoints::getClinicId, crmClinic.getId()).ge(TCrmClinicPoints::getExpireTime, new Date()).last(" and points > use_points"));
            map.put("myPoints", list.stream().mapToInt(item -> item.getPoints() - item.getUsePoints()).sum());
        }
        return R.ok(map);
    }
 
    /**
     * 获取积分商品详情
     */
    @ApiOperation(value = "获取积分商品列表--详情")
    @GetMapping(value = "/detail/{id}")
    public R<TSysGoods> detail(@PathVariable String id) {
        TSysGoods goods = sysGoodsService.getById(id);
        return R.ok(goods);
    }
 
 
    /**
     * 兑换商品
     */
    @ApiOperation(value = "兑换商品--诊所")
    @PostMapping(value = "/exchangeGoods")
    public R<?> exchangeGoods(@RequestBody @Valid ExchangeGoodsDto dto) {
        SysUser user = tokenService.getLoginUser().getUser();
        Integer roleType = user.getRoleType();
        if (roleType == 5) {
            // 诊所
            TCrmClinic crmClinic = crmClinicService.getOne(Wrappers.lambdaQuery(TCrmClinic.class)
                    .eq(TCrmClinic::getUserId, user.getUserId())
                    .last("LIMIT 1"));
            sysGoodsService.exchangeGoods(crmClinic, dto);
            return R.ok();
        }
        return R.fail("请选择正确的角色");
    }
 
 
    /**
     * 兑换商品记录
     */
    @ApiOperation(value = "兑换商品记录--诊所")
    @PostMapping(value = "/exchangeGoodsRecord")
    public R<?> exchangeGoodsRecord(@RequestBody ExchangeGoodsRecordQuery query) {
        SysUser user = tokenService.getLoginUser().getUser();
        Integer roleType = user.getRoleType();
        if (roleType == 5) {
            // 诊所
            TCrmClinic crmClinic = crmClinicService.getOne(Wrappers.lambdaQuery(TCrmClinic.class)
                    .eq(TCrmClinic::getUserId, user.getUserId())
                    .last("LIMIT 1"));
 
            LambdaQueryWrapper<TSysGoodsExchange> eq = new LambdaQueryWrapper<TSysGoodsExchange>().eq(TSysGoodsExchange::getClinicId, crmClinic.getId());
            if (query.getName() != null && !query.getName().isEmpty()) {
                eq.like(TSysGoodsExchange::getGoodsName, query.getName());
            }
            eq.orderByDesc(BaseModel::getCreateTime);
 
 
            PageInfo<TSysGoodsExchange> page = sysGoodsExchangeService.page(new PageInfo<>(query.getPageNum(), query.getPageSize()), eq);
            return R.ok(page);
        }
        return R.fail("请选择正确的角色");
    }
 
    /**
     * 兑换商品记录
     */
    @ApiOperation(value = "兑换商品记录详情--诊所")
    @GetMapping(value = "/exchangeGoodsRecordDetail/{id}")
    public R<ExchangeGoodsRecordDetailVo> exchangeGoodsRecord(@PathVariable String id) {
        SysUser user = tokenService.getLoginUser().getUser();
        Integer roleType = user.getRoleType();
        if (roleType == 5) {
            // 诊所
            TCrmClinic crmClinic = crmClinicService.getOne(Wrappers.lambdaQuery(TCrmClinic.class)
                    .eq(TCrmClinic::getUserId, user.getUserId())
                    .last("LIMIT 1"));
            TSysGoodsExchange exchangeServiceById = sysGoodsExchangeService.getById(id);
            if (exchangeServiceById.getClinicId().equals(crmClinic.getId())) {
                ExchangeGoodsRecordDetailVo vo = new ExchangeGoodsRecordDetailVo();
                BeanUtils.copyProperties(exchangeServiceById, vo);
 
                String goodsId = vo.getGoodsId();
                TSysGoods sysGoods = sysGoodsService.getById(goodsId);
                vo.setGoodsDetail(sysGoods.getGoodsDetail());
                vo.setGoodsCover(sysGoods.getGoodsCover());
 
 
                return R.ok(vo);
            }
        }
        return R.fail("请选择正确的角色");
    }
 
 
    /**
     * 兑换商品记录
     */
    @ApiOperation(value = "积分变更记录--诊所")
    @PostMapping(value = "/pointsRecordDetail")
    public R<PageInfo<TCrmChangePoints>> pointsRecordDetail(@RequestBody BasePage page) {
        SysUser user = tokenService.getLoginUser().getUser();
        Integer roleType = user.getRoleType();
        if (roleType == 5) {
            // 诊所
            TCrmClinic crmClinic = crmClinicService.getOne(Wrappers.lambdaQuery(TCrmClinic.class)
                    .eq(TCrmClinic::getUserId, user.getUserId())
                    .last("LIMIT 1"));
 
            PageInfo<TCrmChangePoints> page1 = crmChangePointsService.page(new PageInfo<>(page.getPageNum(), page.getPageSize()), new LambdaQueryWrapper<TCrmChangePoints>().eq(TCrmChangePoints::getBranchSalespersonId, crmClinic.getId()).eq(TCrmChangePoints::getUserType, 3).orderByDesc(BaseModel::getCreateTime));
 
            return R.ok(page1);
        }
        return R.fail("请选择正确的角色");
    }
 
 
}