|
|
package com.shanzhu.flower.controller;
|
|
|
|
|
|
import com.shanzhu.flower.config.Constant;
|
|
|
import com.shanzhu.flower.config.HttpMsg;
|
|
|
import com.shanzhu.flower.entity.R;
|
|
|
import com.shanzhu.flower.entity.User;
|
|
|
import com.shanzhu.flower.service.UserService;
|
|
|
import org.springframework.web.bind.annotation.*;
|
|
|
import tk.mybatis.mapper.util.StringUtil;
|
|
|
|
|
|
import javax.annotation.Resource;
|
|
|
import java.util.HashMap;
|
|
|
import java.util.List;
|
|
|
import java.util.Map;
|
|
|
|
|
|
/**
|
|
|
* 用户 控制层
|
|
|
* 提供用户相关的RESTful API接口,包括查询用户信息、分页查询用户列表、新增用户、更新用户信息和删除用户。
|
|
|
*
|
|
|
* @author: ShanZhu
|
|
|
* @date: 2024-01-24
|
|
|
*/
|
|
|
@RestController
|
|
|
@RequestMapping("user")
|
|
|
public class UserController {
|
|
|
|
|
|
@Resource
|
|
|
private UserService userService; // 注入用户服务层
|
|
|
|
|
|
/**
|
|
|
* 根据账号查询用户信息
|
|
|
* 查询指定账号的用户详细信息。
|
|
|
*
|
|
|
* @param account 用户账号
|
|
|
* @return 用户信息
|
|
|
*/
|
|
|
@RequestMapping("/queryInfoByAccount")
|
|
|
R queryInfoByAccount(@RequestParam("account") String account) {
|
|
|
R r = new R(); // 创建响应对象
|
|
|
|
|
|
// 检查账号是否为空
|
|
|
if (StringUtil.isEmpty(account)) {
|
|
|
return r.setCode(4000).setMsg(HttpMsg.INVALID_PARAM); // 参数不合法
|
|
|
}
|
|
|
|
|
|
// 查询用户信息
|
|
|
User loginUser = userService.queryInfo(account);
|
|
|
if (loginUser == null) {
|
|
|
return r.setCode(4000).setMsg(HttpMsg.INVALID_USER); // 用户不存在
|
|
|
}
|
|
|
|
|
|
return r.setCode(2000).setData(loginUser); // 返回用户信息
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 分页查询用户列表
|
|
|
* 根据查询条件和页码分页查询用户列表。
|
|
|
*
|
|
|
* @param page 页码
|
|
|
* @param searchKey 查询条件
|
|
|
* @return 分页后的用户列表
|
|
|
*/
|
|
|
@RequestMapping("/find")
|
|
|
R find(@RequestParam("page") int page, @RequestParam("searchKey") String searchKey) {
|
|
|
R r = new R(); // 创建响应对象
|
|
|
Map<String, Object> map = new HashMap<>(); // 创建返回数据的Map
|
|
|
|
|
|
// 查询用户列表
|
|
|
List<User> users = userService.find(searchKey);
|
|
|
if (users == null) {
|
|
|
return r.setCode(2000); // 没有查询到数据
|
|
|
}
|
|
|
|
|
|
// 计算分页信息
|
|
|
int totalSize = users.size(); // 总记录数
|
|
|
int pageSize = Constant.PAGE_SIZE; // 每页大小
|
|
|
int totalPages = (totalSize + pageSize - 1) / pageSize; // 总页数
|
|
|
|
|
|
// 获取当前页的数据
|
|
|
int start = (page - 1) * pageSize;
|
|
|
int end = Math.min(start + pageSize, totalSize);
|
|
|
List<User> items = users.subList(start, end);
|
|
|
|
|
|
// 将分页信息和当前页数据放入Map
|
|
|
map.put("items", items);
|
|
|
map.put("len", totalPages);
|
|
|
|
|
|
return r.setCode(2000).setData(map); // 返回分页后的用户列表
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 新增用户
|
|
|
* 添加一个新的用户记录。
|
|
|
*
|
|
|
* @param user 用户信息
|
|
|
* @return 新增结果
|
|
|
*/
|
|
|
@RequestMapping("/create")
|
|
|
R create(@RequestBody User user) {
|
|
|
R r = new R(); // 创建响应对象
|
|
|
|
|
|
// 调用服务层添加用户
|
|
|
int ans = userService.add(user);
|
|
|
|
|
|
// 根据返回结果设置响应
|
|
|
if (ans == 1) {
|
|
|
return r.setCode(2000).setMsg(HttpMsg.ADD_USER_OK); // 添加成功
|
|
|
}
|
|
|
|
|
|
return r.setCode(4000).setMsg(HttpMsg.ADD_USER_FAILED); // 添加失败
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 更新用户信息
|
|
|
* 更新指定用户的详细信息。
|
|
|
*
|
|
|
* @param user 用户信息
|
|
|
* @return 更新结果
|
|
|
*/
|
|
|
@RequestMapping("/update")
|
|
|
R update(@RequestBody User user) {
|
|
|
R r = new R(); // 创建响应对象
|
|
|
|
|
|
// 调用服务层更新用户
|
|
|
int ans = userService.update(user);
|
|
|
|
|
|
// 根据返回结果设置响应
|
|
|
if (ans >= 0) {
|
|
|
return r.setCode(2000).setMsg(HttpMsg.UPDATE_USER_OK); // 更新成功
|
|
|
}
|
|
|
|
|
|
return r.setCode(4000).setMsg(HttpMsg.UPDATE_USER_FAILED); // 更新失败
|
|
|
}
|
|
|
|
|
|
/**
|
|
|
* 删除用户
|
|
|
* 根据用户ID删除用户记录。
|
|
|
*
|
|
|
* @param id 用户ID
|
|
|
* @return 删除结果
|
|
|
*/
|
|
|
@DeleteMapping("/delete")
|
|
|
R delete(@RequestParam("id") int id) {
|
|
|
R r = new R(); // 创建响应对象
|
|
|
|
|
|
// 调用服务层删除用户
|
|
|
int ans = userService.delete(id);
|
|
|
|
|
|
// 根据返回结果设置响应
|
|
|
if (ans == 1) {
|
|
|
return r.setCode(2000).setMsg(HttpMsg.DELETE_USER_OK); // 删除成功
|
|
|
}
|
|
|
|
|
|
return r.setCode(4000).setMsg(HttpMsg.DELETE_USER_FAILED); // 删除失败
|
|
|
}
|
|
|
}
|