|
|
|
@ -0,0 +1,324 @@
|
|
|
|
|
package com.controller; // 定义包名为com.controller
|
|
|
|
|
|
|
|
|
|
import java.io.File; // 文件操作类
|
|
|
|
|
import java.math.BigDecimal; // 大数字处理类
|
|
|
|
|
import java.net.URL; // 统一资源定位符类
|
|
|
|
|
import java.text.SimpleDateFormat; // 日期格式化类
|
|
|
|
|
import com.alibaba.fastjson.JSONObject; // 阿里巴巴FastJSON库的JSON对象类
|
|
|
|
|
import java.util.*; // 通用工具类集合
|
|
|
|
|
import org.springframework.beans.BeanUtils; // Spring Bean工具类
|
|
|
|
|
import javax.servlet.http.HttpServletRequest; // HTTP请求处理类
|
|
|
|
|
import org.springframework.web.context.ContextLoader; // Spring上下文加载器
|
|
|
|
|
import javax.servlet.ServletContext; // Servlet上下文接口
|
|
|
|
|
import com.service.TokenService; // 自定义Token服务类
|
|
|
|
|
import com.utils.*; // 自定义工具类包
|
|
|
|
|
import java.lang.reflect.InvocationTargetException; // 反射异常类
|
|
|
|
|
|
|
|
|
|
import com.service.DictionaryService; // 自定义字典服务类
|
|
|
|
|
import org.apache.commons.lang3.StringUtils; // Apache字符串工具类
|
|
|
|
|
import com.annotation.IgnoreAuth; // 自定义忽略认证注解
|
|
|
|
|
import org.slf4j.Logger; // 日志接口
|
|
|
|
|
import org.slf4j.LoggerFactory; // 日志工厂类
|
|
|
|
|
import org.springframework.beans.factory.annotation.Autowired; // Spring自动注入注解
|
|
|
|
|
import org.springframework.stereotype.Controller; // Spring控制器注解
|
|
|
|
|
import org.springframework.web.bind.annotation.*; // Spring Web注解集合
|
|
|
|
|
import com.baomidou.mybatisplus.mapper.EntityWrapper; // MyBatis Plus实体包装器
|
|
|
|
|
import com.baomidou.mybatisplus.mapper.Wrapper; // MyBatis Plus包装器接口
|
|
|
|
|
import com.entity.*; // 自定义实体类包
|
|
|
|
|
import com.entity.view.*; // 自定义视图类包
|
|
|
|
|
import com.service.*; // 自定义服务类包
|
|
|
|
|
import com.utils.PageUtils; // 自定义分页工具类
|
|
|
|
|
import com.utils.R; // 自定义返回结果类
|
|
|
|
|
import com.alibaba.fastjson.*; // 阿里巴巴FastJSON库
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//健身资讯
|
|
|
|
|
//后端接口
|
|
|
|
|
//@author
|
|
|
|
|
// @email
|
|
|
|
|
|
|
|
|
|
@RestController
|
|
|
|
|
@Controller
|
|
|
|
|
@RequestMapping("/news")
|
|
|
|
|
public class NewsController {
|
|
|
|
|
private static final Logger logger = LoggerFactory.getLogger(NewsController.class);
|
|
|
|
|
|
|
|
|
|
private static final String TABLE_NAME = "news"; // 数据库表名
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private NewsService newsService; // 健身资讯服务
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private TokenService tokenService; // token服务
|
|
|
|
|
|
|
|
|
|
@Autowired
|
|
|
|
|
private DictionaryService dictionaryService; // 字典服务
|
|
|
|
|
@Autowired
|
|
|
|
|
private ForumService forumService; // 健身论坛服务
|
|
|
|
|
@Autowired
|
|
|
|
|
private JianshenkechengService jianshenkechengService; // 健身课程服务
|
|
|
|
|
@Autowired
|
|
|
|
|
private JianshenkechengCollectionService jianshenkechengCollectionService; // 课程收藏服务
|
|
|
|
|
@Autowired
|
|
|
|
|
private JianshenkechengLiuyanService jianshenkechengLiuyanService; // 课程留言服务
|
|
|
|
|
@Autowired
|
|
|
|
|
private JiaolianService jiaolianService; // 教练服务
|
|
|
|
|
@Autowired
|
|
|
|
|
private JiaolianYuyueService jiaolianYuyueService; // 教练预约申请服务
|
|
|
|
|
@Autowired
|
|
|
|
|
private SingleSeachService singleSeachService; // 单页数据服务
|
|
|
|
|
@Autowired
|
|
|
|
|
private YonghuService yonghuService; // 用户服务
|
|
|
|
|
@Autowired
|
|
|
|
|
private UsersService usersService; // 管理员服务
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//后端列表
|
|
|
|
|
//分页查询健身资讯数据
|
|
|
|
|
//@param params 请求参数,包含分页和查询条件
|
|
|
|
|
//@param request HTTP请求对象
|
|
|
|
|
//@return 返回分页数据和状态信息
|
|
|
|
|
@RequestMapping("/page")
|
|
|
|
|
public R page(@RequestParam Map<String, Object> params, HttpServletRequest request){
|
|
|
|
|
logger.debug("page方法:,,Controller:{},,params:{}",this.getClass().getName(),JSONObject.toJSONString(params));
|
|
|
|
|
String role = String.valueOf(request.getSession().getAttribute("role")); // 获取用户角色
|
|
|
|
|
if(false)
|
|
|
|
|
return R.error(511,"永不会进入");
|
|
|
|
|
else if("用户".equals(role))
|
|
|
|
|
params.put("yonghuId",request.getSession().getAttribute("userId")); // 如果是用户角色,添加用户ID查询条件
|
|
|
|
|
else if("教练".equals(role))
|
|
|
|
|
params.put("jiaolianId",request.getSession().getAttribute("userId")); // 如果是教练角色,添加教练ID查询条件
|
|
|
|
|
CommonUtil.checkMap(params); // 检查参数
|
|
|
|
|
PageUtils page = newsService.queryPage(params); // 分页查询
|
|
|
|
|
|
|
|
|
|
// 字典表数据转换
|
|
|
|
|
List<NewsView> list =(List<NewsView>)page.getList();
|
|
|
|
|
for(NewsView c:list){
|
|
|
|
|
// 修改对应字典表字段
|
|
|
|
|
dictionaryService.dictionaryConvert(c, request);
|
|
|
|
|
}
|
|
|
|
|
return R.ok().put("data", page); // 返回成功和数据
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//后端详情
|
|
|
|
|
//根据ID查询单条健身资讯详情
|
|
|
|
|
//@param id 资讯ID
|
|
|
|
|
//@param request HTTP请求对象
|
|
|
|
|
//@return 返回资讯详情和状态信息
|
|
|
|
|
@RequestMapping("/info/{id}")
|
|
|
|
|
public R info(@PathVariable("id") Long id, HttpServletRequest request){
|
|
|
|
|
logger.debug("info方法:,,Controller:{},,id:{}",this.getClass().getName(),id);
|
|
|
|
|
NewsEntity news = newsService.selectById(id); // 根据ID查询资讯
|
|
|
|
|
if(news !=null){
|
|
|
|
|
// entity转view
|
|
|
|
|
NewsView view = new NewsView();
|
|
|
|
|
BeanUtils.copyProperties( news , view ); // 把实体数据重构到view中
|
|
|
|
|
// 修改对应字典表字段
|
|
|
|
|
dictionaryService.dictionaryConvert(view, request);
|
|
|
|
|
return R.ok().put("data", view); // 返回成功和数据
|
|
|
|
|
}else {
|
|
|
|
|
return R.error(511,"查不到数据"); // 返回错误信息
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//后端保存
|
|
|
|
|
//新增健身资讯
|
|
|
|
|
//@param news 资讯实体
|
|
|
|
|
//@param request HTTP请求对象
|
|
|
|
|
//@return 返回操作结果状态
|
|
|
|
|
@RequestMapping("/save")
|
|
|
|
|
public R save(@RequestBody NewsEntity news, HttpServletRequest request){
|
|
|
|
|
logger.debug("save方法:,,Controller:{},,news:{}",this.getClass().getName(),news.toString());
|
|
|
|
|
|
|
|
|
|
String role = String.valueOf(request.getSession().getAttribute("role")); // 获取用户角色
|
|
|
|
|
if(false)
|
|
|
|
|
return R.error(511,"永远不会进入");
|
|
|
|
|
|
|
|
|
|
// 构建查询条件
|
|
|
|
|
Wrapper<NewsEntity> queryWrapper = new EntityWrapper<NewsEntity>()
|
|
|
|
|
.eq("news_name", news.getNewsName()) // 资讯标题相等
|
|
|
|
|
.eq("news_types", news.getNewsTypes()) // 资讯类型相等
|
|
|
|
|
;
|
|
|
|
|
|
|
|
|
|
logger.info("sql语句:"+queryWrapper.getSqlSegment());
|
|
|
|
|
NewsEntity newsEntity = newsService.selectOne(queryWrapper); // 查询是否已存在
|
|
|
|
|
if(newsEntity==null){
|
|
|
|
|
news.setInsertTime(new Date()); // 设置插入时间
|
|
|
|
|
news.setCreateTime(new Date()); // 设置创建时间
|
|
|
|
|
newsService.insert(news); // 插入新数据
|
|
|
|
|
return R.ok(); // 返回成功
|
|
|
|
|
}else {
|
|
|
|
|
return R.error(511,"表中有相同数据"); // 返回错误信息
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//后端修改
|
|
|
|
|
//更新健身资讯
|
|
|
|
|
//@param news 资讯实体
|
|
|
|
|
//@param request HTTP请求对象
|
|
|
|
|
//@return 返回操作结果状态
|
|
|
|
|
@RequestMapping("/update")
|
|
|
|
|
public R update(@RequestBody NewsEntity news, HttpServletRequest request) throws NoSuchFieldException, ClassNotFoundException, IllegalAccessException, InstantiationException {
|
|
|
|
|
logger.debug("update方法:,,Controller:{},,news:{}",this.getClass().getName(),news.toString());
|
|
|
|
|
NewsEntity oldNewsEntity = newsService.selectById(news.getId()); // 查询原先数据
|
|
|
|
|
|
|
|
|
|
String role = String.valueOf(request.getSession().getAttribute("role")); // 获取用户角色
|
|
|
|
|
// if(false)
|
|
|
|
|
// return R.error(511,"永远不会进入");
|
|
|
|
|
if("".equals(news.getNewsPhoto()) || "null".equals(news.getNewsPhoto())){
|
|
|
|
|
news.setNewsPhoto(null); // 如果图片为空,设置为null
|
|
|
|
|
}
|
|
|
|
|
if("".equals(news.getNewsContent()) || "null".equals(news.getNewsContent())){
|
|
|
|
|
news.setNewsContent(null); // 如果内容为空,设置为null
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
newsService.updateById(news); // 根据id更新
|
|
|
|
|
return R.ok(); // 返回成功
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//删除
|
|
|
|
|
//批量删除健身资讯
|
|
|
|
|
//@param ids 要删除的资讯ID数组
|
|
|
|
|
//@param request HTTP请求对象
|
|
|
|
|
//@return 返回操作结果状态
|
|
|
|
|
@RequestMapping("/delete")
|
|
|
|
|
public R delete(@RequestBody Integer[] ids, HttpServletRequest request){
|
|
|
|
|
logger.debug("delete:,,Controller:{},,ids:{}",this.getClass().getName(),ids.toString());
|
|
|
|
|
List<NewsEntity> oldNewsList =newsService.selectBatchIds(Arrays.asList(ids)); // 查询要删除的数据
|
|
|
|
|
newsService.deleteBatchIds(Arrays.asList(ids)); // 批量删除
|
|
|
|
|
|
|
|
|
|
return R.ok(); // 返回成功
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//批量上传
|
|
|
|
|
//通过Excel文件批量导入健身资讯数据
|
|
|
|
|
//@param fileName Excel文件名
|
|
|
|
|
//@param request HTTP请求对象
|
|
|
|
|
//@return 返回操作结果状态
|
|
|
|
|
@RequestMapping("/batchInsert")
|
|
|
|
|
public R save( String fileName, HttpServletRequest request){
|
|
|
|
|
logger.debug("batchInsert方法:,,Controller:{},,fileName:{}",this.getClass().getName(),fileName);
|
|
|
|
|
Integer yonghuId = Integer.valueOf(String.valueOf(request.getSession().getAttribute("userId"))); // 获取用户ID
|
|
|
|
|
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); // 日期格式化
|
|
|
|
|
try {
|
|
|
|
|
List<NewsEntity> newsList = new ArrayList<>(); // 存储上传的数据
|
|
|
|
|
Map<String, List<String>> seachFields= new HashMap<>(); // 要查询的字段
|
|
|
|
|
Date date = new Date();
|
|
|
|
|
int lastIndexOf = fileName.lastIndexOf("."); // 获取文件后缀
|
|
|
|
|
if(lastIndexOf == -1){
|
|
|
|
|
return R.error(511,"该文件没有后缀"); // 返回错误信息
|
|
|
|
|
}else{
|
|
|
|
|
String suffix = fileName.substring(lastIndexOf); // 获取文件后缀
|
|
|
|
|
if(!".xls".equals(suffix)){
|
|
|
|
|
return R.error(511,"只支持后缀为xls的excel文件"); // 返回错误信息
|
|
|
|
|
}else{
|
|
|
|
|
URL resource = this.getClass().getClassLoader().getResource("static/upload/" + fileName); // 获取文件路径
|
|
|
|
|
File file = new File(resource.getFile());
|
|
|
|
|
if(!file.exists()){
|
|
|
|
|
return R.error(511,"找不到上传文件,请联系管理员"); // 返回错误信息
|
|
|
|
|
}else{
|
|
|
|
|
List<List<String>> dataList = PoiUtil.poiImport(file.getPath()); // 读取xls文件
|
|
|
|
|
dataList.remove(0); // 删除第一行,因为第一行是提示
|
|
|
|
|
for(List<String> data:dataList){
|
|
|
|
|
// 循环处理每行数据
|
|
|
|
|
NewsEntity newsEntity = new NewsEntity();
|
|
|
|
|
// newsEntity.setNewsName(data.get(0)); // 资讯标题 要改的
|
|
|
|
|
// newsEntity.setNewsTypes(Integer.valueOf(data.get(0))); // 资讯类型 要改的
|
|
|
|
|
// newsEntity.setNewsPhoto("");//详情和图片
|
|
|
|
|
// newsEntity.setInsertTime(date);//时间
|
|
|
|
|
// newsEntity.setNewsContent("");//详情和图片
|
|
|
|
|
// newsEntity.setCreateTime(date);//时间
|
|
|
|
|
newsList.add(newsEntity); // 添加到列表
|
|
|
|
|
|
|
|
|
|
// 把要查询是否重复的字段放入map中
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 查询是否重复
|
|
|
|
|
newsService.insertBatch(newsList); // 批量插入
|
|
|
|
|
return R.ok(); // 返回成功
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}catch (Exception e){
|
|
|
|
|
e.printStackTrace();
|
|
|
|
|
return R.error(511,"批量插入数据异常,请联系管理员"); // 返回错误信息
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//前端列表
|
|
|
|
|
//分页查询健身资讯数据(无需认证)
|
|
|
|
|
//@param params 请求参数,包含分页和查询条件
|
|
|
|
|
//@param request HTTP请求对象
|
|
|
|
|
//@return 返回分页数据和状态信息
|
|
|
|
|
@IgnoreAuth
|
|
|
|
|
@RequestMapping("/list")
|
|
|
|
|
public R list(@RequestParam Map<String, Object> params, HttpServletRequest request){
|
|
|
|
|
logger.debug("list方法:,,Controller:{},,params:{}",this.getClass().getName(),JSONObject.toJSONString(params));
|
|
|
|
|
|
|
|
|
|
CommonUtil.checkMap(params); // 检查参数
|
|
|
|
|
PageUtils page = newsService.queryPage(params); // 分页查询
|
|
|
|
|
|
|
|
|
|
// 字典表数据转换
|
|
|
|
|
List<NewsView> list =(List<NewsView>)page.getList();
|
|
|
|
|
for(NewsView c:list)
|
|
|
|
|
dictionaryService.dictionaryConvert(c, request); // 修改对应字典表字段
|
|
|
|
|
|
|
|
|
|
return R.ok().put("data", page); // 返回成功和数据
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//前端详情
|
|
|
|
|
//根据ID查询单条健身资讯详情
|
|
|
|
|
//@param id 资讯ID
|
|
|
|
|
//@param request HTTP请求对象
|
|
|
|
|
//@return 返回资讯详情和状态信息
|
|
|
|
|
@RequestMapping("/detail/{id}")
|
|
|
|
|
public R detail(@PathVariable("id") Integer id, HttpServletRequest request){
|
|
|
|
|
logger.debug("detail方法:,,Controller:{},,id:{}",this.getClass().getName(),id);
|
|
|
|
|
NewsEntity news = newsService.selectById(id); // 根据ID查询资讯
|
|
|
|
|
if(news !=null){
|
|
|
|
|
// entity转view
|
|
|
|
|
NewsView view = new NewsView();
|
|
|
|
|
BeanUtils.copyProperties( news , view ); // 把实体数据重构到view中
|
|
|
|
|
|
|
|
|
|
// 修改对应字典表字段
|
|
|
|
|
dictionaryService.dictionaryConvert(view, request);
|
|
|
|
|
return R.ok().put("data", view); // 返回成功和数据
|
|
|
|
|
}else {
|
|
|
|
|
return R.error(511,"查不到数据"); // 返回错误信息
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//前端保存
|
|
|
|
|
//新增健身资讯
|
|
|
|
|
//@param news 资讯实体
|
|
|
|
|
//@param request HTTP请求对象
|
|
|
|
|
//@return 返回操作结果状态
|
|
|
|
|
@RequestMapping("/add")
|
|
|
|
|
public R add(@RequestBody NewsEntity news, HttpServletRequest request){
|
|
|
|
|
logger.debug("add方法:,,Controller:{},,news:{}",this.getClass().getName(),news.toString());
|
|
|
|
|
Wrapper<NewsEntity> queryWrapper = new EntityWrapper<NewsEntity>()
|
|
|
|
|
.eq("news_name", news.getNewsName()) // 资讯标题相等
|
|
|
|
|
.eq("news_types", news.getNewsTypes()) // 资讯类型相等
|
|
|
|
|
// .notIn("news_types", new Integer[]{102})
|
|
|
|
|
;
|
|
|
|
|
logger.info("sql语句:"+queryWrapper.getSqlSegment());
|
|
|
|
|
NewsEntity newsEntity = newsService.selectOne(queryWrapper); // 查询是否已存在
|
|
|
|
|
if(newsEntity==null){
|
|
|
|
|
news.setInsertTime(new Date()); // 设置插入时间
|
|
|
|
|
news.setCreateTime(new Date()); // 设置创建时间
|
|
|
|
|
newsService.insert(news); // 插入新数据
|
|
|
|
|
|
|
|
|
|
return R.ok(); // 返回成功
|
|
|
|
|
}else {
|
|
|
|
|
return R.error(511,"表中有相同数据"); // 返回错误信息
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|