优化预约表单并支持散客预约

This commit is contained in:
2026-10-20 13:29:55 +08:00
parent 4a7bd90088
commit c952a913e8
17 changed files with 1376 additions and 275 deletions
@@ -16,6 +16,8 @@ public class PlanningAppointmentCreateDTO {
private String appointmentTime;
private String appointmentStartTime;
private Integer durationMinutes;
private BigDecimal amount;
private Boolean createCustomerRecord;
private String manualProductTitle;
private BigDecimal manualProductPrice;
private String manualProductDescription;
@@ -44,6 +44,7 @@ import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.math.BigDecimal;
import java.math.RoundingMode;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
@@ -183,11 +184,18 @@ public class PlanningServiceImpl implements PlanningService {
ensureWithinBusinessHours(shopId, startTime, endTime);
BigDecimal unitPrice = product.getPrice() == null ? BigDecimal.ZERO : product.getPrice();
BigDecimal amount = unitPrice.multiply(BigDecimal.valueOf(quantity));
BigDecimal amount = dto.getAmount() == null
? unitPrice.multiply(BigDecimal.valueOf(quantity))
: dto.getAmount();
if (amount.compareTo(BigDecimal.ZERO) < 0) {
throw new RuntimeException("成交金额不能小于0");
}
OrderDO order = new OrderDO();
order.setOrderNo("ORD" + snowflakeIdGenerator.nextIdString());
order.setUserId(customer.getId());
order.setUserId(customer == null ? null : customer.getId());
order.setCustomerNameSnapshot(customer == null ? trimToNull(dto.getCustomerName()) : null);
order.setCustomerPhoneSnapshot(customer == null ? trimToNull(dto.getPhone()) : null);
order.setShopId(shopId);
order.setTotalAmount(amount);
order.setActualAmount(amount);
@@ -208,7 +216,7 @@ public class PlanningServiceImpl implements PlanningService {
item.setOrderId(order.getId());
item.setProductId(product.getId());
item.setQuantity(quantity);
item.setPrice(unitPrice);
item.setPrice(amount.divide(BigDecimal.valueOf(quantity), 2, RoundingMode.HALF_UP));
item.setArtistId(artist == null ? null : artist.getId());
item.setAppointmentTime(startTime);
item.setIsDelete(0);
@@ -295,7 +303,8 @@ public class PlanningServiceImpl implements PlanningService {
Map<Long, PlanningArtistVO> artistMap = listArtists(order.getShopId(), item.getArtistId()).stream()
.collect(Collectors.toMap(PlanningArtistVO::getId, Function.identity(), (a, b) -> a));
Map<Long, UserDO> customerMap = order.getUserId() == null ? Map.of() : loadUsers(Set.of(order.getUserId()));
return toAppointmentVO(reservation, item, order, product, customerMap.get(order.getUserId()), artistMap);
UserDO customer = order.getUserId() == null ? null : customerMap.get(order.getUserId());
return toAppointmentVO(reservation, item, order, product, customer, artistMap);
}
@Override
@@ -516,7 +525,7 @@ public class PlanningServiceImpl implements PlanningService {
continue;
}
ProductDO product = item == null ? null : productMap.get(item.getProductId());
UserDO customer = customerMap.get(order.getUserId());
UserDO customer = order.getUserId() == null ? null : customerMap.get(order.getUserId());
result.add(toAppointmentVO(reservation, item, order, product, customer, artistMap));
if (reservation.getOrderItemId() != null) {
reservationItemIds.add(reservation.getOrderItemId());
@@ -531,7 +540,7 @@ public class PlanningServiceImpl implements PlanningService {
continue;
}
ProductDO product = productMap.get(item.getProductId());
UserDO customer = customerMap.get(order.getUserId());
UserDO customer = order.getUserId() == null ? null : customerMap.get(order.getUserId());
result.add(toAppointmentVO(null, item, order, product, customer, artistMap));
}
result.sort(Comparator.comparing(PlanningAppointmentVO::getStartTime, Comparator.nullsLast(String::compareTo))
@@ -731,12 +740,19 @@ public class PlanningServiceImpl implements PlanningService {
private ProductDO createManualAppointmentProduct(PlanningAppointmentCreateDTO dto, Long shopId) {
String title = trimToNull(dto.getManualProductTitle());
String imageFileIds = trimToNull(dto.getManualProductImageFileIds());
if (!hasText(title)) {
throw new RuntimeException("请选择预约作品或填写手动作品名称");
title = firstNonBlank(dto.getCustomerName(), hasText(imageFileIds) ? "散客款式" : null);
}
BigDecimal price = dto.getManualProductPrice();
if (price == null || price.compareTo(BigDecimal.ZERO) < 0) {
throw new RuntimeException("手动作品价格不能小于0");
if (!hasText(title) && !hasText(imageFileIds)) {
throw new RuntimeException("请填写美甲款式名称或上传款式图片");
}
BigDecimal price = dto.getManualProductPrice() == null ? dto.getAmount() : dto.getManualProductPrice();
if (price == null) {
throw new RuntimeException("成交金额不能为空");
}
if (price.compareTo(BigDecimal.ZERO) < 0) {
throw new RuntimeException("成交金额不能小于0");
}
int durationMinutes = requireAppointmentDuration(dto.getDurationMinutes());
ProductDO product = new ProductDO();
@@ -746,7 +762,7 @@ public class PlanningServiceImpl implements PlanningService {
product.setTags(trimToNull(dto.getManualProductTags()));
product.setTargetAudience(trimToNull(dto.getManualProductTargetAudience()));
product.setPrecautions(trimToNull(dto.getManualProductPrecautions()));
product.setImageFileIds(trimToNull(dto.getManualProductImageFileIds()));
product.setImageFileIds(imageFileIds);
product.setPrice(price);
product.setLikeCount(0);
product.setCollectCount(0);
@@ -786,11 +802,17 @@ public class PlanningServiceImpl implements PlanningService {
}
return customer;
}
String customerName = firstNonBlank(dto.getCustomerName(), dto.getPhone());
if (!hasText(customerName)) {
throw new RuntimeException("客户姓名不能为空");
}
String customerName = trimToNull(dto.getCustomerName());
String phone = trimToNull(dto.getPhone());
if (!hasText(customerName) && hasText(phone)) {
throw new RuntimeException("填写手机号时请同时填写客户姓名");
}
if (Boolean.FALSE.equals(dto.getCreateCustomerRecord())) {
return null;
}
if (!hasText(customerName)) {
return null;
}
List<com.xiaowang.jayucloud.salon.core.modules.user.vo.UserVO> matches =
userService.findCustomerMatches(customerName, phone, shopId);
if (matches.size() > 1) {
@@ -0,0 +1,348 @@
package com.xiaowang.jayucloud.salon.core.modules.planning.service.impl;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.xiaowang.jayucloud.salon.core.modules.artist.mapper.ArtistScheduleExceptionMapper;
import com.xiaowang.jayucloud.salon.core.modules.artist.mapper.ArtistScheduleMapper;
import com.xiaowang.jayucloud.salon.core.modules.artist.mapper.NailArtistInfoMapper;
import com.xiaowang.jayucloud.salon.core.modules.order.entity.OrderDO;
import com.xiaowang.jayucloud.salon.core.modules.order.entity.OrderItemDO;
import com.xiaowang.jayucloud.salon.core.modules.order.mapper.OrderItemMapper;
import com.xiaowang.jayucloud.salon.core.modules.order.mapper.OrderMapper;
import com.xiaowang.jayucloud.salon.core.modules.order.service.OrderService;
import com.xiaowang.jayucloud.salon.core.modules.order.support.OrderStatus;
import com.xiaowang.jayucloud.salon.core.modules.planning.dto.PlanningAppointmentStatusDTO;
import com.xiaowang.jayucloud.salon.core.modules.planning.dto.PlanningAppointmentCreateDTO;
import com.xiaowang.jayucloud.salon.core.modules.planning.mapper.AppointmentReservationMapper;
import com.xiaowang.jayucloud.salon.core.modules.planning.service.AppointmentCapacityService;
import com.xiaowang.jayucloud.salon.core.modules.product.mapper.ProductMapper;
import com.xiaowang.jayucloud.salon.core.modules.product.entity.ProductDO;
import com.xiaowang.jayucloud.salon.core.modules.security.support.AdminScopeHelper;
import com.xiaowang.jayucloud.salon.core.modules.shop.mapper.ShopMapper;
import com.xiaowang.jayucloud.salon.core.modules.shop.entity.ShopDO;
import com.xiaowang.jayucloud.salon.core.modules.user.entity.UserDO;
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.support.id.TimeOrderedSnowflakeIdGenerator;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.Mock;
import org.mockito.ArgumentCaptor;
import org.mockito.junit.jupiter.MockitoExtension;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class PlanningServiceImplTest {
@Mock private AdminScopeHelper adminScopeHelper;
@Mock private UserMapper userMapper;
@Mock private UserService userService;
@Mock private NailArtistInfoMapper nailArtistInfoMapper;
@Mock private ArtistScheduleMapper artistScheduleMapper;
@Mock private ArtistScheduleExceptionMapper artistScheduleExceptionMapper;
@Mock private AppointmentReservationMapper appointmentReservationMapper;
@Mock private OrderMapper orderMapper;
@Mock private OrderItemMapper orderItemMapper;
@Mock private ProductMapper productMapper;
@Mock private ShopMapper shopMapper;
@Mock private AppointmentCapacityService appointmentCapacityService;
@Mock private OrderService orderService;
@Mock private TimeOrderedSnowflakeIdGenerator snowflakeIdGenerator;
private PlanningServiceImpl service;
@BeforeEach
void setUp() {
service = new PlanningServiceImpl(adminScopeHelper, userMapper, userService, nailArtistInfoMapper, artistScheduleMapper,
artistScheduleExceptionMapper, appointmentReservationMapper, orderMapper, orderItemMapper,
productMapper, shopMapper, appointmentCapacityService, orderService, snowflakeIdGenerator);
}
@Test
void rejectsCompletingUnassignedAppointment() {
UserDO operator = new UserDO();
OrderDO order = order(11L, OrderStatus.PENDING_SERVICE.getCode());
when(orderMapper.selectById(11L)).thenReturn(order);
when(orderItemMapper.selectCount(any(Wrapper.class))).thenReturn(1L);
PlanningAppointmentStatusDTO dto = new PlanningAppointmentStatusDTO();
dto.setStatus(OrderStatus.COMPLETED.getCode());
RuntimeException error = assertThrows(RuntimeException.class, () -> service.updateStatus(operator, 11L, dto));
assertEquals("请先分配员工", error.getMessage());
verify(orderService, never()).processOrder(11L, OrderStatus.COMPLETED.getCode());
}
@Test
void allowsCancellingUnassignedAppointment() {
UserDO operator = new UserDO();
OrderDO order = order(12L, OrderStatus.PENDING_SERVICE.getCode());
when(orderMapper.selectById(12L)).thenReturn(order);
PlanningAppointmentStatusDTO dto = new PlanningAppointmentStatusDTO();
dto.setStatus(OrderStatus.CANCELLED.getCode());
service.updateStatus(operator, 12L, dto);
verify(orderService).processOrder(12L, OrderStatus.CANCELLED.getCode());
}
@Test
void createsUnassignedAppointmentWithoutReservation() {
service = spy(service);
UserDO operator = new UserDO();
operator.setType(2);
UserDO customer = new UserDO();
customer.setId(31L);
customer.setType(0);
ProductDO product = new ProductDO();
product.setId(41L);
product.setShopId(21L);
product.setTitle("纯色美甲");
product.setPrice(java.math.BigDecimal.valueOf(99));
product.setIsDelete(0);
ShopDO shop = new ShopDO();
shop.setId(21L);
shop.setBusinessHours("10:00-22:00");
when(adminScopeHelper.resolveShopScope(operator, null)).thenReturn(21L);
when(productMapper.selectById(41L)).thenReturn(product);
when(userMapper.selectById(31L)).thenReturn(customer);
when(shopMapper.selectById(21L)).thenReturn(shop);
when(snowflakeIdGenerator.nextIdString()).thenReturn("10001");
doAnswer(invocation -> {
OrderDO value = invocation.getArgument(0);
value.setId(51L);
return 1;
}).when(orderMapper).insert(any(OrderDO.class));
doAnswer(invocation -> {
OrderItemDO value = invocation.getArgument(0);
value.setId(61L);
return 1;
}).when(orderItemMapper).insert(any(OrderItemDO.class));
doReturn(new com.xiaowang.jayucloud.salon.core.modules.planning.vo.PlanningAppointmentVO())
.when(service).getAppointment(operator, 61L);
PlanningAppointmentCreateDTO dto = new PlanningAppointmentCreateDTO();
dto.setCustomerUserId(31L);
dto.setProductId(41L);
dto.setAppointmentDate("2026-10-13");
dto.setAppointmentTime("14:00");
dto.setDurationMinutes(60);
service.createAppointment(operator, dto);
ArgumentCaptor<OrderItemDO> itemCaptor = ArgumentCaptor.forClass(OrderItemDO.class);
verify(orderItemMapper).insert(itemCaptor.capture());
assertNull(itemCaptor.getValue().getArtistId());
verify(appointmentCapacityService, never()).reserveConfirmedOrThrow(
any(), any(), any(), any(), any(), any(), any(), any());
}
@Test
void createsWalkInAppointmentWithPhotoOnlyStyleAndSnapshotAmount() {
service = spy(service);
UserDO operator = new UserDO();
operator.setType(2);
ShopDO shop = new ShopDO();
shop.setId(21L);
shop.setBusinessHours("10:00-22:00");
when(adminScopeHelper.resolveShopScope(operator, null)).thenReturn(21L);
when(shopMapper.selectById(21L)).thenReturn(shop);
when(snowflakeIdGenerator.nextIdString()).thenReturn("10002");
doAnswer(invocation -> {
ProductDO value = invocation.getArgument(0);
value.setId(42L);
return 1;
}).when(productMapper).insert(any(ProductDO.class));
doAnswer(invocation -> {
OrderDO value = invocation.getArgument(0);
value.setId(52L);
return 1;
}).when(orderMapper).insert(any(OrderDO.class));
doAnswer(invocation -> {
OrderItemDO value = invocation.getArgument(0);
value.setId(62L);
return 1;
}).when(orderItemMapper).insert(any(OrderItemDO.class));
doReturn(new com.xiaowang.jayucloud.salon.core.modules.planning.vo.PlanningAppointmentVO())
.when(service).getAppointment(operator, 62L);
PlanningAppointmentCreateDTO dto = new PlanningAppointmentCreateDTO();
dto.setCustomerName("小鹿");
dto.setCreateCustomerRecord(false);
dto.setManualProductImageFileIds("file-42");
dto.setAmount(java.math.BigDecimal.valueOf(168));
dto.setAppointmentDate("2026-10-22");
dto.setAppointmentTime("14:00");
dto.setDurationMinutes(60);
service.createAppointment(operator, dto);
ArgumentCaptor<ProductDO> productCaptor = ArgumentCaptor.forClass(ProductDO.class);
verify(productMapper).insert(productCaptor.capture());
assertEquals("小鹿", productCaptor.getValue().getTitle());
assertEquals("file-42", productCaptor.getValue().getImageFileIds());
assertEquals(java.math.BigDecimal.valueOf(168), productCaptor.getValue().getPrice());
ArgumentCaptor<OrderDO> orderCaptor = ArgumentCaptor.forClass(OrderDO.class);
verify(orderMapper).insert(orderCaptor.capture());
assertNull(orderCaptor.getValue().getUserId());
assertEquals("小鹿", orderCaptor.getValue().getCustomerNameSnapshot());
assertEquals(java.math.BigDecimal.valueOf(168), orderCaptor.getValue().getActualAmount());
ArgumentCaptor<OrderItemDO> itemCaptor = ArgumentCaptor.forClass(OrderItemDO.class);
verify(orderItemMapper).insert(itemCaptor.capture());
assertEquals(0, itemCaptor.getValue().getPrice().compareTo(java.math.BigDecimal.valueOf(168)));
verify(userService, never()).createManagerCustomer(any(), any(), any());
}
@Test
void createsWalkInAppointmentWithoutCustomerUsingWalkInStyleTitle() {
service = spy(service);
UserDO operator = new UserDO();
operator.setType(2);
ShopDO shop = new ShopDO();
shop.setId(21L);
shop.setBusinessHours("10:00-22:00");
when(adminScopeHelper.resolveShopScope(operator, null)).thenReturn(21L);
when(shopMapper.selectById(21L)).thenReturn(shop);
when(snowflakeIdGenerator.nextIdString()).thenReturn("10003");
doAnswer(invocation -> {
ProductDO value = invocation.getArgument(0);
value.setId(43L);
return 1;
}).when(productMapper).insert(any(ProductDO.class));
doAnswer(invocation -> {
OrderDO value = invocation.getArgument(0);
value.setId(53L);
return 1;
}).when(orderMapper).insert(any(OrderDO.class));
doAnswer(invocation -> {
OrderItemDO value = invocation.getArgument(0);
value.setId(63L);
return 1;
}).when(orderItemMapper).insert(any(OrderItemDO.class));
doReturn(new com.xiaowang.jayucloud.salon.core.modules.planning.vo.PlanningAppointmentVO())
.when(service).getAppointment(operator, 63L);
PlanningAppointmentCreateDTO dto = new PlanningAppointmentCreateDTO();
dto.setCreateCustomerRecord(false);
dto.setManualProductImageFileIds("file-43");
dto.setAmount(java.math.BigDecimal.valueOf(88));
dto.setAppointmentDate("2026-10-22");
dto.setAppointmentTime("14:00");
dto.setDurationMinutes(45);
service.createAppointment(operator, dto);
ArgumentCaptor<ProductDO> productCaptor = ArgumentCaptor.forClass(ProductDO.class);
verify(productMapper).insert(productCaptor.capture());
assertEquals("散客款式", productCaptor.getValue().getTitle());
ArgumentCaptor<OrderDO> orderCaptor = ArgumentCaptor.forClass(OrderDO.class);
verify(orderMapper).insert(orderCaptor.capture());
assertNull(orderCaptor.getValue().getUserId());
assertNull(orderCaptor.getValue().getCustomerNameSnapshot());
assertNull(orderCaptor.getValue().getCustomerPhoneSnapshot());
verify(userService, never()).createManagerCustomer(any(), any(), any());
}
@Test
void readsWalkInAppointmentCustomerSnapshotWithoutLookingUpNullUserId() {
UserDO operator = new UserDO();
operator.setType(2);
OrderDO order = order(54L, OrderStatus.PENDING_SERVICE.getCode());
order.setCustomerNameSnapshot("散客预约");
order.setCustomerPhoneSnapshot("13800138000");
OrderItemDO item = new OrderItemDO();
item.setId(64L);
item.setOrderId(54L);
item.setQuantity(1);
item.setAppointmentTime(java.time.LocalDateTime.of(2026, 10, 22, 14, 0));
when(orderItemMapper.selectById(64L)).thenReturn(item);
when(orderMapper.selectById(54L)).thenReturn(order);
when(userMapper.selectList(any(Wrapper.class))).thenReturn(java.util.List.of());
com.xiaowang.jayucloud.salon.core.modules.planning.vo.PlanningAppointmentVO result =
service.getAppointment(operator, 64L);
assertNull(result.getCustomerId());
assertEquals("散客预约", result.getCustomerName());
assertEquals("13800138000", result.getCustomerPhone());
verify(userMapper, never()).selectById(org.mockito.ArgumentMatchers.isNull());
}
@Test
void createsCustomerInAppointmentFlowWhenConfirmed() {
service = spy(service);
UserDO operator = new UserDO();
operator.setType(2);
UserDO customer = new UserDO();
customer.setId(71L);
customer.setType(0);
ProductDO product = new ProductDO();
product.setId(41L);
product.setShopId(21L);
product.setTitle("纯色美甲");
product.setPrice(java.math.BigDecimal.valueOf(99));
product.setIsDelete(0);
ShopDO shop = new ShopDO();
shop.setId(21L);
shop.setBusinessHours("10:00-22:00");
when(adminScopeHelper.resolveShopScope(operator, null)).thenReturn(21L);
when(productMapper.selectById(41L)).thenReturn(product);
when(userMapper.selectById(71L)).thenReturn(customer);
when(userService.findCustomerMatches("小周", "13800138000", 21L)).thenReturn(java.util.List.of());
when(userService.createManagerCustomer("小周", "13800138000", 21L)).thenReturn(71L);
when(shopMapper.selectById(21L)).thenReturn(shop);
when(snowflakeIdGenerator.nextIdString()).thenReturn("10004");
doAnswer(invocation -> {
OrderDO value = invocation.getArgument(0);
value.setId(55L);
return 1;
}).when(orderMapper).insert(any(OrderDO.class));
doAnswer(invocation -> {
OrderItemDO value = invocation.getArgument(0);
value.setId(65L);
return 1;
}).when(orderItemMapper).insert(any(OrderItemDO.class));
doReturn(new com.xiaowang.jayucloud.salon.core.modules.planning.vo.PlanningAppointmentVO())
.when(service).getAppointment(operator, 65L);
PlanningAppointmentCreateDTO dto = new PlanningAppointmentCreateDTO();
dto.setCustomerName("小周");
dto.setPhone("13800138000");
dto.setCreateCustomerRecord(true);
dto.setProductId(41L);
dto.setAmount(java.math.BigDecimal.valueOf(128));
dto.setAppointmentDate("2026-10-22");
dto.setAppointmentTime("14:00");
dto.setDurationMinutes(60);
service.createAppointment(operator, dto);
verify(userService).createManagerCustomer("小周", "13800138000", 21L);
ArgumentCaptor<OrderDO> orderCaptor = ArgumentCaptor.forClass(OrderDO.class);
verify(orderMapper).insert(orderCaptor.capture());
assertEquals(71L, orderCaptor.getValue().getUserId());
assertNull(orderCaptor.getValue().getCustomerNameSnapshot());
}
private OrderDO order(Long id, int status) {
OrderDO order = new OrderDO();
order.setId(id);
order.setShopId(21L);
order.setStatus(status);
order.setIsDelete(0);
return order;
}
}