gongzuming
2024-09-10 45536ef89d0593be1e96e014f841935240efebea
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
package com.mzl.flower.service.point;
 
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mzl.flower.config.exception.ValidationException;
import com.mzl.flower.config.security.SecurityUtils;
import com.mzl.flower.constant.Constants;
import com.mzl.flower.dto.request.point.*;
import com.mzl.flower.dto.response.point.PointGoodsDTO;
import com.mzl.flower.dto.response.point.PointGoodsListDTO;
import com.mzl.flower.dto.response.point.PointGoodsRecordDTO;
import com.mzl.flower.entity.point.CustomerPoint;
import com.mzl.flower.entity.point.CustomerPointDetail;
import com.mzl.flower.entity.point.PointGoods;
import com.mzl.flower.entity.point.PointGoodsRecord;
import com.mzl.flower.mapper.point.CustomerPointDetailMapper;
import com.mzl.flower.mapper.point.CustomerPointMapper;
import com.mzl.flower.mapper.point.PointGoodsMapper;
import com.mzl.flower.mapper.point.PointGoodsRecordMapper;
import com.mzl.flower.service.BaseService;
import com.mzl.flower.service.payment.RedisLockService;
import com.mzl.flower.utils.UUIDGenerator;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
 
import java.time.LocalDate;
import java.util.List;
 
@Service
@Transactional
public class PointGoodsService extends BaseService {
 
    @Autowired
    private PointGoodsMapper pointGoodsMapper;
 
    @Autowired
    private PointGoodsRecordMapper pointGoodsRecordMapper;
 
    @Autowired
    private CustomerPointMapper customerPointMapper;
 
    @Autowired
    private CustomerPointDetailMapper customerPointDetailMapper;
 
 
    @Autowired
    private RedisLockService lockService;
 
    public Long addPointGoods(PointGoodsCreateDTO dto){
        PointGoods p = new PointGoods();
        BeanUtils.copyProperties(dto, p);
        p.setPictures(toJSONString(dto.getPictureList()));
        p.setStatus(Constants.POINT_GOODS_STATUS.I.name());
 
        p.create(SecurityUtils.getUserId());
        pointGoodsMapper.insert(p);
 
        return p.getId();
    }
 
    public Long updatePointGoods(PointGoodsUpdateDTO dto){
        PointGoods p = pointGoodsMapper.selectById(dto.getId());
        if(p == null){
            throw new ValidationException("商品未找到");
        }
 
        BeanUtils.copyProperties(dto, p);
        p.setPictures(toJSONString(dto.getPictureList()));
 
        p.update(SecurityUtils.getUserId());
        pointGoodsMapper.updateById(p);
 
        return p.getId();
    }
 
    public void deletePointGoods(String idStr){
        List<String> ids = splitParam(idStr);
        if (ids != null && ids.size() > 0) {
            for(String idd : ids) {
                Long id = Long.parseLong(idd);
                pointGoodsMapper.deleteById(id);
            }
        }
    }
 
    public Page<PointGoodsListDTO> selectGoodsList(Page page, PointGoodsQueryDTO dto){
        List<PointGoodsListDTO> ls = pointGoodsMapper.selectGoodsList(page, dto);
 
        page.setRecords(ls);
        return page;
    }
 
    public PointGoodsDTO getGoodsInfo(Long id){
        PointGoods p = pointGoodsMapper.selectById(id);
        if(p == null){
            throw new ValidationException("商品未找到");
        }
        PointGoodsDTO dto = new PointGoodsDTO();
        BeanUtils.copyProperties(p, dto);
        dto.setPictureList(parseArray(p.getPictures(), String.class));
 
        return dto;
    }
 
    public void updateStatus(String idStr, String status){
        List<String> ids = splitParam(idStr);
        if(ids != null && ids.size() > 0) {
            for(String idd : ids) {
                Long id = Long.parseLong(idd);
                PointGoods p = pointGoodsMapper.selectById(id);
                if (p == null) {
                    continue;
                }
 
                p.setStatus(status);
                p.update(SecurityUtils.getUserId());
                pointGoodsMapper.updateById(p);
            }
        }
    }
 
