master
rong.liu 5 months ago
parent 8b5988c329
commit 5e307ba600
  1. 104
      src/main/java/com/huoran/iasf/controller/CategoryController.java
  2. 13
      src/main/java/com/huoran/iasf/mapper/CategoryMapper.java
  3. 45
      src/main/java/com/huoran/iasf/mapper/xml/CategoryMapper.xml
  4. 1
      src/main/java/com/huoran/iasf/mapper/xml/SysNavigationIconMapper.xml
  5. 9
      src/main/java/com/huoran/iasf/service/SysCategoryService.java
  6. 26
      src/main/java/com/huoran/iasf/service/impl/CategoryServiceImpl.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.exception.code.BaseResponseCode;
import com.huoran.iasf.common.utils.R; import com.huoran.iasf.common.utils.R;
import com.huoran.iasf.entity.Category; import com.huoran.iasf.entity.Category;
import com.huoran.iasf.entity.ProductDetails;
import com.huoran.iasf.service.SysCategoryService; import com.huoran.iasf.service.SysCategoryService;
import com.huoran.iasf.service.SysProductDetailsService;
import io.swagger.annotations.Api; import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam; import io.swagger.annotations.ApiParam;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.ObjectUtils;
import org.springframework.validation.annotation.Validated; import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List; import java.util.List;
/** /**
@ -31,6 +36,9 @@ public class CategoryController {
@Autowired @Autowired
private SysCategoryService service; private SysCategoryService service;
@Autowired
private SysProductDetailsService productDetailsService;
/** /**
* 保存或更新分类信息 * 保存或更新分类信息
@ -47,14 +55,15 @@ public class CategoryController {
boolean isDuplicate = false; boolean isDuplicate = false;
if (category.getCategoryId() != null) { 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 { } else {
// 新增逻辑,直接检查名称是否存在 // 新增逻辑,直接检查名称是否存在
isDuplicate = service.existsByName(category.getName(), category.getSiteId(),category.getType()); isDuplicate = service.existsByName(category.getName(), category.getSiteId(), category.getType());
} }
if (isDuplicate) { if (isDuplicate) {
return R.fail("分类名称已存在,请检查后重新提交"); return R.fail("当前名称已存在,请检查后重新提交");
} }
boolean operationResult = service.saveOrUpdate(category); boolean operationResult = service.saveOrUpdate(category);
@ -76,7 +85,8 @@ public class CategoryController {
*/ */
@PostMapping("/list") @PostMapping("/list")
@ApiOperation("根据类型获取分类列表") @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<Category> queryWrapper = new QueryWrapper<>(); QueryWrapper<Category> queryWrapper = new QueryWrapper<>();
queryWrapper.eq("type", type); queryWrapper.eq("type", type);
queryWrapper.eq("site_id", siteId); queryWrapper.eq("site_id", siteId);
@ -84,6 +94,39 @@ public class CategoryController {
return R.success(categories); 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<Category> 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); boolean delState = service.removeByIds(ids);
return R.result(delState, delState ? "批量删除成功" : "批量删除失败"); 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 ""; // 或者抛出异常,表明未知类型
}
} }

@ -1,7 +1,10 @@
package com.huoran.iasf.mapper; package com.huoran.iasf.mapper;
import com.huoran.iasf.entity.Category;
import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.huoran.iasf.entity.Category;
import org.apache.ibatis.annotations.Param;
import java.util.List;
/** /**
* <p> * <p>
@ -13,4 +16,12 @@ import com.baomidou.mybatisplus.core.mapper.BaseMapper;
*/ */
public interface CategoryMapper extends BaseMapper<Category> { public interface CategoryMapper extends BaseMapper<Category> {
//校验:用于删除分类前校验文章是否引用过,如果引用过就要提示“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<Category> lookUpTheTypeOfReferenceBySite(@Param("siteId")Integer siteId);
} }

@ -2,4 +2,49 @@
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd"> <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.huoran.iasf.mapper.CategoryMapper"> <mapper namespace="com.huoran.iasf.mapper.CategoryMapper">
<!--校验:用于删除分类前校验文章是否引用过,如果引用过就要提示“xxxx已被文章引用,是否确认删除,删除后对应文章内设置的分类将一并被删除!”,参数为站点id,分类id,类型-->
<select id="checkDelete" resultType="java.lang.Integer">
SELECT
COUNT(1)
FROM
sys_product_details
WHERE
site_id = #{siteId}
<if test="type == 1">
AND find_in_set( #{categoryId},category_id )
</if>
<if test="type == 0">
AND classification_tag_id in (#{categoryId})
</if>
</select>
<select id="queryLabelsReferencedWithinSite" resultType="java.lang.String">
SELECT
GROUP_CONCAT( DISTINCT category_id )
FROM
sys_product_details
WHERE
site_id = #{siteId}
AND ( category_id != NULL OR category_id != '' )
AND deleted = 1
</select>
<select id="lookUpTheTypeOfReferenceBySite" resultType="com.huoran.iasf.entity.Category">
SELECT
d.classification_tag_id,
c.*
FROM
sys_product_details d
INNER JOIN sys_category c ON c.category_id = d.classification_tag_id
WHERE
d.site_id = #{siteId}
AND ( d.classification_tag_id != NULL OR d.classification_tag_id != '' )
AND d.deleted = 1
GROUP BY
d.classification_tag_id
</select>
</mapper> </mapper>

@ -27,6 +27,7 @@
<if test='search != null and search != ""'> <if test='search != null and search != ""'>
AND (i.title LIKE CONCAT('%', #{search}, '%') OR i.button_text LIKE CONCAT('%', #{search}, '%')) AND (i.title LIKE CONCAT('%', #{search}, '%') OR i.button_text LIKE CONCAT('%', #{search}, '%'))
</if> </if>
ORDER BY i.sort_order ASC
</select> </select>

@ -3,6 +3,8 @@ package com.huoran.iasf.service;
import com.huoran.iasf.entity.Category; import com.huoran.iasf.entity.Category;
import com.baomidou.mybatisplus.extension.service.IService; import com.baomidou.mybatisplus.extension.service.IService;
import java.util.List;
/** /**
* <p> * <p>
* 产品与课程分类表 服务类 * 产品与课程分类表 服务类
@ -16,4 +18,11 @@ public interface SysCategoryService extends IService<Category> {
boolean existsOtherByName(String name, Integer categoryId,Integer siteId, Integer type); boolean existsOtherByName(String name, Integer categoryId,Integer siteId, Integer type);
boolean existsByName(String name,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<Category> lookUpTheTypeOfReferenceBySite(Integer siteId);
} }

@ -5,8 +5,11 @@ import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huoran.iasf.entity.Category; import com.huoran.iasf.entity.Category;
import com.huoran.iasf.mapper.CategoryMapper; import com.huoran.iasf.mapper.CategoryMapper;
import com.huoran.iasf.service.SysCategoryService; import com.huoran.iasf.service.SysCategoryService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import java.util.List;
/** /**
* <p> * <p>
* 产品与课程分类表 服务实现类 * 产品与课程分类表 服务实现类
@ -43,4 +46,27 @@ public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category> i
} }
return false; 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<Category> lookUpTheTypeOfReferenceBySite(Integer siteId) {
return baseMapper.lookUpTheTypeOfReferenceBySite(siteId);
}
} }

Loading…
Cancel
Save