@ -28,105 +28,145 @@ import com.tamguo.modules.book.service.IBookService;
import com.tamguo.modules.book.service.IDocumentService ;
import com.tamguo.utils.ShiroUtils ;
/ * *
* BookController 类 , 处 理 图 书 相 关 的 请 求
* /
@Controller
public class BookController {
private Logger logger = LoggerFactory . getLogger ( getClass ( ) ) ;
private Logger logger = LoggerFactory . getLogger ( getClass ( ) ) ; // 创建日志记录器对象
@Autowired
private IBookService iBookService ;
private IBookService iBookService ; // 自动注入图书服务
@Autowired
private IDocumentService iDocumentService ;
private IDocumentService iDocumentService ; // 自动注入文档服务
/ * *
* 处 理 编 辑 图 书 的 请 求
* @param bookId 图 书 ID
* @param model 模 型 视 图 对 象
* @return 模 型 视 图 对 象
* /
@RequestMapping ( value = "editBook/{bookId}" , method = RequestMethod . GET )
public ModelAndView edit ( @PathVariable String bookId , ModelAndView model ) {
model . setViewName ( "book/edit" ) ;
model . addObject ( "bookId" , bookId ) ;
return model ;
model . setViewName ( "book/edit" ) ; // 设置视图名称
model . addObject ( "bookId" , bookId ) ; // 添加图书 ID 到模型中
return model ; // 返回模型视图对象
}
/ * *
* 处 理 图 书 列 表 的 请 求
* @param model 模 型 视 图 对 象
* @return 模 型 视 图 对 象
* /
@RequestMapping ( value = { "booklist.html" } , method = RequestMethod . GET )
public ModelAndView list ( ModelAndView model ) {
model . setViewName ( "booklist" ) ;
return model ;
model . setViewName ( "booklist" ) ; // 设置视图名称
return model ; // 返回模型视图对象
}
/ * *
* 处 理 获 取 文 档 列 表 的 请 求
* @param id 图 书 ID
* @return 返 回 处 理 结 果
* /
@SuppressWarnings ( "unchecked" )
@RequestMapping ( value = "getDocumentList" , method = RequestMethod . POST )
@ResponseBody
public Result getDocumentList ( String id ) {
Map < String , Object > map = new HashMap < > ( ) ;
Map < String , Object > map = new HashMap < > ( ) ; // 创建一个 Map 对象
try {
BookEntity book = iBookService . selectById ( id ) ;
List < DocumentEntity > documentList = iDocumentService . selectList ( Condition . create ( ) . eq ( "book_id" , id ) . eq ( "status" , DocumentStatusEnum . NORMAL . getValue ( ) ) ) ;
BookEntity book = iBookService . selectById ( id ) ; // 根据图书 ID 获取图书信息
List < DocumentEntity > documentList = iDocumentService . selectList ( Condition . create ( ) . eq ( "book_id" , id ) . eq ( "status" , DocumentStatusEnum . NORMAL . getValue ( ) ) ) ; // 根据图书 ID 和文档状态获取文档列表
map . put ( "documentList" , this . processDocumentList ( documentList ) ) ;
map . put ( "book" , book ) ;
map . put ( "documentList" , this . processDocumentList ( documentList ) ) ; // 处理文档列表并将结果放入 Map 中
map . put ( "book" , book ) ; // 将图书信息放入 Map 中
} catch ( Exception e ) {
logger . error ( e . getMessage ( ) , e ) ;
return Result . failResult ( "查询失败" ) ;
logger . error ( e . getMessage ( ) , e ) ; // 记录错误日志
return Result . failResult ( "查询失败" ) ; // 返回查询失败的结果
}
return Result . successResult ( map ) ;
return Result . successResult ( map ) ; // 返回查询成功的结果
}
/ * *
* 处 理 获 取 图 书 列 表 的 请 求
* @return 返 回 处 理 结 果
* /
@SuppressWarnings ( "unchecked" )
@RequestMapping ( value = { "getBookList.html" } , method = RequestMethod . POST )
@ResponseBody
public Result getBookList ( ) {
try {
List < BookEntity > bookList = iBookService . selectList ( Condition . create ( ) . eq ( "owner" , ShiroUtils . getMemberId ( ) ) . order Desc( Arrays . asList ( "create_date" ) ) ) ;
return Result . successResult ( bookList ) ;
List < BookEntity > bookList = iBookService . selectList ( Condition . create ( ) . eq ( "owner" , ShiroUtils . getMemberId ( ) ) . order By Desc( Arrays . asList ( "create_date" ) ) ) ; // 根据当前用户 ID 获取图书列表
return Result . successResult ( bookList ) ; // 返回图书列表
} catch ( Exception e ) {
logger . error ( e . getMessage ( ) , e ) ;
return Result . failResult ( "查询失败!" ) ;
logger . error ( e . getMessage ( ) , e ) ; // 记录错误日志
return Result . failResult ( "查询失败!" ) ; // 返回查询失败的结果
}
}
/ * *
* 处 理 保 存 图 书 的 请 求
* @param book 图 书 实 体
* @return 返 回 处 理 结 果
* /
@RequestMapping ( value = { "saveBook" } , method = RequestMethod . POST )
@ResponseBody
public Result saveBook ( @RequestBody BookEntity book ) {
try {
book . setOwner ( ShiroUtils . getMemberId ( ) ) ;
book . setCategoryId ( StringUtils . join ( book . getCategoryIds ( ) , "," ) ) ;
iBookService . saveBook ( book ) ;
return Result . result ( 0 , null , "保存成功" ) ;
book . setOwner ( ShiroUtils . getMemberId ( ) ) ; // 设置图书的所有者为当前用户
book . setCategoryId ( StringUtils . join ( book . getCategoryIds ( ) , "," ) ) ; // 设置图书的分类 ID 为逗号分隔的字符串
iBookService . saveBook ( book ) ; // 保存图书
return Result . result ( 0 , null , "保存成功" ) ; // 返回保存成功的结果
} catch ( Exception e ) {
logger . error ( e . getMessage ( ) , e ) ;
return Result . result ( 1 , null , "保存失败" ) ;
logger . error ( e . getMessage ( ) , e ) ; // 记录错误日志
return Result . result ( 1 , null , "保存失败" ) ; // 返回保存失败的结果
}
}
/ * *
* 处 理 文 档 列 表 , 生 成 JSON 数 组
* @param documentList 文 档 列 表
* @return 返 回 处 理 后 的 JSON 数 组
* /
private JSONArray processDocumentList ( List < DocumentEntity > documentList ) {
JSONArray entitys = new JSONArray ( ) ;
for ( int i = 0 ; i < documentList . size ( ) ; i + + ) {
DocumentEntity doc = documentList . get ( i ) ;
JSONObject entity = new JSONObject ( ) ;
entity . put ( "id" , doc . getId ( ) ) ;
entity . put ( "text" , doc . getName ( ) ) ;
entity . put ( "parent" , "0" . equals ( doc . getParentId ( ) ) ? "#" : doc . getParentId ( ) ) ;
entity . put ( "identify" , doc . getId ( ) ) ;
entity . put ( "version" , doc . getCreateDate ( ) . getTime ( ) ) ;
JSONObject attr = new JSONObject ( ) ;
attr . put ( "is_open" , "0" . equals ( doc . getIsOpen ( ) ) ? false : true ) ;
entity . put ( "a_attr" , attr ) ;
entitys . add ( entity ) ;
JSONArray entitys = new JSONArray ( ) ; // 创建 JSON 数组对象
for ( int i = 0 ; i < documentList . size ( ) ; i + + ) { // 遍历文档列表
DocumentEntity doc = documentList . get ( i ) ; // 获取当前文档
JSONObject entity = new JSONObject ( ) ; // 创建 JSON 对象
entity . put ( "id" , doc . getId ( ) ) ; // 设置文档 ID
entity . put ( "text" , doc . getName ( ) ) ; // 设置文档名称
entity . put ( "parent" , "0" . equals ( doc . getParentId ( ) ) ? "#" : doc . getParentId ( ) ) ; // 设置文档的父节点
entity . put ( "identify" , doc . getId ( ) ) ; // 设置文档的标识
entity . put ( "version" , doc . getCreateDate ( ) . getTime ( ) ) ; // 设置文档的版本
JSONObject attr = new JSONObject ( ) ; // 创建属性对象
attr . put ( "is_open" , "0" . equals ( doc . getIsOpen ( ) ) ? false : true ) ; // 设置文档是否展开
entity . put ( "a_attr" , attr ) ; // 设置文档的属性
entitys . add ( entity ) ; // 将文档对象添加到 JSON 数组中
}
return entitys ;
return entitys ; // 返回 JSON 数组
}
/ * *
* 处 理 获 取 图 书 的 请 求
* @param id 图 书 ID
* @return 返 回 处 理 结 果
* /
@RequestMapping ( value = "book/{id}.html" , method = RequestMethod . GET )
@ResponseBody
public Result book ( @PathVariable String id ) {
try {
BookEntity book = iBookService . selectById ( id ) ;
BookEntity book = iBookService . selectById ( id ) ; // 根据图书 ID 获取图书信息
String categoryIds = book . getCategoryId ( ) ; // 获取图书的分类 ID 字符串
book . setCategoryIds ( Arrays . asList ( categoryIds . split ( "," ) ) ) ; // 将分类 ID 字符串转换为列表
String categoryIds = book . getCategoryId ( ) ;
book . setCategoryIds ( Arrays . asList ( categoryIds . split ( "," ) ) ) ;
// 获取 categoryIds
return Result . result ( 0 , book , "查询成功!" ) ;
return Result . result ( 0 , book , "查询成功!" ) ; // 返回查询成功的结果
} catch ( Exception e ) {
logger . error ( e . getMessage ( ) , e ) ;
return Result . result ( 1 , null , "查询失败!" ) ;
logger . error ( e . getMessage ( ) , e ) ; // 记录错误日志
return Result . result ( 1 , null , "查询失败!" ) ; // 返回查询失败的结果
}
}
}