|
|
@@ -0,0 +1,158 @@
|
|
|
+package shop.alien.store.service.impl;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
|
|
|
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
|
|
|
+import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
|
|
|
+import lombok.RequiredArgsConstructor;
|
|
|
+import lombok.extern.slf4j.Slf4j;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.transaction.annotation.Transactional;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+import shop.alien.entity.store.StoreBookingSettings;
|
|
|
+import shop.alien.mapper.StoreBookingSettingsMapper;
|
|
|
+import shop.alien.store.service.StoreBookingSettingsService;
|
|
|
+import shop.alien.util.common.JwtUtil;
|
|
|
+
|
|
|
+import java.util.regex.Pattern;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 预订服务信息设置表 服务实现类
|
|
|
+ *
|
|
|
+ * @author system
|
|
|
+ * @since 2025-01-XX
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+@Transactional
|
|
|
+@RequiredArgsConstructor
|
|
|
+public class StoreBookingSettingsServiceImpl extends ServiceImpl<StoreBookingSettingsMapper, StoreBookingSettings> implements StoreBookingSettingsService {
|
|
|
+
|
|
|
+ // 时间格式正则:HH:mm
|
|
|
+ private static final Pattern TIME_PATTERN = Pattern.compile("^([0-1][0-9]|2[0-3]):[0-5][0-9]$");
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public StoreBookingSettings getByStoreId(Integer storeId) {
|
|
|
+ log.info("StoreBookingSettingsServiceImpl.getByStoreId?storeId={}", storeId);
|
|
|
+
|
|
|
+ if (storeId == null) {
|
|
|
+ log.warn("查询预订服务信息设置失败:门店ID不能为空");
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ LambdaQueryWrapper<StoreBookingSettings> wrapper = new LambdaQueryWrapper<>();
|
|
|
+ wrapper.eq(StoreBookingSettings::getStoreId, storeId);
|
|
|
+ // @TableLogic 会自动过滤已删除的记录(delete_flag=0)
|
|
|
+
|
|
|
+ return this.getOne(wrapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean saveOrUpdateSettings(StoreBookingSettings settings) {
|
|
|
+ log.info("StoreBookingSettingsServiceImpl.saveOrUpdateSettings?settings={}", settings);
|
|
|
+
|
|
|
+ // 从JWT获取当前登录用户ID
|
|
|
+ Integer userId = getCurrentUserId();
|
|
|
+
|
|
|
+ // 参数验证
|
|
|
+ if (settings.getStoreId() == null) {
|
|
|
+ log.warn("保存预订服务信息设置失败:门店ID不能为空");
|
|
|
+ throw new RuntimeException("门店ID不能为空");
|
|
|
+ }
|
|
|
+ if (settings.getRetainPositionFlag() == null) {
|
|
|
+ log.warn("保存预订服务信息设置失败:未按时到店是否保留位置不能为空");
|
|
|
+ throw new RuntimeException("未按时到店是否保留位置不能为空");
|
|
|
+ }
|
|
|
+ if (settings.getRetentionDuration() == null || settings.getRetentionDuration() <= 0) {
|
|
|
+ log.warn("保存预订服务信息设置失败:保留时长必须大于0");
|
|
|
+ throw new RuntimeException("保留时长必须大于0");
|
|
|
+ }
|
|
|
+ if (settings.getBookingDateDisplayDays() == null || settings.getBookingDateDisplayDays() <= 0) {
|
|
|
+ log.warn("保存预订服务信息设置失败:预订日期显示天数必须大于0");
|
|
|
+ throw new RuntimeException("预订日期显示天数必须大于0");
|
|
|
+ }
|
|
|
+ if (settings.getBookingTimeType() == null) {
|
|
|
+ log.warn("保存预订服务信息设置失败:预订时间类型不能为空");
|
|
|
+ throw new RuntimeException("预订时间类型不能为空");
|
|
|
+ }
|
|
|
+ if (settings.getMaxCapacityPerSlot() == null || settings.getMaxCapacityPerSlot() <= 0) {
|
|
|
+ log.warn("保存预订服务信息设置失败:单时段最大容纳人数必须大于0");
|
|
|
+ throw new RuntimeException("单时段最大容纳人数必须大于0");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果选择非全天,必须填写开始时间和结束时间
|
|
|
+ if (settings.getBookingTimeType() != null && settings.getBookingTimeType() == 0) {
|
|
|
+ if (!StringUtils.hasText(settings.getBookingStartTime())) {
|
|
|
+ log.warn("保存预订服务信息设置失败:非全天时必须填写开始时间");
|
|
|
+ throw new RuntimeException("非全天时必须填写开始时间");
|
|
|
+ }
|
|
|
+ if (!StringUtils.hasText(settings.getBookingEndTime())) {
|
|
|
+ log.warn("保存预订服务信息设置失败:非全天时必须填写结束时间");
|
|
|
+ throw new RuntimeException("非全天时必须填写结束时间");
|
|
|
+ }
|
|
|
+ // 验证时间格式
|
|
|
+ if (!TIME_PATTERN.matcher(settings.getBookingStartTime()).matches()) {
|
|
|
+ log.warn("保存预订服务信息设置失败:开始时间格式不正确,应为HH:mm格式");
|
|
|
+ throw new RuntimeException("开始时间格式不正确,应为HH:mm格式");
|
|
|
+ }
|
|
|
+ if (!TIME_PATTERN.matcher(settings.getBookingEndTime()).matches()) {
|
|
|
+ log.warn("保存预订服务信息设置失败:结束时间格式不正确,应为HH:mm格式");
|
|
|
+ throw new RuntimeException("结束时间格式不正确,应为HH:mm格式");
|
|
|
+ }
|
|
|
+ // 验证开始时间必须小于结束时间
|
|
|
+ if (settings.getBookingStartTime().compareTo(settings.getBookingEndTime()) >= 0) {
|
|
|
+ log.warn("保存预订服务信息设置失败:开始时间必须小于结束时间");
|
|
|
+ throw new RuntimeException("开始时间必须小于结束时间");
|
|
|
+ }
|
|
|
+ } else {
|
|
|
+ // 如果是全天,清空开始时间和结束时间
|
|
|
+ settings.setBookingStartTime(null);
|
|
|
+ settings.setBookingEndTime(null);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 查询是否已存在该门店的设置
|
|
|
+ StoreBookingSettings existingSettings = getByStoreId(settings.getStoreId());
|
|
|
+
|
|
|
+ if (existingSettings != null) {
|
|
|
+ // 更新
|
|
|
+ settings.setId(existingSettings.getId());
|
|
|
+ LambdaUpdateWrapper<StoreBookingSettings> updateWrapper = new LambdaUpdateWrapper<>();
|
|
|
+ updateWrapper.eq(StoreBookingSettings::getId, existingSettings.getId());
|
|
|
+
|
|
|
+ updateWrapper.set(StoreBookingSettings::getRetainPositionFlag, settings.getRetainPositionFlag());
|
|
|
+ updateWrapper.set(StoreBookingSettings::getRetentionDuration, settings.getRetentionDuration());
|
|
|
+ updateWrapper.set(StoreBookingSettings::getBookingDateDisplayDays, settings.getBookingDateDisplayDays());
|
|
|
+ updateWrapper.set(StoreBookingSettings::getBookingTimeType, settings.getBookingTimeType());
|
|
|
+ updateWrapper.set(StoreBookingSettings::getBookingStartTime, settings.getBookingStartTime());
|
|
|
+ updateWrapper.set(StoreBookingSettings::getBookingEndTime, settings.getBookingEndTime());
|
|
|
+ updateWrapper.set(StoreBookingSettings::getMaxCapacityPerSlot, settings.getMaxCapacityPerSlot());
|
|
|
+
|
|
|
+ if (userId != null) {
|
|
|
+ updateWrapper.set(StoreBookingSettings::getUpdatedUserId, userId);
|
|
|
+ }
|
|
|
+
|
|
|
+ return this.update(updateWrapper);
|
|
|
+ } else {
|
|
|
+ // 新增
|
|
|
+ settings.setCreatedUserId(userId);
|
|
|
+ return this.save(settings);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从JWT获取当前登录用户ID
|
|
|
+ *
|
|
|
+ * @return 用户ID,如果未登录返回null
|
|
|
+ */
|
|
|
+ private Integer getCurrentUserId() {
|
|
|
+ try {
|
|
|
+ JSONObject userInfo = JwtUtil.getCurrentUserInfo();
|
|
|
+ if (userInfo != null) {
|
|
|
+ return userInfo.getInteger("userId");
|
|
|
+ }
|
|
|
+ } catch (Exception e) {
|
|
|
+ log.warn("获取当前登录用户ID失败: {}", e.getMessage());
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+}
|