|
|
@@ -0,0 +1,153 @@
|
|
|
+package space.anyi.serve.controller;
|
|
|
+
|
|
|
+import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
|
|
|
+import jakarta.annotation.Resource;
|
|
|
+import jakarta.validation.Valid;
|
|
|
+import jakarta.validation.constraints.NotBlank;
|
|
|
+import jakarta.validation.constraints.NotEmpty;
|
|
|
+import jakarta.validation.constraints.NotNull;
|
|
|
+import org.springframework.beans.BeanUtils;
|
|
|
+import org.springframework.web.bind.annotation.*;
|
|
|
+import space.anyi.serve.entity.PageVo;
|
|
|
+import space.anyi.serve.entity.Response;
|
|
|
+import space.anyi.serve.entity.user.*;
|
|
|
+import space.anyi.serve.service.UserService;
|
|
|
+
|
|
|
+import java.util.List;
|
|
|
+import java.util.stream.Collectors;
|
|
|
+
|
|
|
+
|
|
|
+/**
|
|
|
+ * (User)表控制层
|
|
|
+ *
|
|
|
+ * @author 杨逸
|
|
|
+ * @since 2026-03-31 13:17:55
|
|
|
+ */
|
|
|
+@RestController
|
|
|
+@RequestMapping("user")
|
|
|
+public class UserController {
|
|
|
+ /**
|
|
|
+ * 服务对象
|
|
|
+ */
|
|
|
+ @Resource
|
|
|
+ private UserService userService;
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 分页查询
|
|
|
+ * @param account
|
|
|
+ * @param username
|
|
|
+ * @param role
|
|
|
+ * @param enable
|
|
|
+ * @param pageNum
|
|
|
+ * @param pageSize
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @GetMapping
|
|
|
+ public Response<PageVo<List<UserVo>>> queryByPage(
|
|
|
+ @NotNull @RequestParam(defaultValue = "") String account,
|
|
|
+ @NotNull @RequestParam(defaultValue = "") String username,
|
|
|
+ @NotNull @RequestParam(defaultValue = "") String role,
|
|
|
+ @NotNull @RequestParam(defaultValue = "1") Integer enable,
|
|
|
+ @NotNull(message = "页码不能为null") @RequestParam(defaultValue = "1") Integer pageNum,
|
|
|
+ @NotNull(message = "分页大小不能为null")@RequestParam(defaultValue = "10") Integer pageSize) {
|
|
|
+ User user = new User();
|
|
|
+ user.setAccount(account);
|
|
|
+ user.setUsername(username);
|
|
|
+ user.setRole(role);
|
|
|
+ user.setEnable(enable);
|
|
|
+ Page<User> page = this.userService.queryByPage(user, Page.of(pageNum, pageSize));
|
|
|
+ return Response.ok(new PageVo<List<UserVo>>(page.getTotal(),UserVo.from(page.getRecords())));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 通过主键查询单条数据
|
|
|
+ *
|
|
|
+ * @param id 主键
|
|
|
+ * @return 单条数据
|
|
|
+ */
|
|
|
+ @GetMapping("{id}")
|
|
|
+ public Response<UserVo> queryById(@NotBlank(message = "用户ID不能为空") @PathVariable String id) {
|
|
|
+ User user = this.userService.queryById(Long.valueOf(id));
|
|
|
+ return Response.ok(UserVo.form(user));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 新增数据
|
|
|
+ *
|
|
|
+ * @param userDto 实体
|
|
|
+ * @return 新增结果
|
|
|
+ */
|
|
|
+ @PostMapping
|
|
|
+ public Response<Boolean> add(@Valid@RequestBody UserDto userDto) {
|
|
|
+ User user = new User();
|
|
|
+ BeanUtils.copyProperties(userDto,user);
|
|
|
+ return Response.ok(this.userService.insert(user));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 编辑数据
|
|
|
+ *
|
|
|
+ * @param userDto 实体
|
|
|
+ * @return 编辑结果
|
|
|
+ */
|
|
|
+ @PutMapping
|
|
|
+ public Response<Boolean> edit(@Valid@RequestBody UserDto userDto) {
|
|
|
+ User user = new User();
|
|
|
+ BeanUtils.copyProperties(userDto,user);
|
|
|
+ user.setId(Long.valueOf(userDto.getId()));
|
|
|
+ return Response.ok(this.userService.update(user));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 删除数据
|
|
|
+ *
|
|
|
+ * @param ids 主键
|
|
|
+ * @return 删除是否成功
|
|
|
+ */
|
|
|
+ @DeleteMapping
|
|
|
+ public Response<Boolean> deleteById(@NotEmpty(message = "ID列表不能为空") @RequestParam List<String> ids) {
|
|
|
+ List<Long> list = ids.stream().map(Long::valueOf).toList();
|
|
|
+ return Response.ok(this.userService.deleteById(list));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新用户状态
|
|
|
+ * @param dto
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @PutMapping("/updateStatus")
|
|
|
+ public Response updateUserStatus(@Valid@RequestBody UpdateUserStatusDto dto){
|
|
|
+ User user = new User();
|
|
|
+ BeanUtils.copyProperties(dto,user);
|
|
|
+ user.setId(Long.valueOf(dto.getId()));
|
|
|
+ return Response.ok(userService.updateUserStatus(user));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新用户头像
|
|
|
+ * @param dto
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @PutMapping("/updateAvatar")
|
|
|
+ public Response updateUserAvatar(@Valid@RequestBody UpdateUserAvatarDto dto){
|
|
|
+ User user = new User();
|
|
|
+ BeanUtils.copyProperties(dto,user);
|
|
|
+ user.setId(Long.valueOf(dto.getId()));
|
|
|
+ return Response.ok(userService.updateUserAvatar(user));
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 更新用户密码
|
|
|
+ * @param dto
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ @PutMapping("/updatePassword")
|
|
|
+ public Response updatePassword(@Valid @RequestBody UpdateUserPasswordDto dto){
|
|
|
+ User user = new User();
|
|
|
+ BeanUtils.copyProperties(dto,user);
|
|
|
+ user.setId(Long.valueOf(dto.getId()));
|
|
|
+ return Response.ok(userService.updatePassword(user,dto.getOldPassword()));
|
|
|
+ }
|
|
|
+
|
|
|
+}
|
|
|
+
|