Files
.shoudate/demo4/src/main/java/com/example/demo/controller/IndustryNewsController.java
2026-04-23 17:03:40 +08:00

108 lines
3.4 KiB
Java

package com.example.demo.controller;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.demo.entity.IndustryNews;
import com.example.demo.response.R;
import com.example.demo.service.IndustryNewsService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@RestController
@RequestMapping("/industry")
public class IndustryNewsController {
@Resource
private IndustryNewsService industryNewsService;
/**
* 新增行业动态
* @return
*/
@Operation(summary = "新增行业动态")
@PostMapping("/add")
@CrossOrigin
public R add(@RequestBody IndustryNews industryNews){
industryNewsService.save(industryNews);
return R.success();
}
/**
* 查询所有行业动态(支持分页和条件查询)
* @param industryNews 行业动态查询条件
* @param pagenum 页码
* @param pagesize 每页数量
* @return
*/
@Operation(summary = "查询行业动态列表")
@PostMapping("/list")
@CrossOrigin
public R<PageInfo<IndustryNews>> list(@RequestBody IndustryNews industryNews, @RequestParam Integer pagenum, @RequestParam Integer pagesize){
LambdaQueryWrapper<IndustryNews> industryNewsWrapper = new LambdaQueryWrapper<>();
if(ObjectUtils.isNotEmpty(industryNews.getTitle())){
industryNewsWrapper.like(IndustryNews::getTitle, industryNews.getTitle());
}
if(ObjectUtils.isNotEmpty(industryNews.getContent())){
industryNewsWrapper.like(IndustryNews::getContent, industryNews.getContent());
}
if(ObjectUtils.isNotEmpty(industryNews.getStatus())){
industryNewsWrapper.eq(IndustryNews::getStatus, industryNews.getStatus());
}
PageHelper.startPage(pagenum, pagesize);
List<IndustryNews> industryNewsList = industryNewsService.list(industryNewsWrapper);
PageInfo<IndustryNews> pageInfo = new PageInfo<>(industryNewsList);
return R.data(pageInfo);
}
/**
* 修改行业动态
* @return
*/
@Operation(summary = "修改行业动态")
@PostMapping("/update")
@CrossOrigin
public R update(@RequestBody IndustryNews industryNews){
industryNewsService.updateById(industryNews);
return R.success();
}
/**
* 删除行业动态
* @return
*/
@Operation(summary = "删除行业动态")
@PostMapping("/delete")
@CrossOrigin
public R delete(@RequestParam Long id){
industryNewsService.removeById(id);
return R.success();
}
/**
* 增加行业动态访问量
* @param id 行业动态ID
* @return
*/
@Operation(summary = "增加访问量")
@PostMapping("/increaseViews")
@CrossOrigin
public R increaseViews(@RequestParam Long id){
// 查询行业动态
IndustryNews industryNews = industryNewsService.getById(id);
if(industryNews != null){
// 访问量+1
industryNews.setViews(industryNews.getViews() == null ? 1 : industryNews.getViews() + 1);
// 更新数据库
industryNewsService.updateById(industryNews);
}
return R.success();
}
}