feat: 用户新增、更新功能完成

This commit is contained in:
萌狼蓝天 2024-11-05 16:55:14 +08:00
parent e5b519f56b
commit efdde4e4e4
5 changed files with 61 additions and 7 deletions

View File

@ -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));
}
}

View File

@ -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<UserInfo> {
@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")

View File

@ -18,4 +18,5 @@ public interface UserService {
ResponseResult registerUser(ParamUserAdd userAddParam);
ResponseResult updateUser(ParamUserAdd userAddParam);
}

View File

@ -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<UserInfo> 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);

View File

@ -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},
<!-- 使用嵌套查询来获取merchant_code -->
#{id}, #{name}, #{username}, #{password}, #{avatar}, #{status}, #{telphone}, #{email}, #{createTime},
#{merchantCode},
0, #{roleId}, #{creatorId}
)