From efdde4e4e46c99a2ca19cf9e3dfbdbfbdc17bc06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=90=8C=E7=8B=BC=E8=93=9D=E5=A4=A9?= Date: Tue, 5 Nov 2024 16:55:14 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E7=94=A8=E6=88=B7=E6=96=B0=E5=A2=9E?= =?UTF-8?q?=E3=80=81=E6=9B=B4=E6=96=B0=E5=8A=9F=E8=83=BD=E5=AE=8C=E6=88=90?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../harmony/controller/UserController.java | 18 ++++++-- .../harmony/mapper/UserMapper.java | 3 +- .../harmony/service/UserService.java | 1 + .../harmony/service/impl/UserServiceImpl.java | 43 ++++++++++++++++++- src/main/resources/mapper/UserMapper.xml | 3 +- 5 files changed, 61 insertions(+), 7 deletions(-) diff --git a/src/main/java/com/guaiguailang/harmony/controller/UserController.java b/src/main/java/com/guaiguailang/harmony/controller/UserController.java index 9ae2c87..c244206 100644 --- a/src/main/java/com/guaiguailang/harmony/controller/UserController.java +++ b/src/main/java/com/guaiguailang/harmony/controller/UserController.java @@ -61,7 +61,7 @@ public class UserController { description = "加载可以分配的角色,以供用户添加子用户", tags = {"用户相关接口"} ) - @PostMapping("/add/roleList") + @GetMapping("/add/roleList") public ResponseEntity getRoleListWhenAddUser(@RequestParam String role){ /* @@ -77,10 +77,13 @@ public class UserController { ) @PostMapping("/add") public ResponseEntity addUser(@RequestBody ParamUserAdd userAddParam){ + System.out.println(userAddParam.toString()); + // 输出 ParamUserAdd(creatorId=null, name=测试, username=test, password=123456, email=test@test.com, telphone=null, roleId=null, avatar=null, status=0, createTime=null, merchantCode=null, id=null, code=null) // 校验字段 - if (StringUtils.isAnyBlank(userAddParam.getName(), userAddParam.getUserName(), userAddParam.getPassword(), userAddParam.getRoleId())) { - return ResponseEntity.badRequest().body("用户账号不能为空"); + if (StringUtils.isAnyBlank(userAddParam.getName(), userAddParam.getUsername(), userAddParam.getPassword(), userAddParam.getRoleId())) { + //明明不为空,但是被拦截了 + return ResponseEntity.badRequest().body(ResponseResult.error("请填写用户账号、密码、角色")); } else { return ResponseEntity.ok(userService.addUser(userAddParam)); } @@ -97,4 +100,13 @@ public class UserController { public ResponseEntity registerUser(@RequestBody ParamUserAdd userAddParam){ return ResponseEntity.ok(userService.registerUser(userAddParam)); } + @Operation( + summary = "用户 更新", + description = "用户更新(不涉及密码)", + tags = {"用户相关接口"} + ) + @PostMapping("/update") + public ResponseEntity updateUser(@RequestBody ParamUserAdd userAddParam){ + return ResponseEntity.ok(userService.updateUser(userAddParam)); + } } diff --git a/src/main/java/com/guaiguailang/harmony/mapper/UserMapper.java b/src/main/java/com/guaiguailang/harmony/mapper/UserMapper.java index 686600b..b5295a4 100644 --- a/src/main/java/com/guaiguailang/harmony/mapper/UserMapper.java +++ b/src/main/java/com/guaiguailang/harmony/mapper/UserMapper.java @@ -1,5 +1,6 @@ package com.guaiguailang.harmony.mapper; +import com.baomidou.mybatisplus.core.mapper.BaseMapper; import com.guaiguailang.harmony.domain.dto.ParamUserAdd; import com.guaiguailang.harmony.domain.entity.SystemRole; import com.guaiguailang.harmony.domain.entity.UserInfo; @@ -12,7 +13,7 @@ import java.util.List; import java.util.Map; @Mapper -public interface UserMapper { +public interface UserMapper extends BaseMapper { @Select("SELECT * from user_info where id=#{id} AND status=1") UserInfo getUserById(@Param("id") Long id); @Select("SELECT * from user_info where username=#{username} AND status=1") diff --git a/src/main/java/com/guaiguailang/harmony/service/UserService.java b/src/main/java/com/guaiguailang/harmony/service/UserService.java index afeacd5..8202d53 100644 --- a/src/main/java/com/guaiguailang/harmony/service/UserService.java +++ b/src/main/java/com/guaiguailang/harmony/service/UserService.java @@ -18,4 +18,5 @@ public interface UserService { ResponseResult registerUser(ParamUserAdd userAddParam); + ResponseResult updateUser(ParamUserAdd userAddParam); } diff --git a/src/main/java/com/guaiguailang/harmony/service/impl/UserServiceImpl.java b/src/main/java/com/guaiguailang/harmony/service/impl/UserServiceImpl.java index 2d6c18c..6c260ce 100644 --- a/src/main/java/com/guaiguailang/harmony/service/impl/UserServiceImpl.java +++ b/src/main/java/com/guaiguailang/harmony/service/impl/UserServiceImpl.java @@ -1,6 +1,7 @@ package com.guaiguailang.harmony.service.impl; import cn.dev33.satoken.stp.StpUtil; +import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper; import com.github.yitter.idgen.YitIdHelper; import com.guaiguailang.harmony.domain.dto.ParamUserAdd; import com.guaiguailang.harmony.domain.dto.ParamUserList; @@ -171,8 +172,15 @@ public class UserServiceImpl implements UserService { // 账号状态 默认激活 userAddParam.setStatus(1); + // 其他信息配置 + userAddParam.setEmail(userAddParam.getEmail()==null?"":userAddParam.getEmail()); + userAddParam.setTelphone(userAddParam.getTelphone()==null?"":userAddParam.getTelphone()); + userAddParam.setAvatar(userAddParam.getAvatar()==null?"":userAddParam.getAvatar()); + userAddParam.setCode(userAddParam.getCode()==null?"":userAddParam.getCode()); - + System.out.println("===========用户信息"); + System.out.println(userAddParam.toString()); + System.out.println("==================="); int result = userMapper.addUser(userAddParam); if (result == 1) { @@ -224,6 +232,39 @@ public class UserServiceImpl implements UserService { else return ResponseResult.error("注册失败"); } + + @Override + public ResponseResult updateUser(ParamUserAdd userAddParam) { + // 将 ParamUserAdd 转换为 UserInfo + UserInfo userInfo = new UserInfo(); + userAddParam.setCreateTime(null); + BeanUtils.copyProperties(userAddParam, userInfo); + + // 设置不需要更新的字段为 null + userInfo.setPassword(null); + userInfo.setCreateTime(null); + + // 使用 LambdaUpdateWrapper 构建更新条件 + LambdaUpdateWrapper updateWrapper = new LambdaUpdateWrapper<>(); + updateWrapper.eq(UserInfo::getId, userInfo.getId()) + .set(UserInfo::getName, userInfo.getName()) + .set(UserInfo::getUsername, userInfo.getUsername()) + .set(UserInfo::getEmail, userInfo.getEmail()) + .set(UserInfo::getTelephone, userInfo.getTelephone()) + .set(UserInfo::getRoleId, userInfo.getRoleId()) + .set(UserInfo::getAvatar, userInfo.getAvatar()) + .set(UserInfo::getStatus, userInfo.getStatus()) + .set(UserInfo::getMerchantCode, userInfo.getMerchantCode()); + // 执行更新操作 + boolean result = userMapper.update(userInfo, updateWrapper) > 0; + + if (result) { + return ResponseResult.success(userInfo); + } else { + return ResponseResult.error("更新用户失败"); + } + } + // 方法 public UserInfo getUserByAccount(String username){ return userMapper.getUserByAccount(username); diff --git a/src/main/resources/mapper/UserMapper.xml b/src/main/resources/mapper/UserMapper.xml index 8b7a2ab..837c548 100644 --- a/src/main/resources/mapper/UserMapper.xml +++ b/src/main/resources/mapper/UserMapper.xml @@ -75,8 +75,7 @@ INSERT INTO user_info ( id, name, username, password, avatar, status, telephone, email, create_time, merchant_code, deleted, role_id, creator_id ) VALUES ( - #{id}, #{name}, #{userName}, #{password}, #{avatar}, #{status}, #{telphone}, #{email}, #{createTime}, - + #{id}, #{name}, #{username}, #{password}, #{avatar}, #{status}, #{telphone}, #{email}, #{createTime}, #{merchantCode}, 0, #{roleId}, #{creatorId} )