图书管理
package com.book.manager.controller;
import com.book.manager.entity.Book;
import com.book.manager.service.BookService;
import com.book.manager.util.R;
import com.book.manager.util.http.CodeEnum;
import com.book.manager.util.ro.PageIn;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
/**
• @Description 用户管理
*/
@Api(tags = “图书管理”)
@RestController
@RequestMapping(“/book”)
public class BookController {
@Autowired
private BookService bookService;
@ApiOperation(“图书搜索列表”)
@PostMapping(“/list”)
public R getBookList(@RequestBody PageIn pageIn) {
if (pageIn == null) {
return R.fail(CodeEnum.PARAM_ERROR);
}
return R.success(CodeEnum.SUCCESS,bookService.getBookList(pageIn));
}
@ApiOperation(“添加图书”)
@PostMapping(“/add”)
public R addBook(@RequestBody Book book) {
return R.success(CodeEnum.SUCCESS,bookService.addBook(book));
}
@ApiOperation(“编辑图书”)
@PostMapping(“/update”)
public R modifyBook(@RequestBody Book book) {
return R.success(CodeEnum.SUCCESS,bookService.updateBook(book));
}
@ApiOperation(“图书详情”)
@GetMapping(“/detail”)
public R bookDetail(Integer id) {
return R.success(CodeEnum.SUCCESS,bookService.findBookById(id));
}
@ApiOperation(“图书详情 根据ISBN获取”)
@GetMapping(“/detailByIsbn”)
public R bookDetailByIsbn(String isbn) {
return R.success(CodeEnum.SUCCESS,bookService.findBookByIsbn(isbn));
}
@ApiOperation(“删除图书”)
@GetMapping(“/delete”)
public R delBook(Integer id) {
bookService.deleteBook(id);
return R.success(CodeEnum.SUCCESS);
}
}
借阅管理
package com.book.manager.controller;
import cn.hutool.core.date.DateUtil;
import com.book.manager.entity.Borrow;
import com.book.manager.service.BookService;
import com.book.manager.service.BorrowService;
import com.book.manager.util.R;
import com.book.manager.util.consts.Constants;
import com.book.manager.util.http.CodeEnum;
import com.book.manager.util.ro.RetBookIn;
import com.book.manager.util.vo.BackOut;
import com.book.manager.util.vo.BookOut;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
/**
• @Description 用户管理
*/
@Api(tags = “借阅管理”)
@RestController
@RequestMapping(“/borrow”)
public class BorrowController {
@Autowired
private BorrowService borrowService;
@Autowired
private BookService bookService;
@ApiOperation(“借阅列表”)
@GetMapping(“/list”)
public R getBorrowList(Integer userId) {
return R.success(CodeEnum.SUCCESS,borrowService.findAllBorrowByUserId(userId));
}
@ApiOperation(“借阅图书”)
@PostMapping(“/add”)
public R addBorrow(@RequestBody Borrow borrow) {
Integer result = borrowService.addBorrow(borrow);
if (result == Constants.BOOK_BORROWED) {
return R.success(CodeEnum.BOOK_BORROWED);
}else if (result == Constants.USER_SIZE_NOT_ENOUGH) {
return R.success(CodeEnum.USER_NOT_ENOUGH);
}else if (result == Constants.BOOK_SIZE_NOT_ENOUGH) {
return R.success(CodeEnum.BOOK_NOT_ENOUGH);
}
return R.success(CodeEnum.SUCCESS,Constants.OK);
}
@ApiOperation(“编辑借阅”)
@PostMapping(“/update”)
public R modifyBorrow(@RequestBody Borrow borrow) {
return R.success(CodeEnum.SUCCESS,borrowService.updateBorrow(borrow));
}
@ApiOperation(“借阅详情”)
@GetMapping(“/detail”)
public R borrowDetail(Integer id) {
return R.success(CodeEnum.SUCCESS,borrowService.findById(id));
}
@ApiOperation(“删除归还记录”)
@GetMapping(“/delete”)
public R delBorrow(Integer id) {
borrowService.deleteBorrow(id);
return R.success(CodeEnum.SUCCESS);
}
@ApiOperation(“已借阅列表”)
@GetMapping(“/borrowed”)
public R borrowedList(Integer userId) {
List outs = new ArrayList<>();
if (userId!=null&&userId>0) {
// 获取所有 已借阅 未归还书籍
List borrows = borrowService.findBorrowsByUserIdAndRet(userId, Constants.NO);
for (Borrow borrow : borrows) {
BackOut backOut = new BackOut();
BookOut out = bookService.findBookById(borrow.getBookId());
BeanUtils.copyProperties(out,backOut);
backOut.setBorrowTime(DateUtil.format(borrow.getCreateTime(),Constants.DATE_FORMAT));
String endTimeStr = DateUtil.format(borrow.getEndTime(), Constants.DATE_FORMAT);
backOut.setEndTime(endTimeStr);
// 判断是否逾期
String toDay = DateUtil.format(new Date(), Constants.DATE_FORMAT);
int i = toDay.compareTo(endTimeStr);
if (i>0) {
backOut.setLate(Constants.YES_STR);
}else {
backOut.setLate(Constants.NO_STR);
}
outs.add(backOut);
}
}
return R.success(CodeEnum.SUCCESS,outs);
}
@ApiOperation(“归还书籍”)
@PostMapping(“/ret”)
public R retBook(Integer userId, Integer bookId) {
// 归还图书
borrowService.retBook(userId,bookId);
return R.success(CodeEnum.SUCCESS);
}
}
用户管理
package com.book.manager.controller;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.StrUtil;
import com.book.manager.entity.Users;
import com.book.manager.service.UserService;
import com.book.manager.util.R;
import com.book.manager.util.consts.Constants;
import com.book.manager.util.consts.ConvertUtil;
import com.book.manager.util.http.CodeEnum;
import com.book.manager.util.vo.PageOut;
import com.book.manager.util.ro.PageIn;
import com.book.manager.util.vo.UserOut;
import com.github.pagehelper.PageInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/**
• @Description 用户管理
*/
@Api(tags = “用户管理”)
@RestController
@RequestMapping(“/user”)
public class UsersController {
@Autowired
private UserService userService;
@ApiOperation(“用户列表”)
@PostMapping(“/list”)
public R getUsers(@RequestBody PageIn pageIn) {
if (pageIn == null) {
return R.fail(CodeEnum.PARAM_ERROR);
}
// 封装分页出参对象
PageInfo userList = userService.getUserList(pageIn);
PageOut pageOut = new PageOut();
pageOut.setCurrPage(userList.getPageNum());
pageOut.setPageSize(userList.getPageSize());
pageOut.setTotal((int) userList.getTotal());
List outs = new ArrayList<>();
for (Users users : userList.getList()) {
UserOut out = new UserOut();
BeanUtils.copyProperties(users,out);
out.setIdent(ConvertUtil.identStr(users.getIdentity()));
out.setBirth(DateUtil.format(users.getBirthday(),Constants.DATE_FORMAT));
outs.add(out);
}
pageOut.setList(outs);
return R.success(CodeEnum.SUCCESS,pageOut);
}
// @ApiOperation(“添加用户”)
// @PostMapping(“/add”)
// public R addUsers(@RequestBody Users users) {
// return R.success(CodeEnum.SUCCESS,userService.addUser(users));
// }
@ApiOperation(“添加读者”)
@PostMapping(“/addReader”)
public R addReader(@RequestBody Users users) {
if (users == null) {
return R.fail(CodeEnum.PARAM_ERROR);
}
// 读者默认是普通用户
users.setIsAdmin(1);
return R.success(CodeEnum.SUCCESS,userService.addUser(users));
}
@ApiOperation(“添加管理员”)
@PostMapping(“/addAdmin”)
public R addAdmin(@RequestBody Users users) {
if (users == null) {
return R.fail(CodeEnum.PARAM_ERROR);
}
// 设置管理员权限
users.setIsAdmin(0);
return R.success(CodeEnum.SUCCESS,userService.addUser(users));
}
@ApiOperation(“编辑用户”)
@PostMapping(“/update”)
public R modifyUsers(@RequestBody Users users) {
return R.success(CodeEnum.SUCCESS,userService.updateUser(users));
}
@ApiOperation(“用户详情”)
@GetMapping(“/detail”)
public R userDetail(Integer id) {
Users user = userService.findUserById(id);
if (user!=null) {
UserOut out = new UserOut();
BeanUtils.copyProperties(user,out);
out.setBirth(DateUtil.format(user.getBirthday(),Constants.DATE_FORMAT));
out.setIdent(ConvertUtil.identStr(user.getIdentity()));
return R.success(CodeEnum.SUCCESS,out);
}
return R.fail(CodeEnum.NOT_FOUND);
}
@ApiOperation(“删除用户”)
@GetMapping(“/delete”)
public R delUsers(Integer id) {
userService.deleteUser(id);
return R.success(CodeEnum.SUCCESS);
}
@ApiOperation(“获取当前用户登陆信息”)
@GetMapping(“/currUser”)
public R getCurrUser() {
Object principal = SecurityContextHolder.getContext().getAuthentication().getPrincipal();
if (principal!=null) {
Map<String,Object> map = BeanUtil.beanToMap(principal);
String username = (String) map.get(“username”);
if (StrUtil.isNotBlank(username)) {
Users users = userService.findByUsername(username);
UserOut out = new UserOut();
BeanUtils.copyProperties(users,out);
out.setBirth(DateUtil.format(users.getBirthday(),Constants.DATE_FORMAT));
Integer identity = users.getIdentity();
String ident = “”;
if (identity == Constants.STUDENT) {
ident = Constants.STU_STR;
}else if (identity == Constants.TEACHER) {
ident = Constants.TEA_STR;
}else if (identity == Constants.OTHER) {
ident = Constants.OTHER_STR;
}else if (identity == Constants.ADMIN) {
ident = Constants.ADMIN_STR;
}
out.setIdent(ident);
return R.success(CodeEnum.SUCCESS,out);
}
}
return R.fail(CodeEnum.USER_NOT_FOUND);
}
}
08 使用说明