ADD file via upload

pull/1/head
phfsut2ie 4 months ago
parent eaaec072ae
commit 4872bec264

@ -0,0 +1,330 @@
package com.controller;// 定义包路径表示该文件位于com.controller包下
import java.io.File; // 导入文件操作类
import java.math.BigDecimal; // 导入高精度数学计算类
import java.net.URL; // 导入网络URL处理类
import java.text.SimpleDateFormat; // 导入日期格式化类
import com.alibaba.fastjson.JSONObject; // 导入FastJSON的JSON对象类
import java.util.*; // 导入Java常用工具类集合、日期等
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("/forum")
public class ForumController {
private static final Logger logger = LoggerFactory.getLogger(ForumController.class);
private static final String TABLE_NAME = "forum"; // 数据库表名
@Autowired
private ForumService forumService; // 论坛服务
@Autowired
private TokenService tokenService; // token服务
@Autowired
private DictionaryService dictionaryService; // 字典服务
@Autowired
private JianshenkechengService jianshenkechengService; // 健身课程服务
@Autowired
private JianshenkechengCollectionService jianshenkechengCollectionService; // 课程收藏服务
@Autowired
private JianshenkechengLiuyanService jianshenkechengLiuyanService; // 课程留言服务
@Autowired
private JiaolianService jiaolianService; // 教练服务
@Autowired
private JiaolianYuyueService jiaolianYuyueService; // 教练预约申请服务
@Autowired
private NewsService newsService; // 健身资讯服务
@Autowired
private SingleSeachService singleSeachService; // 单页数据服务
@Autowired
private YonghuService yonghuService; // 用户服务
@Autowired
private UsersService usersService; // 管理员服务
//后端列表
@RequestMapping("/page")
public R page(@RequestParam Map<String, Object> params, HttpServletRequest request){
logger.debug("page方法:,,Controller:{},,params:{}",this.getClass().getName(),JSONObject.toJSONString(params));
CommonUtil.checkMap(params); // 检查参数
PageUtils page = forumService.queryPage(params); // 查询分页数据
// 字典表数据转换
List<ForumView> list =(List<ForumView>)page.getList();
for(ForumView c:list){
// 修改对应字典表字段
dictionaryService.dictionaryConvert(c, request);
}
return R.ok().put("data", page); // 返回分页数据
}
//后端详情
@RequestMapping("/info/{id}")
public R info(@PathVariable("id") Long id, HttpServletRequest request){
logger.debug("info方法:,,Controller:{},,id:{}",this.getClass().getName(),id);
ForumEntity forum = forumService.selectById(id); // 根据ID查询论坛帖子
if(forum !=null){
// entity转view
ForumView view = new ForumView();
BeanUtils.copyProperties( forum , view ); // 把实体数据重构到view中
// 级联表 用户
YonghuEntity yonghu = yonghuService.selectById(forum.getYonghuId());
if(yonghu != null){
BeanUtils.copyProperties( yonghu , view ,new String[]{ "id", "createTime", "insertTime", "updateTime", "username", "password", "newMoney", "yonghuId"
, "jiaolianId"
, "usersId"}); // 把级联的数据添加到view中,并排除id和创建时间字段
view.setYonghuId(yonghu.getId());
}
// 级联表 教练
JiaolianEntity jiaolian = jiaolianService.selectById(forum.getJiaolianId());
if(jiaolian != null){
BeanUtils.copyProperties( jiaolian , view ,new String[]{ "id", "createTime", "insertTime", "updateTime", "username", "password", "newMoney", "yonghuId"
, "jiaolianId"
, "usersId"}); // 把级联的数据添加到view中,并排除id和创建时间字段
view.setJiaolianId(jiaolian.getId());
}
// 级联表 管理员
UsersEntity users = usersService.selectById(forum.getUsersId());
if(users != null){
view.setUsersId(users.getId()); // 设置管理员ID
view.setUusername(users.getUsername()); // 设置管理员用户名
view.setUpassword(users.getPassword()); // 设置管理员密码
view.setUrole(users.getRole()); // 设置管理员角色
view.setUaddtime(users.getAddtime()); // 设置管理员添加时间
}
// 修改对应字典表字段
dictionaryService.dictionaryConvert(view, request);
return R.ok().put("data", view); // 返回帖子详情
}else {
return R.error(511,"查不到数据"); // 查询不到数据返回错误
}
}
//后端保存
@RequestMapping("/save")
public R save(@RequestBody ForumEntity forum, HttpServletRequest request){
logger.debug("save方法:,,Controller:{},,forum:{}",this.getClass().getName(),forum.toString());
String role = String.valueOf(request.getSession().getAttribute("role")); // 获取用户角色
if(false)
return R.error(511,"永远不会进入");
else if("用户".equals(role))
forum.setYonghuId(Integer.valueOf(String.valueOf(request.getSession().getAttribute("userId")))); // 设置用户ID
else if("教练".equals(role))
forum.setJiaolianId(Integer.valueOf(String.valueOf(request.getSession().getAttribute("userId")))); // 设置教练ID
else if("管理员".equals(role))
forum.setUsersId(Integer.valueOf(String.valueOf(request.getSession().getAttribute("userId")))); // 设置管理员ID
Wrapper<ForumEntity> queryWrapper = new EntityWrapper<ForumEntity>()
.eq("forum_name", forum.getForumName()) // 帖子标题
.eq("yonghu_id", forum.getYonghuId()) // 用户ID
.eq("jiaolian_id", forum.getJiaolianId()) // 教练ID
.eq("users_id", forum.getUsersId()) // 管理员ID
.eq("super_ids", forum.getSuperIds()) // 父ID
.eq("forum_state_types", forum.getForumStateTypes()) // 帖子状态
;
logger.info("sql语句:"+queryWrapper.getSqlSegment());
ForumEntity forumEntity = forumService.selectOne(queryWrapper); // 查询是否已存在相同数据
if(forumEntity==null){
forum.setInsertTime(new Date()); // 设置插入时间
forum.setCreateTime(new Date()); // 设置创建时间
forumService.insert(forum); // 插入新数据
return R.ok();
}else {
return R.error(511,"表中有相同数据"); // 数据已存在返回错误
}
}
//后端修改
@RequestMapping("/update")
public R update(@RequestBody ForumEntity forum, HttpServletRequest request) throws NoSuchFieldException, ClassNotFoundException, IllegalAccessException, InstantiationException {
logger.debug("update方法:,,Controller:{},,forum:{}",this.getClass().getName(),forum.toString());
ForumEntity oldForumEntity = forumService.selectById(forum.getId()); // 查询原先数据
String role = String.valueOf(request.getSession().getAttribute("role")); // 获取用户角色
if("".equals(forum.getForumContent()) || "null".equals(forum.getForumContent())){
forum.setForumContent(null); // 处理空内容
}
forum.setUpdateTime(new Date()); // 设置更新时间
forumService.updateById(forum); // 根据ID更新数据
return R.ok();
}
//删除
@RequestMapping("/delete")
public R delete(@RequestBody Integer[] ids, HttpServletRequest request){
logger.debug("delete:,,Controller:{},,ids:{}",this.getClass().getName(),ids.toString());
List<ForumEntity> oldForumList =forumService.selectBatchIds(Arrays.asList(ids)); // 查询要删除的数据
forumService.deleteBatchIds(Arrays.asList(ids)); // 批量删除
return R.ok();
}
//批量上传
@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<ForumEntity> forumList = 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){
// 循环处理每行数据
ForumEntity forumEntity = new ForumEntity();
forumList.add(forumEntity); // 添加到列表
}
// 批量插入数据
forumService.insertBatch(forumList);
return R.ok();
}
}
}
}catch (Exception e){
e.printStackTrace();
return R.error(511,"批量插入数据异常,请联系管理员"); // 异常处理
}
}
// 前端列表
@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 = forumService.queryPage(params); // 查询分页数据
// 字典表数据转换
List<ForumView> list =(List<ForumView>)page.getList();
for(ForumView c:list)
dictionaryService.dictionaryConvert(c, request); // 修改对应字典表字段
return R.ok().put("data", page); // 返回分页数据
}
//前端详情
@RequestMapping("/detail/{id}")
public R detail(@PathVariable("id") Integer id, HttpServletRequest request){
logger.debug("detail方法:,,Controller:{},,id:{}",this.getClass().getName(),id);
ForumEntity forum = forumService.selectById(id); // 根据ID查询帖子
if(forum !=null){
// entity转view
ForumView view = new ForumView();
BeanUtils.copyProperties( forum , view ); // 把实体数据重构到view中
// 级联表用户
YonghuEntity yonghu = yonghuService.selectById(forum.getYonghuId());
if(yonghu != null){
BeanUtils.copyProperties( yonghu , view ,new String[]{ "id", "createTime", "insertTime", "updateTime", "username", "password", "newMoney", "yonghuId"
, "jiaolianId"
, "usersId"}); // 把级联的数据添加到view中,并排除id和创建时间字段
view.setYonghuId(yonghu.getId());
}
// 级联表教练
JiaolianEntity jiaolian = jiaolianService.selectById(forum.getJiaolianId());
if(jiaolian != null){
BeanUtils.copyProperties( jiaolian , view ,new String[]{ "id", "createTime", "insertTime", "updateTime", "username", "password", "newMoney", "yonghuId"
, "jiaolianId"
, "usersId"}); // 把级联的数据添加到view中,并排除id和创建时间字段
view.setJiaolianId(jiaolian.getId());
}
// 级联表管理员
UsersEntity users = usersService.selectById(forum.getUsersId());
if(users != null){
view.setUsersId(users.getId()); // 设置管理员ID
view.setUusername(users.getUsername()); // 设置管理员用户名
view.setUpassword(users.getPassword()); // 设置管理员密码
view.setUrole(users.getRole()); // 设置管理员角色
view.setUaddtime(users.getAddtime()); // 设置管理员添加时间
}
// 修改对应字典表字段
dictionaryService.dictionaryConvert(view, request);
return R.ok().put("data", view); // 返回帖子详情
}else {
return R.error(511,"查不到数据"); // 查询不到数据返回错误
}
}
//前端保存
@RequestMapping("/add")
public R add(@RequestBody ForumEntity forum, HttpServletRequest request){
logger.debug("add方法:,,Controller:{},,forum:{}",this.getClass().getName(),forum.toString());
Wrapper<ForumEntity> queryWrapper = new EntityWrapper<ForumEntity>()
.eq("forum_name", forum.getForumName()) // 帖子标题
.eq("yonghu_id", forum.getYonghuId()) // 用户ID
.eq("jiaolian_id", forum.getJiaolianId()) // 教练ID
.eq("users_id", forum.getUsersId()) // 管理员ID
.eq("super_ids", forum.getSuperIds()) // 父ID
.eq("forum_state_types", forum.getForumStateTypes()) // 帖子状态
;
logger.info("sql语句:"+queryWrapper.getSqlSegment());
ForumEntity forumEntity = forumService.selectOne(queryWrapper); // 查询是否已存在相同数据
if(forumEntity==null){
forum.setInsertTime(new Date()); // 设置插入时间
forum.setCreateTime(new Date()); // 设置创建时间
forumService.insert(forum); // 插入新数据
return R.ok();
}else {
return R.error(511,"表中有相同数据"); // 数据已存在返回错误
}
}
}
Loading…
Cancel
Save