diff --git a/src/main/java/com/huoran/iasf/controller/CategoryController.java b/src/main/java/com/huoran/iasf/controller/CategoryController.java index c1fabfd..c1af9db 100644 --- a/src/main/java/com/huoran/iasf/controller/CategoryController.java +++ b/src/main/java/com/huoran/iasf/controller/CategoryController.java @@ -5,15 +5,20 @@ import com.huoran.iasf.common.exception.BusinessException; import com.huoran.iasf.common.exception.code.BaseResponseCode; import com.huoran.iasf.common.utils.R; import com.huoran.iasf.entity.Category; +import com.huoran.iasf.entity.ProductDetails; import com.huoran.iasf.service.SysCategoryService; +import com.huoran.iasf.service.SysProductDetailsService; import io.swagger.annotations.Api; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; +import org.apache.commons.lang3.StringUtils; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.transaction.annotation.Transactional; +import org.springframework.util.ObjectUtils; import org.springframework.validation.annotation.Validated; import org.springframework.web.bind.annotation.*; +import java.util.ArrayList; import java.util.List; /** @@ -31,6 +36,9 @@ public class CategoryController { @Autowired private SysCategoryService service; + @Autowired + private SysProductDetailsService productDetailsService; + /** * 保存或更新分类信息 @@ -47,14 +55,15 @@ public class CategoryController { boolean isDuplicate = false; if (category.getCategoryId() != null) { // 更新逻辑,排除当前记录自身 - isDuplicate = service.existsOtherByName(category.getName(), category.getCategoryId(), category.getSiteId(),category.getType()); + isDuplicate = service.existsOtherByName(category.getName(), category.getCategoryId(), + category.getSiteId(), category.getType()); } else { // 新增逻辑,直接检查名称是否存在 - isDuplicate = service.existsByName(category.getName(), category.getSiteId(),category.getType()); + isDuplicate = service.existsByName(category.getName(), category.getSiteId(), category.getType()); } if (isDuplicate) { - return R.fail("分类名称已存在,请检查后重新提交"); + return R.fail("当前名称已存在,请检查后重新提交"); } boolean operationResult = service.saveOrUpdate(category); @@ -76,7 +85,8 @@ public class CategoryController { */ @PostMapping("/list") @ApiOperation("根据类型获取分类列表") - public R categoryList(@ApiParam(value = "类型标识") @RequestParam(required = false) Integer type, @ApiParam(value = "站点id") @RequestParam(required = false) Integer siteId) { + public R categoryList(@ApiParam(value = "类型标识") @RequestParam(required = false) Integer type, @ApiParam(value = + "站点id") @RequestParam(required = false) Integer siteId) { QueryWrapper queryWrapper = new QueryWrapper<>(); queryWrapper.eq("type", type); queryWrapper.eq("site_id", siteId); @@ -84,6 +94,39 @@ public class CategoryController { return R.success(categories); } + + @PostMapping("/hasBeenCitedInTheArticle") + @ApiOperation("已被文章引用的(在前台展示)") + public R hasBeenCitedInTheArticle(@ApiParam(value = "类型标识") @RequestParam(required = false) Integer type, + @ApiParam(value = "站点id") @RequestParam(required = false) Integer siteId) { + + List categories = new ArrayList<>(); + if (type == 0) { + //类型 + + categories = service.lookUpTheTypeOfReferenceBySite(siteId); + + + } else { + //分类、标签 + String tagIds = service.queryLabelsReferencedWithinSite(siteId); + if (!StringUtils.isEmpty(tagIds)) { + //逗号循环分隔设置到集合中 + String[] tagArray = tagIds.split(","); + for (String tagId : tagArray) { + Category category = service.getById(tagId); + if (!ObjectUtils.isEmpty(category)) { + categories.add(category); + } + } + + } + + } + + return R.success(categories); + } + /** * 查询分类详情 * @@ -112,4 +155,57 @@ public class CategoryController { boolean delState = service.removeByIds(ids); return R.result(delState, delState ? "批量删除成功" : "批量删除失败"); } + + + // 常量定义 + private static final String SUCCESS_MSG = "未被引用"; + private static final String FAIL_MSG = "已被引用,确认删除将同时移除相关联的"; + + /** + * 校验:用于删除分类前校验文章是否引用过 + * 如果引用过就要提示“xxxx已被文章引用,是否确认删除,删除后对应文章内设置的分类将一并被删除!”, + * + * @param categoryId + * @return + */ + @PostMapping("/checkDelete") + @ApiOperation("检查分类是否已被引用") + public R checkDelete(@ApiParam("分类ID") @RequestParam Integer categoryId) { + // 验证分类ID是否有效 + if (categoryId == null || categoryId <= 0) { + return R.fail("无效的分类ID"); + } + + // 查询分类信息 + Category category = service.getById(categoryId); + if (category == null) { + return R.fail("分类不存在"); + } + + // 提取类型和站点ID + Integer type = category.getType(); + Integer siteId = category.getSiteId(); + + // 构建响应消息前缀 + String front = getFront(type); + + // 业务逻辑调用 + boolean isReferenced = service.checkDelete(siteId, categoryId, type); + + // 根据结果返回响应 + String message = isReferenced ? front + FAIL_MSG : front + SUCCESS_MSG + front; + return isReferenced ? R.warn(message) : R.success(message); + } + + // 辅助方法:根据类型构建响应消息前缀 + private String getFront(Integer type) { + if (type == 0) { + return "类型"; + } else if (type == 1) { + return "标签/分类"; + } + return ""; // 或者抛出异常,表明未知类型 + } + + } diff --git a/src/main/java/com/huoran/iasf/mapper/CategoryMapper.java b/src/main/java/com/huoran/iasf/mapper/CategoryMapper.java index a8834ec..67b66b6 100644 --- a/src/main/java/com/huoran/iasf/mapper/CategoryMapper.java +++ b/src/main/java/com/huoran/iasf/mapper/CategoryMapper.java @@ -1,7 +1,10 @@ package com.huoran.iasf.mapper; -import com.huoran.iasf.entity.Category; import com.baomidou.mybatisplus.core.mapper.BaseMapper; +import com.huoran.iasf.entity.Category; +import org.apache.ibatis.annotations.Param; + +import java.util.List; /** *

@@ -13,4 +16,12 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper; */ public interface CategoryMapper extends BaseMapper { + //校验:用于删除分类前校验文章是否引用过,如果引用过就要提示“xxxx已被文章引用,是否确认删除,删除后对应文章内设置的分类将一并被删除!”,参数为站点id,分类id,类型 + int checkDelete(@Param("siteId") Integer siteId, @Param("categoryId") Integer categoryId,@Param("type") Integer type); + + //查询站点内引用过的标签id + String queryLabelsReferencedWithinSite(@Param("siteId") Integer siteId); + + + List lookUpTheTypeOfReferenceBySite(@Param("siteId")Integer siteId); } diff --git a/src/main/java/com/huoran/iasf/mapper/xml/CategoryMapper.xml b/src/main/java/com/huoran/iasf/mapper/xml/CategoryMapper.xml index 79fac18..f97d401 100644 --- a/src/main/java/com/huoran/iasf/mapper/xml/CategoryMapper.xml +++ b/src/main/java/com/huoran/iasf/mapper/xml/CategoryMapper.xml @@ -2,4 +2,49 @@ + + + + + + + diff --git a/src/main/java/com/huoran/iasf/mapper/xml/SysNavigationIconMapper.xml b/src/main/java/com/huoran/iasf/mapper/xml/SysNavigationIconMapper.xml index f78304e..53765f6 100644 --- a/src/main/java/com/huoran/iasf/mapper/xml/SysNavigationIconMapper.xml +++ b/src/main/java/com/huoran/iasf/mapper/xml/SysNavigationIconMapper.xml @@ -27,6 +27,7 @@ AND (i.title LIKE CONCAT('%', #{search}, '%') OR i.button_text LIKE CONCAT('%', #{search}, '%')) + ORDER BY i.sort_order ASC diff --git a/src/main/java/com/huoran/iasf/service/SysCategoryService.java b/src/main/java/com/huoran/iasf/service/SysCategoryService.java index c315997..d4214ea 100644 --- a/src/main/java/com/huoran/iasf/service/SysCategoryService.java +++ b/src/main/java/com/huoran/iasf/service/SysCategoryService.java @@ -3,6 +3,8 @@ package com.huoran.iasf.service; import com.huoran.iasf.entity.Category; import com.baomidou.mybatisplus.extension.service.IService; +import java.util.List; + /** *

* 产品与课程分类表 服务类 @@ -16,4 +18,11 @@ public interface SysCategoryService extends IService { boolean existsOtherByName(String name, Integer categoryId,Integer siteId, Integer type); boolean existsByName(String name,Integer siteId, Integer type); + + boolean checkDelete(Integer siteId, Integer categoryId, Integer type); + + + String queryLabelsReferencedWithinSite(Integer siteId); + + List lookUpTheTypeOfReferenceBySite(Integer siteId); } diff --git a/src/main/java/com/huoran/iasf/service/impl/CategoryServiceImpl.java b/src/main/java/com/huoran/iasf/service/impl/CategoryServiceImpl.java index c2f790b..c7b3f35 100644 --- a/src/main/java/com/huoran/iasf/service/impl/CategoryServiceImpl.java +++ b/src/main/java/com/huoran/iasf/service/impl/CategoryServiceImpl.java @@ -5,8 +5,11 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.huoran.iasf.entity.Category; import com.huoran.iasf.mapper.CategoryMapper; import com.huoran.iasf.service.SysCategoryService; +import org.apache.commons.lang3.StringUtils; import org.springframework.stereotype.Service; +import java.util.List; + /** *

* 产品与课程分类表 服务实现类 @@ -43,4 +46,27 @@ public class CategoryServiceImpl extends ServiceImpl i } return false; } + + @Override + public boolean checkDelete(Integer siteId, Integer categoryId, Integer type) { + if (baseMapper.checkDelete(siteId, categoryId, type) > 0) { + return true; + } + return false; + } + + @Override + public String queryLabelsReferencedWithinSite(Integer siteId) { + if (!StringUtils.isEmpty(baseMapper.queryLabelsReferencedWithinSite(siteId))) { + return baseMapper.queryLabelsReferencedWithinSite(siteId); + } + return null; + } + + @Override + public List lookUpTheTypeOfReferenceBySite(Integer siteId) { + return baseMapper.lookUpTheTypeOfReferenceBySite(siteId); + } + + }