|
|
@@ -2,25 +2,31 @@ package com.bjtdba.userCenter.service.impl;
|
|
2
|
2
|
|
|
3
|
3
|
import com.bjtdba.entity.order.User;
|
|
4
|
4
|
import com.bjtdba.entity.userCenter.UserBill;
|
|
5
|
|
-import com.bjtdba.userCenter.dao.MerchantDao;
|
|
6
|
|
-import com.bjtdba.userCenter.dao.UserBillDao;
|
|
7
|
|
-import com.bjtdba.userCenter.dao.UserDao;
|
|
8
|
|
-import com.bjtdba.userCenter.dao.UserInfoDao;
|
|
|
5
|
+import com.bjtdba.userCenter.dao.*;
|
|
9
|
6
|
import com.bjtdba.userCenter.dto.request.GetUserListRequest;
|
|
|
7
|
+import com.bjtdba.userCenter.dto.request.UserScoreModifyRequest;
|
|
|
8
|
+import com.bjtdba.userCenter.dto.response.UserScoreModifyResponse;
|
|
10
|
9
|
import com.bjtdba.userCenter.dto.response.common.PageResult;
|
|
11
|
10
|
import com.bjtdba.userCenter.dto.response.UserDTO;
|
|
12
|
11
|
import com.bjtdba.userCenter.entity.UserEntity;
|
|
|
12
|
+import com.bjtdba.userCenter.entity.UserSignScoreEntity;
|
|
|
13
|
+import com.bjtdba.userCenter.exception.BusinessException;
|
|
13
|
14
|
import com.bjtdba.userCenter.service.UserService;
|
|
|
15
|
+import lombok.RequiredArgsConstructor;
|
|
14
|
16
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
15
|
17
|
import org.springframework.stereotype.Service;
|
|
|
18
|
+import org.springframework.transaction.annotation.Transactional;
|
|
16
|
19
|
import org.springframework.util.CollectionUtils;
|
|
17
|
20
|
|
|
18
|
21
|
import java.math.BigDecimal;
|
|
|
22
|
+import java.time.LocalDateTime;
|
|
|
23
|
+import java.time.ZoneOffset;
|
|
19
|
24
|
import java.util.Collections;
|
|
20
|
25
|
import java.util.List;
|
|
21
|
26
|
import java.util.stream.Collectors;
|
|
22
|
27
|
|
|
23
|
28
|
@Service("userService")
|
|
|
29
|
+@RequiredArgsConstructor
|
|
24
|
30
|
public class UserServiceImpl implements UserService {
|
|
25
|
31
|
@Autowired
|
|
26
|
32
|
private UserInfoDao userInfoDao;
|
|
|
@@ -30,6 +36,8 @@ public class UserServiceImpl implements UserService {
|
|
30
|
36
|
private UserBillDao userBillDao;
|
|
31
|
37
|
@Autowired
|
|
32
|
38
|
private UserDao userDao;
|
|
|
39
|
+ @Autowired
|
|
|
40
|
+ private UserSignScoreDao userSignScoreDao;
|
|
33
|
41
|
|
|
34
|
42
|
|
|
35
|
43
|
public User queryByid(Integer uid){
|
|
|
@@ -127,4 +135,64 @@ public class UserServiceImpl implements UserService {
|
|
127
|
135
|
dto.setPv(userEntity.getPv());
|
|
128
|
136
|
return dto;
|
|
129
|
137
|
}
|
|
|
138
|
+
|
|
|
139
|
+ @Override
|
|
|
140
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
141
|
+ public UserScoreModifyResponse modifyUserScore(UserScoreModifyRequest request) {
|
|
|
142
|
+ // 1. 获取用户并加锁
|
|
|
143
|
+ UserEntity userEntity = userDao.getUserScoreByUidForUpdate(request.getUid());
|
|
|
144
|
+ if (userEntity == null) {
|
|
|
145
|
+ throw BusinessException.userNotFound();
|
|
|
146
|
+ }
|
|
|
147
|
+
|
|
|
148
|
+ // 2. 积分操作处理
|
|
|
149
|
+ BigDecimal newScore;
|
|
|
150
|
+ if (request.getPm() == 1) { // 收入
|
|
|
151
|
+ newScore = userEntity.getScore().add(request.getRewardScore());
|
|
|
152
|
+ } else { // 支出
|
|
|
153
|
+ if (userEntity.getScore().compareTo(request.getRewardScore()) < 0) {
|
|
|
154
|
+ throw BusinessException.insufficientScore();
|
|
|
155
|
+ }
|
|
|
156
|
+ newScore = userEntity.getScore().subtract(request.getRewardScore());
|
|
|
157
|
+ }
|
|
|
158
|
+
|
|
|
159
|
+ // 3. 更新用户积分(带乐观锁)
|
|
|
160
|
+ int updateCount = userDao.updateUserScore(
|
|
|
161
|
+ request.getUid(),
|
|
|
162
|
+ newScore
|
|
|
163
|
+ );
|
|
|
164
|
+ if (updateCount == 0) {
|
|
|
165
|
+ throw BusinessException.updateFailed();
|
|
|
166
|
+ }
|
|
|
167
|
+
|
|
|
168
|
+ // 4. 创建积分记录
|
|
|
169
|
+ UserSignScoreEntity record = buildScoreRecord(request, newScore);
|
|
|
170
|
+ userSignScoreDao.insertByEntity(record);
|
|
|
171
|
+
|
|
|
172
|
+ // 5. 构建响应
|
|
|
173
|
+ return UserScoreModifyResponse.builder()
|
|
|
174
|
+ .uid(request.getUid())
|
|
|
175
|
+ .currentScore(newScore)
|
|
|
176
|
+ .transactionId(record.getId().toString())
|
|
|
177
|
+ .build();
|
|
|
178
|
+ }
|
|
|
179
|
+
|
|
|
180
|
+ private UserSignScoreEntity buildScoreRecord(UserScoreModifyRequest request, BigDecimal currentScore) {
|
|
|
181
|
+ UserSignScoreEntity record = new UserSignScoreEntity();
|
|
|
182
|
+ record.setUid(request.getUid());
|
|
|
183
|
+ record.setRewardScore(request.getRewardScore());
|
|
|
184
|
+ record.setPm(request.getPm());
|
|
|
185
|
+ record.setOrderId(request.getOrderId());
|
|
|
186
|
+ record.setOrderType(request.getOrderType());
|
|
|
187
|
+ record.setSurplus(currentScore);
|
|
|
188
|
+ record.setAddtime(LocalDateTime.now().toEpochSecond(ZoneOffset.UTC));
|
|
|
189
|
+ record.setMark(buildRecordMark(request));
|
|
|
190
|
+ return record;
|
|
|
191
|
+ }
|
|
|
192
|
+
|
|
|
193
|
+ private String buildRecordMark(UserScoreModifyRequest request) {
|
|
|
194
|
+ return String.format("%s积分,订单号:%s",
|
|
|
195
|
+ request.getPm() == 1 ? "收入" : "支出",
|
|
|
196
|
+ request.getOrderId());
|
|
|
197
|
+ }
|
|
130
|
198
|
}
|