From 3ec909b27b3eba956aa9d00cc7a94c179bd04bbf Mon Sep 17 00:00:00 2001
From: 龚焕茏 <2842157468@qq.com>
Date: 星期三, 03 七月 2024 18:29:31 +0800
Subject: [PATCH] feat:新增随机时间题目配置

---
 src/main/java/com/mindskip/xzs/controller/admin/UserController.java |  115 +++++++++++++++++++++++++++++++++++++++++++++++++--------
 1 files changed, 99 insertions(+), 16 deletions(-)

diff --git a/src/main/java/com/mindskip/xzs/controller/admin/UserController.java b/src/main/java/com/mindskip/xzs/controller/admin/UserController.java
index 2c151d7..8478851 100644
--- a/src/main/java/com/mindskip/xzs/controller/admin/UserController.java
+++ b/src/main/java/com/mindskip/xzs/controller/admin/UserController.java
@@ -1,27 +1,28 @@
 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.other.KeyValue;
-import com.mindskip.xzs.domain.User;
-import com.mindskip.xzs.domain.UserEventLog;
+import com.mindskip.xzs.domain.*;
 import com.mindskip.xzs.domain.enums.UserStatusEnum;
-import com.mindskip.xzs.service.AuthenticationService;
-import com.mindskip.xzs.service.UserEventLogService;
-import com.mindskip.xzs.service.UserService;
+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.viewmodel.admin.user.*;
 import com.mindskip.xzs.utility.PageInfoHelper;
-import com.github.pagehelper.PageInfo;
-
+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.Date;
-import java.util.List;
-import java.util.UUID;
+import java.util.*;
+import java.util.stream.Collectors;
 
 
 @RestController("AdminUserController")
@@ -31,19 +32,39 @@
     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) {
+    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) {
-        PageInfo<User> pageInfo = userService.userPage(model);
-        PageInfo<UserResponseVM> page = PageInfoHelper.copyMap(pageInfo, d -> UserResponseVM.from(d));
+        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);
     }
 
@@ -63,6 +84,14 @@
     public RestResponse<UserResponseVM> select(@PathVariable Integer id) {
         User user = userService.getUserById(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);
     }
 
@@ -107,6 +136,28 @@
             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());
+        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);
+        }
+        if (ObjectUtils.isNotEmpty(model.getTagIds())) {
+            tagService.removeUserTagByUserId(user.getId());
+            tagService.saveBatchUserTag(model.getTagIds().stream().map(
+                    tagId -> new UserTag() {{
+                        setUserId(user.getId());
+                        setTagId(tagId);
+                    }}
+            ).collect(Collectors.toList()));
+        }
         return RestResponse.ok(user);
     }
 
@@ -123,7 +174,7 @@
 
     @RequestMapping(value = "/changeStatus/{id}", method = RequestMethod.POST)
     public RestResponse<Integer> changeStatus(@PathVariable Integer id) {
-        User user = userService.getUserById(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);
@@ -148,4 +199,36 @@
         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();
+    }
+
 }

--
Gitblit v1.8.0