Files
tuanwei-java/src/main/java/com/gxwebsoft/cms/controller/ArticleController.java
2025-05-22 10:27:57 +08:00

167 lines
5.7 KiB
Java

package com.gxwebsoft.cms.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.gxwebsoft.cms.entity.ArticleCategory;
import com.gxwebsoft.cms.service.ArticleCategoryService;
import com.gxwebsoft.common.core.web.BaseController;
import com.gxwebsoft.cms.service.ArticleService;
import com.gxwebsoft.cms.entity.Article;
import com.gxwebsoft.cms.param.ArticleParam;
import com.gxwebsoft.common.core.web.ApiResult;
import com.gxwebsoft.common.core.web.PageResult;
import com.gxwebsoft.common.core.web.PageParam;
import com.gxwebsoft.common.core.web.BatchParam;
import com.gxwebsoft.common.core.annotation.OperationLog;
import com.gxwebsoft.common.system.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
/**
* 文章记录表控制器
*
* @author WebSoft
* @since 2022-11-16 11:40:27
*/
@Api(tags = "文章记录表管理")
@RestController
@RequestMapping("/api/cms/article")
public class ArticleController extends BaseController {
@Resource
private ArticleService articleService;
@Resource
private ArticleCategoryService articleCategoryService;
@ApiOperation("根据id查询文章记录表")
@PostMapping("/info")
public ApiResult<Article> getInfo(@RequestBody ArticleParam articleParam) {
Article article = articleService.getByIdRel(articleParam.getArticleId());
return success(article);
}
@PreAuthorize("hasAuthority('cms:article:list')")
@OperationLog
@ApiOperation("分页查询文章记录表")
@GetMapping("/page")
public ApiResult<PageResult<Article>> page(ArticleParam param) {
// 使用关联查询
return success(articleService.pageRel(param));
}
@OperationLog
@ApiOperation("查询全部文章记录表")
@GetMapping()
public ApiResult<List<Article>> list(ArticleParam param) {
LambdaQueryWrapper<Article> queryWrapper = new LambdaQueryWrapper<>();
List<Integer> categoryIds = new ArrayList<>();
if (param.getCategoryId() != null) {
List<ArticleCategory> childCateList = articleCategoryService.childList(param.getCategoryId());
if (!childCateList.isEmpty()) {
for (ArticleCategory cate : childCateList) {
categoryIds.add(cate.getCategoryId());
}
}
categoryIds.add(param.getCategoryId());
}
queryWrapper.orderByAsc(Article::getSortNumber)
.orderByDesc(Article::getCreateTime);
if (!categoryIds.isEmpty()) {
queryWrapper.in(Article::getCategoryId, categoryIds);
}
List<Article> articleList = articleService.list(queryWrapper);
return success(articleList);
}
@PreAuthorize("hasAuthority('cms:article:list')")
@OperationLog
@ApiOperation("根据id查询文章记录表")
@GetMapping("/{id}")
public ApiResult<Article> get(@PathVariable("id") Integer id) {
// return success(articleService.getById(id));
// 使用关联查询
Article article = articleService.getByIdRel(id);
article.setArticleId(id);
article.setVirtualViews(article.getVirtualViews() + 1);
articleService.saveOrUpdate(article);
return success(article);
}
@PreAuthorize("hasAuthority('cms:article:save')")
@OperationLog
@ApiOperation("添加文章记录表")
@PostMapping()
public ApiResult<?> save(@RequestBody Article article) {
// 记录当前登录用户id、租户id
User loginUser = getLoginUser();
if (loginUser != null) {
article.setUserId(loginUser.getUserId());
}
if (articleService.save(article)) {
return success("添加成功");
}
return fail("添加失败");
}
@PreAuthorize("hasAuthority('cms:article:update')")
@OperationLog
@ApiOperation("修改文章记录表")
@PutMapping()
public ApiResult<?> update(@RequestBody Article article) {
if (articleService.updateById(article)) {
return success("修改成功");
}
return fail("修改失败");
}
@PreAuthorize("hasAuthority('cms:article:remove')")
@OperationLog
@ApiOperation("删除文章记录表")
@DeleteMapping("/{id}")
public ApiResult<?> remove(@PathVariable("id") Integer id) {
if (articleService.removeById(id)) {
return success("删除成功");
}
return fail("删除失败");
}
@PreAuthorize("hasAuthority('cms:article:save')")
@OperationLog
@ApiOperation("批量添加文章记录表")
@PostMapping("/batch")
public ApiResult<?> saveBatch(@RequestBody List<Article> list) {
if (articleService.saveBatch(list)) {
return success("添加成功");
}
return fail("添加失败");
}
@PreAuthorize("hasAuthority('cms:article:update')")
@OperationLog
@ApiOperation("批量修改文章记录表")
@PutMapping("/batch")
public ApiResult<?> removeBatch(@RequestBody BatchParam<Article> batchParam) {
if (batchParam.update(articleService, "article_id")) {
return success("修改成功");
}
return fail("修改失败");
}
@PreAuthorize("hasAuthority('cms:article:remove')")
@OperationLog
@ApiOperation("批量删除文章记录表")
@DeleteMapping("/batch")
public ApiResult<?> removeBatch(@RequestBody List<Integer> ids) {
if (articleService.removeByIds(ids)) {
return success("删除成功");
}
return fail("删除失败");
}
}