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.
Onlineeducation/src/main/java/com/controller/GoumaidekechengController.java

317 lines
13 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

package com.controller;
// 导入用于格式化日期的类
import java.text.SimpleDateFormat;
// 导入用于创建动态数组的类
import java.util.ArrayList;
// 导入用于操作数组的工具类
import java.util.Arrays;
// 导入用于处理日期和时间的类
import java.util.Calendar;
// 导入用于存储键值对的接口
import java.util.Map;
// 导入 HashMap 类,用于实现 Map 接口
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;
// 导入 Spring 框架的请求参数注解
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.GoumaidekechengEntity;
// 导入购买的课程视图类
import com.entity.view.GoumaidekechengView;
// 导入购买的课程服务类
import com.service.GoumaidekechengService;
// 导入令牌服务类
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:14
*/
// 声明为 REST 控制器,用于处理 HTTP 请求
@RestController
// 映射请求路径,所有以 /goumaidekecheng 开头的请求由该控制器处理
@RequestMapping("/goumaidekecheng")
public class GoumaidekechengController {
// 自动注入购买的课程服务类实例
@Autowired
private GoumaidekechengService goumaidekechengService;
/**
* 后端列表(分页查询)
*/
// 映射请求路径 /goumaidekecheng/page
@RequestMapping("/page")
// 处理后端分页查询请求参数包括查询条件、实体对象、HTTP 请求
public R page(@RequestParam Map<String, Object> params, GoumaidekechengEntity goumaidekecheng,
HttpServletRequest request) {
// 从会话中获取表名(判断用户类型)
String tableName = request.getSession().getAttribute("tableName").toString();
// 如果表名是 "yonghu"(用户表)
if (tableName.equals("yonghu")) {
// 设置查询条件:用户账号为当前登录用户的用户名
goumaidekecheng.setYonghuzhanghao((String) request.getSession().getAttribute("username"));
}
// 创建实体包装器,用于构建查询条件
EntityWrapper<GoumaidekechengEntity> ew = new EntityWrapper<GoumaidekechengEntity>();
// 调用服务类的分页查询方法,包含排序、范围查询、模糊查询处理
PageUtils page = goumaidekechengService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, goumaidekecheng), params), params));
// 返回成功响应,携带分页数据
return R.ok().put("data", page);
}
/**
* 前端列表(分页查询)
*/
// 映射请求路径 /goumaidekecheng/list
@RequestMapping("/list")
// 处理前端分页查询请求参数包括查询条件、实体对象、HTTP 请求
public R list(@RequestParam Map<String, Object> params, GoumaidekechengEntity goumaidekecheng,
HttpServletRequest request) {
// 创建实体包装器,用于构建查询条件
EntityWrapper<GoumaidekechengEntity> ew = new EntityWrapper<GoumaidekechengEntity>();
// 调用服务类的分页查询方法,包含排序、范围查询、模糊查询处理
PageUtils page = goumaidekechengService.queryPage(params, MPUtil.sort(MPUtil.between(MPUtil.likeOrEq(ew, goumaidekecheng), params), params));
// 返回成功响应,携带分页数据
return R.ok().put("data", page);
}
/**
* 列表(不分页,全量查询)
*/
// 映射请求路径 /goumaidekecheng/lists
@RequestMapping("/lists")
// 处理全量查询请求,参数为实体对象
public R list(GoumaidekechengEntity goumaidekecheng) {
// 创建实体包装器
EntityWrapper<GoumaidekechengEntity> ew = new EntityWrapper<GoumaidekechengEntity>();
// 设置查询条件:所有字段与实体对象相等(前缀为 "goumaidekecheng"
ew.allEq(MPUtil.allEQMapPre(goumaidekecheng, "goumaidekecheng"));
// 返回成功响应,携带查询结果(视图列表)
return R.ok().put("data", goumaidekechengService.selectListView(ew));
}
/**
* 查询(单个对象)
*/
// 映射请求路径 /goumaidekecheng/query
@RequestMapping("/query")
// 处理单个对象查询请求,参数为实体对象
public R query(GoumaidekechengEntity goumaidekecheng) {
// 创建实体包装器
EntityWrapper<GoumaidekechengEntity> ew = new EntityWrapper<GoumaidekechengEntity>();
// 设置查询条件:所有字段与实体对象相等(前缀为 "goumaidekecheng"
ew.allEq(MPUtil.allEQMapPre(goumaidekecheng, "goumaidekecheng"));
// 调用服务类的视图查询方法
GoumaidekechengView goumaidekechengView = goumaidekechengService.selectView(ew);
// 返回成功响应,携带查询结果和提示信息
return R.ok("查询购买的课程成功").put("data", goumaidekechengView);
}
/**
* 后端详情(根据 ID 查询)
*/
// 映射请求路径 /goumaidekecheng/info/{id}
@RequestMapping("/info/{id}")
// 处理详情查询请求,参数为路径中的 ID
public R info(@PathVariable("id") Long id) {
// 根据 ID 直接查询实体对象
GoumaidekechengEntity goumaidekecheng = goumaidekechengService.selectById(id);
// 返回成功响应,携带查询到的实体数据
return R.ok().put("data", goumaidekecheng);
}
/**
* 前端详情(根据 ID 查询)
*/
// 映射请求路径 /goumaidekecheng/detail/{id}
@RequestMapping("/detail/{id}")
// 处理详情查询请求,参数为路径中的 ID前端调用
public R detail(@PathVariable("id") Long id) {
// 根据 ID 直接查询实体对象
GoumaidekechengEntity goumaidekecheng = goumaidekechengService.selectById(id);
// 返回成功响应,携带查询到的实体数据
return R.ok().put("data", goumaidekecheng);
}
/**
* 后端保存(新增数据)
*/
// 映射请求路径 /goumaidekecheng/save
@RequestMapping("/save")
// 处理后端新增请求,请求体为实体对象,携带 HTTP 请求
public R save(@RequestBody GoumaidekechengEntity goumaidekecheng, HttpServletRequest request) {
// 生成唯一 ID时间戳 + 随机数)
goumaidekecheng.setId(new Date().getTime() + new Double(Math.floor(Math.random() * 1000)).longValue());
// 实体验证(注释,未启用)
//ValidatorUtils.validateEntity(goumaidekecheng);
// 调用服务类的插入方法
goumaidekechengService.insert(goumaidekecheng);
// 返回成功响应
return R.ok();
}
/**
* 前端保存(新增数据)
*/
// 映射请求路径 /goumaidekecheng/add
@RequestMapping("/add")
// 处理前端新增请求,请求体为实体对象,携带 HTTP 请求
public R add(@RequestBody GoumaidekechengEntity goumaidekecheng, HttpServletRequest request) {
// 生成唯一 ID时间戳 + 随机数)
goumaidekecheng.setId(new Date().getTime() + new Double(Math.floor(Math.random() * 1000)).longValue());
// 实体验证(注释,未启用)
//ValidatorUtils.validateEntity(goumaidekecheng);
// 调用服务类的插入方法
goumaidekechengService.insert(goumaidekecheng);
// 返回成功响应
return R.ok();
}
/**
* 修改(更新数据)
*/
// 映射请求路径 /goumaidekecheng/update
@RequestMapping("/update")
// 处理修改请求,请求体为实体对象,携带 HTTP 请求
public R update(@RequestBody GoumaidekechengEntity goumaidekecheng, HttpServletRequest request) {
// 实体验证(注释,未启用)
//ValidatorUtils.validateEntity(goumaidekecheng);
// 调用服务类的更新方法(根据 ID 全量更新)
goumaidekechengService.updateById(goumaidekecheng); // 全部更新
// 返回成功响应
return R.ok();
}
/**
* 删除(批量删除)
*/
// 映射请求路径 /goumaidekecheng/delete
@RequestMapping("/delete")
// 处理删除请求,请求体为 ID 数组
public R delete(@RequestBody Long[] ids) {
// 调用服务类的批量删除方法(根据 ID 列表)
goumaidekechengService.deleteBatchIds(Arrays.asList(ids));
// 返回成功响应
return R.ok();
}
/**
* 提醒接口(统计符合条件的记录数)
*/
// 映射请求路径 /goumaidekecheng/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<GoumaidekechengEntity> wrapper = new EntityWrapper<GoumaidekechengEntity>();
// 添加提醒开始时间的大于等于条件
if (map.get("remindstart") != null) {
wrapper.ge(columnName, map.get("remindstart"));
}
// 添加提醒结束时间的小于等于条件
if (map.get("remindend") != null) {
wrapper.le(columnName, map.get("remindend"));
}
// 根据表名判断是否添加用户账号条件(用户表场景)
String tableName = request.getSession().getAttribute("tableName").toString();
if (tableName.equals("yonghu")) {
wrapper.eq("yonghuzhanghao", (String) request.getSession().getAttribute("username"));
}
// 调用服务类统计符合条件的记录数
int count = goumaidekechengService.selectCount(wrapper);
// 返回成功响应,携带统计结果
return R.ok().put("count", count);
}
}