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
| package com.mysql.service;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.mysql.entity.User; import com.mysql.mapper.UserMapper; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional;
import java.time.LocalDateTime; import java.util.List;
@Slf4j @Service public class UserService extends ServiceImpl<UserMapper, User> { @Autowired private UserMapper userMapper;
@Transactional public User createUser(User user) { try { log.info("创建用户: {}", user.getUsername()); user.setCreateTime(LocalDateTime.now()); user.setUpdateTime(LocalDateTime.now()); user.setStatus("ACTIVE"); user.setDeleted(0); userMapper.insert(user); log.info("用户创建成功: id={}", user.getId()); return user; } catch (Exception e) { log.error("创建用户失败", e); throw new RuntimeException("创建用户失败", e); } }
@Transactional public boolean updateUser(User user) { try { log.info("更新用户: id={}", user.getId()); user.setUpdateTime(LocalDateTime.now()); boolean result = updateById(user); log.info("用户更新结果: id={}, result={}", user.getId(), result); return result; } catch (Exception e) { log.error("更新用户失败: id={}", user.getId(), e); throw new RuntimeException("更新用户失败", e); } }
@Transactional public boolean deleteUser(Long userId) { try { log.info("删除用户: id={}", userId); boolean result = removeById(userId); log.info("用户删除结果: id={}, result={}", userId, result); return result; } catch (Exception e) { log.error("删除用户失败: id={}", userId, e); throw new RuntimeException("删除用户失败", e); } }
public User getUserById(Long userId) { try { log.debug("查询用户: id={}", userId); User user = getById(userId); log.debug("用户查询结果: id={}, username={}", userId, user != null ? user.getUsername() : "null"); return user; } catch (Exception e) { log.error("查询用户失败: id={}", userId, e); throw new RuntimeException("查询用户失败", e); } }
public User getUserByUsername(String username) { try { log.debug("根据用户名查询用户: username={}", username); QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("username", username); queryWrapper.eq("deleted", 0); User user = getOne(queryWrapper); log.debug("用户查询结果: username={}, id={}", username, user != null ? user.getId() : "null"); return user; } catch (Exception e) { log.error("根据用户名查询用户失败: username={}", username, e); throw new RuntimeException("查询用户失败", e); } }
public List<User> getUserList(int page, int size) { try { log.debug("查询用户列表: page={}, size={}", page, size); QueryWrapper<User> queryWrapper = new QueryWrapper<>(); queryWrapper.eq("deleted", 0); queryWrapper.orderByDesc("create_time"); queryWrapper.last("LIMIT " + (page - 1) * size + ", " + size); List<User> users = list(queryWrapper); log.debug("用户列表查询结果: count={}", users.size()); return users; } catch (Exception e) { log.error("查询用户列表失败", e); throw new RuntimeException("查询用户列表失败", e); } }
@Transactional public boolean updateUserStatus(Long userId, String status) { try { log.info("更新用户状态: id={}, status={}", userId, status); User user = new User(); user.setId(userId); user.setStatus(status); user.setUpdateTime(LocalDateTime.now()); boolean result = updateById(user); log.info("用户状态更新结果: id={}, status={}, result={}", userId, status, result); return result; } catch (Exception e) { log.error("更新用户状态失败: id={}, status={}", userId, status, e); throw new RuntimeException("更新用户状态失败", e); } } }
|