xiangpei
2024-05-31 c9d04bc519b73f7fc4841c34e2f15fca9db7aad2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
package com.ycl.jxkg.controller.student;
 
import com.ycl.jxkg.base.BaseApiController;
import com.ycl.jxkg.base.Result;
import com.ycl.jxkg.domain.Message;
import com.ycl.jxkg.domain.MessageUser;
import com.ycl.jxkg.domain.User;
import com.ycl.jxkg.domain.UserEventLog;
import com.ycl.jxkg.domain.enums.RoleEnum;
import com.ycl.jxkg.domain.enums.UserStatusEnum;
import com.ycl.jxkg.event.UserEvent;
import com.ycl.jxkg.service.AuthenticationService;
import com.ycl.jxkg.service.MessageService;
import com.ycl.jxkg.service.UserEventLogService;
import com.ycl.jxkg.service.UserService;
import com.ycl.jxkg.utils.DateTimeUtil;
import com.ycl.jxkg.utils.PageInfoHelper;
import com.ycl.jxkg.vo.student.user.*;
import com.github.pagehelper.PageInfo;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationEventPublisher;
import org.springframework.web.bind.annotation.*;
 
import javax.validation.Valid;
import java.util.Date;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;
 
@RequiredArgsConstructor
@RestController("StudentUserController")
@RequestMapping(value = "/api/student/user")
public class UserController extends BaseApiController {
 
    private final UserService userService;
    private final UserEventLogService userEventLogService;
    private final MessageService messageService;
    private final AuthenticationService authenticationService;
    private final ApplicationEventPublisher eventPublisher;
 
    @RequestMapping(value = "/current", method = RequestMethod.POST)
    public Result<UserResponseVO> current() {
        User user = getCurrentUser();
        UserResponseVO userVm = UserResponseVO.from(user);
        return Result.ok(userVm);
    }
 
 
    @RequestMapping(value = "/register", method = RequestMethod.POST)
    public Result register(@RequestBody @Valid UserRegisterVO model) {
        User existUser = userService.getUserByUserName(model.getUserName());
        if (null != existUser) {
            return new Result<>(2, "用户已存在");
        }
        User user = new User();
        BeanUtils.copyProperties(model, user);
        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.insertUser(user);
        UserEventLog userEventLog = new UserEventLog(user.getId(), user.getUserName(), user.getRealName(), new Date());
        userEventLog.setContent("欢迎 " + user.getUserName() + " 注册来到学之思开源考试系统");
        eventPublisher.publishEvent(new UserEvent(userEventLog));
        return Result.ok();
    }
 
 
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public Result update(@RequestBody @Valid UserUpdateVO model) {
        if (StringUtils.isBlank(model.getBirthDay())) {
            model.setBirthDay(null);
        }
        User user = userService.getById(getCurrentUser().getId());
        BeanUtils.copyProperties(model, user);
        user.setModifyTime(new Date());
        userService.updateUser(user);
        UserEventLog userEventLog = new UserEventLog(user.getId(), user.getUserName(), user.getRealName(), new Date());
        userEventLog.setContent(user.getUserName() + " 更新了个人资料");
        eventPublisher.publishEvent(new UserEvent(userEventLog));
        return Result.ok();
    }
 
    @RequestMapping(value = "/log", method = RequestMethod.POST)
    public Result<List<UserEventLogVO>> log() {
        User user = getCurrentUser();
        List<UserEventLog> userEventLogs = userEventLogService.getUserEventLogByUserId(user.getId());
        List<UserEventLogVO> userEventLogVOS = userEventLogs.stream().map(d -> {
            UserEventLogVO vo = new UserEventLogVO();
            BeanUtils.copyProperties(d, vo);
            vo.setCreateTime(DateTimeUtil.dateFormat(d.getCreateTime()));
            return vo;
        }).collect(Collectors.toList());
        return Result.ok(userEventLogVOS);
    }
 
    @RequestMapping(value = "/message/page", method = RequestMethod.POST)
    public Result<PageInfo<MessageResponseVO>> messagePageList(@RequestBody MessageRequestVO messageRequestVO) {
        messageRequestVO.setReceiveUserId(getCurrentUser().getId());
        PageInfo<MessageUser> messageUserPageInfo = messageService.studentPage(messageRequestVO);
        List<Integer> ids = messageUserPageInfo.getList().stream().map(d -> d.getMessageId()).collect(Collectors.toList());
        List<Message> messages = ids.size() != 0 ? messageService.selectMessageByIds(ids) : null;
        PageInfo<MessageResponseVO> page = PageInfoHelper.copyMap(messageUserPageInfo, e -> {
            MessageResponseVO vo = new MessageResponseVO();
            BeanUtils.copyProperties(e, vo);
            messages.stream().filter(d -> e.getMessageId().equals(d.getId())).findFirst().ifPresent(message -> {
                vo.setTitle(message.getTitle());
                vo.setContent(message.getContent());
                vo.setSendUserName(message.getSendUserName());
            });
            vo.setCreateTime(DateTimeUtil.dateFormat(e.getCreateTime()));
            return vo;
        });
        return Result.ok(page);
    }
 
    @RequestMapping(value = "/message/unreadCount", method = RequestMethod.POST)
    public Result unReadCount() {
        Integer count = messageService.unReadCount(getCurrentUser().getId());
        return Result.ok(count);
    }
 
    @RequestMapping(value = "/message/read/{id}", method = RequestMethod.POST)
    public Result read(@PathVariable Integer id) {
        messageService.read(id);
        return Result.ok();
    }
 
}