    public void exchangeGoods(ExchangeGoodsDTO dto) {
        String key="EXCHANGE_GOODS:"+dto.getGoodsId()+":"+SecurityUtils.getUserId();
        boolean lock = lockService.getObjectLock(key, "");
        if(!lock){
            throw new ValidationException("系统操作频繁,请稍后重试");
        }
        try {
            PointGoods p = pointGoodsMapper.selectById(dto.getGoodsId());
            if(p == null){
                throw new ValidationException("商品未找到");
            }
            if(!Constants.POINT_GOODS_STATUS.A.name().equals(p.getStatus())){
                throw new ValidationException("商品未上架");
            }
            if(p.getStock()< dto.getNum()){
                throw new ValidationException("商品库存不足");
            }
            CustomerPoint cp = customerPointMapper.selectOne(new LambdaQueryWrapper<CustomerPoint>()
                    .eq(CustomerPoint::getUserId, SecurityUtils.getUserId()));
            if(cp == null || (cp.getTotalPoint()-cp.getUsedPoint()-cp.getExpiredPoint()-cp.getDeductionPoint()) < p.getPoint() * dto.getNum()){
                throw new ValidationException("积分不足");
            }
            //记录兑换记录
            PointGoodsRecord record = new PointGoodsRecord();
            record.setUserId(SecurityUtils.getUserId());
            record.setCustomerId(getCustomerByUserId(SecurityUtils.getUserId()).getId());
            record.setGoodsId(dto.getGoodsId());
            record.setNum(dto.getNum());
            record.setPoint(p.getPoint());
            record.setName(p.getName());
            record.setPictures(p.getPictures());
            record.setDescription(p.getDescription());
            record.setTotalPoint(p.getPoint() * dto.getNum());
            record.setCover(p.getCover());
            record.setRedeemCode(UUIDGenerator.getUUID());
            record.setStatus(Constants.POINT_GOODS_RECORD_STATUS.A.name());//未使用
            pointGoodsRecordMapper.insert(record);
 
            //更新积分汇总
            cp.setUsedPoint(cp.getUsedPoint()+record.getTotalPoint());
            customerPointMapper.updateById(cp);
 
            //记录积分明细
            CustomerPointDetail detail = new CustomerPointDetail();
            detail.setUserId(SecurityUtils.getUserId());
            detail.setCustomerId(record.getCustomerId());
            detail.setPoint(record.getTotalPoint());
            detail.setChangeType(Constants.POINT_CHANGE_TYPE.reduce.name());
            detail.setType(Constants.POINT_TYPE.exchange.name());
            detail.setRecordDate(LocalDate.now());
            detail.setRemarks(record.getName());
            detail.create(SecurityUtils.getUserId());
            customerPointDetailMapper.insert(detail);
 
            //更新库存
            p.setStock(p.getStock()- dto.getNum());
            pointGoodsMapper.updateById(p);
        }catch (Exception e){
            throw new ValidationException("兑换失败");
        }finally {
            lockService.releaseObjectLock(key,"");
        }
 
    }
 
    public Page<PointGoodsRecordDTO> myExchangeGoods(QueryExchangeGoodsDTO dto, Page page) {
        dto.setUserId(SecurityUtils.getUserId());
        List<PointGoodsRecordDTO> list = pointGoodsRecordMapper.selectMyExchangeGoods(dto,page);
        page.setRecords(list);
        return page;
    }
 
    public void useExchangeGoods(Long recordId,String orderId) {
        PointGoodsRecord record = pointGoodsRecordMapper.selectById(recordId);
        if(record == null){
            throw new ValidationException("兑换券不存在");
        }
        if(!Constants.POINT_GOODS_RECORD_STATUS.A.name().equals(record.getStatus())){
            throw new ValidationException("兑换券已使用或过期");
        }
        if(!SecurityUtils.getUserId().equals(record.getUserId())){
            throw new ValidationException("兑换券不属于当前用户");
        }
        record.setStatus(Constants.POINT_GOODS_RECORD_STATUS.U.name());
        record.setOrderId(orderId);
        pointGoodsRecordMapper.updateById(record);
    }
    public void revertExchangeGoods(Long recordId) {
        PointGoodsRecord record = pointGoodsRecordMapper.selectById(recordId);
        if(record == null){
            throw new ValidationException("兑换券不存在");
        }
        if(!Constants.POINT_GOODS_RECORD_STATUS.U.name().equals(record.getStatus())){
            throw new ValidationException("兑换券未使用或过期");
        }
        if(!SecurityUtils.getUserId().equals(record.getUserId())){
            throw new ValidationException("兑换券不属于当前用户");
        }
        record.setStatus(Constants.POINT_GOODS_RECORD_STATUS.A.name());
        record.setOrderId(null);
        pointGoodsRecordMapper.updateById(record);
    }
}