|
@@ -0,0 +1,246 @@
|
|
|
|
|
+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.StoreBookingCategory;
|
|
|
|
|
+import shop.alien.mapper.StoreBookingCategoryMapper;
|
|
|
|
|
+import shop.alien.store.service.StoreBookingCategoryService;
|
|
|
|
|
+import shop.alien.util.common.JwtUtil;
|
|
|
|
|
+
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+
|
|
|
|
|
+/**
|
|
|
|
|
+ * 预订服务分类表 服务实现类
|
|
|
|
|
+ *
|
|
|
|
|
+ * @author system
|
|
|
|
|
+ * @since 2025-01-XX
|
|
|
|
|
+ */
|
|
|
|
|
+@Slf4j
|
|
|
|
|
+@Service
|
|
|
|
|
+@Transactional
|
|
|
|
|
+@RequiredArgsConstructor
|
|
|
|
|
+public class StoreBookingCategoryServiceImpl extends ServiceImpl<StoreBookingCategoryMapper, StoreBookingCategory> implements StoreBookingCategoryService {
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public List<StoreBookingCategory> getCategoryList(Integer storeId) {
|
|
|
|
|
+ log.info("StoreBookingCategoryServiceImpl.getCategoryList?storeId={}", storeId);
|
|
|
|
|
+
|
|
|
|
|
+ LambdaQueryWrapper<StoreBookingCategory> wrapper = new LambdaQueryWrapper<>();
|
|
|
|
|
+ wrapper.eq(StoreBookingCategory::getStoreId, storeId)
|
|
|
|
|
+ .orderByAsc(StoreBookingCategory::getSort) // 按排序字段升序
|
|
|
|
|
+ .orderByDesc(StoreBookingCategory::getCreatedTime); // 如果排序字段相同,按创建时间倒序
|
|
|
|
|
+
|
|
|
|
|
+ return this.list(wrapper);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public boolean addCategory(StoreBookingCategory category) {
|
|
|
|
|
+ log.info("StoreBookingCategoryServiceImpl.addCategory?category={}", category);
|
|
|
|
|
+
|
|
|
|
|
+ // 从JWT获取当前登录用户ID
|
|
|
|
|
+ Integer userId = getCurrentUserId();
|
|
|
|
|
+
|
|
|
|
|
+ // 参数验证
|
|
|
|
|
+ if (category.getStoreId() == null) {
|
|
|
|
|
+ log.warn("新增预订服务分类失败:门店ID不能为空");
|
|
|
|
|
+ throw new RuntimeException("门店ID不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!StringUtils.hasText(category.getCategoryName())) {
|
|
|
|
|
+ log.warn("新增预订服务分类失败:分类名称不能为空");
|
|
|
|
|
+ throw new RuntimeException("分类名称不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (!StringUtils.hasText(category.getFloorPlanImages())) {
|
|
|
|
|
+ log.warn("新增预订服务分类失败:平面图不能为空");
|
|
|
|
|
+ throw new RuntimeException("平面图不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+ if (category.getMaxBookingTime() == null || category.getMaxBookingTime() <= 0) {
|
|
|
|
|
+ log.warn("新增预订服务分类失败:最长预订时间必须大于0");
|
|
|
|
|
+ throw new RuntimeException("最长预订时间必须大于0");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 验证图片数量(最多9张)
|
|
|
|
|
+ String[] images = category.getFloorPlanImages().split(",");
|
|
|
|
|
+ if (images.length > 9) {
|
|
|
|
|
+ log.warn("新增预订服务分类失败:平面图最多9张");
|
|
|
|
|
+ throw new RuntimeException("平面图最多9张");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 检查分类名称是否已存在
|
|
|
|
|
+ LambdaQueryWrapper<StoreBookingCategory> checkWrapper = new LambdaQueryWrapper<>();
|
|
|
|
|
+ checkWrapper.eq(StoreBookingCategory::getStoreId, category.getStoreId())
|
|
|
|
|
+ .eq(StoreBookingCategory::getCategoryName, category.getCategoryName());
|
|
|
|
|
+ StoreBookingCategory existingCategory = this.getOne(checkWrapper);
|
|
|
|
|
+ if (existingCategory != null) {
|
|
|
|
|
+ log.warn("新增预订服务分类失败:分类名称已存在,categoryName={}", category.getCategoryName());
|
|
|
|
|
+ throw new RuntimeException("分类名称已存在:" + category.getCategoryName());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 查询当前最大的排序值
|
|
|
|
|
+ LambdaQueryWrapper<StoreBookingCategory> maxSortWrapper = new LambdaQueryWrapper<>();
|
|
|
|
|
+ maxSortWrapper.eq(StoreBookingCategory::getStoreId, category.getStoreId())
|
|
|
|
|
+ .orderByDesc(StoreBookingCategory::getSort)
|
|
|
|
|
+ .last("LIMIT 1");
|
|
|
|
|
+ StoreBookingCategory maxSortCategory = this.getOne(maxSortWrapper);
|
|
|
|
|
+ int maxSort = (maxSortCategory != null && maxSortCategory.getSort() != null) ? maxSortCategory.getSort() : 0;
|
|
|
|
|
+
|
|
|
|
|
+ // 设置默认值
|
|
|
|
|
+ if (category.getIsDisplay() == null) {
|
|
|
|
|
+ category.setIsDisplay(1); // 默认显示
|
|
|
|
|
+ }
|
|
|
|
|
+ if (category.getSort() == null) {
|
|
|
|
|
+ category.setSort(maxSort + 1); // 设置排序值
|
|
|
|
|
+ }
|
|
|
|
|
+ category.setCreatedUserId(userId);
|
|
|
|
|
+
|
|
|
|
|
+ return this.save(category);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public boolean updateCategory(StoreBookingCategory category) {
|
|
|
|
|
+ log.info("StoreBookingCategoryServiceImpl.updateCategory?category={}", category);
|
|
|
|
|
+
|
|
|
|
|
+ // 从JWT获取当前登录用户ID
|
|
|
|
|
+ Integer userId = getCurrentUserId();
|
|
|
|
|
+
|
|
|
|
|
+ // 参数验证
|
|
|
|
|
+ if (category.getId() == null) {
|
|
|
|
|
+ log.warn("更新预订服务分类失败:分类ID不能为空");
|
|
|
|
|
+ throw new RuntimeException("分类ID不能为空");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ StoreBookingCategory existingCategory = this.getById(category.getId());
|
|
|
|
|
+ if (existingCategory == null) {
|
|
|
|
|
+ log.warn("更新预订服务分类失败:分类不存在,id={}", category.getId());
|
|
|
|
|
+ throw new RuntimeException("分类不存在");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 如果修改了分类名称,检查新分类名称是否已存在
|
|
|
|
|
+ if (StringUtils.hasText(category.getCategoryName()) &&
|
|
|
|
|
+ !category.getCategoryName().equals(existingCategory.getCategoryName())) {
|
|
|
|
|
+ LambdaQueryWrapper<StoreBookingCategory> wrapper = new LambdaQueryWrapper<>();
|
|
|
|
|
+ wrapper.eq(StoreBookingCategory::getStoreId, existingCategory.getStoreId())
|
|
|
|
|
+ .eq(StoreBookingCategory::getCategoryName, category.getCategoryName())
|
|
|
|
|
+ .ne(StoreBookingCategory::getId, category.getId());
|
|
|
|
|
+ StoreBookingCategory duplicateCategory = this.getOne(wrapper);
|
|
|
|
|
+ if (duplicateCategory != null) {
|
|
|
|
|
+ log.warn("更新预订服务分类失败:分类名称已存在,categoryName={}", category.getCategoryName());
|
|
|
|
|
+ throw new RuntimeException("分类名称已存在:" + category.getCategoryName());
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 验证图片数量(最多9张)
|
|
|
|
|
+ if (StringUtils.hasText(category.getFloorPlanImages())) {
|
|
|
|
|
+ String[] images = category.getFloorPlanImages().split(",");
|
|
|
|
|
+ if (images.length > 9) {
|
|
|
|
|
+ log.warn("更新预订服务分类失败:平面图最多9张");
|
|
|
|
|
+ throw new RuntimeException("平面图最多9张");
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 验证最长预订时间
|
|
|
|
|
+ if (category.getMaxBookingTime() != null && category.getMaxBookingTime() <= 0) {
|
|
|
|
|
+ log.warn("更新预订服务分类失败:最长预订时间必须大于0");
|
|
|
|
|
+ throw new RuntimeException("最长预订时间必须大于0");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 更新字段
|
|
|
|
|
+ LambdaUpdateWrapper<StoreBookingCategory> updateWrapper = new LambdaUpdateWrapper<>();
|
|
|
|
|
+ updateWrapper.eq(StoreBookingCategory::getId, category.getId());
|
|
|
|
|
+
|
|
|
|
|
+ if (StringUtils.hasText(category.getCategoryName())) {
|
|
|
|
|
+ updateWrapper.set(StoreBookingCategory::getCategoryName, category.getCategoryName());
|
|
|
|
|
+ }
|
|
|
|
|
+ if (StringUtils.hasText(category.getFloorPlanImages())) {
|
|
|
|
|
+ updateWrapper.set(StoreBookingCategory::getFloorPlanImages, category.getFloorPlanImages());
|
|
|
|
|
+ }
|
|
|
|
|
+ if (category.getIsDisplay() != null) {
|
|
|
|
|
+ updateWrapper.set(StoreBookingCategory::getIsDisplay, category.getIsDisplay());
|
|
|
|
|
+ }
|
|
|
|
|
+ if (category.getMaxBookingTime() != null) {
|
|
|
|
|
+ updateWrapper.set(StoreBookingCategory::getMaxBookingTime, category.getMaxBookingTime());
|
|
|
|
|
+ }
|
|
|
|
|
+ if (category.getSort() != null) {
|
|
|
|
|
+ updateWrapper.set(StoreBookingCategory::getSort, category.getSort());
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ if (userId != null) {
|
|
|
|
|
+ updateWrapper.set(StoreBookingCategory::getUpdatedUserId, userId);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return this.update(updateWrapper);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public boolean deleteCategory(Integer id) {
|
|
|
|
|
+ log.info("StoreBookingCategoryServiceImpl.deleteCategory?id={}", id);
|
|
|
|
|
+
|
|
|
|
|
+ StoreBookingCategory category = this.getById(id);
|
|
|
|
|
+ if (category == null) {
|
|
|
|
|
+ log.warn("删除预订服务分类失败:分类不存在,id={}", id);
|
|
|
|
|
+ throw new RuntimeException("分类不存在");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 逻辑删除
|
|
|
|
|
+ return this.removeById(id);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @Override
|
|
|
|
|
+ public boolean updateCategorySort(Integer storeId, List<Integer> categoryIds) {
|
|
|
|
|
+ log.info("StoreBookingCategoryServiceImpl.updateCategorySort?storeId={}&categoryIds={}", storeId, categoryIds);
|
|
|
|
|
+
|
|
|
|
|
+ if (storeId == null || categoryIds == null || categoryIds.isEmpty()) {
|
|
|
|
|
+ log.warn("更新预订服务分类排序失败:参数不完整");
|
|
|
|
|
+ throw new RuntimeException("参数不完整");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 验证所有分类ID是否属于该门店
|
|
|
|
|
+ LambdaQueryWrapper<StoreBookingCategory> checkWrapper = new LambdaQueryWrapper<>();
|
|
|
|
|
+ checkWrapper.eq(StoreBookingCategory::getStoreId, storeId)
|
|
|
|
|
+ .in(StoreBookingCategory::getId, categoryIds);
|
|
|
|
|
+ long count = this.count(checkWrapper);
|
|
|
|
|
+ if (count != categoryIds.size()) {
|
|
|
|
|
+ log.warn("更新预订服务分类排序失败:部分分类ID不属于该门店");
|
|
|
|
|
+ throw new RuntimeException("部分分类ID不属于该门店");
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ // 从JWT获取当前登录用户ID
|
|
|
|
|
+ Integer userId = getCurrentUserId();
|
|
|
|
|
+
|
|
|
|
|
+ // 批量更新排序值
|
|
|
|
|
+ for (int i = 0; i < categoryIds.size(); i++) {
|
|
|
|
|
+ LambdaUpdateWrapper<StoreBookingCategory> updateWrapper = new LambdaUpdateWrapper<>();
|
|
|
|
|
+ updateWrapper.eq(StoreBookingCategory::getId, categoryIds.get(i))
|
|
|
|
|
+ .set(StoreBookingCategory::getSort, i + 1);
|
|
|
|
|
+ if (userId != null) {
|
|
|
|
|
+ updateWrapper.set(StoreBookingCategory::getUpdatedUserId, userId);
|
|
|
|
|
+ }
|
|
|
|
|
+ this.update(updateWrapper);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ return true;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ /**
|
|
|
|
|
+ * 从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;
|
|
|
|
|
+ }
|
|
|
|
|
+}
|