package com.library.controller; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpSession; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.context.annotation.Scope; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.multipart.MultipartFile; import org.springframework.web.multipart.MultipartHttpServletRequest; import com.library.model.Book; import com.library.model.Bookborrow; import com.library.model.Booktype; import com.library.model.Borrow; import com.library.model.User; import com.library.service.BookBorrowService; import com.library.service.BookService; import com.library.service.BookTypeService; import com.library.service.UserService; /** * 用户 */ @Controller @Scope(value = "prototype") public class UserController { @Autowired private UserService userService; @Autowired private BookService bookService; @Autowired private BookTypeService bookTypeService; @Autowired private BookBorrowService bookBorrowService; @RequestMapping("user_home") public String userHome(HttpServletRequest request) { List<User> users = userService.getUserAll(null); List<Book> books = bookService.getBookAll(null, null); request.setAttribute("users", users); request.setAttribute("books", books); return "user/user_home"; } @RequestMapping("user_book_list") public String userBookList(HttpServletRequest request) { String key = request.getParameter("key"); String type = request.getParameter("type"); if (key != null && !key.isEmpty()) { request.setAttribute("key", key); } if (type != null && !type.isEmpty()) { request.setAttribute("type", type); } List<Book> books = bookService.getBookAll(key, type); List<Booktype> booktypes = bookTypeService.getBooktypeAll(); request.setAttribute("books", books); request.setAttribute("booktypes", booktypes); return "user/user_book_list"; } @RequestMapping("user_book_details") public String userBookDetails(HttpServletRequest request) { String id = request.getParameter("id"); if (id != null && !id.isEmpty()) { Book book = bookService.getBook(Integer.valueOf(id)); if (book != null) { List<Booktype> booktypes = bookTypeService.getBooktypeAll(); request.setAttribute("book", book); request.setAttribute("booktypes", booktypes); return "user/user_book_details"; } } List<Book> books = bookService.getBookAll(null, null); List<Booktype> booktypes = bookTypeService.getBooktypeAll(); request.setAttribute("books", books); request.setAttribute("booktypes", booktypes); return "user/user_book_list"; } @RequestMapping("user_book_borrow") public String userBookBorrow(HttpServletRequest request, HttpSession session) { String id = request.getParameter("id"); if (id != null && !id.isEmpty()) { Book book = bookService.getBook(Integer.valueOf(id)); if (book != null) { User user = (User) session.getAttribute("user"); Bookborrow bookborrow = new Bookborrow(); bookborrow.setUserid(user.getId()); bookborrow.setBookid(book.getId()); bookBorrowService.addBookborrow(bookborrow); int surplus = Integer.valueOf(book.getSurplus()) - 1; int borrow = Integer.valueOf(book.getBorrow()) + 1; book.setSurplus(String.valueOf(surplus)); book.setBorrow(String.valueOf(borrow)); bookService.updateBook(book); return "redirect:user_borrow.cc"; } } List<Book> books = bookService.getBookAll(null, null); List<Booktype> booktypes = bookTypeService.getBooktypeAll(); request.setAttribute("books", books); request.setAttribute("booktypes", booktypes); return "user/user_book_list"; } @RequestMapping("user_borrow") public String userDcShow(HttpServletRequest request, HttpSession session) { List<Booktype> booktypes = bookTypeService.getBooktypeAll(); request.setAttribute("booktypes", booktypes); User user = (User) session.getAttribute("user"); if (user != null) { List<Bookborrow> bookborrows = bookBorrowService.getBookborrowByUserId(user.getId()); List<Borrow> borrows = new ArrayList<>(); for (Bookborrow bookborrow : bookborrows) { Borrow borrow = new Borrow(); Book book = bookService.getBook(bookborrow.getBookid()); if (book == null) { continue; } borrow.setId(bookborrow.getId()); borrow.setUsername(user.getUsername()); borrow.setTelephone(user.getTelephone()); borrow.setBookname(book.getBookname()); borrow.setAuthor(book.getAuthor()); borrow.setType(book.getType()); borrow.setPicture(book.getPicture()); borrows.add(borrow); } request.setAttribute("borrows", borrows); return "user/user_borrow"; } List<Book> books = bookService.getBookAll(null, null); request.setAttribute("books", books); return "user/user_book_list"; } @RequestMapping("user_borrow_details") public String userBorrowDetails(HttpServletRequest request, HttpSession session) { String id = request.getParameter("id"); if (id != null && !id.isEmpty()) { Bookborrow bookborrow = bookBorrowService.getBookborrow(Integer.valueOf(id)); if (bookborrow != null) { Book book = bookService.getBook(bookborrow.getBookid()); if (book != null) { request.setAttribute("book", book); List<Booktype> booktypes = bookTypeService.getBooktypeAll(); request.setAttribute("booktypes", booktypes); return "user/user_borrow_details"; } } } return "redirect:user_book_list.cc"; } @RequestMapping("user_info") public String userInfo(HttpServletRequest request, HttpSession session) { User user = (User) session.getAttribute("user"); request.setAttribute("example", user); return "user/user_info"; } @RequestMapping("user_info_edit") public String userInfoEdit(HttpServletRequest request, HttpSession session) { User user = (User) session.getAttribute("user"); request.setAttribute("example", user); return "user/user_info_edit"; } @RequestMapping("user_info_update") public String userInfoUpdate(HttpServletRequest request, HttpSession session) { String sex = request.getParameter("sex"); String telephone = request.getParameter("telephone"); String address = request.getParameter("address"); if (telephone == null || telephone.isEmpty()) { return "redirect:user_info_edit.cc"; } User user = (User) session.getAttribute("user"); MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) (request); MultipartFile file = multipartRequest.getFile("file"); if (!file.getOriginalFilename().isEmpty()) { String path = request.getServletContext().getRealPath("/") + "page/static/images/touxiang/"; String name = String.valueOf(System.currentTimeMillis() + "_" + file.getOriginalFilename()); File destFile = new File(path, name); try { file.transferTo(destFile); } catch (IllegalStateException | IOException e) { e.printStackTrace(); } user.setPortrait("images/touxiang/" + name); } user.setSex(sex); user.setTelephone(telephone); user.setAddress(address); userService.updateUser(user); session.setAttribute("user", user); return "redirect:user_info.cc"; } }
最近下载更多
zolscy LV24
11月24日
angaochong LV5
10月10日
Darchry LV2
6月29日
lilitu LV6
5月30日
2017143155 LV12
5月13日
pangzhihui LV14
4月11日
weilaizhisheng LV21
3月21日
uid0901 LV2
3月12日
xiaopengzep LV1
3月11日
微信网友_6427471221870592 LV2
3月10日
最近浏览更多
zolscy LV24
11月24日
momomo228 LV2
11月21日
LoveSummer LV6
10月31日
shunlun8855 LV1
10月29日
yayacui LV2
10月28日
半夏bx LV14
10月12日
angaochong LV5
10月10日
zhangjishao
10月10日
暂无贡献等级
zouzou123 LV3
10月10日
微信网友_7202814962503680
10月9日
暂无贡献等级