|
|
|
@ -0,0 +1,266 @@
|
|
|
|
|
package com.controller;
|
|
|
|
|
// 定义控制器所在的包名
|
|
|
|
|
|
|
|
|
|
import java.math.BigDecimal; // 导入BigDecimal类,用于进行高精度的数学运算
|
|
|
|
|
import java.text.SimpleDateFormat; // 导入SimpleDateFormat类,用于格式化和解析日期
|
|
|
|
|
import java.text.ParseException; // 导入ParseException类,用于处理日期解析异常
|
|
|
|
|
import java.util.ArrayList; // 导入ArrayList类,用于创建动态数组
|
|
|
|
|
import java.util.Arrays; // 导入Arrays类,提供了对数组操作的工具方法
|
|
|
|
|
import java.util.Calendar; // 导入Calendar类,用于日期时间的操作
|
|
|
|
|
import java.util.Map; // 导入Map接口,用于键值对集合
|
|
|
|
|
import java.util.HashMap; // 导入HashMap类,基于哈希表的Map实现
|
|
|
|
|
import java.util.Iterator; // 导入Iterator接口,用于遍历集合
|
|
|
|
|
import java.util.Date; // 导入Date类,用于日期时间的表示
|
|
|
|
|
import java.util.List; // 导入List接口,用于列表集合
|
|
|
|
|
|
|
|
|
|
import javax.servlet.http.HttpServletRequest; // 导入HttpServletRequest接口,用于访问HTTP请求信息
|
|
|
|
|
import java.io.IOException; // 导入IOException类,用于处理IO异常
|
|
|
|
|
|
|
|
|
|
import com.utils.ValidatorUtils; // 导入自定义的ValidatorUtils工具类,用于数据验证
|
|
|
|
|
import org.apache.commons.lang3.StringUtils; // 导入Apache Commons Lang库的StringUtils类,用于字符串操作
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired; // 导入Spring的Autowired注解,用于自动注入依赖
|
|
|
|
|
import org.springframework.transaction.annotation.Transactional; // 导入Spring的Transactional注解,用于声明事务
|
|
|
|
|
import org.springframework.format.annotation.DateTimeFormat; // 导入Spring的DateTimeFormat注解,用于日期格式注解
|
|
|
|
|
import org.springframework.web.bind.annotation.PathVariable; // 导入Spring的PathVariable注解,用于将URL中的参数绑定到控制器方法的参数上
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestBody; // 导入Spring的RequestBody注解,用于将HTTP请求的body部分绑定到控制器方法的参数上
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestMapping; // 导入Spring的RequestMapping注解,用于定义请求映射
|
|
|
|
|
import org.springframework.web.bind.annotation.RequestParam; // 导入Spring的RequestParam注解,用于将请求参数绑定到控制器方法的参数上
|
|
|
|
|
import org.springframework.web.bind.annotation.RestController; // 导入Spring的RestController注解,用于定义REST风格的控制器
|
|
|
|
|
|
|
|
|
|
import com.baomidou.mybatisplus.mapper.EntityWrapper; // 导入MyBatis Plus的EntityWrapper类,用于构建查询条件
|
|
|
|
|
import com.baomidou.mybatisplus.mapper.Wrapper; // 导入MyBatis Plus的Wrapper接口,EntityWrapper实现该接口
|
|
|
|
|
import com.annotation.IgnoreAuth; // 导入自定义的IgnoreAuth注解,用于忽略权限验证
|
|
|
|
|
|
|
|
|
|
import com.entity.NewsEntity; // 导入新闻实体类
|
|
|
|
|
import com.entity.view.NewsView; // 导入新闻视图类
|
|
|
|
|
import com.service.NewsService; // 导入新闻服务类
|
|
|
|
|
import com.service.TokenService; // 导入Token服务类
|
|
|
|
|
import com.utils.PageUtils; // 导入自定义的PageUtils工具类,用于分页处理
|
|
|
|
|
import com.utils.R; // 导入自定义的R类,用于封装响应结果
|
|
|
|
|
import com.utils.MD5Util; // 导入自定义的MD5Util工具类,用于MD5加密
|
|
|
|
|
import com.utils.MPUtil; // 导入自定义的MPUtil工具类,提供MyBatis Plus工具方法
|
|
|
|
|
import com.utils.CommonUtil; // 导入自定义的CommonUtil工具类,提供常用方法
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 公告信息
|
|
|
|
|
* 后端接口
|
|
|
|
|
* @author
|
|
|
|
|
* @email
|
|
|
|
|
* @date 2023-02-21 09:46:06
|
|
|
|
|
*/
|
|
|
|
|
@RestController
|
|
|
|
|
@RequestMapping("/news")
|
|
|
|
|
public class NewsController {
|
|
|
|
|
@Autowired
|
|
|
|
|
private NewsService newsService;//注入新闻服务类
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 后端列表
|
|
|
|
|
*/
|
|
|
|
|
@RequestMapping("/page")
|
|
|
|
|
public R page(@RequestParam Map<String, Object> params, NewsEntity news, HttpServletRequest request) {
|
|
|
|
|
// 创建一个EntityWrapper对象,用于构建查询条件
|
|
|
|
|
EntityWrapper<NewsEntity> ew = new EntityWrapper<NewsEntity>();
|
|
|
|
|
|
|
|
|
|
// 使用MPUtil工具类对查询条件进行排序和分页处理
|
|
|
|
|
PageUtils page = newsService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, news), params), params));
|
|
|
|
|
|
|
|
|
|
// 将分页结果设置到request对象中
|
|
|
|
|
request.setAttribute("data", page);
|
|
|
|
|
|
|
|
|
|
// 返回成功响应,包含分页结果
|
|
|
|
|
return R.ok().put("data", page);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 前端列表
|
|
|
|
|
*/
|
|
|
|
|
@IgnoreAuth
|
|
|
|
|
@RequestMapping("/list")
|
|
|
|
|
public R list(@RequestParam Map<String, Object> params, NewsEntity news, HttpServletRequest request) {
|
|
|
|
|
// 创建一个EntityWrapper对象,用于构建查询条件
|
|
|
|
|
EntityWrapper<NewsEntity> ew = new EntityWrapper<NewsEntity>();
|
|
|
|
|
|
|
|
|
|
// 使用MPUtil工具类对查询条件进行排序和分页处理
|
|
|
|
|
PageUtils page = newsService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, news), params), params));
|
|
|
|
|
|
|
|
|
|
// 将分页结果设置到request对象中
|
|
|
|
|
request.setAttribute("data", page);
|
|
|
|
|
|
|
|
|
|
// 返回成功响应,包含分页结果
|
|
|
|
|
return R.ok().put("data", page);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 列表
|
|
|
|
|
*/
|
|
|
|
|
@RequestMapping("/lists")
|
|
|
|
|
//一个Spring MVC的注解,用于将HTTP请求映射到 list 方法。当客户端发送一个HTTP请求到 /lists 路径时,Spring MVC会调用 list 方法来处理这个请求
|
|
|
|
|
public R list(NewsEntity news) //NewsEntity news 参数表示要查询的 NewsEntity 对象
|
|
|
|
|
{
|
|
|
|
|
// 创建一个EntityWrapper对象,用于构建查询条件
|
|
|
|
|
EntityWrapper<NewsEntity> ew = new EntityWrapper<NewsEntity>();
|
|
|
|
|
|
|
|
|
|
// 使用MPUtil工具类对查询条件进行排序和分页处理
|
|
|
|
|
ew.allEq(MPUtil.allEQMapPre(news, "news"));
|
|
|
|
|
|
|
|
|
|
// 返回成功响应,包含查询结果
|
|
|
|
|
return R.ok().put("data", newsService.selectListView(ew));
|
|
|
|
|
//这行代码返回一个表示请求成功的响应对象,并将 newsService.selectListView(ew) 的结果添加到响应对象中,键为 "data"。
|
|
|
|
|
//newsService.selectListView(ew) 方法用于查询满足 ew 对象的查询条件的记录,并返回一个包含查询结果的列表。
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 查询
|
|
|
|
|
*/
|
|
|
|
|
@RequestMapping("/query")
|
|
|
|
|
public R query(NewsEntity news) {
|
|
|
|
|
// 创建一个EntityWrapper对象,用于构建查询条件
|
|
|
|
|
EntityWrapper<NewsEntity> ew = new EntityWrapper<NewsEntity>();
|
|
|
|
|
|
|
|
|
|
// 使用MPUtil工具类对查询条件进行排序和分页处理
|
|
|
|
|
ew.allEq(MPUtil.allEQMapPre(news, "news"));
|
|
|
|
|
|
|
|
|
|
// 调用 newsService 对象的 selectView 方法,查询满足 ew 对象的查询条件的记录,并返回一个 NewsView 对象
|
|
|
|
|
NewsView newsView = newsService.selectView(ew);
|
|
|
|
|
return R.ok("查询公告信息成功").put("data", newsView);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 后端详情
|
|
|
|
|
*/
|
|
|
|
|
//一个Spring MVC的注解,用于将HTTP请求映射到 info 方法。
|
|
|
|
|
//当客户端发送一个HTTP请求到 /info/{id} 路径时,Spring MVC会调用 info 方法来处理这个请求
|
|
|
|
|
@RequestMapping("/info/{id}")
|
|
|
|
|
public R info(@PathVariable("id") Long id) {
|
|
|
|
|
// 根据ID查询并返回结果
|
|
|
|
|
NewsEntity news = newsService.selectById(id);
|
|
|
|
|
//调用 newsService 对象的 selectById 方法,根据 id 参数的值查询数据库中的记录,并返回一个 NewsEntity 对象。
|
|
|
|
|
return R.ok().put("data", news); //将 news 对象添加到响应对象中,键为 "data"
|
|
|
|
|
}
|
|
|
|
|
/**
|
|
|
|
|
* 前端详情
|
|
|
|
|
*/
|
|
|
|
|
@IgnoreAuth
|
|
|
|
|
@RequestMapping("/detail/{id}")
|
|
|
|
|
public R detail(@PathVariable("id") Long id) {
|
|
|
|
|
// 根据ID查询并返回结果
|
|
|
|
|
NewsEntity news = newsService.selectById(id);
|
|
|
|
|
return R.ok().put("data", news);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 后端保存
|
|
|
|
|
*/
|
|
|
|
|
@RequestMapping("/save")
|
|
|
|
|
public R save(@RequestBody NewsEntity news, HttpServletRequest request) {
|
|
|
|
|
// 设置ID为当前时间戳加上一个随机数
|
|
|
|
|
news.setId(new Date().getTime() + new Double(Math.floor(Math.random() * 1000)).longValue());
|
|
|
|
|
|
|
|
|
|
// 验证实体对象,如果需要验证,可以取消注释
|
|
|
|
|
// ValidatorUtils.validateEntity(news);
|
|
|
|
|
|
|
|
|
|
// 插入数据,将 news 对象插入到数据库中
|
|
|
|
|
newsService.insert(news);
|
|
|
|
|
//返回一个表示请求成功的响应对象
|
|
|
|
|
return R.ok();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 前端保存
|
|
|
|
|
*/
|
|
|
|
|
@RequestMapping("/add")
|
|
|
|
|
public R add(@RequestBody NewsEntity news, HttpServletRequest request) {
|
|
|
|
|
// 设置ID为当前时间戳加上一个随机数
|
|
|
|
|
news.setId(new Date().getTime() + new Double(Math.floor(Math.random() * 1000)).longValue());
|
|
|
|
|
|
|
|
|
|
// 验证实体对象,如果需要验证,可以取消注释
|
|
|
|
|
// ValidatorUtils.validateEntity(news);
|
|
|
|
|
|
|
|
|
|
// 插入数据
|
|
|
|
|
newsService.insert(news);
|
|
|
|
|
return R.ok();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 修改
|
|
|
|
|
*/
|
|
|
|
|
@RequestMapping("/update")
|
|
|
|
|
@Transactional
|
|
|
|
|
public R update(@RequestBody NewsEntity news, HttpServletRequest request) {
|
|
|
|
|
// 验证实体对象,如果需要验证,可以取消注释
|
|
|
|
|
// ValidatorUtils.validateEntity(news);
|
|
|
|
|
|
|
|
|
|
// 更新数据
|
|
|
|
|
newsService.updateById(news);
|
|
|
|
|
return R.ok();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 删除
|
|
|
|
|
*/
|
|
|
|
|
@RequestMapping("/delete")//这是一个Spring MVC的注解,用于将HTTP请求映射到 delete方法上
|
|
|
|
|
public R delete(@RequestBody Long[] ids) {
|
|
|
|
|
// 批量删除数据
|
|
|
|
|
newsService.deleteBatchIds(Arrays.asList(ids));
|
|
|
|
|
return R.ok();//R.ok() 方法通常表示请求成功,并返回一个默认的成功响应。
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 提醒接口
|
|
|
|
|
*/
|
|
|
|
|
@RequestMapping("/remind/{columnName}/{type}")
|
|
|
|
|
//这是一个Spring MVC的注解,用于将HTTP请求映射到 remindCount 方法。当客户端发送一个HTTP请求到 /remind/{columnName}/{type} 路径时,Spring MVC会调用 remindCount 方法来处理这个请求。
|
|
|
|
|
public R remindCount(@PathVariable("columnName") String columnName, HttpServletRequest request,
|
|
|
|
|
@PathVariable("type") String type, @RequestParam Map<String, Object> map) {
|
|
|
|
|
// 将列名和类型添加到map中,@PathVariable 注解表示 columnName 和 type 参数的值将从URL路径中获取
|
|
|
|
|
map.put("column", columnName);//将 columnName 参数的值添加到 map 中,键为 "column"。
|
|
|
|
|
map.put("type", type);
|
|
|
|
|
|
|
|
|
|
if (type.equals("2")) //如果 type 参数的值为 "2"
|
|
|
|
|
{
|
|
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
|
|
//创建一个 SimpleDateFormat 对象sdf,用于将日期格式化为 "yyyy-MM-dd" 格式。
|
|
|
|
|
Calendar c = Calendar.getInstance();
|
|
|
|
|
//创建一个 Calendar 对象,用于处理日期和时间。Calendar.getInstance() 方法返回一个表示当前日期和时间的 Calendar 对象。
|
|
|
|
|
Date remindStartDate = null;//声明一个变量 remindStartDate,初始化为 null,这个变量将用于存储提醒开始日期。
|
|
|
|
|
Date remindEndDate = null;
|
|
|
|
|
|
|
|
|
|
if (map.get("remindstart") != null)//如果 map 中存在键为 "remindstart" 的元素
|
|
|
|
|
{
|
|
|
|
|
Integer remindStart = Integer.parseInt(map.get("remindstart").toString());
|
|
|
|
|
//将 map 中键为 "remindstart" 的元素的值转换为 Integer 类型,并赋值给 remindStart
|
|
|
|
|
c.setTime(new Date());//setTime(new Date()) 方法将 Calendar 对象c的时间设置为当前时间。
|
|
|
|
|
c.add(Calendar.DAY_OF_MONTH, remindStart);//将 Calendar 对象的时间增加 remindStart 天。
|
|
|
|
|
remindStartDate = c.getTime();//返回一个表示 Calendar 对象当前时间的 Date 对象,并将其赋值给 remindStartDate。
|
|
|
|
|
map.put("remindstart", sdf.format(remindStartDate));//将 remindStartDate 格式化为 "yyyy-MM-dd" 格式,并将其作为 map 中键为 "remindstart" 的元素的值。
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (map.get("remindend") != null)
|
|
|
|
|
{
|
|
|
|
|
Integer remindEnd = Integer.parseInt(map.get("remindend").toString());
|
|
|
|
|
c.setTime(new Date());//c 的时间设置为当前时间
|
|
|
|
|
c.add(Calendar.DAY_OF_MONTH, remindEnd);//c 的时间增加 remindStart 天
|
|
|
|
|
remindEndDate = c.getTime();
|
|
|
|
|
map.put("remindend", sdf.format(remindEndDate));
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 创建一个EntityWrapper对象,用于构建查询条件
|
|
|
|
|
Wrapper<NewsEntity> wrapper = new EntityWrapper<NewsEntity>();//创建一个EntityWrapper对象,用于构建查询条件
|
|
|
|
|
|
|
|
|
|
if (map.get("remindstart") != null)//如果map中存在键为"remindstart"的元素
|
|
|
|
|
{
|
|
|
|
|
wrapper.ge(columnName, map.get("remindstart"));//将 wrapper 对象的查询条件设置为 columnName>=map 中键为 "remindend" 的元素的值
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (map.get("remindend") != null) {
|
|
|
|
|
wrapper.le(columnName, map.get("remindend"));//将 wrapper 对象的查询条件设置为 columnName<=map 中键为 "remindend" 的元素的值
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 查询并返回结果
|
|
|
|
|
int count = newsService.selectCount(wrapper);//查询满足wrapper对象的查询条件的记录数,并赋值给 count 变量
|
|
|
|
|
return R.ok().put("count", count);//返回一个包含 count 变量的 R 对象(请求成功的响应对象)
|
|
|
|
|
}
|
|
|
|
|
}
|