|
@@ -22,12 +22,12 @@ import java.util.Map;
|
|
|
@Slf4j
|
|
@Slf4j
|
|
|
@Service
|
|
@Service
|
|
|
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {
|
|
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements AdminService {
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
@Override
|
|
@Override
|
|
|
public Result<IPage<Admin>> getAdminList(int page, int pageSize, String username, String phone, Long roleId, Integer status) {
|
|
public Result<IPage<Admin>> getAdminList(int page, int pageSize, String username, String phone, Long roleId, Integer status) {
|
|
|
try {
|
|
try {
|
|
|
LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
|
|
LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 条件查询
|
|
// 条件查询
|
|
|
if (StringUtils.hasText(username)) {
|
|
if (StringUtils.hasText(username)) {
|
|
|
wrapper.and(w -> w.like(Admin::getUsername, username)
|
|
wrapper.and(w -> w.like(Admin::getUsername, username)
|
|
@@ -37,66 +37,67 @@ public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements
|
|
|
wrapper.eq(Admin::getPhone, phone);
|
|
wrapper.eq(Admin::getPhone, phone);
|
|
|
}
|
|
}
|
|
|
if (roleId != null) {
|
|
if (roleId != null) {
|
|
|
- wrapper.eq(Admin::getRoleId, roleId);
|
|
|
|
|
|
|
+ // roleIds是逗号分隔的字符串,使用like模糊匹配
|
|
|
|
|
+ wrapper.like(Admin::getRoleIds, roleId.toString());
|
|
|
}
|
|
}
|
|
|
if (status != null) {
|
|
if (status != null) {
|
|
|
wrapper.eq(Admin::getStatus, status);
|
|
wrapper.eq(Admin::getStatus, status);
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 按创建时间倒序
|
|
// 按创建时间倒序
|
|
|
wrapper.orderByDesc(Admin::getCreateTime);
|
|
wrapper.orderByDesc(Admin::getCreateTime);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 分页查询
|
|
// 分页查询
|
|
|
Page<Admin> pageObj = new Page<>(page, pageSize);
|
|
Page<Admin> pageObj = new Page<>(page, pageSize);
|
|
|
IPage<Admin> result = this.page(pageObj, wrapper);
|
|
IPage<Admin> result = this.page(pageObj, wrapper);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 密码字段置空
|
|
// 密码字段置空
|
|
|
result.getRecords().forEach(admin -> admin.setPassword(null));
|
|
result.getRecords().forEach(admin -> admin.setPassword(null));
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
return Result.success("查询成功", result);
|
|
return Result.success("查询成功", result);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
log.error("查询管理员列表失败", e);
|
|
log.error("查询管理员列表失败", e);
|
|
|
return Result.error(500, "查询失败: " + e.getMessage());
|
|
return Result.error(500, "查询失败: " + e.getMessage());
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
@Override
|
|
@Override
|
|
|
public Result<Void> addAdmin(Admin admin) {
|
|
public Result<Void> addAdmin(Admin admin) {
|
|
|
try {
|
|
try {
|
|
|
// 检查用户名是否已存在
|
|
// 检查用户名是否已存在
|
|
|
- Admin existAdmin = baseMapper.selectByUsername(admin.getUsername());
|
|
|
|
|
|
|
+ Admin existAdmin = lambdaQuery().eq(Admin::getUsername, admin.getUsername()).one();
|
|
|
if (existAdmin != null) {
|
|
if (existAdmin != null) {
|
|
|
return Result.error(400, "用户名已存在");
|
|
return Result.error(400, "用户名已存在");
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 检查手机号是否已存在
|
|
// 检查手机号是否已存在
|
|
|
if (StringUtils.hasText(admin.getPhone())) {
|
|
if (StringUtils.hasText(admin.getPhone())) {
|
|
|
- existAdmin = baseMapper.selectByPhone(admin.getPhone());
|
|
|
|
|
|
|
+ existAdmin = lambdaQuery().eq(Admin::getPhone, admin.getPhone()).one();
|
|
|
if (existAdmin != null) {
|
|
if (existAdmin != null) {
|
|
|
return Result.error(400, "手机号已存在");
|
|
return Result.error(400, "手机号已存在");
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 设置默认值
|
|
// 设置默认值
|
|
|
admin.setStatus(1); // 默认正常状态
|
|
admin.setStatus(1); // 默认正常状态
|
|
|
admin.setCreateTime(LocalDateTime.now());
|
|
admin.setCreateTime(LocalDateTime.now());
|
|
|
admin.setUpdateTime(LocalDateTime.now());
|
|
admin.setUpdateTime(LocalDateTime.now());
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 临时方案:密码明文存储,生产环境需要加密
|
|
// 临时方案:密码明文存储,生产环境需要加密
|
|
|
if (!StringUtils.hasText(admin.getPassword())) {
|
|
if (!StringUtils.hasText(admin.getPassword())) {
|
|
|
admin.setPassword("123456"); // 默认密码
|
|
admin.setPassword("123456"); // 默认密码
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
boolean success = this.save(admin);
|
|
boolean success = this.save(admin);
|
|
|
return success ? Result.success("添加成功", null) : Result.error(500, "添加失败");
|
|
return success ? Result.success("添加成功", null) : Result.error(500, "添加失败");
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
log.error("添加管理员失败", e);
|
|
log.error("添加管理员失败", e);
|
|
|
return Result.error(500, "添加失败: " + e.getMessage());
|
|
return Result.error(500, "添加失败: " + e.getMessage());
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
@Override
|
|
@Override
|
|
|
public Result<Void> updateAdmin(Admin admin) {
|
|
public Result<Void> updateAdmin(Admin admin) {
|
|
|
try {
|
|
try {
|
|
@@ -105,36 +106,36 @@ public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements
|
|
|
if (existAdmin == null) {
|
|
if (existAdmin == null) {
|
|
|
return Result.error(404, "管理员不存在");
|
|
return Result.error(404, "管理员不存在");
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 如果修改了用户名,检查是否重复
|
|
// 如果修改了用户名,检查是否重复
|
|
|
if (!existAdmin.getUsername().equals(admin.getUsername())) {
|
|
if (!existAdmin.getUsername().equals(admin.getUsername())) {
|
|
|
- Admin checkAdmin = baseMapper.selectByUsername(admin.getUsername());
|
|
|
|
|
|
|
+ Admin checkAdmin = lambdaQuery().eq(Admin::getUsername, admin.getUsername()).one();
|
|
|
if (checkAdmin != null) {
|
|
if (checkAdmin != null) {
|
|
|
return Result.error(400, "用户名已存在");
|
|
return Result.error(400, "用户名已存在");
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 如果修改了手机号,检查是否重复
|
|
// 如果修改了手机号,检查是否重复
|
|
|
if (StringUtils.hasText(admin.getPhone()) && !admin.getPhone().equals(existAdmin.getPhone())) {
|
|
if (StringUtils.hasText(admin.getPhone()) && !admin.getPhone().equals(existAdmin.getPhone())) {
|
|
|
- Admin checkAdmin = baseMapper.selectByPhone(admin.getPhone());
|
|
|
|
|
|
|
+ Admin checkAdmin = lambdaQuery().eq(Admin::getPhone, admin.getPhone()).one();
|
|
|
if (checkAdmin != null) {
|
|
if (checkAdmin != null) {
|
|
|
return Result.error(400, "手机号已存在");
|
|
return Result.error(400, "手机号已存在");
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
admin.setUpdateTime(LocalDateTime.now());
|
|
admin.setUpdateTime(LocalDateTime.now());
|
|
|
// 不允许通过此接口修改密码
|
|
// 不允许通过此接口修改密码
|
|
|
admin.setPassword(null);
|
|
admin.setPassword(null);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
boolean success = this.updateById(admin);
|
|
boolean success = this.updateById(admin);
|
|
|
return success ? Result.success("更新成功", null) : Result.error(500, "更新失败");
|
|
return success ? Result.success("更新成功", null) : Result.error(500, "更新失败");
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
log.error("更新管理员失败", e);
|
|
log.error("更新管理员失败", e);
|
|
|
return Result.error(500, "更新失败: " + e.getMessage());
|
|
return Result.error(500, "更新失败: " + e.getMessage());
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
@Override
|
|
@Override
|
|
|
public Result<Void> deleteAdmin(Long id) {
|
|
public Result<Void> deleteAdmin(Long id) {
|
|
|
try {
|
|
try {
|
|
@@ -143,18 +144,18 @@ public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements
|
|
|
if (admin == null) {
|
|
if (admin == null) {
|
|
|
return Result.error(404, "管理员不存在");
|
|
return Result.error(404, "管理员不存在");
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// TODO: 检查是否可以删除(如果是超级管理员则不允许删除)
|
|
// TODO: 检查是否可以删除(如果是超级管理员则不允许删除)
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
boolean success = this.removeById(id);
|
|
boolean success = this.removeById(id);
|
|
|
return success ? Result.success("删除成功", null) : Result.error(500, "删除失败");
|
|
return success ? Result.success("删除成功", null) : Result.error(500, "删除失败");
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
log.error("删除管理员失败", e);
|
|
log.error("删除管理员失败", e);
|
|
|
return Result.error(500, "删除失败: " + e.getMessage());
|
|
return Result.error(500, "删除失败: " + e.getMessage());
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
@Override
|
|
@Override
|
|
|
public Result<Void> resetPassword(Long id, String newPassword) {
|
|
public Result<Void> resetPassword(Long id, String newPassword) {
|
|
|
try {
|
|
try {
|
|
@@ -162,45 +163,45 @@ public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements
|
|
|
if (admin == null) {
|
|
if (admin == null) {
|
|
|
return Result.error(404, "管理员不存在");
|
|
return Result.error(404, "管理员不存在");
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 临时方案:密码明文存储,生产环境需要加密
|
|
// 临时方案:密码明文存储,生产环境需要加密
|
|
|
if (!StringUtils.hasText(newPassword)) {
|
|
if (!StringUtils.hasText(newPassword)) {
|
|
|
newPassword = "123456"; // 默认密码
|
|
newPassword = "123456"; // 默认密码
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
admin.setPassword(newPassword);
|
|
admin.setPassword(newPassword);
|
|
|
admin.setUpdateTime(LocalDateTime.now());
|
|
admin.setUpdateTime(LocalDateTime.now());
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
boolean success = this.updateById(admin);
|
|
boolean success = this.updateById(admin);
|
|
|
return success ? Result.success("密码重置成功", null) : Result.error(500, "密码重置失败");
|
|
return success ? Result.success("密码重置成功", null) : Result.error(500, "密码重置失败");
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
log.error("重置密码失败", e);
|
|
log.error("重置密码失败", e);
|
|
|
return Result.error(500, "重置密码失败: " + e.getMessage());
|
|
return Result.error(500, "重置密码失败: " + e.getMessage());
|
|
|
}
|
|
}
|
|
|
}
|
|
}
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
@Override
|
|
@Override
|
|
|
public Result<Map<String, Object>> getStatistics() {
|
|
public Result<Map<String, Object>> getStatistics() {
|
|
|
try {
|
|
try {
|
|
|
Map<String, Object> statistics = new HashMap<>();
|
|
Map<String, Object> statistics = new HashMap<>();
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 总管理员数
|
|
// 总管理员数
|
|
|
long totalUsers = this.count();
|
|
long totalUsers = this.count();
|
|
|
statistics.put("totalUsers", totalUsers);
|
|
statistics.put("totalUsers", totalUsers);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 正常状态管理员数
|
|
// 正常状态管理员数
|
|
|
long activeUsers = this.count(new LambdaQueryWrapper<Admin>().eq(Admin::getStatus, 1));
|
|
long activeUsers = this.count(new LambdaQueryWrapper<Admin>().eq(Admin::getStatus, 1));
|
|
|
statistics.put("activeUsers", activeUsers);
|
|
statistics.put("activeUsers", activeUsers);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 在线用户数(暂时返回0,后续可通过Redis统计)
|
|
// 在线用户数(暂时返回0,后续可通过Redis统计)
|
|
|
statistics.put("onlineUsers", 0);
|
|
statistics.put("onlineUsers", 0);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
// 今日新增
|
|
// 今日新增
|
|
|
statistics.put("todayNew", 0);
|
|
statistics.put("todayNew", 0);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
return Result.success("获取成功", statistics);
|
|
return Result.success("获取成功", statistics);
|
|
|
-
|
|
|
|
|
|
|
+
|
|
|
} catch (Exception e) {
|
|
} catch (Exception e) {
|
|
|
log.error("获取统计数据失败", e);
|
|
log.error("获取统计数据失败", e);
|
|
|
return Result.error(500, "获取统计数据失败: " + e.getMessage());
|
|
return Result.error(500, "获取统计数据失败: " + e.getMessage());
|