|
|
package com.controller;
|
|
|
|
|
|
// 导入用于格式化日期的类
|
|
|
import java.text.SimpleDateFormat;
|
|
|
// 导入用于创建动态数组的类
|
|
|
import java.util.ArrayList;
|
|
|
// 导入用于操作数组的工具类
|
|
|
import java.util.Arrays;
|
|
|
// 导入用于处理日期和时间的类
|
|
|
import java.util.Calendar;
|
|
|
// 导入用于存储键值对的接口
|
|
|
import java.util.Map;
|
|
|
// 导入用于实现Map接口的HashMap类
|
|
|
import java.util.HashMap;
|
|
|
// 导入用于遍历集合的接口
|
|
|
import java.util.Iterator;
|
|
|
// 导入用于表示日期和时间的类
|
|
|
import java.util.Date;
|
|
|
// 导入用于创建列表的接口
|
|
|
import java.util.List;
|
|
|
// 导入用于处理HTTP请求的类
|
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
|
|
// 导入自定义的验证工具类
|
|
|
import com.utils.ValidatorUtils;
|
|
|
// 导入Apache Commons Lang库中的字符串处理工具类
|
|
|
import org.apache.commons.lang3.StringUtils;
|
|
|
// 导入Spring框架的自动注入注解
|
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
|
// 导入Spring框架的日期格式化注解
|
|
|
import org.springframework.format.annotation.DateTimeFormat;
|
|
|
// 导入Spring框架的路径变量注解
|
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
|
|
// 导入Spring框架的请求体注解
|
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
|
// 导入Spring框架的请求映射注解
|
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
|
// 导入Spring框架的REST控制器注解
|
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
|
// 导入MyBatis-Plus的实体包装器类
|
|
|
import com.baomidou.mybatisplus.mapper.EntityWrapper;
|
|
|
// 导入MyBatis-Plus的包装器接口
|
|
|
import com.baomidou.mybatisplus.mapper.Wrapper;
|
|
|
// 导入自定义的忽略认证注解
|
|
|
import com.annotation.IgnoreAuth;
|
|
|
|
|
|
// 导入留言板实体类
|
|
|
import com.entity.MessagesEntity;
|
|
|
// 导入留言板视图类
|
|
|
import com.entity.view.MessagesView;
|
|
|
|
|
|
// 导入留言板服务类
|
|
|
import com.service.MessagesService;
|
|
|
// 导入令牌服务类
|
|
|
import com.service.TokenService;
|
|
|
// 导入分页工具类
|
|
|
import com.utils.PageUtils;
|
|
|
// 导入自定义的响应类
|
|
|
import com.utils.R;
|
|
|
// 导入MD5加密工具类
|
|
|
import com.utils.MD5Util;
|
|
|
// 导入MyBatis-Plus工具类
|
|
|
import com.utils.MPUtil;
|
|
|
// 导入通用工具类
|
|
|
import com.utils.CommonUtil;
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 留言板
|
|
|
* 后端接口
|
|
|
* @author
|
|
|
* @email
|
|
|
* @date 2021-05-09 15:46:15
|
|
|
*/
|
|
|
// 声明为REST控制器,用于处理HTTP请求
|
|
|
@RestController
|
|
|
// 映射请求路径,所有以/messages开头的请求由该控制器处理
|
|
|
@RequestMapping("/messages")
|
|
|
public class MessagesController {
|
|
|
// 自动注入留言板服务类实例
|
|
|
@Autowired
|
|
|
private MessagesService messagesService;
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 后端列表(分页查询)
|
|
|
*/
|
|
|
// 映射请求路径/messages/page
|
|
|
@RequestMapping("/page")
|
|
|
// 处理后端分页查询请求,参数包括查询条件、实体对象、HTTP请求
|
|
|
public R page(@RequestParam Map<String, Object> params, MessagesEntity messages,
|
|
|
HttpServletRequest request) {
|
|
|
// 如果当前用户不是管理员
|
|
|
if (!request.getSession().getAttribute("role").toString().equals("管理员")) {
|
|
|
// 设置留言的用户ID为当前登录用户的ID
|
|
|
messages.setUserid((Long) request.getSession().getAttribute("userId"));
|
|
|
}
|
|
|
// 创建实体包装器,用于构建查询条件
|
|
|
EntityWrapper<MessagesEntity> ew = new EntityWrapper<MessagesEntity>();
|
|
|
// 调用服务类的分页查询方法,包含排序、范围查询、模糊查询处理
|
|
|
PageUtils page = messagesService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, messages), params), params));
|
|
|
// 返回成功响应,携带分页数据
|
|
|
return R.ok().put("data", page);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 前端列表(分页查询)
|
|
|
*/
|
|
|
// 映射请求路径/messages/list
|
|
|
@RequestMapping("/list")
|
|
|
// 处理前端分页查询请求,参数包括查询条件、实体对象、HTTP请求
|
|
|
public R list(@RequestParam Map<String, Object> params, MessagesEntity messages,
|
|
|
HttpServletRequest request) {
|
|
|
// 如果当前用户不是管理员
|
|
|
if (!request.getSession().getAttribute("role").toString().equals("管理员")) {
|
|
|
// 设置留言的用户ID为当前登录用户的ID
|
|
|
messages.setUserid((Long) request.getSession().getAttribute("userId"));
|
|
|
}
|
|
|
// 创建实体包装器,用于构建查询条件
|
|
|
EntityWrapper<MessagesEntity> ew = new EntityWrapper<MessagesEntity>();
|
|
|
// 调用服务类的分页查询方法,包含排序、范围查询、模糊查询处理
|
|
|
PageUtils page = messagesService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, messages), params), params));
|
|
|
// 返回成功响应,携带分页数据
|
|
|
return R.ok().put("data", page);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 列表(不分页,全量查询)
|
|
|
*/
|
|
|
// 映射请求路径/messages/lists
|
|
|
@RequestMapping("/lists")
|
|
|
// 处理全量查询请求,参数为实体对象
|
|
|
public R list(MessagesEntity messages) {
|
|
|
// 创建实体包装器
|
|
|
EntityWrapper<MessagesEntity> ew = new EntityWrapper<MessagesEntity>();
|
|
|
// 设置查询条件:所有字段与实体对象相等(前缀为"messages")
|
|
|
ew.allEq(MPUtil.allEQMapPre(messages, "messages"));
|
|
|
// 返回成功响应,携带查询结果(视图列表)
|
|
|
return R.ok().put("data", messagesService.selectListView(ew));
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 查询(单个对象)
|
|
|
*/
|
|
|
// 映射请求路径/messages/query
|
|
|
@RequestMapping("/query")
|
|
|
// 处理单个对象查询请求,参数为实体对象
|
|
|
public R query(MessagesEntity messages) {
|
|
|
// 创建实体包装器
|
|
|
EntityWrapper<MessagesEntity> ew = new EntityWrapper<MessagesEntity>();
|
|
|
// 设置查询条件:所有字段与实体对象相等(前缀为"messages")
|
|
|
ew.allEq(MPUtil.allEQMapPre(messages, "messages"));
|
|
|
// 调用服务类的视图查询方法
|
|
|
MessagesView messagesView = messagesService.selectView(ew);
|
|
|
// 返回成功响应,携带查询结果和提示信息
|
|
|
return R.ok("查询留言板成功").put("data", messagesView);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 后端详情(根据ID查询)
|
|
|
*/
|
|
|
// 映射请求路径/messages/info/{id}
|
|
|
@RequestMapping("/info/{id}")
|
|
|
// 处理详情查询请求,参数为路径中的ID
|
|
|
public R info(@PathVariable("id") Long id) {
|
|
|
// 根据ID直接查询实体对象
|
|
|
MessagesEntity messages = messagesService.selectById(id);
|
|
|
// 返回成功响应,携带查询到的实体数据
|
|
|
return R.ok().put("data", messages);
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 前端详情(根据ID查询)
|
|
|
*/
|
|
|
// 映射请求路径/messages/detail/{id}
|
|
|
@RequestMapping("/detail/{id}")
|
|
|
// 处理详情查询请求,参数为路径中的ID(前端调用)
|
|
|
public R detail(@PathVariable("id") Long id) {
|
|
|
// 根据ID直接查询实体对象
|
|
|
MessagesEntity messages = messagesService.selectById(id);
|
|
|
// 返回成功响应,携带查询到的实体数据
|
|
|
return R.ok().put("data", messages);
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 后端保存(新增留言)
|
|
|
*/
|
|
|
// 映射请求路径/messages/save
|
|
|
@RequestMapping("/save")
|
|
|
// 处理后端新增请求,请求体为实体对象,携带HTTP请求
|
|
|
public R save(@RequestBody MessagesEntity messages, HttpServletRequest request) {
|
|
|
// 生成唯一ID(时间戳 + 随机数)
|
|
|
messages.setId(new Date().getTime() + new Double(Math.floor(Math.random() * 1000)).longValue());
|
|
|
// 实体验证(注释,未启用)
|
|
|
//ValidatorUtils.validateEntity(messages);
|
|
|
// 调用服务类的插入方法
|
|
|
messagesService.insert(messages);
|
|
|
// 返回成功响应
|
|
|
return R.ok();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 前端保存(新增留言)
|
|
|
*/
|
|
|
// 映射请求路径/messages/add
|
|
|
@RequestMapping("/add")
|
|
|
// 处理前端新增请求,请求体为实体对象,携带HTTP请求
|
|
|
public R add(@RequestBody MessagesEntity messages, HttpServletRequest request) {
|
|
|
// 生成唯一ID(时间戳 + 随机数)
|
|
|
messages.setId(new Date().getTime() + new Double(Math.floor(Math.random() * 1000)).longValue());
|
|
|
// 实体验证(注释,未启用)
|
|
|
//ValidatorUtils.validateEntity(messages);
|
|
|
// 设置留言的用户ID为当前登录用户的ID
|
|
|
messages.setUserid((Long) request.getSession().getAttribute("userId"));
|
|
|
// 调用服务类的插入方法
|
|
|
messagesService.insert(messages);
|
|
|
// 返回成功响应
|
|
|
return R.ok();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 修改(更新留言)
|
|
|
*/
|
|
|
// 映射请求路径/messages/update
|
|
|
@RequestMapping("/update")
|
|
|
// 处理修改请求,请求体为实体对象,携带HTTP请求
|
|
|
public R update(@RequestBody MessagesEntity messages, HttpServletRequest request) {
|
|
|
// 实体验证(注释,未启用)
|
|
|
//ValidatorUtils.validateEntity(messages);
|
|
|
// 调用服务类的更新方法(根据ID全量更新)
|
|
|
messagesService.updateById(messages); // 全部更新
|
|
|
// 返回成功响应
|
|
|
return R.ok();
|
|
|
}
|
|
|
|
|
|
|
|
|
/**
|
|
|
* 删除(批量删除留言)
|
|
|
*/
|
|
|
// 映射请求路径/messages/delete
|
|
|
@RequestMapping("/delete")
|
|
|
// 处理删除请求,请求体为ID数组
|
|
|
public R delete(@RequestBody Long[] ids) {
|
|
|
// 调用服务类的批量删除方法(根据ID列表)
|
|
|
messagesService.deleteBatchIds(Arrays.asList(ids));
|
|
|
// 返回成功响应
|
|
|
return R.ok();
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 提醒接口(统计符合条件的留言数)
|
|
|
*/
|
|
|
// 映射请求路径/messages/remind/{columnName}/{type}
|
|
|
@RequestMapping("/remind/{columnName}/{type}")
|
|
|
// 处理提醒统计请求,参数包括列名、类型、查询条件映射
|
|
|
public R remindCount(@PathVariable("columnName") String columnName, HttpServletRequest request,
|
|
|
@PathVariable("type") String type, @RequestParam Map<String, Object> map) {
|
|
|
// 向查询条件中添加列名和类型
|
|
|
map.put("column", columnName);
|
|
|
map.put("type", type);
|
|
|
|
|
|
// 如果类型为2(处理日期范围提醒)
|
|
|
if (type.equals("2")) {
|
|
|
// 创建日期格式化对象(yyyy-MM-dd格式)
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
|
|
|
// 创建日历对象
|
|
|
Calendar c = Calendar.getInstance();
|
|
|
// 提醒开始日期和结束日期变量
|
|
|
Date remindStartDate = null;
|
|
|
Date remindEndDate = null;
|
|
|
// 处理提醒开始时间(相对当前日期偏移)
|
|
|
if (map.get("remindstart") != null) {
|
|
|
// 解析偏移天数
|
|
|
Integer remindStart = Integer.parseInt(map.get("remindstart").toString());
|
|
|
// 设置日历为当前时间并添加偏移天数
|
|
|
c.setTime(new Date());
|
|
|
c.add(Calendar.DAY_OF_MONTH, remindStart);
|
|
|
// 获取格式化后的提醒开始日期
|
|
|
remindStartDate = c.getTime();
|
|
|
map.put("remindstart", sdf.format(remindStartDate));
|
|
|
}
|
|
|
// 处理提醒结束时间(相对当前日期偏移)
|
|
|
if (map.get("remindend") != null) {
|
|
|
// 解析偏移天数
|
|
|
Integer remindEnd = Integer.parseInt(map.get("remindend").toString());
|
|
|
// 设置日历为当前时间并添加偏移天数
|
|
|
c.setTime(new Date());
|
|
|
c.add(Calendar.DAY_OF_MONTH, remindEnd);
|
|
|
// 获取格式化后的提醒结束日期
|
|
|
remindEndDate = c.getTime();
|
|
|
map.put("remindend", sdf.format(remindEndDate));
|
|
|
}
|
|
|
}
|
|
|
|
|
|
// 创建包装器用于构建查询条件
|
|
|
Wrapper<MessagesEntity> wrapper = new EntityWrapper<MessagesEntity>();
|
|
|
// 添加提醒开始时间的大于等于条件
|
|
|
if (map.get("remindstart") != null) {
|
|
|
wrapper.ge(columnName, map.get("remindstart"));
|
|
|
}
|
|
|
// 添加提醒结束时间的小于等于条件
|
|
|
if (map.get("remindend") != null) {
|
|
|
wrapper.le(columnName, map.get("remindend"));
|
|
|
}
|
|
|
|
|
|
|
|
|
// 调用服务类统计符合条件的记录数
|
|
|
int count = messagesService.selectCount(wrapper);
|
|
|
// 返回成功响应,携带统计结果
|
|
|
return R.ok().put("count", count);
|
|
|
}
|
|
|
} |