瀏覽代碼

update user apis

deason 9 月之前
父節點
當前提交
e7f52db9d0

+ 9 - 6
src/main/java/cn/com/qmth/scancentral/controller/admin/ExamController.java

@@ -13,6 +13,7 @@ import cn.com.qmth.scancentral.vo.ResultVo;
 import cn.com.qmth.scancentral.vo.examinfo.ExamEdit;
 import cn.com.qmth.scancentral.vo.examinfo.ExamOverview;
 import cn.com.qmth.scancentral.vo.examinfo.ExamQuery;
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
 import com.qmth.boot.api.annotation.Aac;
 import com.qmth.boot.api.constant.ApiConstant;
 import com.qmth.boot.core.collection.PageResult;
@@ -75,18 +76,20 @@ public class ExamController extends BaseController {
     }
 
     @ApiOperation(value = "启用、禁用考试")
-    @RequestMapping(value = "/enable/update", method = RequestMethod.POST)
-    public ResultVo enableUpdate(@RequestParam Long examId, @RequestParam Boolean enable) {
+    @RequestMapping(value = "/toggle", method = RequestMethod.POST)
+    public ResultVo updateEnable(@RequestParam Long examId, @RequestParam Boolean enable) {
         User user = getAccessUser();
         ExamEntity exam = examService.getById(examId);
         if (exam == null) {
             throw ParameterExceptions.EXAM_NOT_FOUND;
         }
 
-        exam.setEnable(enable);
-        exam.setUpdaterId(user.getId());
-        exam.setUpdateTime(System.currentTimeMillis());
-        examService.saveOrUpdate(exam);
+        LambdaUpdateWrapper<ExamEntity> updateWrapper = new LambdaUpdateWrapper<>();
+        updateWrapper.set(ExamEntity::getEnable, enable);
+        updateWrapper.set(ExamEntity::getUpdateTime, System.currentTimeMillis());
+        updateWrapper.set(ExamEntity::getUpdaterId, user.getId());
+        updateWrapper.eq(ExamEntity::getId, exam.getId());
+        examService.update(updateWrapper);
 
         return new ResultVo(System.currentTimeMillis());
     }

+ 62 - 10
src/main/java/cn/com/qmth/scancentral/controller/admin/UserController.java

@@ -2,21 +2,27 @@ package cn.com.qmth.scancentral.controller.admin;
 
 import cn.com.qmth.scancentral.bean.User;
 import cn.com.qmth.scancentral.controller.BaseController;
+import cn.com.qmth.scancentral.entity.UserEntity;
 import cn.com.qmth.scancentral.service.UserService;
-import cn.com.qmth.scancentral.vo.UserVo;
+import cn.com.qmth.scancentral.vo.ResultVo;
+import cn.com.qmth.scancentral.vo.user.UserEdit;
+import cn.com.qmth.scancentral.vo.user.UserQuery;
+import cn.com.qmth.scancentral.vo.user.UserVo;
+import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
 import com.qmth.boot.api.annotation.Aac;
 import com.qmth.boot.api.constant.ApiConstant;
+import com.qmth.boot.core.collection.PageResult;
+import com.qmth.boot.core.exception.ParameterException;
 import io.swagger.annotations.Api;
 import io.swagger.annotations.ApiOperation;
 import org.springframework.beans.factory.annotation.Autowired;
-import org.springframework.web.bind.annotation.PostMapping;
 import org.springframework.web.bind.annotation.RequestMapping;
+import org.springframework.web.bind.annotation.RequestMethod;
+import org.springframework.web.bind.annotation.RequestParam;
 import org.springframework.web.bind.annotation.RestController;
 
-import java.util.List;
-
 @RestController
-@Api(tags = "用户接口")
+@Api(tags = "用户相关接口")
 @RequestMapping(ApiConstant.DEFAULT_URI_PREFIX + "/admin/user")
 @Aac(strict = false, auth = true)
 public class UserController extends BaseController {
@@ -24,11 +30,57 @@ public class UserController extends BaseController {
     @Autowired
     private UserService userService;
 
-    @ApiOperation(value = "审核员列表")
-    @PostMapping("list")
-    public List<UserVo> list() {
+    @ApiOperation(value = "获取用户列表")
+    @RequestMapping(value = "/page", method = RequestMethod.POST)
+    public PageResult<UserVo> page(UserQuery query) {
         User user = getAccessUser();
-        return userService.findAdmin();
+        return userService.findUserPage(query, user);
+    }
+
+    @ApiOperation(value = "新建、修改用户")
+    @RequestMapping(value = "/save", method = RequestMethod.POST)
+    public ResultVo save(UserEdit user) {
+        user.setOperateUserId(getAccessUser().getId());
+        userService.saveUser(user);
+        return new ResultVo(System.currentTimeMillis());
+    }
+
+    @ApiOperation(value = "启用、禁用用户")
+    @RequestMapping(value = "/toggle", method = RequestMethod.POST)
+    public ResultVo updateEnable(@RequestParam Long userId, @RequestParam Boolean enable) {
+        User user = getAccessUser();
+        UserEntity userEntity = userService.getById(userId);
+        if (userEntity == null) {
+            throw new ParameterException("用户不存在");
+        }
+
+        LambdaUpdateWrapper<UserEntity> updateWrapper = new LambdaUpdateWrapper<>();
+        updateWrapper.set(UserEntity::getEnable, enable);
+        updateWrapper.set(UserEntity::getUpdateTime, System.currentTimeMillis());
+        updateWrapper.set(UserEntity::getUpdaterId, user.getId());
+        updateWrapper.eq(UserEntity::getId, userEntity.getId());
+        userService.update(updateWrapper);
+
+        return new ResultVo(System.currentTimeMillis());
+    }
+
+    @ApiOperation(value = "更新用户密码")
+    @RequestMapping(value = "/password/reset", method = RequestMethod.POST)
+    public ResultVo updatePassword(@RequestParam Long userId, @RequestParam String password) {
+        User user = getAccessUser();
+        UserEntity userEntity = userService.getById(userId);
+        if (userEntity == null) {
+            throw new ParameterException("用户不存在");
+        }
+
+        LambdaUpdateWrapper<UserEntity> updateWrapper = new LambdaUpdateWrapper<>();
+        updateWrapper.set(UserEntity::getPassword, password);
+        updateWrapper.set(UserEntity::getUpdateTime, System.currentTimeMillis());
+        updateWrapper.set(UserEntity::getUpdaterId, user.getId());
+        updateWrapper.eq(UserEntity::getId, userEntity.getId());
+        userService.update(updateWrapper);
+
+        return new ResultVo(System.currentTimeMillis());
     }
 
-}
+}

+ 7 - 0
src/main/java/cn/com/qmth/scancentral/dao/UserDao.java

@@ -1,8 +1,15 @@
 package cn.com.qmth.scancentral.dao;
 
 import cn.com.qmth.scancentral.entity.UserEntity;
+import cn.com.qmth.scancentral.vo.user.UserQuery;
+import cn.com.qmth.scancentral.vo.user.UserVo;
 import com.baomidou.mybatisplus.core.mapper.BaseMapper;
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
+import org.apache.ibatis.annotations.Param;
 
 public interface UserDao extends BaseMapper<UserEntity> {
 
+    IPage<UserVo> pageUser(Page<UserVo> page, @Param("query") UserQuery query);
+
 }

+ 12 - 5
src/main/java/cn/com/qmth/scancentral/service/UserService.java

@@ -1,16 +1,19 @@
 package cn.com.qmth.scancentral.service;
 
-import java.util.List;
-
-import cn.com.qmth.scancentral.vo.UserVo;
-import com.baomidou.mybatisplus.extension.service.IService;
-
 import cn.com.qmth.scancentral.bean.ImportUserDomain;
+import cn.com.qmth.scancentral.bean.User;
 import cn.com.qmth.scancentral.entity.UserEntity;
 import cn.com.qmth.scancentral.enums.Role;
 import cn.com.qmth.scancentral.vo.AuditorVo;
 import cn.com.qmth.scancentral.vo.CreateCountVo;
 import cn.com.qmth.scancentral.vo.UpdateCountVo;
+import cn.com.qmth.scancentral.vo.user.UserEdit;
+import cn.com.qmth.scancentral.vo.user.UserQuery;
+import cn.com.qmth.scancentral.vo.user.UserVo;
+import com.baomidou.mybatisplus.extension.service.IService;
+import com.qmth.boot.core.collection.PageResult;
+
+import java.util.List;
 
 public interface UserService extends IService<UserEntity> {
 
@@ -30,4 +33,8 @@ public interface UserService extends IService<UserEntity> {
 
     List<UserVo> findAdmin();
 
+    PageResult<UserVo> findUserPage(UserQuery query, User user);
+
+    Long saveUser(UserEdit user);
+
 }

+ 83 - 4
src/main/java/cn/com/qmth/scancentral/service/impl/UserServiceImpl.java

@@ -1,21 +1,31 @@
 package cn.com.qmth.scancentral.service.impl;
 
 import cn.com.qmth.scancentral.bean.ImportUserDomain;
+import cn.com.qmth.scancentral.bean.User;
 import cn.com.qmth.scancentral.dao.UserDao;
 import cn.com.qmth.scancentral.entity.UserEntity;
 import cn.com.qmth.scancentral.enums.Role;
 import cn.com.qmth.scancentral.service.SessionService;
 import cn.com.qmth.scancentral.service.UserService;
+import cn.com.qmth.scancentral.util.PageUtil;
 import cn.com.qmth.scancentral.vo.AuditorVo;
 import cn.com.qmth.scancentral.vo.CreateCountVo;
 import cn.com.qmth.scancentral.vo.UpdateCountVo;
-import cn.com.qmth.scancentral.vo.UserVo;
+import cn.com.qmth.scancentral.vo.user.UserEdit;
+import cn.com.qmth.scancentral.vo.user.UserQuery;
+import cn.com.qmth.scancentral.vo.user.UserVo;
 import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
 import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
+import com.baomidou.mybatisplus.core.metadata.IPage;
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
 import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
+import com.qmth.boot.core.collection.PageResult;
 import com.qmth.boot.core.exception.ParameterException;
+import org.apache.commons.lang3.StringUtils;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.springframework.beans.factory.annotation.Autowired;
 import org.springframework.stereotype.Service;
 import org.springframework.transaction.annotation.Transactional;
@@ -27,6 +37,8 @@ import java.util.List;
 @Service
 public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements UserService {
 
+    private static final Logger log = LoggerFactory.getLogger(UserServiceImpl.class);
+
     @Autowired
     private SessionService sessionService;
 
@@ -137,13 +149,80 @@ public class UserServiceImpl extends ServiceImpl<UserDao, UserEntity> implements
 
     @Override
     public List<UserVo> findAdmin() {
-        List<UserVo> list = new ArrayList<UserVo>();
+        List<UserVo> list = new ArrayList<>();
         List<UserEntity> users = this.findByRole(Role.AUDITOR, Role.SCHOOL_ADMIN, Role.SCAN_ADMIN);
         for (UserEntity user : users) {
-            UserVo vo = new UserVo(user);
-            vo.setOnline(sessionService.checkAuditorOnline(user.getLoginName()));
+            UserVo vo = new UserVo();
+            vo.setId(user.getId());
+            vo.setLoginName(user.getLoginName());
+            vo.setRole(user.getRole());
+            vo.setRoleName(user.getRole().getName());
+            vo.setEnable(user.getEnable());
             list.add(vo);
         }
         return list;
     }
+
+    @Override
+    public PageResult<UserVo> findUserPage(UserQuery query, User user) {
+        IPage<UserVo> iPage = baseMapper.pageUser(new Page<>(query.getPageNumber(), query.getPageSize()), query);
+        iPage.getRecords().forEach(vo -> {
+            vo.setRoleName(vo.getRole().getName());
+        });
+        return PageUtil.of(iPage);
+    }
+
+    @Override
+    public Long saveUser(UserEdit user) {
+        if (StringUtils.isBlank(user.getLoginName())) {
+            throw new ParameterException("账号不能为空");
+        }
+        if (StringUtils.length(user.getLoginName()) > 50) {
+            throw new ParameterException("账号50字以内");
+        }
+        if (StringUtils.isBlank(user.getPassword())) {
+            throw new ParameterException("密码不能为空");
+        }
+        if (StringUtils.length(user.getPassword()) > 50) {
+            throw new ParameterException("密码50字以内");
+        }
+        if (user.getRole() == null) {
+            throw new ParameterException("角色不能为空");
+        }
+
+        if (user.getId() != null) {
+            // 修改
+            UserEntity userEntity = this.getById(user.getId());
+            if (userEntity == null) {
+                throw new ParameterException("用户不存在");
+            }
+
+            LambdaUpdateWrapper<UserEntity> updateWrapper = new LambdaUpdateWrapper<>();
+            updateWrapper.set(UserEntity::getUpdateTime, System.currentTimeMillis());
+            updateWrapper.set(UserEntity::getUpdaterId, user.getOperateUserId());
+            updateWrapper.set(UserEntity::getPassword, user.getPassword());
+            updateWrapper.eq(UserEntity::getId, user.getId());
+            this.update(updateWrapper);
+
+            return user.getId();
+        }
+
+        // 新增
+        UserEntity userEntity = new UserEntity();
+        userEntity.setRole(user.getRole());
+        userEntity.setLoginName(user.getLoginName());
+        userEntity.setName(user.getLoginName());
+        userEntity.setPassword(user.getPassword());
+        userEntity.setEnable(true);
+        userEntity.setCreatorId(user.getOperateUserId());
+        userEntity.setUpdaterId(user.getOperateUserId());
+        userEntity.setCreateTime(System.currentTimeMillis());
+        userEntity.setUpdateTime(System.currentTimeMillis());
+
+        this.save(userEntity);
+        log.warn("新增用户成功! userId:{}", userEntity.getId());
+
+        return userEntity.getId();
+    }
+
 }

+ 0 - 67
src/main/java/cn/com/qmth/scancentral/vo/UserVo.java

@@ -1,67 +0,0 @@
-package cn.com.qmth.scancentral.vo;
-
-import cn.com.qmth.scancentral.entity.UserEntity;
-
-public class UserVo {
-
-    private Long id;
-
-    private String loginName;
-
-    private String password;
-
-    private String device;
-
-    private boolean online;
-
-    public UserVo() {
-    }
-
-    public UserVo(UserEntity user) {
-        this.password = user.getPassword();
-        this.id = user.getId();
-        this.device = user.getDevice();
-        this.loginName = user.getLoginName();
-    }
-
-    public Long getId() {
-        return id;
-    }
-
-    public void setId(Long id) {
-        this.id = id;
-    }
-
-    public String getLoginName() {
-        return loginName;
-    }
-
-    public void setLoginName(String loginName) {
-        this.loginName = loginName;
-    }
-
-    public String getPassword() {
-        return password;
-    }
-
-    public void setPassword(String password) {
-        this.password = password;
-    }
-
-    public String getDevice() {
-        return device;
-    }
-
-    public void setDevice(String device) {
-        this.device = device;
-    }
-
-    public boolean isOnline() {
-        return online;
-    }
-
-    public void setOnline(boolean online) {
-        this.online = online;
-    }
-
-}

+ 59 - 0
src/main/java/cn/com/qmth/scancentral/vo/user/UserEdit.java

@@ -0,0 +1,59 @@
+package cn.com.qmth.scancentral.vo.user;
+
+import cn.com.qmth.scancentral.enums.Role;
+import io.swagger.annotations.ApiModelProperty;
+
+public class UserEdit {
+
+    private Long id;
+
+    private Role role;
+
+    private String loginName;
+
+    private String password;
+
+    @ApiModelProperty(value = "操作用户ID", hidden = true)
+    private Long operateUserId;
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    public Role getRole() {
+        return role;
+    }
+
+    public void setRole(Role role) {
+        this.role = role;
+    }
+
+    public String getLoginName() {
+        return loginName;
+    }
+
+    public void setLoginName(String loginName) {
+        this.loginName = loginName;
+    }
+
+    public String getPassword() {
+        return password;
+    }
+
+    public void setPassword(String password) {
+        this.password = password;
+    }
+
+    public Long getOperateUserId() {
+        return operateUserId;
+    }
+
+    public void setOperateUserId(Long operateUserId) {
+        this.operateUserId = operateUserId;
+    }
+
+}

+ 31 - 0
src/main/java/cn/com/qmth/scancentral/vo/user/UserQuery.java

@@ -0,0 +1,31 @@
+package cn.com.qmth.scancentral.vo.user;
+
+import cn.com.qmth.scancentral.enums.Role;
+import cn.com.qmth.scancentral.util.PagerQuery;
+import io.swagger.annotations.ApiModelProperty;
+
+public class UserQuery extends PagerQuery {
+
+    @ApiModelProperty(value = "角色")
+    private Role role;
+
+    @ApiModelProperty(value = "是否启用", hidden = true)
+    private Boolean enable;
+
+    public Role getRole() {
+        return role;
+    }
+
+    public void setRole(Role role) {
+        this.role = role;
+    }
+
+    public Boolean getEnable() {
+        return enable;
+    }
+
+    public void setEnable(Boolean enable) {
+        this.enable = enable;
+    }
+
+}

+ 57 - 0
src/main/java/cn/com/qmth/scancentral/vo/user/UserVo.java

@@ -0,0 +1,57 @@
+package cn.com.qmth.scancentral.vo.user;
+
+import cn.com.qmth.scancentral.enums.Role;
+
+public class UserVo {
+
+    private Long id;
+
+    private Role role;
+
+    private String roleName;
+
+    private String loginName;
+
+    private Boolean enable;
+
+    public Long getId() {
+        return id;
+    }
+
+    public void setId(Long id) {
+        this.id = id;
+    }
+
+    public Role getRole() {
+        return role;
+    }
+
+    public void setRole(Role role) {
+        this.role = role;
+    }
+
+    public String getRoleName() {
+        return roleName;
+    }
+
+    public void setRoleName(String roleName) {
+        this.roleName = roleName;
+    }
+
+    public String getLoginName() {
+        return loginName;
+    }
+
+    public void setLoginName(String loginName) {
+        this.loginName = loginName;
+    }
+
+    public Boolean getEnable() {
+        return enable;
+    }
+
+    public void setEnable(Boolean enable) {
+        this.enable = enable;
+    }
+
+}

+ 17 - 0
src/main/resources/mapper/UserMapper.xml

@@ -0,0 +1,17 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
+<mapper namespace="cn.com.qmth.scancentral.dao.UserDao">
+
+    <select id="pageUser" resultType="cn.com.qmth.scancentral.vo.user.UserVo">
+        SELECT t.id, t.name, t.login_name, t.enable, t.role, t.update_time
+        FROM sc_user t
+        where 1=1
+        <if test="query.role != null">
+            AND t.role = #{query.role}
+        </if>
+        <if test="query.enable != null">
+            AND t.enable = #{query.enable}
+        </if>
+    </select>
+
+</mapper>