Compare commits

...

2 Commits

@ -1,63 +1,75 @@
package com.tamguo.web; package com.tamguo.web;
import java.util.List; import java.util.List;
import org.slf4j.Logger; import org.slf4j.Logger; // 导入日志记录器接口
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory; // 导入日志工厂类
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired; // 导入自动注入注解
import org.springframework.stereotype.Controller; import org.springframework.stereotype.Controller; // 导入控制器注解
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping; // 导入请求映射注解
import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestMethod; // 导入请求方法注解
import org.springframework.web.bind.annotation.ResponseBody; import org.springframework.web.bind.annotation.ResponseBody; // 导入响应体注解
import com.alibaba.druid.util.StringUtils;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.Condition;
import com.tamguo.common.utils.Result;
import com.tamguo.modules.book.model.BookCategoryEntity;
import com.tamguo.modules.book.service.IBookCategoryService;
import com.alibaba.druid.util.StringUtils; // 导入字符串工具类
import com.alibaba.fastjson.JSONArray; // 导入 JSON 数组类
import com.alibaba.fastjson.JSONObject; // 导入 JSON 对象类
import com.baomidou.mybatisplus.mapper.Condition; // 导入条件构建器类
import com.tamguo.modules.book.model.BookCategoryEntity; // 导入图书分类实体类
import com.tamguo.modules.book.service.IBookCategoryService; // 导入图书分类服务接口
/**
* BookCategoryController
*/
@Controller @Controller
public class BookCategoryController { public class BookCategoryController {
private Logger logger = LoggerFactory.getLogger(getClass()); private Logger logger = LoggerFactory.getLogger(getClass()); // 创建日志记录器实例
@Autowired @Autowired
private IBookCategoryService iBookCategoryService; private IBookCategoryService iBookCategoryService; // 自动注入图书分类服务实例
/**
* POST
* @param parentId ID
* @return
*/
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@RequestMapping(value = {"getBookCategory.html"}, method = RequestMethod.POST) @RequestMapping(value = {"getBookCategory.html"}, method = RequestMethod.POST)
@ResponseBody @ResponseBody
public Result getBookCategory(String parentId) { public Result getBookCategory(String parentId) {
try { try {
List<BookCategoryEntity> bookCategoryList = null; List<BookCategoryEntity> bookCategoryList = null; // 定义图书分类列表变量
if(StringUtils.isEmpty(parentId)) { if(StringUtils.isEmpty(parentId)) { // 如果父分类 ID 为空
bookCategoryList = iBookCategoryService.selectList(Condition.create().eq("parent_id", "0")); bookCategoryList = iBookCategoryService.selectList(Condition.create().eq("parent_id", "0")); // 查询父分类为 0 的图书分类列表
}else { } else {
bookCategoryList = iBookCategoryService.selectList(Condition.create().eq("parent_id", parentId)); bookCategoryList = iBookCategoryService.selectList(Condition.create().eq("parent_id", parentId)); // 查询指定父分类 ID 的图书分类列表
} }
return Result.successResult(processBookCategoryList(bookCategoryList)); return Result.successResult(processBookCategoryList(bookCategoryList)); // 返回处理后的图书分类列表结果,并标记为成功
} catch (Exception e) { } catch (Exception e) {
logger.error(e.getMessage() , e); logger.error(e.getMessage(), e); // 记录异常日志
return Result.failResult("查询失败!"); return Result.failResult("查询失败!"); // 返回失败结果及错误信息
} }
} }
/**
* JSON
* @param bookCategoryList
* @return JSON
*/
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
private JSONArray processBookCategoryList(List<BookCategoryEntity> bookCategoryList) { private JSONArray processBookCategoryList(List<BookCategoryEntity> bookCategoryList) {
JSONArray entitys = new JSONArray(); JSONArray entitys = new JSONArray(); // 创建 JSON 数组实例
for(int i=0 ; i<bookCategoryList.size() ; i++) { for(int i = 0; i < bookCategoryList.size(); i++) { // 遍历图书分类列表
BookCategoryEntity bc = bookCategoryList.get(i); BookCategoryEntity bc = bookCategoryList.get(i); // 获取当前图书分类实体
JSONObject entity = new JSONObject(); JSONObject entity = new JSONObject(); // 创建 JSON 对象实例
entity.put("label", bc.getName()); entity.put("label", bc.getName()); // 设置标签为分类名称
entity.put("value", bc.getId()); entity.put("value", bc.getId()); // 设置值为分类 ID
Integer count = iBookCategoryService.selectCount(Condition.create().eq("parent_id", bc.getId())); Integer count = iBookCategoryService.selectCount(Condition.create().eq("parent_id", bc.getId())); // 获取当前分类的子分类数量
if(count > 0) { if(count > 0) {
entity.put("children", new JSONArray()); entity.put("children", new JSONArray()); // 如果有子分类,设置子分类数组
} }
entitys.add(entity); entitys.add(entity); // 将当前分类对象添加到 JSON 数组中
} }
return entitys; return entitys; // 返回处理后的 JSON 数组
} }
} }
Loading…
Cancel
Save