完成栏目管理
This commit is contained in:
parent
c26acbeed7
commit
9da2a1efe1
|
|
@ -0,0 +1,101 @@
|
||||||
|
package com.jinrui.reference.admin.controller;
|
||||||
|
|
||||||
|
import com.jinrui.reference.admin.model.entity.AdminUser;
|
||||||
|
import com.jinrui.reference.admin.service.AdminJwtService;
|
||||||
|
import com.jinrui.reference.core.model.vo.PageObject;
|
||||||
|
import com.jinrui.reference.core.model.vo.column.ColumnVO;
|
||||||
|
import com.jinrui.reference.core.service.ColumnService;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
import org.springframework.web.bind.annotation.GetMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestHeader;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@RequestMapping("/column")
|
||||||
|
public class ColumnController {
|
||||||
|
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(ColumnController.class);
|
||||||
|
|
||||||
|
private final ColumnService columnService;
|
||||||
|
|
||||||
|
public ColumnController(ColumnService columnService) {
|
||||||
|
this.columnService = columnService;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 栏目搜索接口
|
||||||
|
*
|
||||||
|
* @param token 登陆Token
|
||||||
|
* @param parent 上级标签ID
|
||||||
|
* @param needChildren 是否需要子列表 默认不需要
|
||||||
|
* @param keyword 关键字 可以为空
|
||||||
|
* @param exclude 不包含的ID
|
||||||
|
* @param page 分页参数 当前页码 默认第一页
|
||||||
|
* @param size 分页参数 当前页长度 默认10条
|
||||||
|
* @param orderBy 排序参数 要排序的字段
|
||||||
|
* @param direction 排序参数 排序方向
|
||||||
|
* @return 标签搜索结果
|
||||||
|
*/
|
||||||
|
@GetMapping
|
||||||
|
public PageObject<ColumnVO> queryColumn(@RequestHeader("auth-token") String token,
|
||||||
|
@RequestParam(value = "parent", required = false) Long parent,
|
||||||
|
@RequestParam(value = "needChildren", required = false, defaultValue = "false") boolean needChildren,
|
||||||
|
@RequestParam(value = "keyword", required = false) String keyword,
|
||||||
|
@RequestParam(value = "exclude", required = false) Long exclude,
|
||||||
|
@RequestParam(value = "page", required = false, defaultValue = "1") int page,
|
||||||
|
@RequestParam(value = "size", required = false, defaultValue = "10") int size,
|
||||||
|
@RequestParam(value = "orderBy", required = false, defaultValue = "order") String orderBy,
|
||||||
|
@RequestParam(value = "direction", required = false, defaultValue = "asc") String direction) {
|
||||||
|
if (!StringUtils.hasText(token)) {
|
||||||
|
return PageObject.failedPage("登陆Token为空!");
|
||||||
|
}
|
||||||
|
|
||||||
|
try {
|
||||||
|
AdminUser adminUser = AdminJwtService.parseToken(token);
|
||||||
|
if (adminUser == null) {
|
||||||
|
log.warn("解析token {}拿不到AdminUser对象!", token);
|
||||||
|
return PageObject.failedPage("登陆Token有误,请联系系统管理员!");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (!adminUser.isActive()) {
|
||||||
|
log.warn("当前用户已被封禁! id = {}", adminUser.getId());
|
||||||
|
return PageObject.failedPage("当前用户已被封禁!请联系系统管理员!");
|
||||||
|
}
|
||||||
|
|
||||||
|
log.info("path: /column, method: GET, request user id: {}, parent: {}, needChildren: {}, " +
|
||||||
|
"keyword: {}, exclude: {}, page: {}, size: {}, orderBy: {}, direction: {}",
|
||||||
|
adminUser.getId(), parent, needChildren, keyword, exclude, page, size, orderBy, direction);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("解析登陆Token出错!", e);
|
||||||
|
return PageObject.failedPage(500, "服务端错误,请联系系统管理员!");
|
||||||
|
}
|
||||||
|
|
||||||
|
return columnService.queryColumn(parent, needChildren, keyword, exclude, page, size, orderBy, direction);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/vip")
|
||||||
|
public PageObject<ColumnVO> querySource(@RequestHeader("auth-token") String token,
|
||||||
|
@RequestParam(value = "keyword", required = false) String keyword,
|
||||||
|
@RequestParam(value = "page", required = false, defaultValue = "1") int page,
|
||||||
|
@RequestParam(value = "size", required = false, defaultValue = "10") int size,
|
||||||
|
@RequestParam(value = "orderBy", required = false, defaultValue = "id") String orderBy,
|
||||||
|
@RequestParam(value = "direction", required = false, defaultValue = "asc") String direction) {
|
||||||
|
return queryColumn(token, 1L, false, keyword, null, page, size, orderBy, direction);
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("/diy")
|
||||||
|
public PageObject<ColumnVO> queryDiy(@RequestHeader("auth-token") String token,
|
||||||
|
@RequestParam(value = "parent", required = false) Long parent,
|
||||||
|
@RequestParam(value = "needChildren", required = false, defaultValue = "false") boolean needChildren,
|
||||||
|
@RequestParam(value = "keyword", required = false) String keyword,
|
||||||
|
@RequestParam(value = "page", required = false, defaultValue = "1") int page,
|
||||||
|
@RequestParam(value = "size", required = false, defaultValue = "10") int size,
|
||||||
|
@RequestParam(value = "orderBy", required = false, defaultValue = "id") String orderBy,
|
||||||
|
@RequestParam(value = "direction", required = false, defaultValue = "asc") String direction) {
|
||||||
|
return queryColumn(token, parent, needChildren, keyword, 1L, page, size, orderBy, direction);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -99,6 +99,6 @@ public class TagController {
|
||||||
@RequestParam(value = "size", required = false, defaultValue = "10") int size,
|
@RequestParam(value = "size", required = false, defaultValue = "10") int size,
|
||||||
@RequestParam(value = "orderBy", required = false, defaultValue = "id") String orderBy,
|
@RequestParam(value = "orderBy", required = false, defaultValue = "id") String orderBy,
|
||||||
@RequestParam(value = "direction", required = false, defaultValue = "asc") String direction) {
|
@RequestParam(value = "direction", required = false, defaultValue = "asc") String direction) {
|
||||||
return queryTag(token, 1L, false, keyword, null, page, size, orderBy, direction);
|
return queryTag(token, parent, needChildren, keyword, 1L, page, size, orderBy, direction);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -0,0 +1,41 @@
|
||||||
|
package com.jinrui.reference.core.mapper;
|
||||||
|
|
||||||
|
import com.jinrui.reference.core.model.entity.Column;
|
||||||
|
import org.apache.ibatis.annotations.Param;
|
||||||
|
import org.apache.ibatis.annotations.Result;
|
||||||
|
import org.apache.ibatis.annotations.Results;
|
||||||
|
import org.apache.ibatis.annotations.Select;
|
||||||
|
import org.apache.ibatis.type.JdbcType;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
public interface ColumnMapper {
|
||||||
|
|
||||||
|
@Results({
|
||||||
|
@Result(column = "id", property = "id", id = true),
|
||||||
|
@Result(column = "parent_id", property = "parentId"),
|
||||||
|
@Result(column = "name", property = "name"),
|
||||||
|
@Result(column = "order", property = "order"),
|
||||||
|
@Result(column = "create_time", property = "createTime", javaType = Date.class, jdbcType = JdbcType.TIMESTAMP),
|
||||||
|
@Result(column = "update_time", property = "updateTime", javaType = Date.class, jdbcType = JdbcType.TIMESTAMP)
|
||||||
|
})
|
||||||
|
@Select("<script>" +
|
||||||
|
"select * from `column`" +
|
||||||
|
"<where>" +
|
||||||
|
"<if test=\"parent != null\">" +
|
||||||
|
"parent_id = #{parent} " +
|
||||||
|
"</if>" +
|
||||||
|
"<if test=\"keyword != null and !keyword.isEmpty()\">" +
|
||||||
|
"and name like concat('%', #{keyword}, '%') " +
|
||||||
|
"</if>" +
|
||||||
|
"<if test=\"exclude != null\">" +
|
||||||
|
"and id <> #{exclude}" +
|
||||||
|
"</if>" +
|
||||||
|
"</where>" +
|
||||||
|
"order by `#{orderBy}` #{direction}" +
|
||||||
|
"</script>")
|
||||||
|
List<Column> queryColumn(@Param("parent") Long parent, @Param("keyword") String keyword,
|
||||||
|
@Param("exclude") Long exclude, @Param("orderBy") String orderBy,
|
||||||
|
@Param("direction") String direction);
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,103 @@
|
||||||
|
package com.jinrui.reference.core.model.vo.column;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.annotation.JsonFormat;
|
||||||
|
import com.jinrui.reference.core.model.entity.Column;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 栏目
|
||||||
|
*/
|
||||||
|
@SuppressWarnings("unused")
|
||||||
|
public class ColumnVO {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 栏目ID
|
||||||
|
*/
|
||||||
|
private Long id;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 栏目名称
|
||||||
|
*/
|
||||||
|
private String name;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 排序
|
||||||
|
*/
|
||||||
|
private Integer order;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 创建时间
|
||||||
|
*/
|
||||||
|
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||||
|
private Date createTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 修改时间
|
||||||
|
*/
|
||||||
|
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
|
||||||
|
private Date updateTime;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 子列表
|
||||||
|
*/
|
||||||
|
private List<ColumnVO> children = new ArrayList<>();
|
||||||
|
|
||||||
|
public ColumnVO(Column column) {
|
||||||
|
this.id = column.getId();
|
||||||
|
this.name = column.getName();
|
||||||
|
this.order = column.getOrder();
|
||||||
|
this.createTime = column.getCreateTime();
|
||||||
|
this.updateTime = column.getUpdateTime();
|
||||||
|
}
|
||||||
|
|
||||||
|
public Long getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(Long id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Integer getOrder() {
|
||||||
|
return order;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setOrder(Integer order) {
|
||||||
|
this.order = order;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Date getCreateTime() {
|
||||||
|
return createTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCreateTime(Date createTime) {
|
||||||
|
this.createTime = createTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Date getUpdateTime() {
|
||||||
|
return updateTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setUpdateTime(Date updateTime) {
|
||||||
|
this.updateTime = updateTime;
|
||||||
|
}
|
||||||
|
|
||||||
|
public List<ColumnVO> getChildren() {
|
||||||
|
return children;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setChildren(List<ColumnVO> children) {
|
||||||
|
this.children = children;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -0,0 +1,94 @@
|
||||||
|
package com.jinrui.reference.core.service;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.JsonProcessingException;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
import com.jinrui.reference.core.mapper.ColumnMapper;
|
||||||
|
import com.jinrui.reference.core.model.entity.Column;
|
||||||
|
import com.jinrui.reference.core.model.vo.PageObject;
|
||||||
|
import com.jinrui.reference.core.model.vo.column.ColumnVO;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.util.CollectionUtils;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.stream.Collectors;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class ColumnService {
|
||||||
|
|
||||||
|
private static final Logger log = LoggerFactory.getLogger(ColumnService.class);
|
||||||
|
|
||||||
|
private final ObjectMapper objectMapper;
|
||||||
|
private final ColumnMapper columnMapper;
|
||||||
|
|
||||||
|
public ColumnService(ObjectMapper objectMapper, ColumnMapper columnMapper) {
|
||||||
|
this.objectMapper = objectMapper;
|
||||||
|
this.columnMapper = columnMapper;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 栏目搜索接口
|
||||||
|
*
|
||||||
|
* @param parent 上级栏目ID
|
||||||
|
* @param needChildren 是否需要子列表 默认不需要
|
||||||
|
* @param keyword 关键字 可以为空
|
||||||
|
* @param exclude 不包含的ID
|
||||||
|
* @param page 分页参数 当前页码 默认第一页
|
||||||
|
* @param size 分页参数 当前页长度 默认10条
|
||||||
|
* @return 标签搜索结果
|
||||||
|
*/
|
||||||
|
public PageObject<ColumnVO> queryColumn(Long parent, boolean needChildren, String keyword,
|
||||||
|
Long exclude, int page, int size, String orderBy, String direction) {
|
||||||
|
List<Column> columns;
|
||||||
|
try {
|
||||||
|
columns = columnMapper.queryColumn(parent, keyword, exclude, orderBy, direction);
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("搜索栏目出错!", e);
|
||||||
|
return PageObject.failedPage(500, "服务端错误,请联系系统管理员!");
|
||||||
|
}
|
||||||
|
|
||||||
|
if (CollectionUtils.isEmpty(columns)) {
|
||||||
|
log.warn("parent: {}, keyword: {}, 找不到对应的栏目!", parent, keyword);
|
||||||
|
return PageObject.empty(page);
|
||||||
|
}
|
||||||
|
|
||||||
|
PageObject<ColumnVO> resultPage = new PageObject<>();
|
||||||
|
resultPage.setCode(200);
|
||||||
|
resultPage.setTotal(columns.size());
|
||||||
|
List<ColumnVO> resultList = columns.stream()
|
||||||
|
.skip((long) (page - 1) * size)
|
||||||
|
.limit(size)
|
||||||
|
.map(ColumnVO::new)
|
||||||
|
.collect(Collectors.toList());
|
||||||
|
if (CollectionUtils.isEmpty(resultList)) {
|
||||||
|
log.info("parent: {}, keyword: {}, page: {}, size: {}, 当前页无数据!", parent, keyword, page, size);
|
||||||
|
resultPage.setCurrent(page);
|
||||||
|
resultPage.setSize(0);
|
||||||
|
resultPage.setData(new ArrayList<>());
|
||||||
|
return resultPage;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (needChildren) {
|
||||||
|
for (ColumnVO columnVO : resultList) {
|
||||||
|
Long id = columnVO.getId();
|
||||||
|
columns = columnMapper.queryColumn(id, null, null, "order", "asc");
|
||||||
|
if (!CollectionUtils.isEmpty(columns)) {
|
||||||
|
List<ColumnVO> childList = columns.stream().map(ColumnVO::new).collect(Collectors.toList());
|
||||||
|
columnVO.setChildren(childList);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
resultPage.setData(resultList);
|
||||||
|
|
||||||
|
try {
|
||||||
|
String pageString = objectMapper.writeValueAsString(resultPage);
|
||||||
|
log.info("查询结果: {}", pageString);
|
||||||
|
} catch (JsonProcessingException e) {
|
||||||
|
log.error("查询标签返回值json映射出错!", e);
|
||||||
|
}
|
||||||
|
return resultPage;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
@ -62,7 +62,7 @@ public class TagService {
|
||||||
.limit(size)
|
.limit(size)
|
||||||
.map(TagVO::new)
|
.map(TagVO::new)
|
||||||
.collect(Collectors.toList());
|
.collect(Collectors.toList());
|
||||||
if (CollectionUtils.isEmpty(tags)) {
|
if (CollectionUtils.isEmpty(resultList)) {
|
||||||
log.info("parent: {}, keyword: {}, page: {}, size: {}, 当前页无数据!", parent, keyword, page, size);
|
log.info("parent: {}, keyword: {}, page: {}, size: {}, 当前页无数据!", parent, keyword, page, size);
|
||||||
resultPage.setCurrent(page);
|
resultPage.setCurrent(page);
|
||||||
resultPage.setSize(0);
|
resultPage.setSize(0);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue