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 // 标识为RESTful控制器 @Controller // 标识为Spring控制器 @RequestMapping("/jianshenkechengCollection") // 定义请求映射路径 public class JianshenkechengCollectionController { private static final Logger logger = LoggerFactory.getLogger(JianshenkechengCollectionController.class); // 日志记录器 private static final String TABLE_NAME = "jianshenkechengCollection"; // 数据库表名 @Autowired private JianshenkechengCollectionService jianshenkechengCollectionService; // 课程收藏服务 @Autowired private TokenService tokenService; // Token服务 @Autowired private DictionaryService dictionaryService; // 字典服务 @Autowired private ForumService forumService; // 健身论坛服务 @Autowired private JianshenkechengService jianshenkechengService; // 健身课程服务 @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 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 = jianshenkechengCollectionService.queryPage(params); // 查询分页数据 // 字典表数据转换 List list =(List)page.getList(); for(JianshenkechengCollectionView 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); // 记录日志 JianshenkechengCollectionEntity jianshenkechengCollection = jianshenkechengCollectionService.selectById(id); // 根据ID查询收藏记录 if(jianshenkechengCollection !=null){ // entity转view JianshenkechengCollectionView view = new JianshenkechengCollectionView(); BeanUtils.copyProperties( jianshenkechengCollection , view ); // 把实体数据重构到view中 // 级联表 健身课程 JianshenkechengEntity jianshenkecheng = jianshenkechengService.selectById(jianshenkechengCollection.getJianshenkechengId()); if(jianshenkecheng != null){ BeanUtils.copyProperties( jianshenkecheng , view ,new String[]{ "id", "createTime", "insertTime", "updateTime", "username", "password", "newMoney", "yonghuId"}); // 把级联的数据添加到view中,并排除id和创建时间字段 view.setJianshenkechengId(jianshenkecheng.getId()); } // 级联表 用户 YonghuEntity yonghu = yonghuService.selectById(jianshenkechengCollection.getYonghuId()); if(yonghu != null){ BeanUtils.copyProperties( yonghu , view ,new String[]{ "id", "createTime", "insertTime", "updateTime", "username", "password", "newMoney", "yonghuId"}); // 把级联的数据添加到view中,并排除id和创建时间字段 view.setYonghuId(yonghu.getId()); } // 修改对应字典表字段 dictionaryService.dictionaryConvert(view, request); return R.ok().put("data", view); // 返回详情数据 }else { return R.error(511,"查不到数据"); // 查询不到数据返回错误 } } //后端保存 @RequestMapping("/save") // 处理保存请求 public R save(@RequestBody JianshenkechengCollectionEntity jianshenkechengCollection, HttpServletRequest request){ logger.debug("save方法:,,Controller:{},,jianshenkechengCollection:{}",this.getClass().getName(),jianshenkechengCollection.toString()); // 记录日志 String role = String.valueOf(request.getSession().getAttribute("role")); // 获取用户角色 if(false) return R.error(511,"永远不会进入"); // 永远不会执行的代码 else if("用户".equals(role)) jianshenkechengCollection.setYonghuId(Integer.valueOf(String.valueOf(request.getSession().getAttribute("userId")))); // 如果是用户角色,设置用户ID Wrapper queryWrapper = new EntityWrapper() .eq("jianshenkecheng_id", jianshenkechengCollection.getJianshenkechengId()) // 健身课程ID .eq("yonghu_id", jianshenkechengCollection.getYonghuId()) // 用户ID .eq("jianshenkecheng_collection_types", jianshenkechengCollection.getJianshenkechengCollectionTypes()) // 收藏类型 ; logger.info("sql语句:"+queryWrapper.getSqlSegment()); // 记录SQL语句 JianshenkechengCollectionEntity jianshenkechengCollectionEntity = jianshenkechengCollectionService.selectOne(queryWrapper); // 查询是否已存在相同数据 if(jianshenkechengCollectionEntity==null){ jianshenkechengCollection.setInsertTime(new Date()); // 设置插入时间 jianshenkechengCollection.setCreateTime(new Date()); // 设置创建时间 jianshenkechengCollectionService.insert(jianshenkechengCollection); // 插入新数据 return R.ok(); // 返回成功 }else { return R.error(511,"表中有相同数据"); // 数据已存在返回错误 } } //后端修改 @RequestMapping("/update") // 处理更新请求 public R update(@RequestBody JianshenkechengCollectionEntity jianshenkechengCollection, HttpServletRequest request) throws NoSuchFieldException, ClassNotFoundException, IllegalAccessException, InstantiationException { logger.debug("update方法:,,Controller:{},,jianshenkechengCollection:{}",this.getClass().getName(),jianshenkechengCollection.toString()); // 记录日志 JianshenkechengCollectionEntity oldJianshenkechengCollectionEntity = jianshenkechengCollectionService.selectById(jianshenkechengCollection.getId()); // 查询原先数据 jianshenkechengCollectionService.updateById(jianshenkechengCollection); // 根据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 oldJianshenkechengCollectionList =jianshenkechengCollectionService.selectBatchIds(Arrays.asList(ids)); // 查询要删除的数据 jianshenkechengCollectionService.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 jianshenkechengCollectionList = new ArrayList<>(); // 创建收藏记录列表 Map> 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> dataList = PoiUtil.poiImport(file.getPath()); // 读取xls文件 dataList.remove(0); // 删除第一行提示信息 for(List data:dataList){ // 循环处理每行数据 JianshenkechengCollectionEntity jianshenkechengCollectionEntity = new JianshenkechengCollectionEntity(); jianshenkechengCollectionList.add(jianshenkechengCollectionEntity); // 添加到列表 } // 批量插入数据 jianshenkechengCollectionService.insertBatch(jianshenkechengCollectionList); return R.ok(); // 返回成功 } } } }catch (Exception e){ e.printStackTrace(); // 打印异常堆栈 return R.error(511,"批量插入数据异常,请联系管理员"); // 异常处理 } } //前端列表 @IgnoreAuth // 忽略认证 @RequestMapping("/list") // 处理列表请求 public R list(@RequestParam Map params, HttpServletRequest request){ logger.debug("list方法:,,Controller:{},,params:{}",this.getClass().getName(),JSONObject.toJSONString(params)); // 记录日志 CommonUtil.checkMap(params); // 检查参数 PageUtils page = jianshenkechengCollectionService.queryPage(params); // 查询分页数据 // 字典表数据转换 List list =(List)page.getList(); for(JianshenkechengCollectionView 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); // 记录日志 JianshenkechengCollectionEntity jianshenkechengCollection = jianshenkechengCollectionService.selectById(id); // 根据ID查询收藏记录 if(jianshenkechengCollection !=null){ // entity转view JianshenkechengCollectionView view = new JianshenkechengCollectionView(); BeanUtils.copyProperties( jianshenkechengCollection , view ); // 把实体数据重构到view中 // 级联表健身课程 JianshenkechengEntity jianshenkecheng = jianshenkechengService.selectById(jianshenkechengCollection.getJianshenkechengId()); if(jianshenkecheng != null){ BeanUtils.copyProperties( jianshenkecheng , view ,new String[]{ "id", "createTime", "insertTime", "updateTime", "username", "password", "newMoney", "yonghuId"}); // 把级联的数据添加到view中,并排除id和创建时间字段 view.setJianshenkechengId(jianshenkecheng.getId()); } // 级联表用户 YonghuEntity yonghu = yonghuService.selectById(jianshenkechengCollection.getYonghuId()); if(yonghu != null){ BeanUtils.copyProperties( yonghu , view ,new String[]{ "id", "createTime", "insertTime", "updateTime", "username", "password", "newMoney", "yonghuId"}); // 把级联的数据添加到view中,并排除id和创建时间字段 view.setYonghuId(yonghu.getId()); } // 修改对应字典表字段 dictionaryService.dictionaryConvert(view, request); return R.ok().put("data", view); // 返回详情数据 }else { return R.error(511,"查不到数据"); // 查询不到数据返回错误 } } //前端保存 @RequestMapping("/add") // 处理添加请求 public R add(@RequestBody JianshenkechengCollectionEntity jianshenkechengCollection, HttpServletRequest request){ logger.debug("add方法:,,Controller:{},,jianshenkechengCollection:{}",this.getClass().getName(),jianshenkechengCollection.toString()); // 记录日志 Wrapper queryWrapper = new EntityWrapper() .eq("jianshenkecheng_id", jianshenkechengCollection.getJianshenkechengId()) // 健身课程ID .eq("yonghu_id", jianshenkechengCollection.getYonghuId()) // 用户ID .eq("jianshenkecheng_collection_types", jianshenkechengCollection.getJianshenkechengCollectionTypes()) // 收藏类型 ; logger.info("sql语句:"+queryWrapper.getSqlSegment()); // 记录SQL语句 JianshenkechengCollectionEntity jianshenkechengCollectionEntity = jianshenkechengCollectionService.selectOne(queryWrapper); // 查询是否已存在相同数据 if(jianshenkechengCollectionEntity==null){ jianshenkechengCollection.setInsertTime(new Date()); // 设置插入时间 jianshenkechengCollection.setCreateTime(new Date()); // 设置创建时间 jianshenkechengCollectionService.insert(jianshenkechengCollection); // 插入新数据 return R.ok(); // 返回成功 }else { return R.error(511,"您已经收藏过了"); // 已经收藏过返回错误 } } }