记得上下班打卡 | git大法好,push需谨慎

Commit e1ec8264 authored by 姜秀龙's avatar 姜秀龙

LostFoundAdmin 解决手机号重复添加问题

parent 371d22d3
package com.liquidnet.service.sweet.service;
import com.baomidou.mybatisplus.extension.service.IService;
import com.liquidnet.service.base.ResponseDto;
import com.liquidnet.service.sweet.entity.SweetLostFoundAdmin;
import com.liquidnet.service.sweet.param.SweetLostFoundAdminParam;
import com.liquidnet.service.sweet.vo.SweetLostFoundAdminVo;
......@@ -21,7 +22,7 @@ public interface ISweetLostFoundAdminService extends IService<SweetLostFoundAdmi
* @param admin 管理员信息
* @return 是否成功
*/
boolean addAdmin(SweetLostFoundAdminParam admin);
ResponseDto<Boolean> addAdmin(SweetLostFoundAdminParam admin);
/**
* 编辑管理员
......@@ -29,7 +30,7 @@ public interface ISweetLostFoundAdminService extends IService<SweetLostFoundAdmi
* @param admin 管理员信息
* @return 是否成功
*/
boolean editAdmin(SweetLostFoundAdminParam admin);
ResponseDto<Boolean> editAdmin(SweetLostFoundAdminParam admin);
/**
* 删除管理员
......
......@@ -32,13 +32,13 @@ public class SweetLostFoundAdminController {
@PostMapping("/add")
@ApiOperation("添加管理员")
public ResponseDto<Boolean> addAdmin(@RequestBody @Validated SweetLostFoundAdminParam admin) {
return ResponseDto.success(sweetLostFoundAdminService.addAdmin(admin));
return sweetLostFoundAdminService.addAdmin(admin);
}
@PutMapping("/update")
@ApiOperation("编辑管理员")
public ResponseDto<Boolean> updateAdmin(@RequestBody @Validated({ValidationGroups.Update.class}) SweetLostFoundAdminParam admin) {
return ResponseDto.success(sweetLostFoundAdminService.editAdmin(admin));
return sweetLostFoundAdminService.editAdmin(admin);
}
@DeleteMapping("/delete/{id}")
......
......@@ -3,6 +3,7 @@ package com.liquidnet.service.sweet.service.impl;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.liquidnet.commons.lang.util.IDGenerator;
import com.liquidnet.service.base.ResponseDto;
import com.liquidnet.service.sweet.entity.SweetLostFoundAdmin;
import com.liquidnet.service.sweet.mapper.SweetLostFoundAdminMapper;
import com.liquidnet.service.sweet.param.SweetLostFoundAdminParam;
......@@ -29,42 +30,33 @@ public class SweetLostFoundAdminServiceImpl extends ServiceImpl<SweetLostFoundAd
private LostFoundRedisUtils lostFoundRedisUtils;
@Override
public boolean addAdmin(SweetLostFoundAdminParam admin) {
public ResponseDto<Boolean> addAdmin(SweetLostFoundAdminParam admin) {
String phone = admin.getPhone().trim();
String performanceId = admin.getPerformanceId().trim();
Integer authScope = admin.getAuthScope();
if (phone == null || phone.trim().isEmpty()) {
throw new IllegalArgumentException("手机号不能为空");
}
if (performanceId == null || performanceId.isEmpty()) {
throw new IllegalArgumentException("演出ID不能为空");
// 统一查询:检查当前手机号在所有相关场景下的冲突
QueryWrapper<SweetLostFoundAdmin> conflictWrapper = new QueryWrapper<>();
conflictWrapper.eq("phone", phone)
.eq("is_deleted", 0);
// 规则1:如果创建的是全站管理员,检查是否已存在全站管理员
if (authScope != null && authScope == 2) {
conflictWrapper.eq("auth_scope", 2);
}
// 规则1: 检查在当前演出下是否已创建过管理员
QueryWrapper<SweetLostFoundAdmin> existWrapper = new QueryWrapper<>();
existWrapper.eq("phone", phone)
.eq("performance_id", performanceId)
.eq("is_deleted", 0);
if (baseMapper.selectCount(existWrapper) > 0) {
throw new IllegalStateException("该手机号在当前演出下已存在管理员记录");
// 规则2:如果创建的是特定演出管理员,检查是否已存在该演出记录或全站管理员
else {
conflictWrapper.and(wrapper ->
wrapper.eq("performance_id", performanceId)
.or()
.eq("auth_scope", 2)
);
}
// 规则2: 如果创建的是全站管理员,检查该手机号是否已在其他演出下创建过全站管理员
if (authScope != null && authScope == 2) {
QueryWrapper<SweetLostFoundAdmin> globalWrapper = new QueryWrapper<>();
globalWrapper.eq("phone", phone)
.eq("auth_scope", 2)
.ne("performance_id", performanceId)
.eq("is_deleted", 0);
if (baseMapper.selectCount(globalWrapper) > 0) {
throw new IllegalStateException("该手机号已作为全站管理员存在,不能再次创建");
}
if (baseMapper.selectCount(conflictWrapper) > 0) {
return ResponseDto.failure("该手机号已存在");
}
SweetLostFoundAdmin foundAdmin = SweetLostFoundAdmin.getNew();
foundAdmin.setAdminId(IDGenerator.nextSnowId());
foundAdmin.setPhone(phone);
......@@ -72,59 +64,56 @@ public class SweetLostFoundAdminServiceImpl extends ServiceImpl<SweetLostFoundAd
foundAdmin.setPermissionType(admin.getPermissionType());
foundAdmin.setAuthScope(admin.getAuthScope());
foundAdmin.setPerformanceId(performanceId);
return baseMapper.insert(foundAdmin) > 0;
return ResponseDto.success(baseMapper.insert(foundAdmin) > 0);
}
// 假设:
// - 已存在记录:phone=13800138000, performanceId="A", authScope=1
// - 现在创建:phone=13800138000, performanceId="B", authScope=2
//
// 当前写法:允许创建(因为只在全站管理员场景检查)
// 建议写法:会报错(因为会查到performanceId="A"的记录)
// 假设:
// - 已存在记录:phone=13800138000, performanceId="A", authScope=2
// - 现在创建:phone=13800138000, performanceId="B", authScope=1
//
// 当前行为:会允许创建,但这可能不符合业务需求
@Override
public boolean editAdmin(SweetLostFoundAdminParam admin) {
public ResponseDto<Boolean> editAdmin(SweetLostFoundAdminParam admin) {
Long id = admin.getId();
String phone = admin.getPhone().trim();
String performanceId = admin.getPerformanceId().trim();
Integer authScope = admin.getAuthScope();
if (id == null) {
throw new IllegalArgumentException("管理员ID不能为空");
}
if (phone == null || phone.isEmpty()) {
throw new IllegalArgumentException("手机号不能为空");
}
if (performanceId == null || performanceId.isEmpty()) {
throw new IllegalArgumentException("演出ID不能为空");
}
// 检查要修改的管理员是否存在
SweetLostFoundAdmin existingAdmin = baseMapper.selectById(id);
if (existingAdmin == null || existingAdmin.getIsDeleted() == 1) {
throw new IllegalStateException("管理员记录不存在或已删除");
return ResponseDto.failure("管理员记录不存在或已删除");
}
// 规则1: 检查修改后的手机号在当前演出下是否已存在(排除自身)
QueryWrapper<SweetLostFoundAdmin> existWrapper = new QueryWrapper<>();
existWrapper.eq("phone", phone)
.eq("performance_id", performanceId)
.ne("id", id)
.eq("is_deleted", 0);
if (baseMapper.selectCount(existWrapper) > 0) {
throw new IllegalStateException("该手机号在当前演出下已存在管理员记录");
}
// 规则2: 如果修改为全站管理员,检查该手机号是否已在其他演出下创建过全站管理员(排除自身)
// 统一查询:检查修改后手机号在所有相关场景下的冲突(排除自身)
QueryWrapper<SweetLostFoundAdmin> conflictWrapper = new QueryWrapper<>();
conflictWrapper.eq("phone", phone)
.ne("id", id)
.eq("is_deleted", 0);
// 规则1:如果修改为全站管理员,检查是否已存在全站管理员
if (authScope != null && authScope == 2) {
QueryWrapper<SweetLostFoundAdmin> globalWrapper = new QueryWrapper<>();
globalWrapper.eq("phone", phone)
.eq("auth_scope", 2)
.ne("id", id)
.ne("performance_id", performanceId)
.eq("is_deleted", 0);
if (baseMapper.selectCount(globalWrapper) > 0) {
throw new IllegalStateException("该手机号已作为全站管理员存在,不能再次创建");
}
conflictWrapper.eq("auth_scope", 2);
}
// 规则2:如果修改为特定演出管理员,检查是否已存在该演出记录或全站管理员
else {
conflictWrapper.and(wrapper ->
wrapper.eq("performance_id", performanceId)
.or()
.eq("auth_scope", 2)
);
}
if (baseMapper.selectCount(conflictWrapper) > 0) {
return ResponseDto.failure("该手机号已存在");
}
SweetLostFoundAdmin foundAdmin = SweetLostFoundAdmin.getNew();
foundAdmin.setId(id);
foundAdmin.setPhone(phone);
......@@ -135,11 +124,11 @@ public class SweetLostFoundAdminServiceImpl extends ServiceImpl<SweetLostFoundAd
// 更新缓存
lostFoundRedisUtils.deleteAdminCache(phone, performanceId);
if (!phone.trim().equals(existingAdmin.getPhone())) {
if (!phone.equals(existingAdmin.getPhone())) {
lostFoundRedisUtils.deleteAdminCache(existingAdmin.getPhone(), performanceId);
}
return baseMapper.updateById(foundAdmin) > 0;
return ResponseDto.success(baseMapper.updateById(foundAdmin) > 0);
}
@Override
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment