package com.project.controller;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mysql.cj.util.StringUtils;
import com.project.entity.Company;
import com.project.entity.Teacher;
import com.project.entity.User;
import com.project.entity.Work;
import com.project.service.CompanyService;
import com.project.service.StudentService;
import com.project.service.TeacherService;
import com.project.service.UserService;
import com.project.service.WorkService;
import com.project.uitls.Constant;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Random;
import static com.project.enums.UserRole.*;
import static com.project.uitls.Constant.CURRENT_USER;
@RestController
@RequestMapping("/user")
public class UserController {
@Autowired
private UserService userService;
@Autowired
private CompanyService companyService;
@Autowired
private TeacherService teacherService;
@Autowired
private StudentService studentService;
@Autowired
private WorkService workService;
@Autowired
private HttpSession session;
@Value("${upload}")
private String upload;
@ApiOperation("获取验证码")
@GetMapping("/getVerificationCode")
public Object getVerificationCode() {
String[] codes = {"a", "b", "c", "d", "e", "f", "g", "h", "i",
"j","k", "m", "n", "p", "q", "r",
"s", "t", "u", "v", "w", "x", "y", "z"};
List<String> randomCodes = new ArrayList<>();
StringBuilder codeStr = new StringBuilder();
for (int i = 0; i < 4; i++) {
String code = codes[Math.abs(new Random().nextInt() % codes.length)];
randomCodes.add(code);
codeStr.append(code);
}
session.setAttribute(Constant.CODE, codeStr.toString());
return R.ok(randomCodes);
}
@GetMapping("/getById")
@ApiOperation("根据id获取用户")
@ApiImplicitParam(value = "用户id", required = true)
public Object getById(Integer id) {
User user = userService.getById(id);
return user == null ? R.failed("该用户不存在") : R.ok(user);
}
@GetMapping("/deleteById")
@ApiOperation("根据id删除用户")
public Object deleteById(@RequestParam Integer id) {
boolean b = userService.removeById(id);
return b ? R.ok(null) : R.failed("删除失败");
}
@RequestMapping("/save")
public Object save(User user) {
if (user.getId() == null) {
userService.save(user);
}else {
userService.updateById(user);
}
return R.ok(null);
}
@PostMapping("/updateById")
@ApiOperation("根据id更用户")
public Object updateById(@RequestBody User user) {
if (!StringUtils.isNullOrEmpty(user.getUserName())) {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.lambda().eq(User::getUserName, user.getUserName());
List<User> users = userService.list(queryWrapper);
for (User u : users) {
if (!u.getId().equals(user.getId()))
return R.failed("该用户已存在");
}
}
boolean b = userService.updateById(user);
return b ? R.ok(null) : R.failed("更新失败");
}
@GetMapping("/addCompany")
public Object addCompany(@RequestParam String userName,
@RequestParam String password,
@RequestParam String phone,
@RequestParam String role,
@RequestParam String name) {
User user = registerUser(userName, password, phone, COMPANY);
Company company = new Company();
company.setUid(user.getId());
company.setContactsPhone(phone);
company.setName(name);
return companyService.save(company) ? R.ok(null) : R.failed("添加失败");
}
@PostMapping("/addTeacher")
public Object addTeacher(@RequestBody Teacher teacher) {
User user = registerUser(teacher.getName(), PASSWORD, teacher.getPhone(), STUDENT);
teacher.setUid(user.getId());
return teacherService.save(teacher) ? R.ok(null) : R.failed("添加失败");
}
@PostMapping("/addWork")
public Object addWork(@RequestBody Work work) {
User user = registerUser(work.getLeaderNumber(), PASSWORD, work.getPhone(), STUDENT);
work.setUid(user.getId());
return workService.save(work) ? R.ok(null) : R.failed("添加失败");
}
@GetMapping("/getUserPage")
@ApiOperation("获取用户分页")
public Object getUserPage(@RequestParam Integer pageNum,
@RequestParam Integer pageSize,
User user, Date beginTime, Date endTime) {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.lambda()
.like(!StringUtils.isNullOrEmpty(user.getUserName()), User::getUserName, user.getUserName())
.like(user.getStatus() != null, User::getStatus, user.getStatus())
.like(!StringUtils.isNullOrEmpty(user.getEmail()), User::getEmail, user.getEmail())
.like(!StringUtils.isNullOrEmpty(user.getPhone()), User::getPhone, user.getPhone());
IPage<User> page = userService.page(new Page<>(pageNum, pageSize), queryWrapper);
return R.ok(page);
}
@RequestMapping("/login")
@ApiOperation("登录")
public Object login(User user) {
QueryWrapper<User> queryWrapper = new QueryWrapper<>();
queryWrapper.lambda().eq(User::getUserName, user.getUserName()).eq(User::getPassword, user.getPassword());
List<User> users = userService.list(queryWrapper);
if (users.isEmpty())
return R.failed("用户名或密码错误");
User currentUser = users.get(0);
userService.updateById(currentUser);
session.setAttribute(CURRENT_USER, currentUser);
return R.ok(currentUser);
}
@GetMapping("/logout")
@ApiOperation("登出")
public Object logout() {
session.removeAttribute(CURRENT_USER);
return R.ok(null);
}
@PostMapping("/resetPassword")
@ApiOperation("重置密码, 默认密码000000")
public Object resetPassword(@RequestParam Integer userId, String password) {
if (StringUtils.isNullOrEmpty(password))
password = "000000";
User user = new User();
user.setId(userId);
user.setPassword(password);
boolean b = userService.updateById(user);
return b ? R.ok(null) : R.failed("重置失败");
}
@PostMapping("/updatePassword")
@ApiOperation("修改密码")
public Object updatePassword(@RequestParam Integer userId,
@RequestParam String oldPassword,
@RequestParam Stri