You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
235 lines
8.5 KiB
235 lines
8.5 KiB
package com.controller;
|
|
|
|
import java.text.SimpleDateFormat;
|
|
import java.util.ArrayList;
|
|
import java.util.Arrays;
|
|
import java.util.Calendar;
|
|
import java.util.Map;
|
|
import java.util.HashMap;
|
|
import java.util.Iterator;
|
|
import java.util.Date;
|
|
import java.util.List;
|
|
import javax.servlet.http.HttpServletRequest;
|
|
|
|
import com.utils.ValidatorUtils;
|
|
import org.apache.commons.lang3.StringUtils;
|
|
import org.springframework.beans.factory.annotation.Autowired;
|
|
import org.springframework.transaction.annotation.Transactional;
|
|
import org.springframework.format.annotation.DateTimeFormat;
|
|
import org.springframework.web.bind.annotation.PathVariable;
|
|
import org.springframework.web.bind.annotation.RequestBody;
|
|
import org.springframework.web.bind.annotation.RequestMapping;
|
|
import org.springframework.web.bind.annotation.RequestParam;
|
|
import org.springframework.web.bind.annotation.RestController;
|
|
import com.baomidou.mybatisplus.mapper.EntityWrapper;
|
|
import com.baomidou.mybatisplus.mapper.Wrapper;
|
|
import com.annotation.IgnoreAuth;
|
|
|
|
import com.entity.NewsEntity;
|
|
import com.entity.view.NewsView;
|
|
|
|
import com.service.NewsService;
|
|
import com.service.TokenService;
|
|
import com.utils.PageUtils;
|
|
import com.utils.R;
|
|
import com.utils.MD5Util;
|
|
import com.utils.MPUtil;
|
|
import com.utils.CommonUtil;
|
|
import java.io.IOException;
|
|
|
|
/**
|
|
* 公告信息管理后端接口
|
|
* @author [作者姓名]
|
|
* @email [作者邮箱]
|
|
* @date 2022-05-06 08:33:49
|
|
*/
|
|
@RestController
|
|
@RequestMapping("/news")
|
|
public class NewsController {
|
|
@Autowired
|
|
private NewsService newsService; // 注入新闻服务
|
|
|
|
/**
|
|
* 后端列表
|
|
* @param params 请求参数
|
|
* @param news 新闻实体
|
|
* @param request HTTP请求对象
|
|
* @return 分页数据
|
|
*/
|
|
@RequestMapping("/page")
|
|
public R page(@RequestParam Map<String, Object> params, NewsEntity news,
|
|
HttpServletRequest request) {
|
|
EntityWrapper<NewsEntity> ew = new EntityWrapper<>(); // 创建查询条件
|
|
PageUtils page = newsService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, news), params), params));
|
|
|
|
return R.ok().put("data", page); // 返回分页数据
|
|
}
|
|
|
|
/**
|
|
* 前端列表
|
|
* @param params 请求参数
|
|
* @param news 新闻实体
|
|
* @param request HTTP请求对象
|
|
* @return 分页数据
|
|
*/
|
|
@IgnoreAuth
|
|
@RequestMapping("/list")
|
|
public R list(@RequestParam Map<String, Object> params, NewsEntity news,
|
|
HttpServletRequest request) {
|
|
EntityWrapper<NewsEntity> ew = new EntityWrapper<>(); // 创建查询条件
|
|
PageUtils page = newsService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, news), params), params));
|
|
|
|
return R.ok().put("data", page); // 返回分页数据
|
|
}
|
|
|
|
/**
|
|
* 列表(无分页)
|
|
* @param news 新闻实体
|
|
* @return 数据列表
|
|
*/
|
|
@RequestMapping("/lists")
|
|
public R list(NewsEntity news) {
|
|
EntityWrapper<NewsEntity> ew = new EntityWrapper<>();
|
|
ew.allEq(MPUtil.allEQMapPre(news, "news")); // 设置查询条件
|
|
return R.ok().put("data", newsService.selectListView(ew)); // 返回数据列表
|
|
}
|
|
|
|
/**
|
|
* 查询单条记录
|
|
* @param news 新闻实体
|
|
* @return 查询结果
|
|
*/
|
|
@RequestMapping("/query")
|
|
public R query(NewsEntity news) {
|
|
EntityWrapper<NewsEntity> ew = new EntityWrapper<>();
|
|
ew.allEq(MPUtil.allEQMapPre(news, "news")); // 设置查询条件
|
|
NewsView newsView = newsService.selectView(ew); // 查询视图
|
|
return R.ok("查询公告信息成功").put("data", newsView); // 返回查询结果
|
|
}
|
|
|
|
/**
|
|
* 后端详情
|
|
* @param id 记录ID
|
|
* @return 单条记录详情
|
|
*/
|
|
@RequestMapping("/info/{id}")
|
|
public R info(@PathVariable("id") Long id) {
|
|
NewsEntity news = newsService.selectById(id); // 查询单条记录
|
|
return R.ok().put("data", news); // 返回记录详情
|
|
}
|
|
|
|
/**
|
|
* 前端详情
|
|
* @param id 记录ID
|
|
* @return 单条记录详情
|
|
*/
|
|
@IgnoreAuth
|
|
@RequestMapping("/detail/{id}")
|
|
public R detail(@PathVariable("id") Long id) {
|
|
NewsEntity news = newsService.selectById(id); // 查询单条记录
|
|
return R.ok().put("data", news); // 返回记录详情
|
|
}
|
|
|
|
/**
|
|
* 后端保存
|
|
* @param news 新闻实体
|
|
* @param request HTTP请求对象
|
|
* @return 保存结果
|
|
*/
|
|
@RequestMapping("/save")
|
|
public R save(@RequestBody NewsEntity news, HttpServletRequest request) {
|
|
news.setId(new Date().getTime() + new Double(Math.floor(Math.random() * 1000)).longValue()); // 生成唯一ID
|
|
// ValidatorUtils.validateEntity(news); // 验证实体(可选)
|
|
newsService.insert(news); // 插入数据
|
|
return R.ok(); // 返回成功结果
|
|
}
|
|
|
|
/**
|
|
* 前端保存
|
|
* @param news 新闻实体
|
|
* @param request HTTP请求对象
|
|
* @return 保存结果
|
|
*/
|
|
@RequestMapping("/add")
|
|
public R add(@RequestBody NewsEntity news, HttpServletRequest request) {
|
|
news.setId(new Date().getTime() + new Double(Math.floor(Math.random() * 1000)).longValue()); // 生成唯一ID
|
|
// ValidatorUtils.validateEntity(news); // 验证实体(可选)
|
|
newsService.insert(news); // 插入数据
|
|
return R.ok(); // 返回成功结果
|
|
}
|
|
|
|
/**
|
|
* 修改记录
|
|
* @param news 新闻实体
|
|
* @param request HTTP请求对象
|
|
* @return 修改结果
|
|
*/
|
|
@RequestMapping("/update")
|
|
@Transactional
|
|
public R update(@RequestBody NewsEntity news, HttpServletRequest request) {
|
|
// ValidatorUtils.validateEntity(news); // 验证实体(可选)
|
|
newsService.updateById(news); // 全量更新
|
|
return R.ok(); // 返回成功结果
|
|
}
|
|
|
|
/**
|
|
* 删除记录
|
|
* @param ids 记录ID数组
|
|
* @return 删除结果
|
|
*/
|
|
@RequestMapping("/delete")
|
|
public R delete(@RequestBody Long[] ids) {
|
|
newsService.deleteBatchIds(Arrays.asList(ids)); // 批量删除
|
|
return R.ok(); // 返回成功结果
|
|
}
|
|
|
|
/**
|
|
* 提醒接口
|
|
* @param columnName 字段名
|
|
* @param request HTTP请求对象
|
|
* @param type 提醒类型
|
|
* @param map 请求参数
|
|
* @return 提醒结果
|
|
*/
|
|
@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); // 添加提醒类型到参数
|
|
|
|
if (type.equals("2")) { // 如果是相对日期提醒
|
|
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<NewsEntity> wrapper = new EntityWrapper<>(); // 创建查询条件
|
|
if (map.get("remindstart") != null) { // 如果有开始日期
|
|
wrapper.ge(columnName, map.get("remindstart")); // 设置大于等于条件
|
|
}
|
|
if (map.get("remindend") != null) { // 如果有结束日期
|
|
wrapper.le(columnName, map.get("remindend")); // 设置小于等于条件
|
|
}
|
|
|
|
int count = newsService.selectCount(wrapper); // 查询符合条件的记录数
|
|
return R.ok().put("count", count); // 返回提醒结果
|
|
}
|
|
}
|