|
|
@@ -3,23 +3,26 @@ package com.bjtdba.userCenter.service.impl;
|
|
3
|
3
|
import com.bjtdba.entity.order.User;
|
|
4
|
4
|
import com.bjtdba.entity.userCenter.UserBill;
|
|
5
|
5
|
import com.bjtdba.userCenter.dao.*;
|
|
6
|
|
-import com.bjtdba.userCenter.dto.request.GetUserListRequest;
|
|
7
|
|
-import com.bjtdba.userCenter.dto.request.UpdateUserInfoRequest;
|
|
8
|
|
-import com.bjtdba.userCenter.dto.request.UserScoreModifyRequest;
|
|
|
6
|
+import com.bjtdba.userCenter.dto.request.*;
|
|
|
7
|
+import com.bjtdba.userCenter.dto.response.UserContributeModifyResponse;
|
|
9
|
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.UserSignGongxianEntity;
|
|
13
|
13
|
import com.bjtdba.userCenter.entity.UserSignScoreEntity;
|
|
14
|
14
|
import com.bjtdba.userCenter.exception.BusinessException;
|
|
15
|
15
|
import com.bjtdba.userCenter.service.UserService;
|
|
|
16
|
+import com.bjtdba.userCenter.util.PasswordUtil;
|
|
16
|
17
|
import lombok.RequiredArgsConstructor;
|
|
17
|
18
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
18
|
19
|
import org.springframework.stereotype.Service;
|
|
19
|
20
|
import org.springframework.transaction.annotation.Transactional;
|
|
20
|
21
|
import org.springframework.util.CollectionUtils;
|
|
21
|
22
|
|
|
|
23
|
+import javax.servlet.http.HttpServletRequest;
|
|
22
|
24
|
import java.math.BigDecimal;
|
|
|
25
|
+import java.sql.Timestamp;
|
|
23
|
26
|
import java.time.LocalDateTime;
|
|
24
|
27
|
import java.time.ZoneOffset;
|
|
25
|
28
|
import java.util.Collections;
|
|
|
@@ -39,6 +42,10 @@ public class UserServiceImpl implements UserService {
|
|
39
|
42
|
private UserDao userDao;
|
|
40
|
43
|
@Autowired
|
|
41
|
44
|
private UserSignScoreDao userSignScoreDao;
|
|
|
45
|
+ @Autowired
|
|
|
46
|
+ private UserSignGongxianDao userSignGongxianDao;
|
|
|
47
|
+ @Autowired
|
|
|
48
|
+ private HttpServletRequest httpServletRequest;
|
|
42
|
49
|
|
|
43
|
50
|
|
|
44
|
51
|
public User queryByid(Integer uid){
|
|
|
@@ -108,16 +115,17 @@ public class UserServiceImpl implements UserService {
|
|
108
|
115
|
return Collections.emptyList();
|
|
109
|
116
|
}
|
|
110
|
117
|
return userEntityList.stream()
|
|
111
|
|
- .map(this::convertToBasicDTO)
|
|
|
118
|
+ .map(this::convertToUserDTO)
|
|
112
|
119
|
.collect(Collectors.toList());
|
|
113
|
120
|
}
|
|
114
|
121
|
|
|
115
|
|
- private UserDTO convertToBasicDTO(UserEntity userEntity) {
|
|
|
122
|
+ private UserDTO convertToUserDTO(UserEntity userEntity) {
|
|
116
|
123
|
UserDTO dto = new UserDTO();
|
|
117
|
124
|
dto.setUid(userEntity.getUid());
|
|
118
|
125
|
dto.setAccount(userEntity.getAccount());
|
|
119
|
126
|
dto.setRealName(userEntity.getRealName());
|
|
120
|
127
|
dto.setNickname(userEntity.getNickname());
|
|
|
128
|
+ dto.setSex(userEntity.getSex());
|
|
121
|
129
|
dto.setSpreadUid(userEntity.getSpreadUid());
|
|
122
|
130
|
dto.setAvatar(userEntity.getAvatar());
|
|
123
|
131
|
dto.setPhone(userEntity.getPhone());
|
|
|
@@ -163,7 +171,7 @@ public class UserServiceImpl implements UserService {
|
|
163
|
171
|
newScore
|
|
164
|
172
|
);
|
|
165
|
173
|
if (updateCount == 0) {
|
|
166
|
|
- throw BusinessException.updateFailed();
|
|
|
174
|
+ throw BusinessException.updateScoreFailed();
|
|
167
|
175
|
}
|
|
168
|
176
|
|
|
169
|
177
|
// 4. 创建积分记录
|
|
|
@@ -178,6 +186,7 @@ public class UserServiceImpl implements UserService {
|
|
178
|
186
|
.build();
|
|
179
|
187
|
}
|
|
180
|
188
|
|
|
|
189
|
+
|
|
181
|
190
|
private UserSignScoreEntity buildScoreRecord(UserScoreModifyRequest request, BigDecimal currentScore) {
|
|
182
|
191
|
UserSignScoreEntity record = new UserSignScoreEntity();
|
|
183
|
192
|
record.setUid(request.getUid());
|
|
|
@@ -200,16 +209,155 @@ public class UserServiceImpl implements UserService {
|
|
200
|
209
|
|
|
201
|
210
|
@Override
|
|
202
|
211
|
@Transactional(rollbackFor = Exception.class)
|
|
203
|
|
- public UserDTO updateUserInfo(UpdateUserInfoRequest request) {
|
|
|
212
|
+ public UserContributeModifyResponse modifyUserContribute(UserContributeModifyRequest request) {
|
|
204
|
213
|
// 1. 获取用户并加锁
|
|
205
|
214
|
UserEntity userEntity = userDao.getUserInfoByUidForUpdate(request.getUid());
|
|
206
|
215
|
if (userEntity == null) {
|
|
207
|
216
|
throw BusinessException.userNotFound();
|
|
208
|
217
|
}
|
|
209
|
218
|
|
|
210
|
|
- // 2. 判断积分值是否有变动
|
|
|
219
|
+ // 2. 贡献值操作处理
|
|
|
220
|
+ BigDecimal newContribute;
|
|
|
221
|
+ if (request.getType() == 1) { // 赠送
|
|
|
222
|
+ newContribute = userEntity.getContribute().add(request.getNumber());
|
|
|
223
|
+ } else { // 消耗
|
|
|
224
|
+ if (userEntity.getContribute().compareTo(request.getNumber()) < 0) {
|
|
|
225
|
+ throw BusinessException.insufficientContribute();
|
|
|
226
|
+ }
|
|
|
227
|
+ newContribute = userEntity.getScore().subtract(request.getNumber());
|
|
|
228
|
+ }
|
|
|
229
|
+
|
|
|
230
|
+ // 3. 更新用户贡献值(带乐观锁)
|
|
|
231
|
+ int updateCount = userDao.updateUserContribute(
|
|
|
232
|
+ request.getUid(),
|
|
|
233
|
+ newContribute
|
|
|
234
|
+ );
|
|
|
235
|
+ if (updateCount == 0) {
|
|
|
236
|
+ throw BusinessException.updateContributeFailed();
|
|
|
237
|
+ }
|
|
|
238
|
+
|
|
|
239
|
+ // 4. 创建贡献值记录
|
|
|
240
|
+ UserSignGongxianEntity record = buildContributeRecord(request, newContribute);
|
|
|
241
|
+ int createResult = userSignGongxianDao.insertByEntity(record);
|
|
|
242
|
+ if (createResult == 0) {
|
|
|
243
|
+ throw BusinessException.createContributeFailed();
|
|
|
244
|
+ }
|
|
|
245
|
+
|
|
|
246
|
+ // 5. 构建响应
|
|
|
247
|
+ return UserContributeModifyResponse.builder()
|
|
|
248
|
+ .uid(request.getUid())
|
|
|
249
|
+ .currentContribute(newContribute)
|
|
|
250
|
+ .transactionId(record.getId().toString())
|
|
|
251
|
+ .build();
|
|
|
252
|
+ }
|
|
|
253
|
+
|
|
|
254
|
+ private UserSignGongxianEntity buildContributeRecord(UserContributeModifyRequest request, BigDecimal newContribute) {
|
|
|
255
|
+ UserSignGongxianEntity record = new UserSignGongxianEntity();
|
|
|
256
|
+ record.setUid(request.getUid());
|
|
|
257
|
+ record.setMerId(request.getMerId());
|
|
|
258
|
+ record.setNumber(request.getNumber());
|
|
|
259
|
+ record.setStatus(request.getStatus());
|
|
|
260
|
+ record.setMark(buildContributeMark(request));
|
|
|
261
|
+ record.setDesc(request.getDesc());
|
|
|
262
|
+ record.setOrderId(request.getOrderId());
|
|
|
263
|
+ record.setOrderType(request.getOrderType());
|
|
|
264
|
+ record.setSurplus(newContribute);
|
|
|
265
|
+ record.setType(request.getType());
|
|
|
266
|
+ if (request.getStatus() == 1) {
|
|
|
267
|
+ record.setSettlementTime(Timestamp.valueOf(LocalDateTime.now()));
|
|
|
268
|
+ }
|
|
|
269
|
+ record.setAddtime(LocalDateTime.now().toEpochSecond(ZoneOffset.UTC));
|
|
|
270
|
+ return record;
|
|
|
271
|
+ }
|
|
|
272
|
+
|
|
|
273
|
+ private String buildContributeMark(UserContributeModifyRequest request) {
|
|
|
274
|
+ return String.format("%s贡献值,订单号:%s",
|
|
|
275
|
+ request.getType() == 1 ? "收入" : "支出",
|
|
|
276
|
+ request.getOrderId());
|
|
|
277
|
+ }
|
|
|
278
|
+
|
|
|
279
|
+
|
|
|
280
|
+ @Override
|
|
|
281
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
282
|
+ public UserDTO createUserInfo(CreateUserInfoRequest request) {
|
|
|
283
|
+ // 1. 获取用户并加锁
|
|
|
284
|
+ UserEntity userEntity = userDao.getUserInfoByAccount(request.getAccount());
|
|
|
285
|
+ if (userEntity != null) {
|
|
|
286
|
+ throw BusinessException.userIsExist();
|
|
|
287
|
+ }
|
|
|
288
|
+ userEntity = new UserEntity();
|
|
|
289
|
+ userEntity.setAccount(request.getAccount());
|
|
|
290
|
+ userEntity.setPhone(request.getPhone());
|
|
|
291
|
+ userEntity.setPwd(PasswordUtil.hashPassword("123456"));
|
|
|
292
|
+ userEntity.setNickname(request.getNickname());
|
|
|
293
|
+ userEntity.setRealName(request.getRealName());
|
|
|
294
|
+ userEntity.setAvatar("https://cdn.bjtdba.com/vod/image/2025-01-20/20250120125206881.png");
|
|
|
295
|
+ userEntity.setSpreadUid(request.getSpreadUid());
|
|
|
296
|
+ userEntity.setSex(request.getSex());
|
|
|
297
|
+ userEntity.setExternal(request.getExternal());
|
|
|
298
|
+ userEntity.setExternalId(request.getExternalId());
|
|
|
299
|
+ userEntity.setLevelId(request.getLevelId());
|
|
|
300
|
+ // 设置系统默认值
|
|
|
301
|
+ userEntity.setCreateTime(new Timestamp(System.currentTimeMillis()));
|
|
|
302
|
+ userEntity.setLastTime(new Timestamp(System.currentTimeMillis()));
|
|
|
303
|
+ userEntity.setLastIp(request.getLastIp());
|
|
|
304
|
+ userEntity.setStatus(1); // 默认状态:正常
|
|
|
305
|
+ userEntity.setIsDel(0); // 未删除
|
|
|
306
|
+ userEntity.setScore(BigDecimal.ZERO); // 默认积分
|
|
|
307
|
+ userEntity.setUserType(httpServletRequest.getHeader("X-API-KEY"));
|
|
|
308
|
+ int uid = userDao.insertUserByUserEntity(userEntity);
|
|
|
309
|
+ if (uid <= 0) {
|
|
|
310
|
+ throw BusinessException.createUserFailed();
|
|
|
311
|
+ }
|
|
|
312
|
+ return convertToUserDTO(userEntity);
|
|
|
313
|
+ }
|
|
211
|
314
|
|
|
212
|
315
|
|
|
213
|
|
- return null;
|
|
|
316
|
+ @Override
|
|
|
317
|
+ @Transactional(rollbackFor = Exception.class)
|
|
|
318
|
+ public UserDTO updateUserInfo(UpdateUserInfoRequest request) {
|
|
|
319
|
+ // 1. 获取用户并加锁
|
|
|
320
|
+ UserEntity userEntity = userDao.getUserInfoByUidForUpdate(request.getUid());
|
|
|
321
|
+ if (userEntity == null) {
|
|
|
322
|
+ // 如果用户不存在则创建
|
|
|
323
|
+ throw BusinessException.userNotFound();
|
|
|
324
|
+ }
|
|
|
325
|
+ UserEntity userEntityByAccount = userDao.getUserInfoByAccount(request.getAccount());
|
|
|
326
|
+ if (userEntityByAccount != null && !userEntityByAccount.getUid().equals(request.getUid())) {
|
|
|
327
|
+ throw BusinessException.userAccountIsExist();
|
|
|
328
|
+ }
|
|
|
329
|
+ // 只更新非空字段(根据业务需求调整)
|
|
|
330
|
+ if (request.getAccount() != null) {
|
|
|
331
|
+ userEntity.setAccount(request.getAccount());
|
|
|
332
|
+ }
|
|
|
333
|
+ if (request.getPhone() != null) {
|
|
|
334
|
+ userEntity.setPhone(request.getPhone());
|
|
|
335
|
+ }
|
|
|
336
|
+ if (request.getNickname() != null) {
|
|
|
337
|
+ userEntity.setNickname(request.getNickname());
|
|
|
338
|
+ }
|
|
|
339
|
+ if (request.getRealName() != null) {
|
|
|
340
|
+ userEntity.setRealName(request.getRealName());
|
|
|
341
|
+ }
|
|
|
342
|
+ if (request.getAvatar() != null) {
|
|
|
343
|
+ userEntity.setAvatar(request.getAvatar());
|
|
|
344
|
+ }
|
|
|
345
|
+ if (request.getSpreadUid() != null) {
|
|
|
346
|
+ userEntity.setSpreadUid(request.getSpreadUid());
|
|
|
347
|
+ }
|
|
|
348
|
+ if (request.getSex() != null) {
|
|
|
349
|
+ userEntity.setSex(request.getSex());
|
|
|
350
|
+ }
|
|
|
351
|
+ if (request.getLevelId() != null) {
|
|
|
352
|
+ userEntity.setLevelId(request.getLevelId());
|
|
|
353
|
+ }
|
|
|
354
|
+ // 更新最后活跃时间
|
|
|
355
|
+ userEntity.setLastTime(new Timestamp(System.currentTimeMillis()));
|
|
|
356
|
+ // 执行更新操作
|
|
|
357
|
+ int updateCount = userDao.updateUserByUid(userEntity);
|
|
|
358
|
+ if (updateCount <= 0) {
|
|
|
359
|
+ throw BusinessException.updateUserFailed();
|
|
|
360
|
+ }
|
|
|
361
|
+ return convertToUserDTO(userEntity);
|
|
214
|
362
|
}
|
|
215
|
363
|
}
|