From d6ff1bc6ce5cc7088abbde238ab01fa278c63ae2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=B8=A9=E6=96=87=E9=9D=99WWW?= <15144434+wen-wenjing-www@user.noreply.gitee.com> Date: Wed, 23 Jul 2025 10:16:44 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9=E6=96=87=E7=AB=A0=E5=AE=8C?= =?UTF-8?q?=E5=96=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/CpArticleController.java | 87 +++------------- .../article/domain/dto/CpArticleDTO.java | 14 +-- .../article/mapper/CpArticleMapper.java | 5 +- .../modules/article/mapper/CpLabelMapper.java | 2 + .../article/service/CpArticleService.java | 7 +- .../service/impl/CpArticleServiceImpl.java | 99 +++++++++++++++++-- .../mapper/article/CpArticleMapper.xml | 34 +++++-- .../mapper/article/CpLabelMapper.xml | 7 ++ 8 files changed, 158 insertions(+), 97 deletions(-) diff --git a/aircraft-system/src/main/java/com/aircraft/modules/article/controller/CpArticleController.java b/aircraft-system/src/main/java/com/aircraft/modules/article/controller/CpArticleController.java index 6166051..e1c3f94 100644 --- a/aircraft-system/src/main/java/com/aircraft/modules/article/controller/CpArticleController.java +++ b/aircraft-system/src/main/java/com/aircraft/modules/article/controller/CpArticleController.java @@ -51,23 +51,19 @@ public class CpArticleController { @Autowired private CpArticleService entityService; @Autowired - private CpArticleMapper cpArticleMapper; - @Autowired private CpTextService cpTextService; @Autowired private CpLabelMapper cpLabelMapper; - @ApiOperation(value = "分页查询文章(支持标题模糊查询)", notes = "分页查询文章,仅根据文章标题进行模糊查询") + @ApiOperation(value = "分页查询文章(title模糊查询)", notes = "分页查询文章,仅根据文章标题进行模糊查询") @RequestMapping(method = RequestMethod.GET) @ApiImplicitParams({ @ApiImplicitParam(name = "size", value = "分页大小", paramType = "query"), - @ApiImplicitParam(name = "current", value = "当前页面:从1开始", paramType = "query"), - @ApiImplicitParam(name = "titleKeyword", value = "搜索关键词,用于模糊查询文章标题", paramType = "query") + @ApiImplicitParam(name = "current", value = "当前页面:从1开始", paramType = "query") }) public ResponseEntity> findByPage( - final Page page, - @RequestParam(required = false) String titleKeyword) { - IPage records = entityService.findByPage(page, titleKeyword); + final Page page, CpArticleDTO cpArticleDTO) { + IPage records = entityService.findByPage(page, cpArticleDTO); return new ResponseEntity<>(records, HttpStatus.OK); } @@ -121,6 +117,12 @@ public class CpArticleController { @ApiOperation(value = "添加文章") @RequestMapping(method = {RequestMethod.POST}) + @ApiImplicitParams({ + @ApiImplicitParam(name = "title", value = "文章标题", required = true, paramType = "query"), + @ApiImplicitParam(name = "text", value = "文章正文内容", required = true, paramType = "query"), + @ApiImplicitParam(name = "labelId", value = "标签id", required = true, paramType = "query"), + @ApiImplicitParam(name = "moduleId", value = "模块id", required = true, paramType = "query") + }) public ResponseEntity add(@Valid @RequestBody final CpArticleDTO cpArticleDTO, final BindingResult result) { try { // 处理参数校验错误 @@ -197,72 +199,9 @@ public class CpArticleController { @ApiOperation(value = "修改文章") @RequestMapping(method = {RequestMethod.PUT}) - public ResponseEntity update(@Valid @RequestBody final CpArticleVo entity, final BindingResult result) { - try { - // 处理参数校验错误 - if (result.hasErrors()) { - Map error = new HashMap<>(); - error.put("code", 400); - // 提取第一个错误信息 - FieldError firstError = result.getFieldErrors().get(0); - error.put("msg", firstError.getField() + ":" + firstError.getDefaultMessage()); - return ResponseEntity.badRequest().body(error); - } - - // 校验ID是否存在 - if (entity.getId() == null) { - Map error = new HashMap<>(); - error.put("code", 400); - error.put("msg", "id不能为空"); - return ResponseEntity.badRequest().body(error); - } - - // 查找标签信息 - CpLabel cpLabelVo = cpLabelMapper.findById(entity.getCplabelId()); - if (cpLabelVo == null) { - Map error = new HashMap<>(); - error.put("code", 400); - error.put("msg", "标签不存在"); - return ResponseEntity.badRequest().body(error); - } - - // 校验文章是否存在 - CpArticle existingArticle = entityService.getById(entity.getId()); - if (existingArticle == null) { - Map error = new HashMap<>(); - error.put("code", 404); - error.put("msg", "文章不存在"); - return ResponseEntity.status(404).body(error); - } - - // 更新文章内容 - if (existingArticle.getTextid() == null) { - Map error = new HashMap<>(); - error.put("code", 400); - error.put("msg", "文章内容ID不存在"); - return ResponseEntity.badRequest().body(error); - } - boolean textUpdated = cpTextService.lambdaUpdate() - .set(CpText::getText, entity.getText()) - .eq(CpText::getId, existingArticle.getTextid()) - .update(); - if (!textUpdated) { - Map error = new HashMap<>(); - error.put("code", 500); - error.put("msg", "文章内容更新失败"); - return ResponseEntity.status(500).body(error); - } - - // 成功响应:返回更新后的文章数据 - return ResponseEntity.ok(entity); - - } catch (Exception e) { - LOG.error("修改文章失败", e); - Map error = new HashMap<>(); - error.put("code", 500); - error.put("msg", "修改文章失败:" + e.getMessage()); - return ResponseEntity.status(500).body(error); - } + @ApiImplicitParam(name = "id", value = "文章id", required = true, paramType = "path") + public ResponseEntity update(@Valid @RequestBody final CpArticleDTO entity, final BindingResult result) { + return entityService.updateById(entity, result); } @ApiOperation(value = "全部文章") diff --git a/aircraft-system/src/main/java/com/aircraft/modules/article/domain/dto/CpArticleDTO.java b/aircraft-system/src/main/java/com/aircraft/modules/article/domain/dto/CpArticleDTO.java index 6a766b9..972653d 100644 --- a/aircraft-system/src/main/java/com/aircraft/modules/article/domain/dto/CpArticleDTO.java +++ b/aircraft-system/src/main/java/com/aircraft/modules/article/domain/dto/CpArticleDTO.java @@ -2,12 +2,13 @@ package com.aircraft.modules.article.domain.dto; import io.swagger.annotations.ApiModelProperty; import lombok.Data; -import javax.validation.constraints.NotBlank; @Data public class CpArticleDTO { - @NotBlank(message = "标题不能为空") - @ApiModelProperty(value = "文章标题", required = true) + @ApiModelProperty(value = "id") + private Long id; + + @ApiModelProperty(value = "文章标题") private String title; @ApiModelProperty(value = "封面图片URL") @@ -19,14 +20,13 @@ public class CpArticleDTO { @ApiModelProperty(value = "文章外链") private String url; - @ApiModelProperty(value = "标签ID", required = true) + @ApiModelProperty(value = "标签ID") private int labelId; - @ApiModelProperty(value = "模块ID", required = true) + @ApiModelProperty(value = "模块ID") private int moduleId; - @NotBlank(message = "文章内容不能为空") - @ApiModelProperty(value = "文章正文内容", required = true) + @ApiModelProperty(value = "文章正文内容") private String text; @ApiModelProperty(value = "是否置顶:1-置顶,0-不置顶") diff --git a/aircraft-system/src/main/java/com/aircraft/modules/article/mapper/CpArticleMapper.java b/aircraft-system/src/main/java/com/aircraft/modules/article/mapper/CpArticleMapper.java index f528b6a..644e303 100644 --- a/aircraft-system/src/main/java/com/aircraft/modules/article/mapper/CpArticleMapper.java +++ b/aircraft-system/src/main/java/com/aircraft/modules/article/mapper/CpArticleMapper.java @@ -1,6 +1,7 @@ package com.aircraft.modules.article.mapper; import com.aircraft.modules.article.domain.CpArticle; +import com.aircraft.modules.article.domain.dto.CpArticleDTO; import com.aircraft.modules.article.domain.dto.CpArticleVo; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; import com.baomidou.mybatisplus.core.mapper.BaseMapper; @@ -22,9 +23,11 @@ public interface CpArticleMapper extends BaseMapper { void updateDelFlagById(Long id, Integer delFlag); - IPage selectVoPage(Page page, String titleKeyword); + IPage selectVoPage(Page page, CpArticleDTO dto); CpArticleVo selectVoById(Integer id); void updateViewCountById(Integer id); + + void updateById(CpArticleDTO entity); } diff --git a/aircraft-system/src/main/java/com/aircraft/modules/article/mapper/CpLabelMapper.java b/aircraft-system/src/main/java/com/aircraft/modules/article/mapper/CpLabelMapper.java index b6c099e..c234048 100644 --- a/aircraft-system/src/main/java/com/aircraft/modules/article/mapper/CpLabelMapper.java +++ b/aircraft-system/src/main/java/com/aircraft/modules/article/mapper/CpLabelMapper.java @@ -32,4 +32,6 @@ public interface CpLabelMapper extends BaseMapper { CpLabel findById(int labelId); IPage selectVoPage(Page page, String name); + + void updateModuleIdById(int moduleId); } diff --git a/aircraft-system/src/main/java/com/aircraft/modules/article/service/CpArticleService.java b/aircraft-system/src/main/java/com/aircraft/modules/article/service/CpArticleService.java index a7cdc11..cf1e455 100644 --- a/aircraft-system/src/main/java/com/aircraft/modules/article/service/CpArticleService.java +++ b/aircraft-system/src/main/java/com/aircraft/modules/article/service/CpArticleService.java @@ -1,10 +1,13 @@ package com.aircraft.modules.article.service; import com.aircraft.modules.article.domain.CpArticle; +import com.aircraft.modules.article.domain.dto.CpArticleDTO; import com.aircraft.modules.article.domain.dto.CpArticleVo; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.IService; +import org.springframework.http.ResponseEntity; +import org.springframework.validation.BindingResult; import java.util.List; @@ -22,7 +25,9 @@ public interface CpArticleService extends IService { void updateDelFlagById(Long id, Integer delFlag); - IPage findByPage(Page page, String titleKeyword); + IPage findByPage(Page page, CpArticleDTO cpArticleDTO); CpArticleVo getArticleById(Integer id); + + ResponseEntity updateById(CpArticleDTO entity, BindingResult result); } diff --git a/aircraft-system/src/main/java/com/aircraft/modules/article/service/impl/CpArticleServiceImpl.java b/aircraft-system/src/main/java/com/aircraft/modules/article/service/impl/CpArticleServiceImpl.java index 98a6f21..f99baf3 100644 --- a/aircraft-system/src/main/java/com/aircraft/modules/article/service/impl/CpArticleServiceImpl.java +++ b/aircraft-system/src/main/java/com/aircraft/modules/article/service/impl/CpArticleServiceImpl.java @@ -1,25 +1,31 @@ package com.aircraft.modules.article.service.impl; +import com.aircraft.modules.article.controller.CpArticleController; import com.aircraft.modules.article.domain.CpArticle; import com.aircraft.modules.article.domain.CpLabel; +import com.aircraft.modules.article.domain.CpText; +import com.aircraft.modules.article.domain.dto.CpArticleDTO; import com.aircraft.modules.article.domain.dto.CpArticleVo; import com.aircraft.modules.article.mapper.CpArticleMapper; import com.aircraft.modules.article.mapper.CpLabelMapper; -import com.aircraft.modules.article.mapper.CpModuleMapper; -import com.aircraft.modules.article.mapper.CpTextMapper; import com.aircraft.modules.article.service.CpArticleService; +import com.aircraft.modules.article.service.CpTextService; import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper; import com.baomidou.mybatisplus.core.metadata.IPage; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; -import org.springframework.beans.BeanUtils; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; -import org.springframework.util.StringUtils; +import org.springframework.validation.BindingResult; +import org.springframework.validation.FieldError; +import java.util.HashMap; import java.util.List; -import java.util.stream.Collectors; +import java.util.Map; /** *

@@ -31,13 +37,19 @@ import java.util.stream.Collectors; */ @Service public class CpArticleServiceImpl extends ServiceImpl implements CpArticleService { + private static final Logger LOG = LoggerFactory.getLogger(CpArticleController.class); + @Autowired private CpArticleMapper cpArticleMapper; + @Autowired + private CpLabelMapper cpLabelMapper; + @Autowired + private CpTextService cpTextService; @Override - public IPage findByPage(Page page, String titleKeyword) { + public IPage findByPage(Page page, CpArticleDTO cpArticleDTO) { // 执行分页查询 - IPage cpArticleVoIPage = cpArticleMapper.selectVoPage(page, titleKeyword); + IPage cpArticleVoIPage = cpArticleMapper.selectVoPage(page, cpArticleDTO); return cpArticleVoIPage; } @@ -47,6 +59,77 @@ public class CpArticleServiceImpl extends ServiceImpl updateById(CpArticleDTO entity, BindingResult result) { + try { + // 处理参数校验错误 + if (result.hasErrors()) { + Map error = new HashMap<>(); + error.put("code", 400); + // 提取第一个错误信息 + FieldError firstError = result.getFieldErrors().get(0); + error.put("msg", firstError.getField() + ":" + firstError.getDefaultMessage()); + return ResponseEntity.badRequest().body(error); + } + + // 校验ID是否存在 + if (entity.getId() == null) { + Map error = new HashMap<>(); + error.put("code", 400); + error.put("msg", "id不能为空"); + return ResponseEntity.badRequest().body(error); + } + + // 查找标签信息 + CpLabel cpLabelVo = cpLabelMapper.findById(entity.getLabelId()); + if (cpLabelVo == null) { + Map error = new HashMap<>(); + error.put("code", 400); + error.put("msg", "标签不存在"); + return ResponseEntity.badRequest().body(error); + } + //更新模块信息 + cpLabelMapper.updateModuleIdById(entity.getModuleId()); + + // 校验文章是否存在 + CpArticle existingArticle = getById(entity.getId()); + if (existingArticle == null) { + Map error = new HashMap<>(); + error.put("code", 404); + error.put("msg", "文章不存在"); + return ResponseEntity.status(404).body(error); + } + + // 更新文章内容 + if (existingArticle.getTextid() == null) { + Map error = new HashMap<>(); + error.put("code", 400); + error.put("msg", "文章内容ID不存在"); + return ResponseEntity.badRequest().body(error); + } + boolean textUpdated = cpTextService.lambdaUpdate() + .set(CpText::getText, entity.getText()) + .eq(CpText::getId, existingArticle.getTextid()) + .update(); + if (!textUpdated) { + Map error = new HashMap<>(); + error.put("code", 500); + error.put("msg", "文章内容更新失败"); + return ResponseEntity.status(500).body(error); + } + + cpArticleMapper.updateById(entity); + return ResponseEntity.ok().build(); + + } catch (Exception e) { + LOG.error("修改文章失败", e); + Map error = new HashMap<>(); + error.put("code", 500); + error.put("msg", "修改文章失败:" + e.getMessage()); + return ResponseEntity.status(500).body(error); + } + } + @Override public List list(CpArticle example) { // 转换example为QueryWrapper条件 diff --git a/aircraft-system/src/main/resources/mapper/article/CpArticleMapper.xml b/aircraft-system/src/main/resources/mapper/article/CpArticleMapper.xml index 525082d..98f5a4a 100644 --- a/aircraft-system/src/main/resources/mapper/article/CpArticleMapper.xml +++ b/aircraft-system/src/main/resources/mapper/article/CpArticleMapper.xml @@ -20,17 +20,23 @@ a.*, t.text, l.name AS labelName, - m.id AS moduleId, -- 映射模块ID - m.module_name AS moduleName -- 映射模块名称 + m.id AS moduleId, -- 映射模块ID + m.module_name AS moduleName -- 映射模块名称 FROM cp_article a LEFT JOIN cp_text t ON a.textid = t.id LEFT JOIN cp_label l ON a.cplabel_id = l.id - LEFT JOIN cp_module m ON l.module_id = m.id -- 添加模块表关联 + LEFT JOIN cp_module m ON l.module_id = m.id -- 添加模块表关联 WHERE a.del_flag = 0 - - AND a.title LIKE CONCAT('%', #{titleKeyword}, '%') + + AND a.title LIKE CONCAT('%', #{dto.title}, '%') - ORDER BY create_time DESC + + AND m.id =#{dto.moduleId} + + + AND l.id =#{dto.labelId} + + ORDER BY a.create_time DESC @@ -55,4 +61,20 @@ WHERE id = #{id} AND del_flag = 0 -- 确保只更新未删除的文章 + + + + UPDATE cp_article + + title = #{title}, + photo = #{photo}, + article_type = #{articleType}, + url = #{url}, + cplabel_id = #{labelId}, + top = #{top}, + update_time = NOW() + + WHERE id = #{id} + AND del_flag = 0 + diff --git a/aircraft-system/src/main/resources/mapper/article/CpLabelMapper.xml b/aircraft-system/src/main/resources/mapper/article/CpLabelMapper.xml index 746bfc5..28bb1ac 100644 --- a/aircraft-system/src/main/resources/mapper/article/CpLabelMapper.xml +++ b/aircraft-system/src/main/resources/mapper/article/CpLabelMapper.xml @@ -28,4 +28,11 @@ ORDER BY create_time DESC + + + UPDATE cp_label + SET module_id = #{moduleId} + WHERE id = #{id} + AND del_flag = 0 +