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; @RestController @RequestMapping("user") public class UserController { @Resource private UserService userService; @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); } @RequestMapping("/find") R find(@RequestParam("page") int page, @RequestParam("searchKey") String searchKey) { R r = new R(); Map map = new HashMap<>(); List 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 items = users.subList(start, end); map.put("items", items); map.put("len", totalPages); return r.setCode(2000).setData(map); } @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); } @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); } @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); } }