|
|
@@ -0,0 +1,199 @@
|
|
|
+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.StoreBookingTable;
|
|
|
+import shop.alien.mapper.StoreBookingTableMapper;
|
|
|
+import shop.alien.store.service.StoreBookingTableService;
|
|
|
+import shop.alien.util.common.JwtUtil;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 预订服务桌号表 服务实现类
|
|
|
+ *
|
|
|
+ * @author system
|
|
|
+ * @since 2025-01-XX
|
|
|
+ */
|
|
|
+@Slf4j
|
|
|
+@Service
|
|
|
+@Transactional
|
|
|
+@RequiredArgsConstructor
|
|
|
+public class StoreBookingTableServiceImpl extends ServiceImpl<StoreBookingTableMapper, StoreBookingTable> implements StoreBookingTableService {
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public List<StoreBookingTable> getTableList(Integer storeId, Integer categoryId) {
|
|
|
+ log.info("StoreBookingTableServiceImpl.getTableList?storeId={}, categoryId={}", storeId, categoryId);
|
|
|
+
|
|
|
+ LambdaQueryWrapper<StoreBookingTable> wrapper = new LambdaQueryWrapper<>();
|
|
|
+ wrapper.eq(StoreBookingTable::getStoreId, storeId);
|
|
|
+
|
|
|
+ // 如果指定了分类ID,则按分类筛选;否则查询全部
|
|
|
+ if (categoryId != null) {
|
|
|
+ wrapper.eq(StoreBookingTable::getCategoryId, categoryId);
|
|
|
+ }
|
|
|
+
|
|
|
+ // 按创建时间倒序排列
|
|
|
+ wrapper.orderByDesc(StoreBookingTable::getCreatedTime);
|
|
|
+
|
|
|
+ return this.list(wrapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean addTable(StoreBookingTable table) {
|
|
|
+ log.info("StoreBookingTableServiceImpl.addTable?table={}", table);
|
|
|
+
|
|
|
+ // 从JWT获取当前登录用户ID
|
|
|
+ Integer userId = getCurrentUserId();
|
|
|
+
|
|
|
+ // 参数验证
|
|
|
+ if (table.getStoreId() == null) {
|
|
|
+ log.warn("新增预订服务桌号失败:门店ID不能为空");
|
|
|
+ throw new RuntimeException("门店ID不能为空");
|
|
|
+ }
|
|
|
+ if (table.getCategoryId() == null) {
|
|
|
+ log.warn("新增预订服务桌号失败:分类ID不能为空");
|
|
|
+ throw new RuntimeException("分类ID不能为空");
|
|
|
+ }
|
|
|
+ if (!StringUtils.hasText(table.getTableNumber())) {
|
|
|
+ log.warn("新增预订服务桌号失败:桌号不能为空");
|
|
|
+ throw new RuntimeException("桌号不能为空");
|
|
|
+ }
|
|
|
+ if (table.getSeatingCapacity() == null || table.getSeatingCapacity() <= 0) {
|
|
|
+ log.warn("新增预订服务桌号失败:座位数必须大于0");
|
|
|
+ throw new RuntimeException("座位数必须大于0");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 处理桌号:去除前后空格
|
|
|
+ String tableNumber = table.getTableNumber().trim();
|
|
|
+ table.setTableNumber(tableNumber);
|
|
|
+
|
|
|
+ // 检查当前门店下该分类是否已存在相同的桌号
|
|
|
+ LambdaQueryWrapper<StoreBookingTable> checkWrapper = new LambdaQueryWrapper<>();
|
|
|
+ checkWrapper.eq(StoreBookingTable::getStoreId, table.getStoreId())
|
|
|
+ .eq(StoreBookingTable::getCategoryId, table.getCategoryId())
|
|
|
+ .eq(StoreBookingTable::getTableNumber, tableNumber);
|
|
|
+ // @TableLogic 会自动过滤已删除的记录(delete_flag=0)
|
|
|
+ StoreBookingTable existingTable = this.getOne(checkWrapper);
|
|
|
+ if (existingTable != null) {
|
|
|
+ log.warn("新增预订服务桌号失败:当前分类下桌号已存在,storeId={}, categoryId={}, tableNumber={}",
|
|
|
+ table.getStoreId(), table.getCategoryId(), tableNumber);
|
|
|
+ throw new RuntimeException("此桌号已存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ table.setCreatedUserId(userId);
|
|
|
+
|
|
|
+ return this.save(table);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean updateTable(StoreBookingTable table) {
|
|
|
+ log.info("StoreBookingTableServiceImpl.updateTable?table={}", table);
|
|
|
+
|
|
|
+ // 从JWT获取当前登录用户ID
|
|
|
+ Integer userId = getCurrentUserId();
|
|
|
+
|
|
|
+ // 参数验证
|
|
|
+ if (table.getId() == null) {
|
|
|
+ log.warn("更新预订服务桌号失败:桌号ID不能为空");
|
|
|
+ throw new RuntimeException("桌号ID不能为空");
|
|
|
+ }
|
|
|
+
|
|
|
+ StoreBookingTable existingTable = this.getById(table.getId());
|
|
|
+ if (existingTable == null) {
|
|
|
+ log.warn("更新预订服务桌号失败:桌号不存在,id={}", table.getId());
|
|
|
+ throw new RuntimeException("桌号不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 如果修改了桌号,检查新桌号是否已存在
|
|
|
+ if (StringUtils.hasText(table.getTableNumber())) {
|
|
|
+ // 处理桌号:去除前后空格
|
|
|
+ String tableNumber = table.getTableNumber().trim();
|
|
|
+ table.setTableNumber(tableNumber);
|
|
|
+
|
|
|
+ // 如果桌号有变化,检查当前门店下该分类是否已存在相同的桌号
|
|
|
+ if (!tableNumber.equals(existingTable.getTableNumber()) ||
|
|
|
+ (table.getCategoryId() != null && !table.getCategoryId().equals(existingTable.getCategoryId()))) {
|
|
|
+ // 确定要检查的分类ID(如果传入了新的分类ID,使用新的;否则使用原有的)
|
|
|
+ Integer checkCategoryId = table.getCategoryId() != null ? table.getCategoryId() : existingTable.getCategoryId();
|
|
|
+
|
|
|
+ LambdaQueryWrapper<StoreBookingTable> wrapper = new LambdaQueryWrapper<>();
|
|
|
+ wrapper.eq(StoreBookingTable::getStoreId, existingTable.getStoreId())
|
|
|
+ .eq(StoreBookingTable::getCategoryId, checkCategoryId)
|
|
|
+ .eq(StoreBookingTable::getTableNumber, tableNumber)
|
|
|
+ .ne(StoreBookingTable::getId, table.getId());
|
|
|
+ // @TableLogic 会自动过滤已删除的记录(delete_flag=0)
|
|
|
+ StoreBookingTable duplicateTable = this.getOne(wrapper);
|
|
|
+ if (duplicateTable != null) {
|
|
|
+ log.warn("更新预订服务桌号失败:当前分类下桌号已存在,storeId={}, categoryId={}, tableNumber={}, id={}",
|
|
|
+ existingTable.getStoreId(), checkCategoryId, tableNumber, table.getId());
|
|
|
+ throw new RuntimeException("此桌号已存在");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ // 验证座位数
|
|
|
+ if (table.getSeatingCapacity() != null && table.getSeatingCapacity() <= 0) {
|
|
|
+ log.warn("更新预订服务桌号失败:座位数必须大于0");
|
|
|
+ throw new RuntimeException("座位数必须大于0");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 更新字段
|
|
|
+ LambdaUpdateWrapper<StoreBookingTable> updateWrapper = new LambdaUpdateWrapper<>();
|
|
|
+ updateWrapper.eq(StoreBookingTable::getId, table.getId());
|
|
|
+
|
|
|
+ if (StringUtils.hasText(table.getTableNumber())) {
|
|
|
+ updateWrapper.set(StoreBookingTable::getTableNumber, table.getTableNumber());
|
|
|
+ }
|
|
|
+ if (table.getCategoryId() != null) {
|
|
|
+ updateWrapper.set(StoreBookingTable::getCategoryId, table.getCategoryId());
|
|
|
+ }
|
|
|
+ if (table.getSeatingCapacity() != null) {
|
|
|
+ updateWrapper.set(StoreBookingTable::getSeatingCapacity, table.getSeatingCapacity());
|
|
|
+ }
|
|
|
+
|
|
|
+ if (userId != null) {
|
|
|
+ updateWrapper.set(StoreBookingTable::getUpdatedUserId, userId);
|
|
|
+ }
|
|
|
+
|
|
|
+ return this.update(updateWrapper);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean deleteTable(Integer id) {
|
|
|
+ log.info("StoreBookingTableServiceImpl.deleteTable?id={}", id);
|
|
|
+
|
|
|
+ StoreBookingTable table = this.getById(id);
|
|
|
+ if (table == null) {
|
|
|
+ log.warn("删除预订服务桌号失败:桌号不存在,id={}", id);
|
|
|
+ throw new RuntimeException("桌号不存在");
|
|
|
+ }
|
|
|
+
|
|
|
+ // 逻辑删除
|
|
|
+ return this.removeById(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 从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;
|
|
|
+ }
|
|
|
+}
|