From d4f41a0166ca1af88b7c74a93e393c4a57576827 Mon Sep 17 00:00:00 2001 From: moyangzhan Date: Tue, 28 May 2024 18:54:36 +0800 Subject: [PATCH] =?UTF-8?q?=E5=A2=9E=E5=8A=A0:=E7=AE=A1=E7=90=86=E5=91=98-?= =?UTF-8?q?>=E7=94=A8=E6=88=B7=E6=A8=A1=E5=9D=97=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/AdminModelController.java | 39 ++++++++ .../admin/controller/AdminUserController.java | 68 ++++++++++++++ .../controller/SystemConfigController.java | 17 +++- .../adi/chat/controller/UserController.java | 11 ++- .../com/moyz/adi/common/dto/AiModelDto.java | 26 ++++++ .../com/moyz/adi/common/dto/SysConfigDto.java | 18 ++++ .../com/moyz/adi/common/dto/UserAddReq.java | 17 ++++ .../com/moyz/adi/common/dto/UserEditReq.java | 28 ++++++ .../com/moyz/adi/common/dto/UserInfoDto.java | 40 ++++++++ .../com/moyz/adi/common/dto/UsersReq.java | 15 +++ .../com/moyz/adi/common/enums/ErrorEnum.java | 1 + .../moyz/adi/common/filter/TokenFilter.java | 5 + .../adi/common/service/AiModelService.java | 55 +++++++++++ .../adi/common/service/SysConfigService.java | 19 ++++ .../moyz/adi/common/service/UserService.java | 92 ++++++++++++++++++- 15 files changed, 439 insertions(+), 12 deletions(-) create mode 100644 adi-admin/src/main/java/com/moyz/adi/admin/controller/AdminModelController.java create mode 100644 adi-admin/src/main/java/com/moyz/adi/admin/controller/AdminUserController.java create mode 100644 adi-common/src/main/java/com/moyz/adi/common/dto/AiModelDto.java create mode 100644 adi-common/src/main/java/com/moyz/adi/common/dto/SysConfigDto.java create mode 100644 adi-common/src/main/java/com/moyz/adi/common/dto/UserAddReq.java create mode 100644 adi-common/src/main/java/com/moyz/adi/common/dto/UserEditReq.java create mode 100644 adi-common/src/main/java/com/moyz/adi/common/dto/UserInfoDto.java create mode 100644 adi-common/src/main/java/com/moyz/adi/common/dto/UsersReq.java diff --git a/adi-admin/src/main/java/com/moyz/adi/admin/controller/AdminModelController.java b/adi-admin/src/main/java/com/moyz/adi/admin/controller/AdminModelController.java new file mode 100644 index 0000000..78aaf15 --- /dev/null +++ b/adi-admin/src/main/java/com/moyz/adi/admin/controller/AdminModelController.java @@ -0,0 +1,39 @@ +package com.moyz.adi.admin.controller; + +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.moyz.adi.common.dto.AiModelDto; +import com.moyz.adi.common.service.AiModelService; +import jakarta.annotation.Resource; +import jakarta.validation.constraints.Min; +import jakarta.validation.constraints.NotNull; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; + +@RestController +@RequestMapping("/admin/model") +@Validated +public class AdminModelController { + + @Resource + private AiModelService aiModelService; + + @GetMapping("/list") + public Page page(@RequestParam String platform, @NotNull @Min(1) Integer currentPage, @NotNull @Min(10) Integer pageSize) { + return aiModelService.page(platform, currentPage, pageSize); + } + + @PostMapping("/disable/{id}") + public void disable(@PathVariable Long id) { + aiModelService.disable(id); + } + + @PostMapping("/enable/{id}") + public void enable(@PathVariable Long id) { + aiModelService.enable(id); + } + + @PostMapping("/delete/{id}") + public void delete(@PathVariable Long id) { + aiModelService.softDelete(id); + } +} diff --git a/adi-admin/src/main/java/com/moyz/adi/admin/controller/AdminUserController.java b/adi-admin/src/main/java/com/moyz/adi/admin/controller/AdminUserController.java new file mode 100644 index 0000000..1eaf03f --- /dev/null +++ b/adi-admin/src/main/java/com/moyz/adi/admin/controller/AdminUserController.java @@ -0,0 +1,68 @@ +package com.moyz.adi.admin.controller; + +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.moyz.adi.common.base.ThreadContext; +import com.moyz.adi.common.dto.UserAddReq; +import com.moyz.adi.common.dto.UserEditReq; +import com.moyz.adi.common.dto.UserInfoDto; +import com.moyz.adi.common.dto.UsersReq; +import com.moyz.adi.common.entity.User; +import com.moyz.adi.common.service.UserService; +import com.talanlabs.avatargenerator.Avatar; +import com.talanlabs.avatargenerator.cat.CatAvatar; +import io.swagger.v3.oas.annotations.Operation; +import jakarta.annotation.Resource; +import jakarta.servlet.http.HttpServletResponse; +import jakarta.validation.constraints.Min; +import jakarta.validation.constraints.NotNull; +import org.springframework.beans.BeanUtils; +import org.springframework.http.MediaType; +import org.springframework.validation.annotation.Validated; +import org.springframework.web.bind.annotation.*; + +import javax.imageio.ImageIO; +import java.awt.image.BufferedImage; +import java.io.IOException; + +@RestController +@RequestMapping("/admin/user") +@Validated +public class AdminUserController { + + @Resource + private UserService userService; + + @PostMapping("/list") + public Page userList(@RequestBody UsersReq usersReq, @NotNull @Min(1) Integer currentPage, @NotNull @Min(10) Integer pageSize) { + return userService.listUsers(usersReq, currentPage, pageSize); + } + + @Operation(summary = "用户信息") + @GetMapping("/info/{uuid}") + public UserInfoDto info(@PathVariable String uuid) { + User user = userService.getByUuidOrThrow(uuid); + UserInfoDto result = new UserInfoDto(); + BeanUtils.copyProperties(user, result); + return result; + } + + @PostMapping("/addOne") + public UserInfoDto addOne(@Validated @RequestBody UserAddReq addUserReq) { + return userService.addUser(addUserReq); + } + + @PostMapping("/active/{uuid}") + public void activeByUuid(@PathVariable String uuid) { + userService.activeByUuid(uuid); + } + + @PostMapping("/freeze/{uuid}") + public void freezeByUuid(@PathVariable String uuid) { + userService.freeze(uuid); + } + + @PostMapping("/edit") + public void editUser(@Validated @RequestBody UserEditReq userEditReq) { + userService.editUser(userEditReq); + } +} diff --git a/adi-admin/src/main/java/com/moyz/adi/admin/controller/SystemConfigController.java b/adi-admin/src/main/java/com/moyz/adi/admin/controller/SystemConfigController.java index 5395a50..90a0f17 100644 --- a/adi-admin/src/main/java/com/moyz/adi/admin/controller/SystemConfigController.java +++ b/adi-admin/src/main/java/com/moyz/adi/admin/controller/SystemConfigController.java @@ -1,6 +1,7 @@ package com.moyz.adi.admin.controller; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; +import com.moyz.adi.common.dto.SysConfigDto; import com.moyz.adi.common.entity.SysConfig; import com.moyz.adi.common.service.KnowledgeBaseService; import com.moyz.adi.common.service.SysConfigService; @@ -8,8 +9,7 @@ import jakarta.annotation.Resource; import jakarta.validation.constraints.Min; import jakarta.validation.constraints.NotNull; import org.springframework.validation.annotation.Validated; -import org.springframework.web.bind.annotation.RequestMapping; -import org.springframework.web.bind.annotation.RestController; +import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("/admin/sys-config") @@ -19,7 +19,18 @@ public class SystemConfigController { @Resource private SysConfigService sysConfigService; - public Page list(String keyword, @NotNull @Min(1) Integer currentPage, @NotNull @Min(10) Integer pageSize) { + @GetMapping("/list") + public Page list(@RequestParam String keyword, @NotNull @Min(1) Integer currentPage, @NotNull @Min(10) Integer pageSize) { return sysConfigService.search(keyword, currentPage, pageSize); } + + @PostMapping("/edit") + public void edit(@Validated @RequestBody SysConfigDto sysConfigDto) { + sysConfigService.edit(sysConfigDto); + } + + @PostMapping("/delete/{id}") + public void delete(@PathVariable Long id) { + sysConfigService.softDelete(id); + } } diff --git a/adi-chat/src/main/java/com/moyz/adi/chat/controller/UserController.java b/adi-chat/src/main/java/com/moyz/adi/chat/controller/UserController.java index 50a9b70..3951774 100644 --- a/adi-chat/src/main/java/com/moyz/adi/chat/controller/UserController.java +++ b/adi-chat/src/main/java/com/moyz/adi/chat/controller/UserController.java @@ -83,13 +83,16 @@ public class UserController { if(null != user){ userId = user.getId(); } - Avatar avatar = CatAvatar.newAvatarBuilder().build(); - BufferedImage bufferedImage = avatar.create(userId); - //把图片写给浏览器 try { - ImageIO.write(bufferedImage, "png", response.getOutputStream()); + writeToResponse(userId, response); } catch (IOException e) { throw new RuntimeException(e); } } + + private synchronized void writeToResponse(Long userId, HttpServletResponse response) throws IOException{ + Avatar avatar = CatAvatar.newAvatarBuilder().size(64, 64).build(); + BufferedImage bufferedImage = avatar.create(userId); + ImageIO.write(bufferedImage, "png", response.getOutputStream()); + } } diff --git a/adi-common/src/main/java/com/moyz/adi/common/dto/AiModelDto.java b/adi-common/src/main/java/com/moyz/adi/common/dto/AiModelDto.java new file mode 100644 index 0000000..946b35b --- /dev/null +++ b/adi-common/src/main/java/com/moyz/adi/common/dto/AiModelDto.java @@ -0,0 +1,26 @@ +package com.moyz.adi.common.dto; + +import lombok.Data; + +import java.time.LocalDateTime; + +@Data +public class AiModelDto { + + private Long id; + + private String type; + + private String name; + + private String platform; + + private String remark; + + private Boolean isEnable; + + private LocalDateTime createTime; + + private LocalDateTime updateTime; + +} diff --git a/adi-common/src/main/java/com/moyz/adi/common/dto/SysConfigDto.java b/adi-common/src/main/java/com/moyz/adi/common/dto/SysConfigDto.java new file mode 100644 index 0000000..fc93154 --- /dev/null +++ b/adi-common/src/main/java/com/moyz/adi/common/dto/SysConfigDto.java @@ -0,0 +1,18 @@ +package com.moyz.adi.common.dto; + +import jakarta.validation.constraints.Min; +import jakarta.validation.constraints.NotBlank; +import lombok.Data; +import org.springframework.validation.annotation.Validated; + +@Data +@Validated +public class SysConfigDto { + + @Min(1) + private Long id; + @NotBlank + private String name; + @NotBlank + private String value; +} diff --git a/adi-common/src/main/java/com/moyz/adi/common/dto/UserAddReq.java b/adi-common/src/main/java/com/moyz/adi/common/dto/UserAddReq.java new file mode 100644 index 0000000..8891d0b --- /dev/null +++ b/adi-common/src/main/java/com/moyz/adi/common/dto/UserAddReq.java @@ -0,0 +1,17 @@ +package com.moyz.adi.common.dto; + +import jakarta.validation.constraints.NotBlank; +import lombok.Data; +import org.springframework.validation.annotation.Validated; + +@Data +@Validated +public class UserAddReq { + String name; + + @NotBlank + String email; + + @NotBlank + String password; +} diff --git a/adi-common/src/main/java/com/moyz/adi/common/dto/UserEditReq.java b/adi-common/src/main/java/com/moyz/adi/common/dto/UserEditReq.java new file mode 100644 index 0000000..8d9978c --- /dev/null +++ b/adi-common/src/main/java/com/moyz/adi/common/dto/UserEditReq.java @@ -0,0 +1,28 @@ +package com.moyz.adi.common.dto; + +import com.moyz.adi.common.enums.UserStatusEnum; +import lombok.Data; + +@Data +public class UserEditReq { + + private String uuid; + + private String name; + + private UserStatusEnum userStatus; + + private Integer quotaByTokenDaily; + + private Integer quotaByTokenMonthly; + + private Integer quotaByRequestDaily; + + private Integer quotaByRequestMonthly; + + private Integer quotaByImageDaily; + + private Integer quotaByImageMonthly; + + private Boolean isAdmin; +} diff --git a/adi-common/src/main/java/com/moyz/adi/common/dto/UserInfoDto.java b/adi-common/src/main/java/com/moyz/adi/common/dto/UserInfoDto.java new file mode 100644 index 0000000..25c6c89 --- /dev/null +++ b/adi-common/src/main/java/com/moyz/adi/common/dto/UserInfoDto.java @@ -0,0 +1,40 @@ +package com.moyz.adi.common.dto; + +import com.moyz.adi.common.enums.UserStatusEnum; +import lombok.Data; + +import java.time.LocalDateTime; + +@Data +public class UserInfoDto{ + + private Long id; + + private String name; + + private String email; + + private String uuid; + + private Integer quotaByTokenDaily; + + private Integer quotaByTokenMonthly; + + private Integer quotaByRequestDaily; + + private Integer quotaByRequestMonthly; + + private Integer quotaByImageDaily; + + private Integer quotaByImageMonthly; + + private UserStatusEnum userStatus; + + private LocalDateTime activeTime; + + private Boolean isAdmin; + + private LocalDateTime createTime; + + private LocalDateTime updateTime; +} diff --git a/adi-common/src/main/java/com/moyz/adi/common/dto/UsersReq.java b/adi-common/src/main/java/com/moyz/adi/common/dto/UsersReq.java new file mode 100644 index 0000000..6d0ed0a --- /dev/null +++ b/adi-common/src/main/java/com/moyz/adi/common/dto/UsersReq.java @@ -0,0 +1,15 @@ +package com.moyz.adi.common.dto; + +import com.moyz.adi.common.enums.UserStatusEnum; +import lombok.Data; + +@Data +public class UsersReq { + String name; + String email; + String uuid; + + UserStatusEnum userStatus; + + Boolean isAdmin; +} diff --git a/adi-common/src/main/java/com/moyz/adi/common/enums/ErrorEnum.java b/adi-common/src/main/java/com/moyz/adi/common/enums/ErrorEnum.java index 26e5525..ef9b382 100644 --- a/adi-common/src/main/java/com/moyz/adi/common/enums/ErrorEnum.java +++ b/adi-common/src/main/java/com/moyz/adi/common/enums/ErrorEnum.java @@ -23,6 +23,7 @@ public enum ErrorEnum { A_QA_ASK_LIMIT("A0019", "请求次数太多"), A_QA_ITEM_LIMIT("A0020", "知识点生成已超额度"), A_CONVERSATION_EXIST("A0021", "对话已存在"), + A_MODEL_NOT_FOUND("A0022", "模型不存在"), B_UNCAUGHT_ERROR("B0001", "未捕捉异常"), B_COMMON_ERROR("B0002", "业务出错"), B_GLOBAL_ERROR("B0003", "全局异常"), diff --git a/adi-common/src/main/java/com/moyz/adi/common/filter/TokenFilter.java b/adi-common/src/main/java/com/moyz/adi/common/filter/TokenFilter.java index f387b6d..3a64e14 100644 --- a/adi-common/src/main/java/com/moyz/adi/common/filter/TokenFilter.java +++ b/adi-common/src/main/java/com/moyz/adi/common/filter/TokenFilter.java @@ -72,6 +72,11 @@ public class TokenFilter extends OncePerRequestFilter { response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); return; } + if (!user.getIsAdmin() && requestUri.startsWith("/admin/")) { + log.warn("无管理权限:{}", requestUri); + response.setStatus(HttpServletResponse.SC_UNAUTHORIZED); + return; + } ThreadContext.setCurrentUser(user); ThreadContext.setToken(token); log.info("response::" + response); diff --git a/adi-common/src/main/java/com/moyz/adi/common/service/AiModelService.java b/adi-common/src/main/java/com/moyz/adi/common/service/AiModelService.java index ab37486..dee7c3d 100644 --- a/adi-common/src/main/java/com/moyz/adi/common/service/AiModelService.java +++ b/adi-common/src/main/java/com/moyz/adi/common/service/AiModelService.java @@ -1,10 +1,21 @@ package com.moyz.adi.common.service; +import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.toolkit.ChainWrappers; +import com.moyz.adi.common.dto.AiModelDto; import com.moyz.adi.common.entity.AiModel; +import com.moyz.adi.common.enums.ErrorEnum; +import com.moyz.adi.common.exception.BaseException; import com.moyz.adi.common.mapper.AiModelMapper; +import com.moyz.adi.common.util.MPPageUtil; +import dev.langchain4j.agent.tool.P; +import jakarta.validation.constraints.Min; +import jakarta.validation.constraints.NotNull; import lombok.extern.slf4j.Slf4j; +import org.apache.commons.lang3.StringUtils; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; @@ -48,4 +59,48 @@ public class AiModelService extends ServiceImpl { return null == aiModel ? 0l : aiModel.getId(); } + public AiModel getByIdOrThrow(Long id) { + AiModel existModel = baseMapper.selectById(id); + if (null == existModel) { + throw new BaseException(ErrorEnum.A_MODEL_NOT_FOUND); + } + return existModel; + } + + public Page page(String platform, Integer currentPage, Integer pageSize) { + LambdaQueryWrapper lambdaQueryWrapper = new LambdaQueryWrapper<>(); + if (StringUtils.isNotBlank(platform)) { + lambdaQueryWrapper.eq(AiModel::getPlatform, platform); + } + lambdaQueryWrapper.eq(AiModel::getIsDeleted, false); + lambdaQueryWrapper.orderByDesc(AiModel::getUpdateTime); + Page aiModelPage = baseMapper.selectPage(new Page<>(currentPage, pageSize), lambdaQueryWrapper); + return MPPageUtil.convertToPage(aiModelPage, new Page<>(), AiModelDto.class); + } + + public void disable(Long id) { + AiModel model = new AiModel(); + model.setId(id); + model.setIsEnable(false); + baseMapper.updateById(model); + } + + public void enable(Long id) { + AiModel model = new AiModel(); + model.setId(id); + model.setIsEnable(true); + baseMapper.updateById(model); + } + + public void softDelete(Long id) { + AiModel existModel = getByIdOrThrow(id); + + AiModel model = new AiModel(); + model.setId(id); + model.setIsDeleted(true); + baseMapper.updateById(model); + + MODEL_NAME_TO_OBJ.remove(existModel.getName()); + MODEL_ID_TO_OBJ.remove(existModel.getId()); + } } diff --git a/adi-common/src/main/java/com/moyz/adi/common/service/SysConfigService.java b/adi-common/src/main/java/com/moyz/adi/common/service/SysConfigService.java index dc31273..76c1681 100644 --- a/adi-common/src/main/java/com/moyz/adi/common/service/SysConfigService.java +++ b/adi-common/src/main/java/com/moyz/adi/common/service/SysConfigService.java @@ -4,6 +4,7 @@ import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.moyz.adi.common.cosntant.AdiConstant; +import com.moyz.adi.common.dto.SysConfigDto; import com.moyz.adi.common.entity.SysConfig; import com.moyz.adi.common.mapper.SysConfigMapper; import com.moyz.adi.common.util.JsonUtil; @@ -11,6 +12,7 @@ import com.moyz.adi.common.util.LocalCache; import com.moyz.adi.common.vo.RequestRateLimit; import lombok.extern.slf4j.Slf4j; import org.apache.commons.lang3.StringUtils; +import org.springframework.beans.BeanUtils; import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; @@ -52,6 +54,23 @@ public class SysConfigService extends ServiceImpl { LocalCache.IMAGE_RATE_LIMIT_CONFIG.setType(RequestRateLimit.TYPE_IMAGE); } + public void edit(SysConfigDto sysConfigDto) { + SysConfig sysConfig = new SysConfig(); + BeanUtils.copyProperties(sysConfigDto, sysConfig); + baseMapper.updateById(sysConfig); + + reload(); + } + + public void softDelete(Long id) { + SysConfig sysConfig = new SysConfig(); + sysConfig.setIsDeleted(true); + sysConfig.setId(id); + baseMapper.updateById(sysConfig); + + reload(); + } + public int getConversationMaxNum() { String maxNum = LocalCache.CONFIGS.get(AdiConstant.SysConfigKey.CONVERSATION_MAX_NUM); return Integer.parseInt(maxNum); diff --git a/adi-common/src/main/java/com/moyz/adi/common/service/UserService.java b/adi-common/src/main/java/com/moyz/adi/common/service/UserService.java index 05db49d..a1344f6 100644 --- a/adi-common/src/main/java/com/moyz/adi/common/service/UserService.java +++ b/adi-common/src/main/java/com/moyz/adi/common/service/UserService.java @@ -1,15 +1,13 @@ package com.moyz.adi.common.service; import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper; +import com.baomidou.mybatisplus.extension.plugins.pagination.Page; import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl; import com.baomidou.mybatisplus.extension.toolkit.ChainWrappers; import com.moyz.adi.common.base.ThreadContext; import com.moyz.adi.common.cosntant.AdiConstant; import com.moyz.adi.common.cosntant.RedisKeyConstant; -import com.moyz.adi.common.dto.ConfigResp; -import com.moyz.adi.common.dto.LoginReq; -import com.moyz.adi.common.dto.LoginResp; -import com.moyz.adi.common.dto.UserUpdateReq; +import com.moyz.adi.common.dto.*; import com.moyz.adi.common.entity.User; import com.moyz.adi.common.enums.ErrorEnum; import com.moyz.adi.common.enums.UserStatusEnum; @@ -18,6 +16,7 @@ import com.moyz.adi.common.helper.AdiMailSender; import com.moyz.adi.common.mapper.UserMapper; import com.moyz.adi.common.util.JsonUtil; import com.moyz.adi.common.util.LocalCache; +import com.moyz.adi.common.util.MPPageUtil; import com.moyz.adi.common.vo.CostStat; import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; @@ -29,6 +28,7 @@ import org.springframework.data.redis.core.StringRedisTemplate; import org.springframework.stereotype.Service; import java.text.MessageFormat; +import java.time.LocalDateTime; import java.util.UUID; import java.util.concurrent.TimeUnit; @@ -169,6 +169,7 @@ public class UserService extends ServiceImpl { User updateUser = new User(); updateUser.setId(user.getId()); updateUser.setUserStatus(UserStatusEnum.NORMAL); + updateUser.setActiveTime(LocalDateTime.now()); baseMapper.updateById(updateUser); setLoginToken(user); @@ -177,6 +178,31 @@ public class UserService extends ServiceImpl { conversationService.createDefault(user.getId()); } + public void activeByUuid(String uuid) { + User user = this.getByUuidOrThrow(uuid); + User updateUser = new User(); + updateUser.setId(user.getId()); + updateUser.setUserStatus(UserStatusEnum.NORMAL); + updateUser.setActiveTime(LocalDateTime.now()); + baseMapper.updateById(updateUser); + } + + public void freeze(String uuid) { + User user = this.getByUuidOrThrow(uuid); + User updateUser = new User(); + updateUser.setId(user.getId()); + updateUser.setUserStatus(UserStatusEnum.FREEZE); + baseMapper.updateById(updateUser); + } + + public void editUser(UserEditReq userEditReq) { + User user = this.getByUuidOrThrow(userEditReq.getUuid()); + User editUser = new User(); + editUser.setId(user.getId()); + BeanUtils.copyProperties(userEditReq, editUser); + baseMapper.updateById(editUser); + } + public LoginResp login(LoginReq loginReq) { //captcha check String failCountKey = MessageFormat.format(RedisKeyConstant.LOGIN_FAIL_COUNT, loginReq.getEmail()); @@ -314,10 +340,66 @@ public class UserService extends ServiceImpl { adiMailSender.send("欢迎注册AIDeepIn", "激活链接(" + AdiConstant.AUTH_ACTIVE_CODE_EXPIRE + "小时内有效):" + backendUrl + "/auth/active?code=" + activeCode, email); } - public User getByUuid(String uuid){ + public User getByUuid(String uuid) { return ChainWrappers.lambdaQueryChain(baseMapper) .eq(User::getUuid, uuid) .one(); } + public User getByUuidOrThrow(String uuid) { + User user = this.getByUuid(uuid); + if (null == user) { + throw new BaseException(A_USER_NOT_EXIST); + } + return user; + } + + public Page listUsers(UsersReq usersReq, Integer currentPage, Integer pageSize) { + LambdaQueryWrapper wrapper = new LambdaQueryWrapper<>(); + if (StringUtils.isNotBlank(usersReq.getName())) { + wrapper.eq(User::getName, usersReq.getName()); + } + if (StringUtils.isNotBlank(usersReq.getUuid())) { + wrapper.eq(User::getUuid, usersReq.getUuid()); + } + if (StringUtils.isNotBlank(usersReq.getEmail())) { + wrapper.eq(User::getEmail, usersReq.getEmail()); + } + if (null != usersReq.getUserStatus()) { + wrapper.eq(User::getUserStatus, usersReq.getUserStatus()); + } + wrapper.orderByDesc(User::getUpdateTime); + Page page = baseMapper.selectPage(new Page<>(currentPage, pageSize), wrapper); + Page result = new Page<>(); + return MPPageUtil.convertToPage(page, result, UserInfoDto.class); + } + + public UserInfoDto addUser(UserAddReq addUserReq) { + User user = this.lambdaQuery() + .eq(User::getIsDeleted, false) + .eq(User::getEmail, addUserReq.getEmail()) + .one(); + if (null != user) { + throw new BaseException(A_USER_EXIST); + } + + String hashed = BCrypt.hashpw(addUserReq.getPassword(), BCrypt.gensalt()); + String uuid = UUID.randomUUID().toString().replace("-", ""); + User newOne = new User(); + if (StringUtils.isNotBlank(addUserReq.getName())) { + newOne.setName(addUserReq.getName()); + } else { + newOne.setName(StringUtils.substringBefore(addUserReq.getEmail(), "@")); + } + newOne.setUuid(uuid); + newOne.setEmail(addUserReq.getEmail()); + newOne.setPassword(hashed); + newOne.setUserStatus(UserStatusEnum.NORMAL); + baseMapper.insert(newOne); + + UserInfoDto result = new UserInfoDto(); + User newUser = this.getByUuid(uuid); + BeanUtils.copyProperties(newUser, result); + return result; + } }