完成标签搜索接口

This commit is contained in:
xpecya 2024-12-05 19:12:53 +08:00
parent 7f06178d2f
commit d0bc971d15
8 changed files with 396 additions and 7 deletions

View File

@ -43,6 +43,8 @@ public class AdminUserController {
* @param phone 用户手机号 模糊搜索
* @param page 分页参数 当前页码 默认第一页
* @param size 分页参数 当前页长度 默认10条
* @param orderBy 排序参数 要排序的字段
* @param direction 排序参数 排序方向
* @return 分页搜索结果
*/
@GetMapping
@ -50,7 +52,9 @@ public class AdminUserController {
@RequestParam(value = "name", required = false) String name,
@RequestParam(value = "phone", required = false) String phone,
@RequestParam(value = "page", required = false, defaultValue = "1") int page,
@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 = "direction", required = false, defaultValue = "asc") String direction) {
if (!StringUtils.hasText(token)) {
return PageObject.failedPage("登陆Token为空!");
}
@ -67,14 +71,15 @@ public class AdminUserController {
return PageObject.failedPage("当前用户已被封禁!请联系系统管理员!");
}
log.info("path: /admin/user, method: GET, request user id: {}, name: {}, phone: {}, page: {}, size: {}",
adminUser.getId(), name, phone, page, size);
log.info("path: /admin/user, method: GET, request user id: {}, name: {}, " +
"phone: {}, page: {}, size: {}, orderBy: {}, direction: {}",
adminUser.getId(), name, phone, page, size, orderBy, direction);
} catch (Exception e) {
log.error("解析登陆Token出错!", e);
return PageObject.failedPage(500, "服务端错误,请联系系统管理员!");
}
return adminUserService.findAdminUser(name, phone, page, size);
return adminUserService.findAdminUser(name, phone, page, size, orderBy, direction);
}
/**

View File

@ -0,0 +1,82 @@
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.tag.TagVO;
import com.jinrui.reference.core.service.TagService;
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("/tag")
public class TagController {
private static final Logger log = LoggerFactory.getLogger(TagController.class);
private final TagService tagService;
public TagController(TagService tagService) {
this.tagService = tagService;
}
/**
* 标签搜索接口
*
* @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<TagVO> queryTag(@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 = "id") 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: /tag, 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 tagService.queryTag(parent, needChildren, keyword, exclude, page, size, orderBy, direction);
}
}

View File

@ -44,8 +44,10 @@ public interface AdminUserMapper {
"and phone like concat('%', #{phone}, '%') " +
"</if>" +
"</where>" +
"order by #{orderBy} #{direction}" +
"</script>")
List<AdminUser> queryAdminUser(@Param("name") String name, @Param("phone") String phone);
List<AdminUser> queryAdminUser(@Param("name") String name, @Param("phone") String phone,
@Param("orderBy") String orderBy, @Param("direction") String direction);
@Update("update admin_user set active = 0, update_time = now() where id = #{id}")
void ban(@Param("id") long id);

View File

@ -175,10 +175,13 @@ public class AdminUserService {
* @param phone 手机号 模糊匹配
* @param page 当前页码 默认第一页
* @param size 每页长度 默认10条
* @param orderBy 排序参数 要排序的字段
* @param direction 排序参数 排序方向
* @return 查询结果
*/
public PageObject<AdminUserVO> findAdminUser(String name, String phone, int page, int size) {
List<AdminUser> resultList = adminUserMapper.queryAdminUser(name, phone);
public PageObject<AdminUserVO> findAdminUser(String name, String phone, int page, int size,
String orderBy, String direction) {
List<AdminUser> resultList = adminUserMapper.queryAdminUser(name, phone, orderBy, direction);
if (CollectionUtils.isEmpty(resultList)) {
return PageObject.empty(page);
}

View File

@ -0,0 +1,39 @@
package com.jinrui.reference.core.mapper;
import com.jinrui.reference.core.model.entity.Tag;
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 TagMapper {
@Results({
@Result(column = "id", property = "id", id = true),
@Result(column = "parent_id", property = "parentId"),
@Result(column = "name", property = "name"),
@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 tag" +
"<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 &lt;&gt; #{exclude}" +
"</if>" +
"</where>" +
"order by #{orderBy} #{direction}" +
"</script>")
List<Tag> queryTag(@Param("parent") Long parent, @Param("keyword") String keyword, @Param("exclude") Long exclude,
@Param("orderBy") String orderBy, @Param("direction") String direction);
}

View File

@ -0,0 +1,75 @@
package com.jinrui.reference.core.model.entity;
import java.util.Date;
/**
* 标签
*/
@SuppressWarnings("unused")
public class Tag {
/**
* 标签ID
*/
private Long id;
/**
* 上级标签ID
*/
private Long parentId;
/**
* 标签名称
*/
private String name;
/**
* 创建时间
*/
private Date createTime;
/**
* 修改时间
*/
private Date updateTime;
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public Long getParentId() {
return parentId;
}
public void setParentId(Long parentId) {
this.parentId = parentId;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
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;
}
}

View File

@ -0,0 +1,89 @@
package com.jinrui.reference.core.model.vo.tag;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.jinrui.reference.core.model.entity.Tag;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
* 标签
*/
@SuppressWarnings("unused")
public class TagVO {
/**
* 标签ID
*/
private Long id;
/**
* 标签名称
*/
private String name;
/**
* 创建时间
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date createTime;
/**
* 修改时间
*/
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss")
private Date updateTime;
/**
* 子列表
*/
private List<TagVO> children = new ArrayList<>();
public TagVO(Tag tag) {
this.id = tag.getId();
this.name = tag.getName();
this.createTime = tag.getCreateTime();
this.updateTime = tag.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 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<TagVO> getChildren() {
return children;
}
public void setChildren(List<TagVO> children) {
this.children = children;
}
}

View File

@ -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.TagMapper;
import com.jinrui.reference.core.model.entity.Tag;
import com.jinrui.reference.core.model.vo.PageObject;
import com.jinrui.reference.core.model.vo.tag.TagVO;
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 TagService {
private static final Logger log = LoggerFactory.getLogger(TagService.class);
private final ObjectMapper objectMapper;
private final TagMapper tagMapper;
public TagService(ObjectMapper objectMapper, TagMapper tagMapper) {
this.objectMapper = objectMapper;
this.tagMapper = tagMapper;
}
/**
* 标签搜索接口
*
* @param parent 上级标签ID
* @param needChildren 是否需要子列表 默认不需要
* @param keyword 关键字 可以为空
* @param exclude 不包含的ID
* @param page 分页参数 当前页码 默认第一页
* @param size 分页参数 当前页长度 默认10条
* @return 标签搜索结果
*/
public PageObject<TagVO> queryTag(Long parent, boolean needChildren, String keyword,
Long exclude, int page, int size, String orderBy, String direction) {
List<Tag> tags;
try {
tags = tagMapper.queryTag(parent, keyword, exclude, orderBy, direction);
} catch (Exception e) {
log.error("搜索标签出错!", e);
return PageObject.failedPage(500, "服务端错误,请联系系统管理员!");
}
if (CollectionUtils.isEmpty(tags)) {
log.warn("parent: {}, keyword: {}, 找不到对应的标签!", parent, keyword);
return PageObject.empty(page);
}
PageObject<TagVO> resultPage = new PageObject<>();
resultPage.setCode(200);
resultPage.setTotal(tags.size());
List<TagVO> resultList = tags.stream()
.skip((long) (page - 1) * size)
.limit(size)
.map(TagVO::new)
.collect(Collectors.toList());
if (CollectionUtils.isEmpty(tags)) {
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 (TagVO tagVO : resultList) {
Long id = tagVO.getId();
tags = tagMapper.queryTag(id, null, null, "id", "asc");
if (!CollectionUtils.isEmpty(tags)) {
List<TagVO> childList = tags.stream().map(TagVO::new).collect(Collectors.toList());
tagVO.setChildren(childList);
}
}
}
resultPage.setData(resultList);
try {
String pageString = objectMapper.writeValueAsString(resultPage);
log.info("查询结果: {}", pageString);
} catch (JsonProcessingException e) {
log.error("查询标签返回值json映射出错!", e);
}
return resultPage;
}
}