|
@@ -0,0 +1,150 @@
|
|
|
|
|
+package vip.xiaonuo.exam.controller.wx.student;
|
|
|
|
|
+
|
|
|
|
|
+import vip.xiaonuo.exam.base.RestResponse;
|
|
|
|
|
+import vip.xiaonuo.exam.controller.wx.BaseWXApiController;
|
|
|
|
|
+import vip.xiaonuo.exam.domain.Message;
|
|
|
|
|
+import vip.xiaonuo.exam.domain.MessageUser;
|
|
|
|
|
+import vip.xiaonuo.exam.domain.User;
|
|
|
|
|
+import vip.xiaonuo.exam.domain.UserEventLog;
|
|
|
|
|
+import vip.xiaonuo.exam.domain.enums.RoleEnum;
|
|
|
|
|
+import vip.xiaonuo.exam.domain.enums.UserStatusEnum;
|
|
|
|
|
+import vip.xiaonuo.exam.event.UserEvent;
|
|
|
|
|
+import vip.xiaonuo.exam.service.AuthenticationService;
|
|
|
|
|
+import vip.xiaonuo.exam.service.MessageService;
|
|
|
|
|
+import vip.xiaonuo.exam.service.UserEventLogService;
|
|
|
|
|
+import vip.xiaonuo.exam.service.UserService;
|
|
|
|
|
+import vip.xiaonuo.exam.utility.DateTimeUtil;
|
|
|
|
|
+import vip.xiaonuo.exam.utility.PageInfoHelper;
|
|
|
|
|
+
|
|
|
|
|
+import com.github.pagehelper.PageInfo;
|
|
|
|
|
+import org.apache.commons.lang3.StringUtils;
|
|
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
|
|
+import org.springframework.context.ApplicationEventPublisher;
|
|
|
|
|
+import org.springframework.stereotype.Controller;
|
|
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
|
|
+import vip.xiaonuo.exam.viewmodel.student.user.*;
|
|
|
|
|
+
|
|
|
|
|
+import javax.validation.Valid;
|
|
|
|
|
+import java.util.Date;
|
|
|
|
|
+import java.util.List;
|
|
|
|
|
+import java.util.UUID;
|
|
|
|
|
+import java.util.stream.Collectors;
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+@Controller("WXStudentUserController")
|
|
|
|
|
+@RequestMapping(value = "/api/wx/student/user")
|
|
|
|
|
+@ResponseBody
|
|
|
|
|
+public class UserController extends BaseWXApiController {
|
|
|
|
|
+
|
|
|
|
|
+ private final UserService userService;
|
|
|
|
|
+ private final UserEventLogService userEventLogService;
|
|
|
|
|
+ private final MessageService messageService;
|
|
|
|
|
+ private final AuthenticationService authenticationService;
|
|
|
|
|
+ private final ApplicationEventPublisher eventPublisher;
|
|
|
|
|
+
|
|
|
|
|
+ @Autowired
|
|
|
|
|
+ public UserController(UserService userService, UserEventLogService userEventLogService, MessageService messageService, AuthenticationService authenticationService, ApplicationEventPublisher eventPublisher) {
|
|
|
|
|
+ this.userService = userService;
|
|
|
|
|
+ this.userEventLogService = userEventLogService;
|
|
|
|
|
+ this.messageService = messageService;
|
|
|
|
|
+ this.authenticationService = authenticationService;
|
|
|
|
|
+ this.eventPublisher = eventPublisher;
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @RequestMapping(value = "/current", method = RequestMethod.POST)
|
|
|
|
|
+ public RestResponse<UserResponseVM> current() {
|
|
|
|
|
+ User user = getCurrentUser();
|
|
|
|
|
+ UserResponseVM userVm = UserResponseVM.from(user);
|
|
|
|
|
+ userVm.setBirthDay(DateTimeUtil.dateShortFormat(user.getBirthDay()));
|
|
|
|
|
+ return RestResponse.ok(userVm);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @RequestMapping(value = "/register", method = RequestMethod.POST)
|
|
|
|
|
+ public RestResponse register(@Valid UserRegisterVM model) {
|
|
|
|
|
+ User existUser = userService.getUserByUserName(model.getUserName());
|
|
|
|
|
+ if (null != existUser) {
|
|
|
|
|
+ return new RestResponse<>(2, "用户已存在");
|
|
|
|
|
+ }
|
|
|
|
|
+ User user = modelMapper.map(model, User.class);
|
|
|
|
|
+ String encodePwd = authenticationService.pwdEncode(model.getPassword());
|
|
|
|
|
+ user.setUserUuid(UUID.randomUUID().toString());
|
|
|
|
|
+ user.setPassword(encodePwd);
|
|
|
|
|
+ user.setRole(RoleEnum.STUDENT.getCode());
|
|
|
|
|
+ user.setStatus(UserStatusEnum.Enable.getCode());
|
|
|
|
|
+ user.setLastActiveTime(new Date());
|
|
|
|
|
+ user.setCreateTime(new Date());
|
|
|
|
|
+ user.setDeleted(false);
|
|
|
|
|
+ userService.insertByFilter(user);
|
|
|
|
|
+ UserEventLog userEventLog = new UserEventLog(user.getId(), user.getUserName(), user.getRealName(), new Date());
|
|
|
|
|
+ userEventLog.setContent("欢迎 " + user.getUserName() + " 注册来到学之思开源考试系统");
|
|
|
|
|
+ eventPublisher.publishEvent(new UserEvent(userEventLog));
|
|
|
|
|
+ return RestResponse.ok();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @RequestMapping(value = "/update", method = RequestMethod.POST)
|
|
|
|
|
+ public RestResponse<UserResponseVM> update(@Valid UserUpdateVM model) {
|
|
|
|
|
+ if (StringUtils.isBlank(model.getBirthDay())) {
|
|
|
|
|
+ model.setBirthDay(null);
|
|
|
|
|
+ }
|
|
|
|
|
+ User user = userService.selectById(getCurrentUser().getId());
|
|
|
|
|
+ modelMapper.map(model, user);
|
|
|
|
|
+ user.setModifyTime(new Date());
|
|
|
|
|
+ userService.updateByIdFilter(user);
|
|
|
|
|
+ UserEventLog userEventLog = new UserEventLog(user.getId(), user.getUserName(), user.getRealName(), new Date());
|
|
|
|
|
+ userEventLog.setContent(user.getUserName() + " 更新了个人资料");
|
|
|
|
|
+ eventPublisher.publishEvent(new UserEvent(userEventLog));
|
|
|
|
|
+ UserResponseVM userVm = UserResponseVM.from(user);
|
|
|
|
|
+ return RestResponse.ok(userVm);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @RequestMapping(value = "/log", method = RequestMethod.POST)
|
|
|
|
|
+ public RestResponse<List<UserEventLogVM>> log() {
|
|
|
|
|
+ User user = getCurrentUser();
|
|
|
|
|
+ List<UserEventLog> userEventLogs = userEventLogService.getUserEventLogByUserId(user.getId());
|
|
|
|
|
+ List<UserEventLogVM> userEventLogVMS = userEventLogs.stream().map(d -> {
|
|
|
|
|
+ UserEventLogVM vm = modelMapper.map(d, UserEventLogVM.class);
|
|
|
|
|
+ vm.setCreateTime(DateTimeUtil.dateFormat(d.getCreateTime()));
|
|
|
|
|
+ return vm;
|
|
|
|
|
+ }).collect(Collectors.toList());
|
|
|
|
|
+ return RestResponse.ok(userEventLogVMS);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @RequestMapping(value = "/message/page", method = RequestMethod.POST)
|
|
|
|
|
+ public RestResponse<PageInfo<MessageResponseVM>> messagePageList(MessageRequestVM messageRequestVM) {
|
|
|
|
|
+ messageRequestVM.setReceiveUserId(getCurrentUser().getId());
|
|
|
|
|
+ PageInfo<MessageUser> messageUserPageInfo = messageService.studentPage(messageRequestVM);
|
|
|
|
|
+ List<Integer> ids = messageUserPageInfo.getList().stream().map(d -> d.getMessageId()).collect(Collectors.toList());
|
|
|
|
|
+ List<Message> messages = ids.size() != 0 ? messageService.selectMessageByIds(ids) : null;
|
|
|
|
|
+ PageInfo<MessageResponseVM> page = PageInfoHelper.copyMap(messageUserPageInfo, e -> {
|
|
|
|
|
+ MessageResponseVM vm = modelMapper.map(e, MessageResponseVM.class);
|
|
|
|
|
+ messages.stream().filter(d -> e.getMessageId().equals(d.getId())).findFirst().ifPresent(message -> {
|
|
|
|
|
+ vm.setTitle(message.getTitle());
|
|
|
|
|
+ vm.setContent(message.getContent());
|
|
|
|
|
+ vm.setSendUserName(message.getSendUserName());
|
|
|
|
|
+ });
|
|
|
|
|
+ vm.setCreateTime(DateTimeUtil.dateFormat(e.getCreateTime()));
|
|
|
|
|
+ return vm;
|
|
|
|
|
+ });
|
|
|
|
|
+ return RestResponse.ok(page);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @RequestMapping(value = "/message/detail/{id}", method = RequestMethod.POST)
|
|
|
|
|
+ public RestResponse messageDetail(@PathVariable Integer id) {
|
|
|
|
|
+ Message message = messageService.messageDetail(id);
|
|
|
|
|
+ return RestResponse.ok(message);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+
|
|
|
|
|
+ @RequestMapping(value = "/message/unreadCount", method = RequestMethod.POST)
|
|
|
|
|
+ public RestResponse unReadCount() {
|
|
|
|
|
+ Integer count = messageService.unReadCount(getCurrentUser().getId());
|
|
|
|
|
+ return RestResponse.ok(count);
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ @RequestMapping(value = "/message/read/{id}", method = RequestMethod.POST)
|
|
|
|
|
+ public RestResponse read(@PathVariable Integer id) {
|
|
|
|
|
+ messageService.read(id);
|
|
|
|
|
+ return RestResponse.ok();
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+}
|