龚焕茏
2024-05-07 49429bad1036c81c056faeadfa009c53ba777fad
src/main/java/com/mindskip/xzs/controller/admin/UserController.java
@@ -2,15 +2,12 @@
import com.mindskip.xzs.base.BaseApiController;
import com.mindskip.xzs.base.RestResponse;
import com.mindskip.xzs.domain.*;
import com.mindskip.xzs.domain.other.KeyValue;
import com.mindskip.xzs.domain.User;
import com.mindskip.xzs.domain.UserEventLog;
import com.mindskip.xzs.domain.enums.UserStatusEnum;
import com.mindskip.xzs.domain.vo.UserVO;
import com.mindskip.xzs.service.AuthenticationService;
import com.mindskip.xzs.service.DepartmentService;
import com.mindskip.xzs.service.UserEventLogService;
import com.mindskip.xzs.service.UserService;
import com.mindskip.xzs.repository.UserDepartmentMapper;
import com.mindskip.xzs.service.*;
import com.mindskip.xzs.utility.DateTimeUtil;
import com.mindskip.xzs.utility.convert.UserClassConvert;
import com.mindskip.xzs.utility.excel.ExcelUtils;
@@ -18,12 +15,14 @@
import com.mindskip.xzs.utility.PageInfoHelper;
import com.github.pagehelper.PageInfo;
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.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.UUID;
@@ -38,20 +37,39 @@
    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) {
    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));
        PageInfo<UserResponseVM> page = PageInfoHelper.copyMap(pageInfo, d ->
                UserResponseVM.from(d));
        page.setList(page.getList().stream().map(e->{
            List<UserDepartment> userDepartments = userDepartmentMapper.selectByUserId(e.getId());
            String deptIds = "";
            for (UserDepartment userDepartment : userDepartments) {
                Department byId = departmentService.getById(userDepartment.getDepartmentId());
                if(byId != null){
                    deptIds = deptIds + byId.getName();
                    break;
                }
            }
            e.setDeptNames(deptIds);
            e.setTagNames(tagService.selectTagNamesByUserId(e.getId()));
            return e;
        }).collect(Collectors.toList()));
        return RestResponse.ok(page);
    }
@@ -71,6 +89,13 @@
    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()));
        return RestResponse.ok(userVm);
    }
@@ -115,6 +140,23 @@
            user.setModifyTime(new Date());
            userService.updateByIdFilter(user);
        }
        userDepartmentMapper.removeByUserId(user.getId());
        for (String s : model.getDeptIds().split(",")) {
            UserDepartment userDepartment = new UserDepartment();
            userDepartment.setUserId(user.getId());
            userDepartment.setDepartmentId(Integer.parseInt(s));
            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()));
        }
        user.setDeptIds(model.getDeptIds());
        return RestResponse.ok(user);
    }
@@ -175,4 +217,23 @@
        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();
    }
    @RequestMapping(value = "/setStatus", method = RequestMethod.POST)
    public RestResponse<String> setStatus(@RequestBody UserVO user) {
        userService.setStatus(user);
        return RestResponse.ok("操作成功");
    }
}