xiangpei
2025-05-14 47cd9ecc0eff38ffe6b3b794b2bf197e958f4403
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
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
package com.mindskip.xzs.controller.admin;
 
import com.github.pagehelper.PageInfo;
import com.mindskip.xzs.base.BaseApiController;
import com.mindskip.xzs.base.RestResponse;
import com.mindskip.xzs.domain.*;
import com.mindskip.xzs.domain.enums.UserStatusEnum;
import com.mindskip.xzs.domain.other.KeyValue;
import com.mindskip.xzs.domain.vo.UserVO;
import com.mindskip.xzs.repository.UserDepartmentMapper;
import com.mindskip.xzs.service.*;
import com.mindskip.xzs.utility.DateTimeUtil;
import com.mindskip.xzs.utility.PageInfoHelper;
import com.mindskip.xzs.utility.convert.UserClassConvert;
import com.mindskip.xzs.utility.excel.ExcelUtils;
import com.mindskip.xzs.viewmodel.admin.user.*;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
 
import javax.validation.Valid;
import java.util.*;
import java.util.stream.Collectors;
 
 
@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 DepartmentService departmentService;
    private final UserDepartmentMapper userDepartmentMapper;
    private final TagService tagService;
 
    @Autowired
    public UserController(UserService userService, UserEventLogService userEventLogService, AuthenticationService authenticationService, DepartmentService departmentService, UserDepartmentMapper userDepartmentMapper, TagService tagService) {
        this.userService = userService;
        this.userEventLogService = userEventLogService;
        this.authenticationService = authenticationService;
        this.departmentService = departmentService;
        this.userDepartmentMapper = userDepartmentMapper;
        this.tagService = tagService;
    }
 
 
    @RequestMapping(value = "/page/list", method = RequestMethod.POST)
    public RestResponse<PageInfo<UserResponseVM>> pageList(@RequestBody UserPageRequestVM model) {
        model.setDepartmentId(ObjectUtils.isNotEmpty(model.getDepartmentId()) ? model.getDepartmentId() : getAdminDeptIds());
        PageInfo<UserResponseVM> page = userService.userPage(model);
        page.setList(page.getList().stream().map(e->{
            List<UserDepartment> userDepartments = userDepartmentMapper.selectByUserId(e.getId());
            List<Department> list = new ArrayList<>();
            for (UserDepartment userDepartment : userDepartments) {
                Department byId = departmentService.getById(userDepartment.getDepartmentId());
                if (ObjectUtils.isNotEmpty(byId)) {
                    list.add(byId);
                }
            }
            e.setDeptNames(StringUtils.join(list.stream().map(Department::getName).collect(Collectors.toList()), ","));
            e.setDeptIdList(list.stream().map(Department::getId).collect(Collectors.toList()));
            e.setTagNames(tagService.selectTagNamesByUserId(e.getId()));
            return e;
        }).collect(Collectors.toList()));
        return RestResponse.ok(page);
    }
 
 
    @RequestMapping(value = "/event/page/list", method = RequestMethod.POST)
    public RestResponse<PageInfo<UserEventLogVM>> eventPageList(@RequestBody UserEventPageRequestVM model) {
        PageInfo<UserEventLog> pageInfo = userEventLogService.page(model);
        PageInfo<UserEventLogVM> page = PageInfoHelper.copyMap(pageInfo, d -> {
            UserEventLogVM vm = modelMapper.map(d, UserEventLogVM.class);
            vm.setCreateTime(DateTimeUtil.dateFormat(d.getCreateTime()));
            return vm;
        });
        return RestResponse.ok(page);
    }
 
    @RequestMapping(value = "/select/{id}", method = RequestMethod.POST)
    public RestResponse<UserResponseVM> select(@PathVariable Integer id) {
        User user = userService.getUserInfoById(id);
        UserResponseVM userVm = UserResponseVM.from(user);
        List<UserDepartment> userDepartments = userDepartmentMapper.selectByUserId(user.getId());
        String deptIds = "";
        for (UserDepartment userDepartment : userDepartments) {
            deptIds = deptIds + userDepartment.getDepartmentId().toString() + ",";
        }
        userVm.setDeptIds(deptIds.equals("") ? "" : deptIds.substring(0,deptIds.length()-1));
        userVm.setTagIds(tagService.selectTagIdsByUserId(user.getId()));
        userVm.setPassword(authenticationService.pwdDecode(user.getPassword()));
        return RestResponse.ok(userVm);
    }
 
    @RequestMapping(value = "/current", method = RequestMethod.POST)
    public RestResponse<UserResponseVM> current() {
        User user = getCurrentUser();
        UserResponseVM userVm = UserResponseVM.from(user);
        return RestResponse.ok(userVm);
    }
 
 
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public RestResponse<User> edit(@RequestBody @Valid UserCreateVM model) {
        if (model.getId() == null) {  //create
            User existUser = userService.getUserByUserName(model.getUserName());
            if (null != existUser) {
                return new RestResponse<>(2, "用户已存在");
            }
 
            if (StringUtils.isBlank(model.getPassword())) {
                return new RestResponse<>(3, "密码不能为空");
            }
        }
        if (StringUtils.isBlank(model.getBirthDay())) {
            model.setBirthDay(null);
        }
        User user = modelMapper.map(model, User.class);
 
        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());
            user.setDeleted(false);
            userService.insertByFilter(user);
        } else {
            if (!StringUtils.isBlank(model.getPassword())) {
                String encodePwd = authenticationService.pwdEncode(model.getPassword());
                user.setPassword(encodePwd);
            }
            user.setModifyTime(new Date());
            userService.updateByIdFilter(user);
        }
        Map<Integer, UserDepartment> oldDeptUser = userDepartmentMapper.selectByUserId(user.getId()).stream().collect(Collectors.toMap(UserDepartment::getDepartmentId, userDept -> userDept));
        userDepartmentMapper.removeByUserId(user.getId());
        if (ObjectUtils.isNotEmpty(model.getDeptIds())) {
            for (Integer s : model.getDeptIds()) {
                UserDepartment userDepartment = new UserDepartment();
                userDepartment.setUserId(user.getId());
                userDepartment.setDepartmentId(s);
                if (Objects.nonNull(oldDeptUser.get(s))) {
                    userDepartment.setDeptAdmin(oldDeptUser.get(s).getDeptAdmin());
                } else {
                    userDepartment.setDeptAdmin("0");
                }
                userDepartmentMapper.insert(userDepartment);
            }
        }
        tagService.removeUserTagByUserId(user.getId());
        if (ObjectUtils.isNotEmpty(model.getTagIds())) {
            tagService.saveBatchUserTag(model.getTagIds().stream().map(
                    tagId -> new UserTag() {{
                        setUserId(user.getId());
                        setTagId(tagId);
                    }}
            ).collect(Collectors.toList()));
        }
        return RestResponse.ok(user);
    }
 
 
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public RestResponse update(@RequestBody @Valid UserUpdateVM model) {
        User user = userService.selectById(getCurrentUser().getId());
        modelMapper.map(model, user);
        user.setModifyTime(new Date());
        userService.updateByIdFilter(user);
        return RestResponse.ok();
    }
 
 
    @RequestMapping(value = "/changeStatus/{id}", method = RequestMethod.POST)
    public RestResponse<Integer> changeStatus(@PathVariable Integer id) {
        User user = userService.selectById(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.updateByIdFilter(user);
        return RestResponse.ok(newStatus);
    }
 
 
    @RequestMapping(value = "/delete/{id}", method = RequestMethod.POST)
    public RestResponse delete(@PathVariable Integer id) {
        User user = userService.getUserInfoById(id);
        user.setDeleted(true);
        userService.updateByIdFilter(user);
        return RestResponse.ok();
    }
 
 
    @RequestMapping(value = "/selectByUserName", method = RequestMethod.POST)
    public RestResponse<List<KeyValue>> selectByUserName(@RequestBody String userName) {
        List<KeyValue> keyValues = userService.selectByUserName(userName);
        return RestResponse.ok(keyValues);
    }
 
    @PostMapping("/import")
    public RestResponse importUser(@RequestPart("file") MultipartFile file) throws Exception {
        List<UserVO> userVOS = ExcelUtils.readMultipartFile(file, UserVO.class)
                .stream().map(e -> {
                    e.setUserLevel(departmentService.getName(e.getDepartment()).getId());
                    String encodePwd = authenticationService.pwdEncode(e.getPassword());
                    e.setPassword(encodePwd);
                    e.setUserUuid(UUID.randomUUID().toString());
                    e.setCreateTime(new Date());
                    e.setLastActiveTime(new Date());
                    e.setDeleted(false);
                    e.setAge(null);
                    return e;
                }).collect(Collectors.toList());
        List<User> users = UserClassConvert.INSTANCE.UserVOListToUserList(userVOS);
        userService.insertUsers(users);
        return RestResponse.ok();
    }
 
    @RequestMapping(value = "/conversion", method = RequestMethod.GET)
    public RestResponse conversion() {
        List<User> users = userService.getUsers();
        for (User user : users) {
            UserDepartment userDepartment = new UserDepartment();
            userDepartment.setUserId(user.getId());
            userDepartment.setDepartmentId(user.getUserLevel());
            userDepartmentMapper.insert(userDepartment);
        }
 
        return RestResponse.ok();
    }
 
    @GetMapping("/getFailExamUser/{examPaperId}")
    public RestResponse<List<User>> getFailExamUser(@PathVariable Integer examPaperId) {
        return RestResponse.ok(userService.getFailExamUser(examPaperId));
    }
 
    @GetMapping("/getFailTemplateUser/{templateId}")
    public RestResponse<List<User>> getFailTemplateUser(@PathVariable Integer templateId) {
        return RestResponse.ok(userService.getFailTemplateUser(templateId));
    }
 
}