xuhy
4 天以前 433bc1e314ecfd483379ca0e2e74e7c815d3261f
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
package com.ruoyi.system.service.impl;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.system.dto.ExchangeGoodsDto;
import com.ruoyi.system.mapper.TCrmChangePointsMapper;
import com.ruoyi.system.mapper.TCrmClinicPointsMapper;
import com.ruoyi.system.mapper.TSysGoodsExchangeMapper;
import com.ruoyi.system.mapper.TSysGoodsMapper;
import com.ruoyi.system.model.*;
import com.ruoyi.system.service.TSysGoodsService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
 
/**
 * <p>
 * 积分商品信息 服务实现类
 * </p>
 *
 * @author xiaochen
 * @since 2025-08-20
 */
@Service
public class TSysGoodsServiceImpl extends ServiceImpl<TSysGoodsMapper, TSysGoods> implements TSysGoodsService {
 
    @Resource
    private TSysGoodsExchangeMapper sysGoodsExchangeMapper;
 
    @Resource
    private TCrmClinicPointsMapper crmClinicPointsMapper;
 
    @Resource
    private TCrmChangePointsMapper crmChangePointsMapper;
 
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void exchangeGoods(TCrmClinic crmClinic, ExchangeGoodsDto dto) {
        // 盘点库存是否足够  判断积分是否足够  减积分  记录积分变更 添加购买记录
        TSysGoods tSysGoods = this.getById(dto.getGoodsId());
        int needPoints = tSysGoods.getRequiredPoints().intValue() * dto.getCount();
 
        // 判断已经购买的了多少
        List<TSysGoodsExchange> list = sysGoodsExchangeMapper.selectList(new LambdaQueryWrapper<TSysGoodsExchange>().eq(TSysGoodsExchange::getGoodsId, dto.getGoodsId()));
        int sum = list.stream().mapToInt(TSysGoodsExchange::getGoodsCount).sum();
        if(sum+dto.getCount()>tSysGoods.getGoodsTotal()){
            throw new RuntimeException("商品库存不足");
        }
        if(sum+dto.getCount()>tSysGoods.getConvertibleQuantity()){
            throw new RuntimeException("可兑换数量不足");
        }
        List<TCrmClinicPoints> points = crmClinicPointsMapper.selectList(new LambdaQueryWrapper<TCrmClinicPoints>().eq(TCrmClinicPoints::getClinicId, crmClinic.getId()).ge(TCrmClinicPoints::getExpireTime, new Date()).last(" and points > use_points ORDER BY create_time ASC"));
        // 可用积分
        int userPoints = points.stream().mapToInt(item -> item.getPoints() - item.getUsePoints()).sum();
        if(userPoints<needPoints){
            throw new RuntimeException("积分不足");
        }
 
        for (TCrmClinicPoints point : points) {
            if(needPoints==0){
                break;
            }
            if(point.getPoints()-point.getUsePoints()>=needPoints){
                point.setUsePoints(point.getUsePoints()+needPoints);
                crmClinicPointsMapper.updateById(point);
                needPoints = 0;
            }else {
                point.setUsePoints(point.getPoints());
                crmClinicPointsMapper.updateById(point);
                needPoints= needPoints - (point.getPoints() - point.getUsePoints());
            }
        }
 
        // 添加兑换记录
        TSysGoodsExchange exchange = new TSysGoodsExchange();
        exchange.setClinicName(crmClinic.getClinicName());
        exchange.setGoodsName(tSysGoods.getGoodsName());
        exchange.setGoodsId(tSysGoods.getId());
        exchange.setGoodsCount(dto.getCount());
        exchange.setUsePoints(tSysGoods.getRequiredPoints().intValue() * dto.getCount());
        exchange.setExchangeTime(LocalDateTime.now());
        exchange.setClinicId(crmClinic.getId());
        sysGoodsExchangeMapper.insert(exchange);
 
        // 记录
        TCrmChangePoints tCrmChangePoints = new TCrmChangePoints();
        tCrmChangePoints.setBranchSalespersonId(crmClinic.getId());
        tCrmChangePoints.setExchangeId(exchange.getId());
        tCrmChangePoints.setUserType(3);
        tCrmChangePoints.setChangeType(2);
        tCrmChangePoints.setChangeReason(2);
        tCrmChangePoints.setChangeValue(exchange.getUsePoints());
        crmChangePointsMapper.insert(tCrmChangePoints);
 
    }
}