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.
gym/JiaolianController.java

575 lines
24 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.

// 声明当前 Java 文件所属的包此包用于存放控制器类处理与教练相关的HTTP请求
package com.controller;
// 导入Java IO包中的File类用于文件操作
import java.io.File;
// 导入Java数学包中的BigDecimal类用于高精度计算
import java.math.BigDecimal;
// 导入Java网络包中的URL类用于处理统一资源定位符
import java.net.URL;
// 导入Java文本包中的SimpleDateFormat类用于日期格式化
import java.text.SimpleDateFormat;
// 导入FastJSON库中的JSONObject类用于JSON数据处理
import com.alibaba.fastjson.JSONObject;
// 导入Java工具包中的所有类包括集合、日期等工具
import java.util.*;
// 导入Spring框架的BeanUtils类用于对象属性复制
import org.springframework.beans.BeanUtils;
// 导入Servlet的HttpServletRequest类用于处理HTTP请求
import javax.servlet.http.HttpServletRequest;
// 导入Spring框架的ContextLoader类用于获取应用上下文
import org.springframework.web.context.ContextLoader;
// 导入Servlet的ServletContext类表示Servlet上下文
import javax.servlet.ServletContext;
// 导入自定义的TokenService类用于令牌管理
import com.service.TokenService;
// 导入自定义工具类包中的所有工具类
import com.utils.*;
// 导入反射相关的InvocationTargetException类
import java.lang.reflect.InvocationTargetException;
// 导入自定义的DictionaryService类用于字典数据管理
import com.service.DictionaryService;
// 导入Apache Commons Lang3库的StringUtils类用于字符串处理
import org.apache.commons.lang3.StringUtils;
// 导入自定义的IgnoreAuth注解用于标记不需要认证的接口
import com.annotation.IgnoreAuth;
// 导入SLF4J日志框架的Logger接口
import org.slf4j.Logger;
// 导入SLF4J日志框架的LoggerFactory类
import org.slf4j.LoggerFactory;
// 导入Spring框架的Autowired注解用于依赖注入
import org.springframework.beans.factory.annotation.Autowired;
// 导入Spring框架的Controller注解标记该类为控制器
import org.springframework.stereotype.Controller;
// 导入Spring框架的RestController和RequestMapping等注解
import org.springframework.web.bind.annotation.*;
// 导入MyBatis Plus的EntityWrapper类用于构建查询条件
import com.baomidou.mybatisplus.mapper.EntityWrapper;
// 导入MyBatis Plus的Wrapper接口
import com.baomidou.mybatisplus.mapper.Wrapper;
// 导入自定义实体类包中的所有类
import com.entity.*;
// 导入自定义视图实体类包中的所有类
import com.entity.view.*;
// 导入自定义服务类包中的所有类
import com.service.*;
// 导入自定义分页工具类
import com.utils.PageUtils;
// 导入自定义统一返回结果类
import com.utils.R;
// 导入FastJSON库中的所有类
import com.alibaba.fastjson.*;
/**
* 教练控制器
* 后端接口
*/
@RestController // 标记为RESTful控制器
@Controller // 标记为Spring控制器
@RequestMapping("/jiaolian") // 定义基础请求路径
public class JiaolianController {
// 创建日志记录器
private static final Logger logger = LoggerFactory.getLogger(JiaolianController.class);
// 定义常量表名
private static final String TABLE_NAME = "jiaolian";
// 自动注入教练服务
@Autowired
private JiaolianService jiaolianService;
// 自动注入令牌服务
@Autowired
private TokenService tokenService;
// 自动注入字典服务
@Autowired
private DictionaryService dictionaryService;
// 自动注入论坛服务
@Autowired
private ForumService forumService;
// 自动注入健身课程服务
@Autowired
private JianshenkechengService jianshenkechengService;
// 自动注入课程收藏服务
@Autowired
private JianshenkechengCollectionService jianshenkechengCollectionService;
// 自动注入课程留言服务
@Autowired
private JianshenkechengLiuyanService jianshenkechengLiuyanService;
// 自动注入教练预约服务
@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("分页查询方法:,,控制器:{},,参数:{}",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"));
else if("教练".equals(role))
params.put("jiaolianId",request.getSession().getAttribute("userId"));
// 设置数据删除状态条件
params.put("dataDeleteStart",1);
params.put("dataDeleteEnd",1);
// 检查参数有效性
CommonUtil.checkMap(params);
// 调用服务层查询分页数据
PageUtils page = jiaolianService.queryPage(params);
// 字典数据转换
List<JiaolianView> list =(List<JiaolianView>)page.getList();
for(JiaolianView 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("详情查询方法:,,控制器:{},,ID:{}",this.getClass().getName(),id);
// 根据ID查询教练信息
JiaolianEntity jiaolian = jiaolianService.selectById(id);
if(jiaolian !=null){
// 实体转视图
JiaolianView view = new JiaolianView();
// 复制属性
BeanUtils.copyProperties( jiaolian , view );
// 转换字典表字段
dictionaryService.dictionaryConvert(view, request);
return R.ok().put("data", view);
}else {
return R.error(511,"查不到数据");
}
}
/**
* 后端保存教练信息
*/
@RequestMapping("/save")
public R save(@RequestBody JiaolianEntity jiaolian, HttpServletRequest request){
logger.debug("保存方法:,,控制器:{},,教练信息:{}",this.getClass().getName(),jiaolian.toString());
// 获取当前用户角色
String role = String.valueOf(request.getSession().getAttribute("role"));
if(false)
return R.error(511,"永远不会进入");
// 构建查询条件,检查用户名和手机号是否已存在
Wrapper<JiaolianEntity> queryWrapper = new EntityWrapper<JiaolianEntity>()
.eq("username", jiaolian.getUsername())
.or()
.eq("jiaolian_phone", jiaolian.getJiaolianPhone())
.eq("data_delete", 1)
;
logger.info("SQL语句:"+queryWrapper.getSqlSegment());
// 执行查询
JiaolianEntity jiaolianEntity = jiaolianService.selectOne(queryWrapper);
if(jiaolianEntity==null){
// 设置默认值
jiaolian.setDataDelete(1); // 数据删除状态
jiaolian.setInsertTime(new Date()); // 插入时间
jiaolian.setCreateTime(new Date()); // 创建时间
jiaolian.setPassword("123456"); // 默认密码
// 保存教练信息
jiaolianService.insert(jiaolian);
return R.ok();
}else {
return R.error(511,"账户或者教练手机号已经被使用");
}
}
/**
* 后端修改教练信息
*/
@RequestMapping("/update")
public R update(@RequestBody JiaolianEntity jiaolian, HttpServletRequest request) throws NoSuchFieldException, ClassNotFoundException, IllegalAccessException, InstantiationException {
logger.debug("修改方法:,,控制器:{},,教练信息:{}",this.getClass().getName(),jiaolian.toString());
// 查询原有数据
JiaolianEntity oldJiaolianEntity = jiaolianService.selectById(jiaolian.getId());
// 获取当前用户角色
String role = String.valueOf(request.getSession().getAttribute("role"));
// 处理空字段
if("".equals(jiaolian.getJiaolianPhoto()) || "null".equals(jiaolian.getJiaolianPhoto())){
jiaolian.setJiaolianPhoto(null);
}
if("".equals(jiaolian.getJiaolianContent()) || "null".equals(jiaolian.getJiaolianContent())){
jiaolian.setJiaolianContent(null);
}
// 更新数据
jiaolianService.updateById(jiaolian);
return R.ok();
}
/**
* 删除教练信息(逻辑删除)
*/
@RequestMapping("/delete")
public R delete(@RequestBody Integer[] ids, HttpServletRequest request){
logger.debug("删除方法:,,控制器:{},,IDs:{}",this.getClass().getName(),ids.toString());
// 查询要删除的数据
List<JiaolianEntity> oldJiaolianList =jiaolianService.selectBatchIds(Arrays.asList(ids));
// 准备更新列表
ArrayList<JiaolianEntity> list = new ArrayList<>();
for(Integer id:ids){
JiaolianEntity jiaolianEntity = new JiaolianEntity();
jiaolianEntity.setId(id);
jiaolianEntity.setDataDelete(2); // 标记为删除
list.add(jiaolianEntity);
}
// 批量更新删除状态
if(list != null && list.size() >0){
jiaolianService.updateBatchById(list);
}
return R.ok();
}
/**
* 批量导入教练数据
*/
@RequestMapping("/batchInsert")
public R save( String fileName, HttpServletRequest request){
logger.debug("批量导入方法:,,控制器:{},,文件名:{}",this.getClass().getName(),fileName);
// 获取当前用户ID
Integer yonghuId = Integer.valueOf(String.valueOf(request.getSession().getAttribute("userId")));
// 创建日期格式化对象
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
try {
// 准备导入的数据列表
List<JiaolianEntity> jiaolianList = 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{
// 读取Excel文件
List<List<String>> dataList = PoiUtil.poiImport(file.getPath());
dataList.remove(0);// 删除标题行
for(List<String> data:dataList){
// 创建教练实体
JiaolianEntity jiaolianEntity = new JiaolianEntity();
// 设置各字段值示例代码实际应根据Excel列设置
// jiaolianEntity.setUsername(data.get(0)); // 账户
// jiaolianEntity.setPassword("123456"); // 密码
// jiaolianEntity.setJiaolianName(data.get(0)); // 教练名称
// jiaolianEntity.setJiaolianPhone(data.get(0)); // 教练手机号
// jiaolianEntity.setJiaolianPhoto(""); // 照片
// jiaolianEntity.setSexTypes(Integer.valueOf(data.get(0))); // 性别
// jiaolianEntity.setJiaolianEmail(data.get(0)); // 邮箱
// jiaolianEntity.setJiaolianContent(""); // 内容
// jiaolianEntity.setDataDelete(1); // 删除状态
// jiaolianEntity.setInsertTime(date); // 插入时间
// jiaolianEntity.setCreateTime(date); // 创建时间
jiaolianList.add(jiaolianEntity);
// 收集需要检查重复的字段
// 账户字段检查
if(seachFields.containsKey("username")){
List<String> username = seachFields.get("username");
username.add(data.get(0));
}else{
List<String> username = new ArrayList<>();
username.add(data.get(0));
seachFields.put("username",username);
}
// 手机号字段检查
if(seachFields.containsKey("jiaolianPhone")){
List<String> jiaolianPhone = seachFields.get("jiaolianPhone");
jiaolianPhone.add(data.get(0));
}else{
List<String> jiaolianPhone = new ArrayList<>();
jiaolianPhone.add(data.get(0));
seachFields.put("jiaolianPhone",jiaolianPhone);
}
}
// 检查账户是否重复
List<JiaolianEntity> jiaolianEntities_username = jiaolianService.selectList(new EntityWrapper<JiaolianEntity>().in("username", seachFields.get("username")).eq("data_delete", 1));
if(jiaolianEntities_username.size() >0 ){
ArrayList<String> repeatFields = new ArrayList<>();
for(JiaolianEntity s:jiaolianEntities_username){
repeatFields.add(s.getUsername());
}
return R.error(511,"数据库的该表中的 [账户] 字段已经存在 存在数据为:"+repeatFields.toString());
}
// 检查手机号是否重复
List<JiaolianEntity> jiaolianEntities_jiaolianPhone = jiaolianService.selectList(new EntityWrapper<JiaolianEntity>().in("jiaolian_phone", seachFields.get("jiaolianPhone")).eq("data_delete", 1));
if(jiaolianEntities_jiaolianPhone.size() >0 ){
ArrayList<String> repeatFields = new ArrayList<>();
for(JiaolianEntity s:jiaolianEntities_jiaolianPhone){
repeatFields.add(s.getJiaolianPhone());
}
return R.error(511,"数据库的该表中的 [教练手机号] 字段已经存在 存在数据为:"+repeatFields.toString());
}
// 批量插入数据
jiaolianService.insertBatch(jiaolianList);
return R.ok();
}
}
}
}catch (Exception e){
e.printStackTrace();
return R.error(511,"批量插入数据异常,请联系管理员");
}
}
/**
* 教练登录
*/
@IgnoreAuth // 忽略认证
@RequestMapping(value = "/login")
public R login(String username, String password, String captcha, HttpServletRequest request) {
// 根据用户名查询教练
JiaolianEntity jiaolian = jiaolianService.selectOne(new EntityWrapper<JiaolianEntity>().eq("username", username));
// 验证账号密码
if(jiaolian==null || !jiaolian.getPassword().equals(password))
return R.error("账号或密码不正确");
// 检查账号状态
else if(jiaolian.getDataDelete() != 1)
return R.error("账户已被删除");
// 生成令牌
String token = tokenService.generateToken(jiaolian.getId(),username, "jiaolian", "教练");
// 构建返回结果
R r = R.ok();
r.put("token", token);
r.put("role","教练");
r.put("username",jiaolian.getJiaolianName());
r.put("tableName","jiaolian");
r.put("userId",jiaolian.getId());
return r;
}
/**
* 教练注册
*/
@IgnoreAuth // 忽略认证
@PostMapping(value = "/register")
public R register(@RequestBody JiaolianEntity jiaolian, HttpServletRequest request) {
// 构建查询条件,检查用户名和手机号是否已存在
Wrapper<JiaolianEntity> queryWrapper = new EntityWrapper<JiaolianEntity>()
.eq("username", jiaolian.getUsername())
.or()
.eq("jiaolian_phone", jiaolian.getJiaolianPhone())
.andNew()
.eq("data_delete", 1)
;
// 执行查询
JiaolianEntity jiaolianEntity = jiaolianService.selectOne(queryWrapper);
if(jiaolianEntity != null)
return R.error("账户或者教练手机号已经被使用");
// 设置默认值
jiaolian.setDataDelete(1); // 数据状态
jiaolian.setInsertTime(new Date()); // 插入时间
jiaolian.setCreateTime(new Date()); // 创建时间
// 保存教练信息
jiaolianService.insert(jiaolian);
return R.ok();
}
/**
* 重置密码
*/
@GetMapping(value = "/resetPassword")
public R resetPassword(Integer id, HttpServletRequest request) {
// 根据ID查询教练
JiaolianEntity jiaolian = jiaolianService.selectById(id);
// 重置密码为默认值
jiaolian.setPassword("123456");
// 更新密码
jiaolianService.updateById(jiaolian);
return R.ok();
}
/**
* 修改密码
*/
@GetMapping(value = "/updatePassword")
public R updatePassword(String oldPassword, String newPassword, HttpServletRequest request) {
// 获取当前登录教练
JiaolianEntity jiaolian = jiaolianService.selectById((Integer)request.getSession().getAttribute("userId"));
// 验证新密码
if(newPassword == null){
return R.error("新密码不能为空") ;
}
// 验证旧密码
if(!oldPassword.equals(jiaolian.getPassword())){
return R.error("原密码输入错误");
}
// 检查新旧密码是否相同
if(newPassword.equals(jiaolian.getPassword())){
return R.error("新密码不能和原密码一致") ;
}
// 更新密码
jiaolian.setPassword(newPassword);
jiaolianService.updateById(jiaolian);
return R.ok();
}
/**
* 忘记密码(重置密码)
*/
@IgnoreAuth // 忽略认证
@RequestMapping(value = "/resetPass")
public R resetPass(String username, HttpServletRequest request) {
// 根据用户名查询教练
JiaolianEntity jiaolian = jiaolianService.selectOne(new EntityWrapper<JiaolianEntity>().eq("username", username));
if(jiaolian!=null){
// 重置密码为默认值
jiaolian.setPassword("123456");
// 更新密码
jiaolianService.updateById(jiaolian);
return R.ok();
}else {
return R.error("账号不存在");
}
}
/**
* 获取当前登录教练的会话信息
*/
@RequestMapping("/session")
public R getCurrJiaolian(HttpServletRequest request){
// 从会话中获取用户ID
Integer id = (Integer)request.getSession().getAttribute("userId");
// 查询教练信息
JiaolianEntity jiaolian = jiaolianService.selectById(id);
if(jiaolian !=null){
// 实体转视图
JiaolianView view = new JiaolianView();
// 复制属性
BeanUtils.copyProperties( jiaolian , view );
// 转换字典字段
dictionaryService.dictionaryConvert(view, request);
return R.ok().put("data", view);
}else {
return R.error(511,"查不到数据");
}
}
/**
* 退出登录
*/
@GetMapping(value = "logout")
public R logout(HttpServletRequest request) {
// 使会话失效
request.getSession().invalidate();
return R.ok("退出成功");
}
/**
* 前端列表查询
*/
@IgnoreAuth // 忽略认证
@RequestMapping("/list")
public R list(@RequestParam Map<String, Object> params, HttpServletRequest request){
logger.debug("前端列表查询方法:,,控制器:{},,参数:{}",this.getClass().getName(),JSONObject.toJSONString(params));
// 检查参数有效性
CommonUtil.checkMap(params);
// 查询分页数据
PageUtils page = jiaolianService.queryPage(params);
// 字典数据转换
List<JiaolianView> list =(List<JiaolianView>)page.getList();
for(JiaolianView 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("前端详情查询方法:,,控制器:{},,ID:{}",this.getClass().getName(),id);
// 根据ID查询教练
JiaolianEntity jiaolian = jiaolianService.selectById(id);
if(jiaolian !=null){
// 实体转视图
JiaolianView view = new JiaolianView();
// 复制属性
BeanUtils.copyProperties( jiaolian , view );
// 转换字典字段
dictionaryService.dictionaryConvert(view, request);
return R.ok().put("data", view);
}else {
return R.error(511,"查不到数据");
}
}
/**
* 前端保存教练信息
*/
@RequestMapping("/add")
public R add(@RequestBody JiaolianEntity jiaolian, HttpServletRequest request){
logger.debug("前端保存方法:,,控制器:{},,教练信息:{}",this.getClass().getName(),jiaolian.toString());
// 构建查询条件,检查用户名和手机号是否已存在
Wrapper<JiaolianEntity> queryWrapper = new EntityWrapper<JiaolianEntity>()
.eq("username", jiaolian.getUsername())
.or()
.eq("jiaolian_phone", jiaolian.getJiaolianPhone())
.andNew()
.eq("data_delete", 1)
;
logger.info("SQL语句:"+queryWrapper.getSqlSegment());
// 执行查询
JiaolianEntity jiaolianEntity = jiaolianService.selectOne(queryWrapper);
if(jiaolianEntity==null){
// 设置默认值
jiaolian.setDataDelete(1); // 数据状态
jiaolian.setInsertTime(new Date()); // 插入时间
jiaolian.setCreateTime(new Date()); // 创建时间
jiaolian.setPassword("123456"); // 默认密码
// 保存教练信息
jiaolianService.insert(jiaolian);
return R.ok();
}else {
return R.error(511,"账户或者教练手机号已经被使用");
}
}
}