优化预约客户输入与唯一性校验

This commit is contained in:
2026-10-16 17:33:58 +08:00
parent 3d18a1972b
commit 4a7bd90088
12 changed files with 318 additions and 164 deletions
@@ -141,6 +141,16 @@ public class AdminManagerController {
return Result.success(result);
}
@GetMapping("/customers/matches")
@RequirePermission("user:view")
public Result<List<UserVO>> customerMatches(HttpServletRequest request,
@RequestParam String nickname,
@RequestParam(required = false) String phone) {
UserDO currentUser = adminScopeHelper.getCurrentUser(request);
Long shopId = adminScopeHelper.resolveShopScope(currentUser, null);
return Result.success(userService.findCustomerMatches(nickname, phone, shopId));
}
@GetMapping("/customers/{id}")
@RequirePermission("user:view")
public Result<UserVO> customer(HttpServletRequest request, @PathVariable Long id) {
@@ -157,18 +167,7 @@ public class AdminManagerController {
UserDO currentUser = adminScopeHelper.getCurrentUser(request);
adminScopeHelper.requireShopContext(currentUser, "请先选择店铺后再新增客户");
Long shopId = adminScopeHelper.resolveShopScope(currentUser, null);
String phone = dto.getPhone().trim();
UserDTO userDTO = new UserDTO();
userDTO.setNickname(dto.getNickname().trim());
userDTO.setPhone(phone);
userDTO.setContactPhone(phone);
userDTO.setAccount(phone);
userDTO.setType(0);
userDTO.setShopId(shopId);
userDTO.setShopIds(List.of(shopId));
userDTO.setLevel("普通");
userDTO.setVipId(0L);
return Result.success(userService.createUser(userDTO));
return Result.success(userService.createManagerCustomer(dto.getNickname(), dto.getPhone(), shopId));
}
@GetMapping("/employees")
@@ -3,7 +3,6 @@ package com.xiaowang.jayucloud.salon.core.modules.manager.dto;
import lombok.Data;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern;
@Data
public class ManagerCustomerCreateDTO {
@@ -11,7 +10,5 @@ public class ManagerCustomerCreateDTO {
@NotBlank(message = "客户姓名不能为空")
private String nickname;
@NotBlank(message = "手机号不能为空")
@Pattern(regexp = "^1[3-9]\\d{9}$", message = "手机号格式不正确")
private String phone;
}
@@ -23,6 +23,7 @@ import com.xiaowang.jayucloud.salon.core.modules.order.mapper.OrderMapper;
import com.xiaowang.jayucloud.salon.core.modules.product.mapper.ProductMapper;
import com.xiaowang.jayucloud.salon.core.modules.shop.mapper.ShopMapper;
import com.xiaowang.jayucloud.salon.core.modules.user.mapper.UserMapper;
import com.xiaowang.jayucloud.salon.core.modules.user.service.UserService;
import com.xiaowang.jayucloud.salon.core.modules.planning.service.AppointmentCapacityService;
import com.xiaowang.jayucloud.salon.core.modules.order.service.OrderService;
import com.xiaowang.jayucloud.salon.core.modules.planning.service.PlanningService;
@@ -71,7 +72,6 @@ public class PlanningServiceImpl implements PlanningService {
private static final String RESERVATION_UNASSIGNED = "UNASSIGNED";
private static final String ASSIGNMENT_ASSIGNED = "ASSIGNED";
private static final String PRODUCT_SOURCE_TYPE_MANAGER_APPOINTMENT = "MANAGER_APPOINTMENT";
private static final String ENCODED_UNSET_PASSWORD = "$2a$10$EixZaYVK1fsbw1ZfbX3OXePaWxn96p36WQoeG6Lruj3vjPGga31lW";
private static final List<String> ACTIVE_RESERVATION_STATUS = List.of("LOCKED", RESERVATION_PENDING_QUOTE, RESERVATION_CONFIRMED);
private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
private static final List<String> ARTIST_COLORS = List.of(
@@ -80,6 +80,7 @@ public class PlanningServiceImpl implements PlanningService {
private final AdminScopeHelper adminScopeHelper;
private final UserMapper userMapper;
private final UserService userService;
private final NailArtistInfoMapper nailArtistInfoMapper;
private final ArtistScheduleMapper artistScheduleMapper;
private final ArtistScheduleExceptionMapper artistScheduleExceptionMapper;
@@ -780,34 +781,26 @@ public class PlanningServiceImpl implements PlanningService {
private UserDO resolveCustomer(Long shopId, PlanningAppointmentCreateDTO dto) {
if (dto.getCustomerUserId() != null) {
UserDO customer = userMapper.selectById(dto.getCustomerUserId());
if (customer == null || Integer.valueOf(1).equals(customer.getIsDelete())) {
if (customer == null || Integer.valueOf(1).equals(customer.getIsDelete()) || !Integer.valueOf(0).equals(customer.getType())) {
throw new RuntimeException("客户不存在");
}
return customer;
}
if (!hasText(dto.getPhone())) {
throw new RuntimeException("客户手机号不能为空");
String customerName = firstNonBlank(dto.getCustomerName(), dto.getPhone());
if (!hasText(customerName)) {
throw new RuntimeException("客户姓名不能为空");
}
QueryWrapper<UserDO> wrapper = new QueryWrapper<>();
wrapper.eq("is_delete", 0)
.and(query -> query.eq("phone", dto.getPhone()).or().eq("contact_phone", dto.getPhone()))
.last("LIMIT 1");
UserDO existing = userMapper.selectOne(wrapper);
if (existing != null) {
return existing;
String phone = trimToNull(dto.getPhone());
List<com.xiaowang.jayucloud.salon.core.modules.user.vo.UserVO> matches =
userService.findCustomerMatches(customerName, phone, shopId);
if (matches.size() > 1) {
throw new RuntimeException("存在多个匹配客户,请先选择已有客户");
}
UserDO customer = new UserDO();
customer.setShopId(shopId);
customer.setNickname(firstNonBlank(dto.getCustomerName(), dto.getPhone()));
customer.setPhone(dto.getPhone());
customer.setContactPhone(dto.getPhone());
customer.setAccount(dto.getPhone());
customer.setPassword(ENCODED_UNSET_PASSWORD);
customer.setType(0);
customer.setStatus(1);
customer.setIsDelete(0);
userMapper.insert(customer);
return customer;
if (!matches.isEmpty()) {
return userMapper.selectById(matches.get(0).getId());
}
Long customerId = userService.createManagerCustomer(customerName, phone, shopId);
return userMapper.selectById(customerId);
}
private OrderDO requireAccessibleOrder(UserDO currentUser, Long orderId) {
@@ -34,6 +34,11 @@ public interface UserService extends IService<UserDO> {
*/
Long createUser(UserDTO userDTO);
Long createManagerCustomer(String nickname, String phone, Long shopId);
List<UserVO> findCustomerMatches(String nickname, String phone, Long shopId);
/**
* Update an existing record.
* Call guide: pass the named parameters below from the controller or caller; the method validates them and returns the business result.
@@ -69,6 +69,7 @@ import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;
import com.xiaowang.jayucloud.salon.core.modules.auth.dto.BindEmailRequestDTO;
@@ -356,6 +357,10 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
@Override
@Transactional(rollbackFor = Exception.class)
public Long createUser(UserDTO userDTO) {
return createUserInternal(userDTO, false);
}
private Long createUserInternal(UserDTO userDTO, boolean allowMissingCustomerPhone) {
String validatedAccount = AccountValidationUtils.requireValidAccount(userDTO.getAccount());
// 检查账号是否已存在
UserDO existingUser = userMapper.selectOne(
@@ -374,8 +379,12 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
List<Long> validatedShopIds = resolveValidatedShopIds(userDTO.getShopIds(), userDTO.getShopId());
Long validatedShopId = pickPrimaryShopId(validatedShopIds);
String validatedPhone = validateUserPhone(userDTO.getPhone(), null, true);
Integer userType = userDTO.getType();
String validatedPhone = validateUserPhone(userDTO.getPhone(), null,
!(allowMissingCustomerPhone && Integer.valueOf(0).equals(userType)));
if (Integer.valueOf(0).equals(userType)) {
ensureCustomerUnique(userDTO.getNickname(), validatedPhone);
}
String validatedWechatId = UserProfileFieldSupport.normalizeOptionalText(userDTO.getWechatId());
UserProfileFieldSupport.requireWechatIdForServiceUser(userType, validatedWechatId);
@@ -461,6 +470,68 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
return userDO.getId();
}
@Override
@Transactional(rollbackFor = Exception.class)
public Long createManagerCustomer(String nickname, String phone, Long shopId) {
if (!StringUtils.hasText(nickname)) {
throw new RuntimeException("客户姓名不能为空");
}
if (shopId == null || shopId <= 0) {
throw new RuntimeException("请先选择店铺后再新增客户");
}
String normalizedPhone = phone == null || phone.trim().isEmpty() ? null : phone.trim();
ensureCustomerUnique(nickname, normalizedPhone);
normalizedPhone = validateUserPhone(normalizedPhone, null, false);
UserDTO customer = new UserDTO();
customer.setNickname(nickname.trim());
customer.setPhone(normalizedPhone);
customer.setContactPhone(normalizedPhone);
customer.setAccount(normalizedPhone == null
? "customer" + UUID.randomUUID().toString().replace("-", "")
: normalizedPhone);
customer.setType(0);
customer.setShopId(shopId);
customer.setShopIds(List.of(shopId));
customer.setLevel(VIP_LEVEL_NORMAL);
customer.setVipId(0L);
return createUserInternal(customer, true);
}
@Override
public List<UserVO> findCustomerMatches(String nickname, String phone, Long shopId) {
if (!StringUtils.hasText(nickname)) {
return List.of();
}
LambdaQueryWrapper<UserDO> wrapper = Wrappers.lambdaQuery(UserDO.class)
.eq(UserDO::getNickname, nickname.trim())
.eq(UserDO::getType, 0)
.eq(UserDO::getIsDelete, 0);
if (StringUtils.hasText(phone)) {
String normalizedPhone = phone.trim();
wrapper.and(query -> query.eq(UserDO::getPhone, normalizedPhone)
.or().eq(UserDO::getContactPhone, normalizedPhone));
}
if (shopId != null && shopId > 0) {
List<Long> shopUserIds = userShopBindingSupport.listUserIdsByShopId(shopId, true);
if (shopUserIds.isEmpty()) {
wrapper.eq(UserDO::getShopId, shopId);
} else {
wrapper.and(query -> query.eq(UserDO::getShopId, shopId)
.or().in(UserDO::getId, shopUserIds));
}
}
return userMapper.selectList(wrapper).stream().map(user -> {
UserVO customer = new UserVO();
customer.setId(user.getId());
customer.setNickname(user.getNickname());
customer.setAccount(user.getAccount());
customer.setPhone(user.getPhone());
customer.setContactPhone(user.getContactPhone());
customer.setAvatar(resolveAccessibleAvatar(user.getAvatar()));
return customer;
}).collect(Collectors.toList());
}
/**
* 分配默认权限
* @param userId 用户ID
@@ -3047,6 +3118,23 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
throw new RuntimeException("手机号已被使用");
}
}
private void ensureCustomerUnique(String nickname, String phone) {
if (!StringUtils.hasText(nickname)) {
throw new RuntimeException("客户姓名不能为空");
}
LambdaQueryWrapper<UserDO> wrapper = Wrappers.lambdaQuery(UserDO.class)
.eq(UserDO::getNickname, nickname.trim())
.eq(UserDO::getType, 0)
.eq(UserDO::getIsDelete, 0);
if (StringUtils.hasText(phone)) {
wrapper.and(query -> query.eq(UserDO::getPhone, phone)
.or().eq(UserDO::getContactPhone, phone));
}
if (userMapper.selectCount(wrapper) > 0) {
throw new RuntimeException("客户已存在,请选择已有客户");
}
}
/**
* Run business method `buildRelationVO`.
* Call guide: pass the named parameters below from the controller or caller; the method validates them and returns the business result.