From 9555c55cfb97ad821af73001f8280a6be2ba7ea1 Mon Sep 17 00:00:00 2001 From: sunflower2014 Date: Tue, 27 May 2025 22:07:46 +0800 Subject: [PATCH] =?UTF-8?q?=E8=A1=8C=E4=B8=9A=E5=88=86=E7=B1=BB=E6=96=B0?= =?UTF-8?q?=E5=A2=9E=E5=88=A0=E9=99=A4=E4=BB=A5=E5=8F=8A=E8=B5=84=E8=AE=AF?= =?UTF-8?q?=E8=AF=A6=E6=83=85=E9=A1=B5=E9=9D=A2=E8=A1=8C=E4=B8=9A=E5=88=86?= =?UTF-8?q?=E7=B1=BB=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../admin/controller/IndustryController.java | 105 +++++++++++++++++ .../reference/core/mapper/IndustryMapper.java | 48 ++++++++ .../core/model/dto/news/SaveIndustryDTO.java | 32 ++++++ .../reference/core/model/entity/Industry.java | 15 +++ .../model/vo/news/NewsDetailIndustry.java | 20 +++- .../core/model/vo/tag/IndustryVO.java | 103 +++++++++++++++++ .../core/service/IndustryService.java | 107 ++++++++++++++++++ .../reference/core/service/NewsService.java | 6 +- 8 files changed, 429 insertions(+), 7 deletions(-) create mode 100644 core/src/main/java/com/jinrui/reference/core/model/dto/news/SaveIndustryDTO.java create mode 100644 core/src/main/java/com/jinrui/reference/core/model/vo/tag/IndustryVO.java diff --git a/admin/src/main/java/com/jinrui/reference/admin/controller/IndustryController.java b/admin/src/main/java/com/jinrui/reference/admin/controller/IndustryController.java index 859e424..79f5f0f 100644 --- a/admin/src/main/java/com/jinrui/reference/admin/controller/IndustryController.java +++ b/admin/src/main/java/com/jinrui/reference/admin/controller/IndustryController.java @@ -2,9 +2,21 @@ package com.jinrui.reference.admin.controller; 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.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; +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; +import com.jinrui.reference.admin.model.entity.AdminUser; +import com.jinrui.reference.admin.service.AdminJwtService; +import com.jinrui.reference.core.model.dto.news.SaveIndustryDTO; +import com.jinrui.reference.core.model.vo.PageObject; +import com.jinrui.reference.core.model.vo.ResultObject; +import com.jinrui.reference.core.model.vo.tag.IndustryVO; import com.jinrui.reference.core.service.IndustryService; /** @@ -20,5 +32,98 @@ public class IndustryController { public IndustryController(IndustryService industryService) { this.industryService = industryService; } + + /** + * 创建频道标签 + * + * @param token + * @return + */ + @PostMapping("/create") + public ResultObject create(@RequestHeader("auth-token") String token, + @RequestBody SaveIndustryDTO saveIndustryDTO + ) { + AdminUser adminUser = AdminJwtService.parseToken(token); + if (adminUser == null) { + log.warn("解析token {}拿不到AdminUser对象!", token); + return ResultObject.failed("登陆Token有误,请联系系统管理员!"); + } + + if (!adminUser.isActive()) { + log.warn("当前用户已被封禁! id = {}", adminUser.getId()); + return ResultObject.failed("当前用户已被封禁!请联系系统管理员!"); + } + + return industryService.create(saveIndustryDTO.getPrimaryName(), saveIndustryDTO.getSecondaryName()); + } + + /** + * 行业分类删除 + * @param token + * @param saveTagsDTO + * @return + */ + @PostMapping("/delete") + public ResultObject delete(@RequestHeader("auth-token") String token, + @RequestBody SaveIndustryDTO saveIndustryDTO + ) { + AdminUser adminUser = AdminJwtService.parseToken(token); + if (adminUser == null) { + log.warn("解析token {}拿不到AdminUser对象!", token); + return ResultObject.failed("登陆Token有误,请联系系统管理员!"); + } + + if (!adminUser.isActive()) { + log.warn("当前用户已被封禁! id = {}", adminUser.getId()); + return ResultObject.failed("当前用户已被封禁!请联系系统管理员!"); + } + + return industryService.delete(saveIndustryDTO.getId()); + } + + /** + * 行业分类搜索接口 + * + * @param token 登陆Token + * @param keyword 关键字 可以为空 + * @param page 分页参数 当前页码 默认第一页 + * @param size 分页参数 当前页长度 默认10条 + * @param orderBy 排序参数 要排序的字段 + * @param direction 排序参数 排序方向 + * @return 标签搜索结果 + */ + @GetMapping + public PageObject queryIndustry(@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) { + 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: {}, " + + "keyword: {}, exclude: {}, page: {}, size: {}, orderBy: {}, direction: {}", + adminUser.getId(), keyword, page, size, orderBy, direction); + } catch (Exception e) { + log.error("解析登陆Token出错!", e); + return PageObject.failedPage(500, "服务端错误,请联系系统管理员!"); + } + + return industryService.queryIndustry(keyword, page, size, orderBy, direction); + } } diff --git a/core/src/main/java/com/jinrui/reference/core/mapper/IndustryMapper.java b/core/src/main/java/com/jinrui/reference/core/mapper/IndustryMapper.java index 2aec1be..06e23db 100644 --- a/core/src/main/java/com/jinrui/reference/core/mapper/IndustryMapper.java +++ b/core/src/main/java/com/jinrui/reference/core/mapper/IndustryMapper.java @@ -3,6 +3,9 @@ package com.jinrui.reference.core.mapper; import java.util.Date; import java.util.List; +import org.apache.ibatis.annotations.Delete; +import org.apache.ibatis.annotations.Insert; +import org.apache.ibatis.annotations.Options; import org.apache.ibatis.annotations.Param; import org.apache.ibatis.annotations.Result; import org.apache.ibatis.annotations.Results; @@ -31,4 +34,49 @@ public interface IndustryMapper { @Select("select id, news_id as newsId, industry_id as industryId " + "from news_industry_rel where news_id = #{newsId}") List getNewsIndustryRelList(@Param("newsId") Long newsId); + + @Select("select * from industry where primary_name = #{primayName} and parent_id = 0") + Industry queryPrimaryIndustry(String primayName); + + @Select("select * from industry where secondary_name = #{secondaryName} and parent_id = #{parentId}") + Industry querySecondaryIndustry(Long parentId,String secondaryName); + + @Options(useGeneratedKeys = true, keyProperty = "id", keyColumn = "id") + @Insert("insert into industry(parent_id, primary_name, secondary_name, create_time, update_time) values (#{parentId}, #{primaryName}, #{secondaryName}, now(), now())") + void saveIndustry(Industry industry); + + @Delete("delete from industry where id = #{id}") + void deleteIndustry(@Param("id") Long id); + + // 获取行业分类是否有关联过资讯 + @Select("SELECT count(id) " + + "from news_industry_rel where industry_id = #{industryId}") + Long countNewsIndustryRelList(@Param("industryId") Long industryId); + + // 获取行业分类是否有关联过资讯 + @Select("SELECT count(id) " + + "from draft_industry_rel where industry_id = #{industryId}") + Long countDraftIndustryRelList(@Param("industryId") Long industryId); + + @Results({ + @Result(column = "id", property = "id", id = true), + @Result(column = "parent_id", property = "parentId"), + @Result(column = "primay_name", property = "primayName"), + @Result(column = "secondary_name", property = "secondaryName"), + @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("") + List queryIndustry(@Param("keyword") String keyword, @Param("orderBy") String orderBy, @Param("direction") String direction); } diff --git a/core/src/main/java/com/jinrui/reference/core/model/dto/news/SaveIndustryDTO.java b/core/src/main/java/com/jinrui/reference/core/model/dto/news/SaveIndustryDTO.java new file mode 100644 index 0000000..8d044f5 --- /dev/null +++ b/core/src/main/java/com/jinrui/reference/core/model/dto/news/SaveIndustryDTO.java @@ -0,0 +1,32 @@ +package com.jinrui.reference.core.model.dto.news; + +@SuppressWarnings("unused") +public class SaveIndustryDTO { + private Long id; + private String primaryName; + private String secondaryName; + + public Long getId() { + return id; + } + + public void setId(Long id) { + this.id = id; + } + + public String getPrimaryName() { + return primaryName; + } + + public void setPrimaryName(String primaryName) { + this.primaryName = primaryName; + } + + public String getSecondaryName() { + return secondaryName; + } + + public void setSecondaryName(String secondaryName) { + this.secondaryName = secondaryName; + } +} diff --git a/core/src/main/java/com/jinrui/reference/core/model/entity/Industry.java b/core/src/main/java/com/jinrui/reference/core/model/entity/Industry.java index 8625299..22ecb43 100644 --- a/core/src/main/java/com/jinrui/reference/core/model/entity/Industry.java +++ b/core/src/main/java/com/jinrui/reference/core/model/entity/Industry.java @@ -2,6 +2,8 @@ package com.jinrui.reference.core.model.entity; import java.util.Date; +import org.apache.ibatis.annotations.Param; + /** * 行业分类 */ @@ -11,6 +13,11 @@ public class Industry { * 行业分类ID */ private Long id; + + /** + * 一级分类ID + */ + private Long parentId; /** * 一级行业 @@ -39,6 +46,14 @@ public class Industry { public void setId(Long id) { this.id = id; } + + public Long getParentId() { + return parentId; + } + + public void setParentId(Long parentId) { + this.parentId = parentId; + } public String getPrimayName() { return primayName; diff --git a/core/src/main/java/com/jinrui/reference/core/model/vo/news/NewsDetailIndustry.java b/core/src/main/java/com/jinrui/reference/core/model/vo/news/NewsDetailIndustry.java index 4c31a56..0d50a07 100644 --- a/core/src/main/java/com/jinrui/reference/core/model/vo/news/NewsDetailIndustry.java +++ b/core/src/main/java/com/jinrui/reference/core/model/vo/news/NewsDetailIndustry.java @@ -4,7 +4,9 @@ package com.jinrui.reference.core.model.vo.news; public class NewsDetailIndustry { private Long id; - private String name; + private String primayName; + + private String secondaryName; public Long getId() { return id; @@ -14,11 +16,19 @@ public class NewsDetailIndustry { this.id = id; } - public String getName() { - return name; + public String getPrimayName() { + return primayName; } - public void setName(String name) { - this.name = name; + public void setPrimayName(String primayName) { + this.primayName = primayName; + } + + public String getSecondaryName() { + return secondaryName; + } + + public void setSecondaryName(String secondaryName) { + this.secondaryName = secondaryName; } } diff --git a/core/src/main/java/com/jinrui/reference/core/model/vo/tag/IndustryVO.java b/core/src/main/java/com/jinrui/reference/core/model/vo/tag/IndustryVO.java new file mode 100644 index 0000000..ba90ce1 --- /dev/null +++ b/core/src/main/java/com/jinrui/reference/core/model/vo/tag/IndustryVO.java @@ -0,0 +1,103 @@ +package com.jinrui.reference.core.model.vo.tag; + +import java.util.ArrayList; +import java.util.Date; +import java.util.List; + +import com.fasterxml.jackson.annotation.JsonFormat; +import com.jinrui.reference.core.model.entity.Industry; +import com.jinrui.reference.core.model.entity.Tag; + +/** + * 行业分类 + */ +@SuppressWarnings("unused") +public class IndustryVO { + /** + * 行业分类ID + */ + private Long id; + + private Long parentId; + + /** + * 一级行业名称 + */ + private String primayName; + + /** + * 二级行业名称 + */ + private String secondaryName; + + /** + * 创建时间 + */ + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Shanghai") + private Date createTime; + + /** + * 修改时间 + */ + @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "Asia/Shanghai") + private Date updateTime; + + + public IndustryVO(Industry industry) { + this.id = industry.getId(); + this.parentId = industry.getParentId(); + this.primayName = industry.getPrimayName(); + this.secondaryName = industry.getSecondaryName(); + this.createTime = industry.getCreateTime(); + this.updateTime = industry.getUpdateTime(); + } + + 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 getPrimayName() { + return primayName; + } + + public void setPrimayName(String primayName) { + this.primayName = primayName; + } + + public String getSecondaryName() { + return secondaryName; + } + + public void setSecondaryName(String secondaryName) { + this.secondaryName = secondaryName; + } + + 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; + } + +} diff --git a/core/src/main/java/com/jinrui/reference/core/service/IndustryService.java b/core/src/main/java/com/jinrui/reference/core/service/IndustryService.java index a03ba68..5cecf8b 100644 --- a/core/src/main/java/com/jinrui/reference/core/service/IndustryService.java +++ b/core/src/main/java/com/jinrui/reference/core/service/IndustryService.java @@ -1,11 +1,22 @@ package com.jinrui.reference.core.service; +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.stereotype.Service; +import org.springframework.util.CollectionUtils; +import org.springframework.util.StringUtils; +import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.ObjectMapper; import com.jinrui.reference.core.mapper.IndustryMapper; +import com.jinrui.reference.core.model.entity.Industry; +import com.jinrui.reference.core.model.vo.PageObject; +import com.jinrui.reference.core.model.vo.ResultObject; +import com.jinrui.reference.core.model.vo.tag.IndustryVO; @Service public class IndustryService { @@ -18,5 +29,101 @@ public class IndustryService { this.objectMapper = objectMapper; this.industryMapper = industryMapper; } + + public ResultObject create(String primaryName, String secondaryName) { + Industry industry = industryMapper.queryPrimaryIndustry(primaryName); + if (industry == null) { + Industry primaryIndustry = new Industry(); + primaryIndustry.setParentId(0L); + primaryIndustry.setPrimayName(primaryName); + industryMapper.saveIndustry(primaryIndustry); + industry = primaryIndustry; + } + + Industry secondaryIndustry = industryMapper.querySecondaryIndustry(industry.getId(), secondaryName); + + if(secondaryIndustry != null){ + return ResultObject.failed("该行业分类已存在!"); + } + + secondaryIndustry = new Industry(); + secondaryIndustry.setParentId(industry.getId()); + secondaryIndustry.setPrimayName(primaryName); + secondaryIndustry.setSecondaryName(secondaryName); + industryMapper.saveIndustry(secondaryIndustry); + return ResultObject.success(); + } + + public ResultObject delete(Long id) { + + if(industryMapper.countNewsIndustryRelList(id)> 0 || industryMapper.countDraftIndustryRelList(id)>0){ + return ResultObject.failed("行业分类已经关联过资讯,无法删除"); + } + + industryMapper.deleteIndustry(id); + return ResultObject.success(); + } + + /** + * 标签搜索接口 + * + * @param keyword 关键字 可以为空 + * @param page 分页参数 当前页码 默认第一页 + * @param size 分页参数 当前页长度 默认10条 + * @return 标签搜索结果 + */ + public PageObject queryIndustry(String keyword,int page, int size, String orderBy, String direction) { + if (StringUtils.hasText(orderBy)) { + switch (orderBy) { + case "updateTime": { + orderBy = "update_time"; + break; + } + case "createTime": { + orderBy = "create_time"; + break; + } + } + } + + List industrys; + try { + industrys = industryMapper.queryIndustry(keyword, orderBy, direction); + } catch (Exception e) { + log.error("搜索行业分类出错!", e); + return PageObject.failedPage(500, "服务端错误,请联系系统管理员!"); + } + + if (CollectionUtils.isEmpty(industrys)) { + log.warn("keyword: {}, 找不到对应的行业分类!", keyword); + return PageObject.empty(page); + } + + PageObject resultPage = new PageObject<>(); + resultPage.setCode(200); + resultPage.setTotal(industrys.size()); + List resultList = industrys.stream() + .skip((long) (page - 1) * size) + .limit(size) + .map(IndustryVO::new) + .collect(Collectors.toList()); + if (CollectionUtils.isEmpty(resultList)) { + log.info("keyword: {}, page: {}, size: {}, 当前页无数据!", keyword, page, size); + resultPage.setCurrent(page); + resultPage.setSize(0); + resultPage.setData(new ArrayList<>()); + return resultPage; + } + + resultPage.setData(resultList); + + try { + String pageString = objectMapper.writeValueAsString(resultPage); + log.info("查询结果: {}", pageString); + } catch (JsonProcessingException e) { + log.error("查询行业分类返回值json映射出错!", e); + } + return resultPage; + } } diff --git a/core/src/main/java/com/jinrui/reference/core/service/NewsService.java b/core/src/main/java/com/jinrui/reference/core/service/NewsService.java index cfced35..8531de0 100644 --- a/core/src/main/java/com/jinrui/reference/core/service/NewsService.java +++ b/core/src/main/java/com/jinrui/reference/core/service/NewsService.java @@ -191,7 +191,8 @@ public class NewsService { Industry industry = industryMap.get(industryId); NewsDetailIndustry newsDetailIndustry = new NewsDetailIndustry(); newsDetailIndustry.setId(industryId); - newsDetailIndustry.setName(industry.getPrimayName() + "-" + industry.getSecondaryName()); + newsDetailIndustry.setPrimayName(industry.getPrimayName()); + newsDetailIndustry.setSecondaryName(industry.getSecondaryName()); newsIndustryList.add(newsDetailIndustry); } } @@ -301,7 +302,8 @@ public class NewsService { Industry industry = industryMap.get(industryId); NewsDetailIndustry newsDetailIndustry = new NewsDetailIndustry(); newsDetailIndustry.setId(industryId); - newsDetailIndustry.setName(industry.getPrimayName() + "-" + industry.getSecondaryName()); + newsDetailIndustry.setPrimayName(industry.getPrimayName()); + newsDetailIndustry.setSecondaryName(industry.getSecondaryName()); newsIndustryList.add(newsDetailIndustry); } }