保存客户与美甲师表单改造检查点

This commit is contained in:
2026-10-29 17:02:57 +08:00
parent bb3d00c51e
commit b3a8c7e01c
24 changed files with 425 additions and 60 deletions
@@ -39,6 +39,9 @@ public class NailArtistInfoDTO {
*/
private String skills;
/** 规划页使用的专属颜色。 */
private String color;
/**
* 评分
*/
@@ -37,6 +37,9 @@ public class NailArtistInfoDO extends BaseEntity {
*/
private String skills;
/** 规划页使用的专属颜色。 */
private String color;
/**
* 评分
*/
@@ -331,6 +331,7 @@ public class NailArtistInfoServiceImpl extends ServiceImpl<NailArtistInfoMapper,
if (dto.getIdentity() != null) target.setIdentity(dto.getIdentity());
if (dto.getExperience() != null) target.setExperience(dto.getExperience());
if (dto.getSkills() != null) target.setSkills(dto.getSkills());
if (dto.getColor() != null) target.setColor(dto.getColor());
if (dto.getRating() != null) target.setRating(dto.getRating());
if (dto.getStatus() != null) target.setStatus(dto.getStatus());
if (dto.getParticipateProduction() != null) target.setParticipateProduction(dto.getParticipateProduction());
@@ -167,7 +167,7 @@ public class AdminManagerController {
UserDO currentUser = adminScopeHelper.getCurrentUser(request);
adminScopeHelper.requireShopContext(currentUser, "请先选择店铺后再新增客户");
Long shopId = adminScopeHelper.resolveShopScope(currentUser, null);
return Result.success(userService.createManagerCustomer(dto.getNickname(), dto.getPhone(), shopId));
return Result.success(userService.createManagerCustomer(dto.getNickname(), dto.getWechatId(), dto.getPhone(), shopId));
}
@GetMapping("/employees")
@@ -10,5 +10,7 @@ public class ManagerCustomerCreateDTO {
@NotBlank(message = "客户姓名不能为空")
private String nickname;
private String wechatId;
private String phone;
}
@@ -406,7 +406,8 @@ public class PlanningServiceImpl implements PlanningService {
vo.setShortName(PlanningCalendarSupport.shortName(vo.getName()));
vo.setPhone(firstNonBlank(user.getContactPhone(), user.getPhone(), ""));
vo.setAvatar(user.getAvatar());
vo.setColor(colorForArtist(user.getId(), i));
vo.setColor(info != null && StringUtils.hasText(info.getColor())
? info.getColor() : colorForArtist(user.getId(), i));
vo.setStatus(info == null || info.getStatus() == null ? user.getStatus() : info.getStatus());
vo.setStatusText(Integer.valueOf(0).equals(vo.getStatus()) ? "休息" : "可预约");
vo.setWorkStartTime(info == null || info.getWorkStartTime() == null ? null : info.getWorkStartTime().toString());
@@ -88,6 +88,9 @@ public class UserDTO {
*/
private String wechatId;
/** 美甲师在规划页展示的专属颜色(#RRGGBB)。 */
private String color;
/**
* 微信二维码
*/
@@ -73,6 +73,10 @@ public class UserDO extends BaseEntity {
@TableField(value = "contact_phone_hash", fill = FieldFill.INSERT_UPDATE)
private String contactPhoneHash;
/** 客户姓名、微信号和手机号的组合HMAC,用于原子去重。 */
@TableField("customer_identity_hash")
private String customerIdentityHash;
/**
* 生日
*/
@@ -22,7 +22,7 @@ public interface UserMapper extends BaseMapper<UserDO> {
@Select("SELECT id FROM t_user WHERE account = #{account} AND account_hash IS NULL AND is_delete = 0 LIMIT 1")
Long findLegacyIdByAccount(@Param("account") String account);
@Select("SELECT id FROM t_user WHERE phone = #{phone} AND phone_hash IS NULL AND is_delete = 0 LIMIT 1")
@Select("SELECT id FROM t_user WHERE phone = #{phone} AND phone_hash IS NULL AND is_delete = 0 AND type <> 0 LIMIT 1")
Long findLegacyIdByPhone(@Param("phone") String phone);
@Select("SELECT id FROM t_user WHERE contact_phone = #{phone} AND contact_phone_hash IS NULL AND is_delete = 0 LIMIT 1")
@@ -36,6 +36,8 @@ public interface UserService extends IService<UserDO> {
Long createManagerCustomer(String nickname, String phone, Long shopId);
Long createManagerCustomer(String nickname, String wechatId, String phone, Long shopId);
List<UserVO> findCustomerMatches(String nickname, String phone, Long shopId);
@@ -389,12 +389,13 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
List<Long> validatedShopIds = resolveValidatedShopIds(userDTO.getShopIds(), userDTO.getShopId());
Long validatedShopId = pickPrimaryShopId(validatedShopIds);
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 validatedPhone = Integer.valueOf(0).equals(userType)
? validateCustomerPhone(userDTO.getPhone(), !(allowMissingCustomerPhone && Integer.valueOf(0).equals(userType)))
: validateUserPhone(userDTO.getPhone(), null, true);
String validatedWechatId = UserProfileFieldSupport.normalizeOptionalText(userDTO.getWechatId());
if (Integer.valueOf(0).equals(userType)) {
ensureCustomerUnique(userDTO.getNickname(), validatedWechatId, validatedPhone);
}
UserProfileFieldSupport.requireWechatIdForServiceUser(userType, validatedWechatId);
/**
@@ -409,7 +410,8 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
// 创建用户实体并设置属性
UserDO userDO = new UserDO();
userDO.setShopId(validatedShopId);
userDO.setNickname(userDTO.getNickname());
userDO.setNickname(Integer.valueOf(0).equals(userType) && userDTO.getNickname() != null
? userDTO.getNickname().trim() : userDTO.getNickname());
userDO.setAccount(validatedAccount);
String resolvedPassword = AdminDefaultPasswordSupport.resolveCreatePassword(
userDTO.getPassword(),
@@ -418,6 +420,9 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
validatePasswordStrength(resolvedPassword);
userDO.setPassword(passwordEncoder.encode(resolvedPassword));
userDO.setPhone(validatedPhone);
if (Integer.valueOf(0).equals(userType)) {
userDO.setCustomerIdentityHash(customerIdentityHash(userDO.getNickname(), validatedWechatId, validatedPhone));
}
userDO.setContactPhone(UserProfileFieldSupport.normalizeOptionalText(userDTO.getContactPhone()));
userDO.setBirthday(userDTO.getBirthday());
userDO.setWechatId(validatedWechatId);
@@ -490,6 +495,7 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
nailArtistInfoDTO.setRating(DEFAULT_NAIL_ARTIST_RATING); // 默认评分为4.5
nailArtistInfoDTO.setStatus(1); // 默认状态为营业中
nailArtistInfoDTO.setPortfolio(""); // 默认作品集为空
nailArtistInfoDTO.setColor(normalizeArtistColor(userDTO.getColor()));
nailArtistInfoService.saveOrUpdateNailArtistInfo(nailArtistInfoDTO);
}
@@ -499,6 +505,12 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
@Override
@Transactional(rollbackFor = Exception.class)
public Long createManagerCustomer(String nickname, String phone, Long shopId) {
return createManagerCustomer(nickname, null, phone, shopId);
}
@Override
@Transactional(rollbackFor = Exception.class)
public Long createManagerCustomer(String nickname, String wechatId, String phone, Long shopId) {
if (!StringUtils.hasText(nickname)) {
throw new RuntimeException("客户姓名不能为空");
}
@@ -506,15 +518,15 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
throw new RuntimeException("请先选择店铺后再新增客户");
}
String normalizedPhone = phone == null || phone.trim().isEmpty() ? null : phone.trim();
ensureCustomerUnique(nickname, normalizedPhone);
normalizedPhone = validateUserPhone(normalizedPhone, null, false);
normalizedPhone = validateCustomerPhone(normalizedPhone, false);
String normalizedWechatId = UserProfileFieldSupport.normalizeOptionalText(wechatId);
ensureCustomerUnique(nickname, normalizedWechatId, normalizedPhone);
UserDTO customer = new UserDTO();
customer.setNickname(nickname.trim());
customer.setWechatId(normalizedWechatId);
customer.setPhone(normalizedPhone);
customer.setContactPhone(normalizedPhone);
customer.setAccount(normalizedPhone == null
? "customer" + UUID.randomUUID().toString().replace("-", "")
: normalizedPhone);
customer.setAccount("customer" + UUID.randomUUID().toString().replace("-", ""));
customer.setType(0);
customer.setShopId(shopId);
customer.setShopIds(List.of(shopId));
@@ -532,8 +544,9 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
.eq(UserDO::getNickname, nickname.trim())
.eq(UserDO::getType, 0)
.eq(UserDO::getIsDelete, 0);
if (StringUtils.hasText(phone)) {
String normalizedPhone = phone.trim();
String normalizedName = nickname.trim();
String normalizedPhone = StringUtils.hasText(phone) ? phone.trim() : null;
if (normalizedPhone != null) {
addPhoneMatch(wrapper, normalizedPhone);
}
if (shopId != null && shopId > 0) {
@@ -545,7 +558,10 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
.or().in(UserDO::getId, shopUserIds));
}
}
return userMapper.selectList(wrapper).stream().map(user -> {
return userMapper.selectList(wrapper).stream()
.filter(user -> java.util.Objects.equals(normalizedName, user.getNickname()))
.filter(user -> normalizedPhone == null || java.util.Objects.equals(normalizedPhone, decryptPhone(user.getPhone())))
.map(user -> {
UserVO customer = new UserVO();
customer.setId(user.getId());
customer.setNickname(user.getNickname());
@@ -641,7 +657,8 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
userDO.setShopId(validateAndNormalizeShopId(userDTO.getShopId()));
}
if (userDTO.getNickname() != null) {
userDO.setNickname(userDTO.getNickname());
userDO.setNickname(Integer.valueOf(0).equals(userDO.getType())
? userDTO.getNickname().trim() : userDTO.getNickname());
}
if (userDTO.getPassword() != null) {
validatePasswordStrength(userDTO.getPassword());
@@ -710,6 +727,13 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
}
Integer targetUserType = userDO.getType();
UserProfileFieldSupport.requireWechatIdForServiceUser(targetUserType, userDO.getWechatId());
if (Integer.valueOf(0).equals(targetUserType)) {
String customerPhone = validateCustomerPhone(userDO.getPhone(), false);
ensureCustomerUnique(userDO.getNickname(), userDO.getWechatId(), customerPhone, id);
userDO.setCustomerIdentityHash(customerIdentityHash(userDO.getNickname(), userDO.getWechatId(), customerPhone));
} else {
userDO.setCustomerIdentityHash(null);
}
List<Long> validatedShopIds = null;
boolean shopBindingChanged = userDTO.getShopIds() != null || userDTO.getShopId() != null;
if (shopBindingChanged) {
@@ -3229,7 +3253,11 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
}
}
private void ensureCustomerUnique(String nickname, String phone) {
private void ensureCustomerUnique(String nickname, String wechatId, String phone) {
ensureCustomerUnique(nickname, wechatId, phone, null);
}
private void ensureCustomerUnique(String nickname, String wechatId, String phone, Long ignoredCustomerId) {
if (!StringUtils.hasText(nickname)) {
throw new RuntimeException("客户姓名不能为空");
}
@@ -3237,14 +3265,57 @@ public class UserServiceImpl extends ServiceImpl<UserMapper, UserDO> implements
.eq(UserDO::getNickname, nickname.trim())
.eq(UserDO::getType, 0)
.eq(UserDO::getIsDelete, 0);
if (StringUtils.hasText(phone)) {
addPhoneMatch(wrapper, phone);
}
if (userMapper.selectCount(wrapper) > 0) {
String normalizedWechatId = UserProfileFieldSupport.normalizeOptionalText(wechatId);
String normalizedPhone = StringUtils.hasText(phone) ? phone.trim() : null;
boolean duplicate = userMapper.selectList(wrapper).stream().anyMatch(existing ->
!java.util.Objects.equals(ignoredCustomerId, existing.getId())
&& java.util.Objects.equals(normalizedWechatId, UserProfileFieldSupport.normalizeOptionalText(existing.getWechatId()))
&& java.util.Objects.equals(normalizedPhone, decryptPhone(existing.getPhone())));
if (duplicate) {
throw new RuntimeException("客户已存在,请选择已有客户");
}
}
private String decryptPhone(String value) {
return StringUtils.hasText(value) ? phoneCrypto.decrypt(value).trim() : null;
}
private String validateCustomerPhone(String phone, boolean required) {
String normalizedPhone = phone == null ? null : phone.trim();
if (!StringUtils.hasText(normalizedPhone)) {
if (required) throw new RuntimeException("手机号不能为空");
return null;
}
if (!normalizedPhone.matches(PHONE_REGEX)) {
throw new RuntimeException("手机号格式不正确");
}
return normalizedPhone;
}
private String customerIdentityHash(String nickname, String wechatId, String phone) {
String normalizedName = StringUtils.hasText(nickname) ? nickname.trim() : "";
String normalizedWechatId = UserProfileFieldSupport.normalizeOptionalText(wechatId);
String normalizedPhone = StringUtils.hasText(phone) ? phone.trim() : null;
return phoneCrypto.index(identityPart(normalizedName)
+ identityPart(normalizedWechatId == null ? "" : normalizedWechatId)
+ identityPart(normalizedPhone == null ? "" : normalizedPhone));
}
private String identityPart(String value) {
return value.length() + ":" + value;
}
private String normalizeArtistColor(String color) {
if (!StringUtils.hasText(color)) {
return null;
}
String value = color.trim().toUpperCase(java.util.Locale.ROOT);
if (!value.matches("#[0-9A-F]{6}")) {
throw new RuntimeException("美甲师专属颜色格式不正确");
}
return value;
}
private void addPhoneMatch(LambdaQueryWrapper<UserDO> wrapper, String phone) {
String hash = phoneCrypto.index(phone.trim());
List<Long> legacyIds = new ArrayList<>();
@@ -0,0 +1,110 @@
-- 客户允许重名及重复手机号;仅姓名、微信号、手机号组合完全相同时拒绝创建。
-- 美甲师专属颜色持久化到资料表,规划页与员工页面共用同一颜色。
DROP PROCEDURE IF EXISTS salon_v8_add_column_if_missing;
DROP PROCEDURE IF EXISTS salon_v8_add_index_if_missing;
DROP PROCEDURE IF EXISTS salon_v8_add_unique_index_if_missing;
DROP PROCEDURE IF EXISTS salon_v8_drop_index_if_exists;
DELIMITER $$
CREATE PROCEDURE salon_v8_add_column_if_missing(
IN p_table_name VARCHAR(64),
IN p_column_name VARCHAR(64),
IN p_definition TEXT
)
BEGIN
IF NOT EXISTS (
SELECT 1 FROM information_schema.COLUMNS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = p_table_name
AND COLUMN_NAME = p_column_name
) THEN
SET @salon_v8_sql = CONCAT('ALTER TABLE `', p_table_name, '` ADD COLUMN `', p_column_name, '` ', p_definition);
PREPARE salon_v8_stmt FROM @salon_v8_sql;
EXECUTE salon_v8_stmt;
DEALLOCATE PREPARE salon_v8_stmt;
END IF;
END$$
CREATE PROCEDURE salon_v8_add_index_if_missing(
IN p_table_name VARCHAR(64),
IN p_index_name VARCHAR(64),
IN p_columns TEXT
)
BEGIN
IF NOT EXISTS (
SELECT 1 FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = p_table_name
AND INDEX_NAME = p_index_name
) THEN
SET @salon_v8_sql = CONCAT('ALTER TABLE `', p_table_name, '` ADD INDEX `', p_index_name, '` ', p_columns);
PREPARE salon_v8_stmt FROM @salon_v8_sql;
EXECUTE salon_v8_stmt;
DEALLOCATE PREPARE salon_v8_stmt;
END IF;
END$$
CREATE PROCEDURE salon_v8_add_unique_index_if_missing(
IN p_table_name VARCHAR(64),
IN p_index_name VARCHAR(64),
IN p_columns TEXT
)
BEGIN
IF NOT EXISTS (
SELECT 1 FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = p_table_name
AND INDEX_NAME = p_index_name
) THEN
SET @salon_v8_sql = CONCAT('ALTER TABLE `', p_table_name, '` ADD UNIQUE INDEX `', p_index_name, '` ', p_columns);
PREPARE salon_v8_stmt FROM @salon_v8_sql;
EXECUTE salon_v8_stmt;
DEALLOCATE PREPARE salon_v8_stmt;
END IF;
END$$
CREATE PROCEDURE salon_v8_drop_index_if_exists(
IN p_table_name VARCHAR(64),
IN p_index_name VARCHAR(64)
)
BEGIN
IF EXISTS (
SELECT 1 FROM information_schema.STATISTICS
WHERE TABLE_SCHEMA = DATABASE()
AND TABLE_NAME = p_table_name
AND INDEX_NAME = p_index_name
) THEN
SET @salon_v8_sql = CONCAT('ALTER TABLE `', p_table_name, '` DROP INDEX `', p_index_name, '`');
PREPARE salon_v8_stmt FROM @salon_v8_sql;
EXECUTE salon_v8_stmt;
DEALLOCATE PREPARE salon_v8_stmt;
END IF;
END$$
DELIMITER ;
CALL salon_v8_add_column_if_missing(
't_user', 'customer_identity_hash',
'char(64) NULL COMMENT ''客户姓名、微信号和手机号组合HMAC'''
);
CALL salon_v8_add_column_if_missing(
't_user', 'active_customer_identity_hash',
'char(64) GENERATED ALWAYS AS (CASE WHEN type = 0 AND COALESCE(is_delete, 0) = 0 THEN customer_identity_hash ELSE NULL END) STORED COMMENT ''有效客户组合身份索引'''
);
CALL salon_v8_add_column_if_missing(
't_nail_artist_info', 'color',
'varchar(7) NULL COMMENT ''美甲师规划专属颜色'''
);
CALL salon_v8_drop_index_if_exists('t_user', 'uk_user_phone_hash');
CALL salon_v8_add_index_if_missing('t_user', 'idx_user_phone_hash', '(`phone_hash`)');
CALL salon_v8_add_unique_index_if_missing(
't_user', 'uk_user_active_customer_identity_hash', '(`active_customer_identity_hash`)'
);
DROP PROCEDURE IF EXISTS salon_v8_add_column_if_missing;
DROP PROCEDURE IF EXISTS salon_v8_add_index_if_missing;
DROP PROCEDURE IF EXISTS salon_v8_add_unique_index_if_missing;
DROP PROCEDURE IF EXISTS salon_v8_drop_index_if_exists;