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 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417
|
@Service @Slf4j public class UserServiceImpl implements UserService { @Autowired private UserRepository userRepository; @Override public User getUserById(Long id) { log.info("根据ID获取用户,ID: {}", id); try { if (id == null || id <= 0) { throw new IllegalArgumentException("用户ID不能为空或小于等于0"); } User user = userRepository.findById(id).orElse(null); if (user == null) { log.warn("用户不存在,ID: {}", id); return null; } log.info("获取用户成功,ID: {}, 用户名: {}", id, user.getUsername()); return user; } catch (Exception e) { log.error("根据ID获取用户异常,ID: {}", id, e); throw new RuntimeException("获取用户失败: " + e.getMessage()); } } @Override public User getUserByUsername(String username) { log.info("根据用户名获取用户,用户名: {}", username); try { if (username == null || username.isEmpty()) { throw new IllegalArgumentException("用户名不能为空"); } User user = userRepository.findByUsername(username); if (user == null) { log.warn("用户不存在,用户名: {}", username); return null; } log.info("获取用户成功,用户名: {}, ID: {}", username, user.getId()); return user; } catch (Exception e) { log.error("根据用户名获取用户异常,用户名: {}", username, e); throw new RuntimeException("获取用户失败: " + e.getMessage()); } } @Override public boolean createUser(User user) { log.info("创建用户,用户名: {}", user.getUsername()); try { if (user == null) { throw new IllegalArgumentException("用户信息不能为空"); } if (user.getUsername() == null || user.getUsername().isEmpty()) { throw new IllegalArgumentException("用户名不能为空"); } User existingUser = userRepository.findByUsername(user.getUsername()); if (existingUser != null) { log.warn("用户名已存在,用户名: {}", user.getUsername()); return false; } user.setCreateTime(LocalDateTime.now()); user.setUpdateTime(LocalDateTime.now()); user.setStatus("ACTIVE"); userRepository.save(user); log.info("创建用户成功,用户名: {}, ID: {}", user.getUsername(), user.getId()); return true; } catch (Exception e) { log.error("创建用户异常,用户名: {}", user.getUsername(), e); throw new RuntimeException("创建用户失败: " + e.getMessage()); } } @Override public boolean updateUser(User user) { log.info("更新用户,ID: {}", user.getId()); try { if (user == null || user.getId() == null) { throw new IllegalArgumentException("用户信息或ID不能为空"); } User existingUser = userRepository.findById(user.getId()).orElse(null); if (existingUser == null) { log.warn("用户不存在,ID: {}", user.getId()); return false; } existingUser.setUsername(user.getUsername()); existingUser.setEmail(user.getEmail()); existingUser.setPhone(user.getPhone()); existingUser.setRealName(user.getRealName()); existingUser.setUpdateTime(LocalDateTime.now()); userRepository.save(existingUser); log.info("更新用户成功,ID: {}, 用户名: {}", user.getId(), user.getUsername()); return true; } catch (Exception e) { log.error("更新用户异常,ID: {}", user.getId(), e); throw new RuntimeException("更新用户失败: " + e.getMessage()); } } @Override public boolean deleteUser(Long id) { log.info("删除用户,ID: {}", id); try { if (id == null || id <= 0) { throw new IllegalArgumentException("用户ID不能为空或小于等于0"); } User existingUser = userRepository.findById(id).orElse(null); if (existingUser == null) { log.warn("用户不存在,ID: {}", id); return false; } userRepository.deleteById(id); log.info("删除用户成功,ID: {}", id); return true; } catch (Exception e) { log.error("删除用户异常,ID: {}", id, e); throw new RuntimeException("删除用户失败: " + e.getMessage()); } } @Override public List<User> getUserList(int pageNum, int pageSize) { log.info("获取用户列表,页码: {}, 页大小: {}", pageNum, pageSize); try { if (pageNum <= 0 || pageSize <= 0) { throw new IllegalArgumentException("页码和页大小必须大于0"); } Pageable pageable = PageRequest.of(pageNum - 1, pageSize); Page<User> userPage = userRepository.findAll(pageable); List<User> users = userPage.getContent(); log.info("获取用户列表成功,总数: {}, 当前页: {}", userPage.getTotalElements(), users.size()); return users; } catch (Exception e) { log.error("获取用户列表异常,页码: {}, 页大小: {}", pageNum, pageSize, e); throw new RuntimeException("获取用户列表失败: " + e.getMessage()); } } @Override public List<User> getUsersByIds(List<Long> ids) { log.info("批量获取用户,ID数量: {}", ids.size()); try { if (ids == null || ids.isEmpty()) { throw new IllegalArgumentException("用户ID列表不能为空"); } List<User> users = userRepository.findAllById(ids); log.info("批量获取用户成功,请求数量: {}, 实际数量: {}", ids.size(), users.size()); return users; } catch (Exception e) { log.error("批量获取用户异常,ID数量: {}", ids.size(), e); throw new RuntimeException("批量获取用户失败: " + e.getMessage()); } } }
@Service @Slf4j public class OrderServiceImpl implements OrderService { @Autowired private OrderRepository orderRepository; @Autowired private UserService userService; @Autowired private ProductService productService; @Override public Order getOrderById(Long id) { log.info("根据ID获取订单,ID: {}", id); try { if (id == null || id <= 0) { throw new IllegalArgumentException("订单ID不能为空或小于等于0"); } Order order = orderRepository.findById(id).orElse(null); if (order == null) { log.warn("订单不存在,ID: {}", id); return null; } log.info("获取订单成功,ID: {}, 订单号: {}", id, order.getOrderNo()); return order; } catch (Exception e) { log.error("根据ID获取订单异常,ID: {}", id, e); throw new RuntimeException("获取订单失败: " + e.getMessage()); } } @Override public boolean createOrder(Order order) { log.info("创建订单,用户ID: {}", order.getUserId()); try { if (order == null) { throw new IllegalArgumentException("订单信息不能为空"); } if (order.getUserId() == null) { throw new IllegalArgumentException("用户ID不能为空"); } User user = userService.getUserById(order.getUserId()); if (user == null) { log.warn("用户不存在,用户ID: {}", order.getUserId()); return false; } if (order.getProductId() != null) { Product product = productService.getProductById(order.getProductId()); if (product == null) { log.warn("商品不存在,商品ID: {}", order.getProductId()); return false; } } order.setOrderNo(generateOrderNo()); order.setCreateTime(LocalDateTime.now()); order.setUpdateTime(LocalDateTime.now()); order.setStatus("PENDING"); orderRepository.save(order); log.info("创建订单成功,订单号: {}, ID: {}", order.getOrderNo(), order.getId()); return true; } catch (Exception e) { log.error("创建订单异常,用户ID: {}", order.getUserId(), e); throw new RuntimeException("创建订单失败: " + e.getMessage()); } } @Override public boolean updateOrderStatus(Long id, String status) { log.info("更新订单状态,ID: {}, 状态: {}", id, status); try { if (id == null || id <= 0) { throw new IllegalArgumentException("订单ID不能为空或小于等于0"); } if (status == null || status.isEmpty()) { throw new IllegalArgumentException("订单状态不能为空"); } Order existingOrder = orderRepository.findById(id).orElse(null); if (existingOrder == null) { log.warn("订单不存在,ID: {}", id); return false; } existingOrder.setStatus(status); existingOrder.setUpdateTime(LocalDateTime.now()); orderRepository.save(existingOrder); log.info("更新订单状态成功,ID: {}, 状态: {}", id, status); return true; } catch (Exception e) { log.error("更新订单状态异常,ID: {}, 状态: {}", id, status, e); throw new RuntimeException("更新订单状态失败: " + e.getMessage()); } } @Override public List<Order> getUserOrders(Long userId, int pageNum, int pageSize) { log.info("获取用户订单列表,用户ID: {}, 页码: {}, 页大小: {}", userId, pageNum, pageSize); try { if (userId == null || userId <= 0) { throw new IllegalArgumentException("用户ID不能为空或小于等于0"); } if (pageNum <= 0 || pageSize <= 0) { throw new IllegalArgumentException("页码和页大小必须大于0"); } Pageable pageable = PageRequest.of(pageNum - 1, pageSize); Page<Order> orderPage = orderRepository.findByUserId(userId, pageable); List<Order> orders = orderPage.getContent(); log.info("获取用户订单列表成功,用户ID: {}, 总数: {}, 当前页: {}", userId, orderPage.getTotalElements(), orders.size()); return orders; } catch (Exception e) { log.error("获取用户订单列表异常,用户ID: {}, 页码: {}, 页大小: {}", userId, pageNum, pageSize, e); throw new RuntimeException("获取用户订单列表失败: " + e.getMessage()); } } @Override public boolean cancelOrder(Long id) { log.info("取消订单,ID: {}", id); try { if (id == null || id <= 0) { throw new IllegalArgumentException("订单ID不能为空或小于等于0"); } Order existingOrder = orderRepository.findById(id).orElse(null); if (existingOrder == null) { log.warn("订单不存在,ID: {}", id); return false; } if (!"PENDING".equals(existingOrder.getStatus())) { log.warn("订单状态不允许取消,ID: {}, 状态: {}", id, existingOrder.getStatus()); return false; } existingOrder.setStatus("CANCELLED"); existingOrder.setUpdateTime(LocalDateTime.now()); orderRepository.save(existingOrder); log.info("取消订单成功,ID: {}", id); return true; } catch (Exception e) { log.error("取消订单异常,ID: {}", id, e); throw new RuntimeException("取消订单失败: " + e.getMessage()); } }
private String generateOrderNo() { return "ORD" + System.currentTimeMillis() + RandomUtils.nextInt(1000, 9999); } }
|