package com.ycl.jxkg.controller.admin; import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper; import com.github.pagehelper.PageInfo; import com.ycl.jxkg.base.BaseApiController; import com.ycl.jxkg.base.Result; import com.ycl.jxkg.base.SystemCode; import com.ycl.jxkg.context.WebContext; import com.ycl.jxkg.domain.entity.Classes; import com.ycl.jxkg.domain.entity.SysConfig; import com.ycl.jxkg.domain.entity.User; import com.ycl.jxkg.domain.entity.UserEventLog; import com.ycl.jxkg.domain.other.KeyValue; import com.ycl.jxkg.domain.vo.StudentVO; import com.ycl.jxkg.domain.vo.admin.user.*; import com.ycl.jxkg.enums.RoleEnum; import com.ycl.jxkg.enums.UserStatusEnum; import com.ycl.jxkg.service.*; import com.ycl.jxkg.utils.DateTimeUtil; import com.ycl.jxkg.utils.PageInfoHelper; import lombok.RequiredArgsConstructor; import org.apache.commons.lang3.StringUtils; import org.springframework.beans.BeanUtils; import org.springframework.web.bind.annotation.*; import org.springframework.web.multipart.MultipartFile; import javax.servlet.http.HttpServletResponse; import javax.validation.Valid; import java.util.Date; import java.util.List; import java.util.UUID; import java.util.stream.Collectors; @RequiredArgsConstructor @RestController("AdminUserController") @RequestMapping(value = "/api/admin/user") public class UserController extends BaseApiController { private final UserService userService; private final UserEventLogService userEventLogService; private final AuthenticationService authenticationService; private final WebContext webContext; private final ClassesUserService classesUserService; private final ClassesService classesService; private final Integer teacher = 2; private final Integer student = 1; @RequestMapping(value = "/page/list", method = RequestMethod.POST) public Result> pageList(@RequestBody UserPageRequestVO model) { if(!student.equals(model.getRole())) { User currentUser = webContext.getCurrentUser(); if (teacher.equals(currentUser.getRole())) { return Result.fail(SystemCode.InnerError.getCode(), "您没有访问权限"); } } PageInfo pageInfo = userService.userPage(model); PageInfo page = PageInfoHelper.copyMap(pageInfo, d -> UserResponseVO.from(d)); // 找到每个用户对应的班级 List list = page.getList(); list.stream().peek(item -> { List classesIds = classesUserService.getByUserId(item.getId()); List classNames = classesIds.stream().map(classesId -> { Classes classes = classesService.getById(classesId); return classes.getClassName(); }).collect(Collectors.toList()); item.setClassName(classNames); }).collect(Collectors.toList()); page.setList(list); return Result.ok(page); } @RequestMapping(value = "/student/list", method = RequestMethod.GET) public Result studentList() { List studentList = new LambdaQueryChainWrapper<>(userService.getBaseMapper()) .eq(User::getRole, RoleEnum.STUDENT.getCode()) .orderByDesc(User::getCreateTime) .list(); List studentVOS = studentList.stream().map(item -> { StudentVO student = new StudentVO(); BeanUtils.copyProperties(item, student); return student; }).collect(Collectors.toList()); return Result.ok().data(studentVOS); } @RequestMapping(value = "/teacher/list", method = RequestMethod.GET) public Result teacherList() { List teacherList = new LambdaQueryChainWrapper<>(userService.getBaseMapper()) .eq(User::getRole, RoleEnum.TEACHER.getCode()) .orderByDesc(User::getCreateTime) .list(); List teachers = teacherList.stream().map(item -> { StudentVO student = new StudentVO(); BeanUtils.copyProperties(item, student); return student; }).collect(Collectors.toList()); return Result.ok().data(teachers); } @RequestMapping(value = "/classes/students", method = RequestMethod.GET) public Result classesStudent(@RequestParam Integer classesId) { List studentList = userService.classesStudent(classesId); return Result.ok().data(studentList); } @RequestMapping(value = "/event/page/list", method = RequestMethod.POST) public Result> eventPageList(@RequestBody UserEventPageRequestVO model) { PageInfo pageInfo = userEventLogService.page(model); PageInfo page = PageInfoHelper.copyMap(pageInfo, d -> { UserEventLogVO vo = new UserEventLogVO(); BeanUtils.copyProperties(d, vo); vo.setCreateTime(DateTimeUtil.dateFormat(d.getCreateTime())); return vo; }); return Result.ok(page); } @RequestMapping(value = "/select/{id}", method = RequestMethod.POST) public Result select(@PathVariable Integer id) { User user = userService.getUserById(id); UserResponseVO userVm = UserResponseVO.from(user); return Result.ok(userVm); } @RequestMapping(value = "/current", method = RequestMethod.POST) public Result current() { User user = getCurrentUser(); UserResponseVO userVm = UserResponseVO.from(user); return Result.ok(userVm); } @RequestMapping(value = "/edit", method = RequestMethod.POST) public Result edit(@RequestBody @Valid UserCreateVO model) { if (model.getId() == null) { //create User existUser = userService.getUserByUserName(model.getUserName()); if (null != existUser) { return new Result<>(2, "用户已存在"); } if (StringUtils.isBlank(model.getPassword())) { return new Result<>(3, "密码不能为空"); } } User user = new User(); BeanUtils.copyProperties(model, user); user.setAge(model.getAge()); if (model.getId() == null) { String encodePwd = authenticationService.pwdEncode(model.getPassword()); user.setPassword(encodePwd); user.setUserUuid(UUID.randomUUID().toString()); user.setCreateTime(new Date()); user.setLastActiveTime(new Date()); userService.insertUser(user); } else { if (!StringUtils.isBlank(model.getPassword())) { String encodePwd = authenticationService.pwdEncode(model.getPassword()); user.setPassword(encodePwd); } user.setModifyTime(new Date()); userService.updateById(user); } return Result.ok(user); } @RequestMapping(value = "/update", method = RequestMethod.POST) public Result update(@RequestBody @Valid UserUpdateVO model) { User user = userService.getById(getCurrentUser().getId()); BeanUtils.copyProperties(model, user); user.setModifyTime(new Date()); userService.updateById(user); return Result.ok(); } @RequestMapping(value = "/changeStatus/{id}", method = RequestMethod.POST) public Result changeStatus(@PathVariable Integer id) { User user = userService.getUserById(id); UserStatusEnum userStatusEnum = UserStatusEnum.fromCode(user.getStatus()); Integer newStatus = userStatusEnum == UserStatusEnum.Enable ? UserStatusEnum.Disable.getCode() : UserStatusEnum.Enable.getCode(); user.setStatus(newStatus); user.setModifyTime(new Date()); userService.updateById(user); return Result.ok(newStatus); } @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST) public Result delete(@PathVariable Integer id) { User user = userService.getUserById(id); userService.removeById(user.getId()); return Result.ok(); } @RequestMapping(value = "/selectByUserName", method = RequestMethod.POST) public Result> selectByUserName(@RequestBody String userName) { List keyValues = userService.selectByUserName(userName); return Result.ok(keyValues); } @GetMapping("importTemplate") public void importTemplate(HttpServletResponse response) { userService.importTemplate(response); } @PostMapping("import") public Result importExcel(MultipartFile file) { return Result.ok(userService.importExcel(file)); } @PostMapping("/reset/password/{id}") public Result resetPassword(@PathVariable Integer id) { User user = userService.getUserById(id); String encodePwd = authenticationService.pwdEncode(new SysConfig().getPasswordDefault()); user.setPassword(encodePwd); userService.updateById(user); return Result.ok(); } @PostMapping("/edit/password") public Result editPassword(@RequestBody UserEditPasswordVO model) { User user = userService.getUserById(model.getId()); String newPassword = authenticationService.pwdEncode(model.getNewPassword()); user.setPassword(newPassword); userService.updateById(user); return Result.ok(); } }