首页>代码>基于SSM实现的图书借阅管理系统>/代码/library/src/com/library/controller/AdminController.java
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 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 AdminController {

	@Autowired
	private UserService userService;
	@Autowired
	private BookService bookService;
	@Autowired
	private BookTypeService bookTypeService;
	@Autowired
	private BookBorrowService bookBorrowService;

	@RequestMapping("admin_home")
	public String adminHome(HttpServletRequest request) {
		List<User> users = userService.getUserAll(null);
		List<Book> books = bookService.getBookAll(null, null);
		request.setAttribute("users", users);
		request.setAttribute("books", books);
		return "admin/admin_home";
	}

	@RequestMapping("admin_user_list")
	public String adminUserList(HttpServletRequest request) {
		String key = request.getParameter("key");
		List<User> users = userService.getUserAll(key);
		if (key != null && !key.isEmpty()) {
			request.setAttribute("key", key);
		}
		request.setAttribute("users", users);
		return "admin/admin_user_list";
	}

	@RequestMapping("admin_user_edit")
	public String adminUserEdit(HttpServletRequest request) {
		String id = request.getParameter("id");
		if (id != null && !id.isEmpty()) {
			User user = userService.getUser(Integer.valueOf(id));
			if (user != null) {
				request.setAttribute("example", user);
				return "admin/admin_user_edit";
			}
		}
		String key = request.getParameter("key");
		List<User> users = userService.getUserAll(key);
		if (key != null && !key.isEmpty()) {
			request.setAttribute("key", key);
		}
		request.setAttribute("users", users);
		return "admin/admin_user_list";
	}

	@RequestMapping("admin_user_update")
	public String adminUserUpdate(HttpServletRequest request) {
		String id = request.getParameter("id");
		String sex = request.getParameter("sex");
		String telephone = request.getParameter("telephone");
		String address = request.getParameter("address");
		if (id != null && !id.isEmpty()) {
			User user = userService.getUser(Integer.valueOf(id));
			if (user != null) {
				user.setSex(sex);
				user.setTelephone(telephone);
				user.setAddress(address);
				userService.updateUser(user);
			}
		}
		String key = request.getParameter("key");
		List<User> users = userService.getUserAll(key);
		if (key != null && !key.isEmpty()) {
			request.setAttribute("key", key);
		}
		request.setAttribute("users", users);
		return "admin/admin_user_list";
	}

	@RequestMapping("admin_user_delete")
	public String adminUserDelete(HttpServletRequest request) {
		String id = request.getParameter("id");
		String key = request.getParameter("key");
		if (id != null && !id.isEmpty()) {
			User user = userService.getUser(Integer.valueOf(id));
			if (user != null) {
				userService.deleteUser(user.getId());
			}
		}
		List<User> users = userService.getUserAll(key);
		if (key != null && !key.isEmpty()) {
			request.setAttribute("key", key);
		}
		request.setAttribute("users", users);
		return "admin/admin_user_list";
	}

	@RequestMapping("admin_type")
	public String adminType(HttpServletRequest request) {
		List<Booktype> booktypes = bookTypeService.getBooktypeAll();
		request.setAttribute("booktypes", booktypes);
		return "admin/admin_type_list";
	}

	@RequestMapping("admin_type_add")
	public String adminTypeAdd(HttpServletRequest request) {
		String typename = request.getParameter("typename");
		if (typename == null || typename.isEmpty()) {
			return "admin/admin_type_add";
		}
		Booktype booktype = new Booktype();
		booktype.setTypename(typename);
		bookTypeService.addBooktype(booktype);
		List<Booktype> booktypes = bookTypeService.getBooktypeAll();
		request.setAttribute("booktypes", booktypes);
		return "admin/admin_type_list";
	}

	@RequestMapping("admin_type_delete")
	public String adminTypeDelete(HttpServletRequest request) {
		String id = request.getParameter("id");
		if (id != null && !id.isEmpty()) {
			Booktype booktype = bookTypeService.getBooktype(Integer.valueOf(id));
			if (booktype != null) {
				bookTypeService.deleteBooktype(booktype.getId());
			}
		}
		List<Booktype> booktypes = bookTypeService.getBooktypeAll();
		request.setAttribute("booktypes", booktypes);
		return "admin/admin_type_list";
	}

	@RequestMapping("admin_book_list")
	public String adminBookList(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 "admin/admin_book_list";
	}

	@RequestMapping("admin_book_add")
	public String adminBookAdd(HttpServletRequest request) {
		List<Booktype> booktypes = bookTypeService.getBooktypeAll();
		request.setAttribute("booktypes", booktypes);

		String bookname = request.getParameter("bookname");
		String author = request.getParameter("author");
		String introduce = request.getParameter("introduce");
		String surplus = request.getParameter("surplus");
		String type = request.getParameter("type");
		String headUrl = "/static/images/touxiang/01.jpg";
		if (bookname == null || bookname.isEmpty()) {
			return "admin/admin_book_add";
		}
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) (request);
		MultipartFile file = multipartRequest.getFile("file");
		if (!file.getOriginalFilename().isEmpty()) {
			String path = request.getServletContext().getRealPath("/") + "page/static/images/picture/";
			String name = String.valueOf(System.currentTimeMillis() + "_" + file.getOriginalFilename());
			File destFile = new File(path, name);
			try {
				file.transferTo(destFile);
			} catch (IllegalStateException | IOException e) {
				e.printStackTrace();
			}
			headUrl = "/images/picture/" + name;
		}

		Book book = new Book();
		book.setBookname(bookname);
		book.setAuthor(author);
		book.setIntroduce(introduce);
		book.setSurplus(surplus);
		book.setType(type);
		book.setPicture(headUrl);
		book.setBorrow("0");
		book.setComment("0");
		bookService.addBook(book);
		List<Book> books = bookService.getBookAll(null, null);
		request.setAttribute("books", books);
		return "admin/admin_book_list";
	}

	@RequestMapping("admin_book_edit")
	public String adminBookEdit(HttpServletRequest request) {
		List<Booktype> booktypes = bookTypeService.getBooktypeAll();
		request.setAttribute("booktypes", booktypes);

		String id = request.getParameter("id");
		String key = request.getParameter("key");
		String bookname = request.getParameter("bookname");
		String author = request.getParameter("author");
		String introduce = request.getParameter("introduce");
		String surplus = request.getParameter("surplus");
		String type = request.getParameter("type");
		String pictureUrl = request.getParameter("picture");
		if (id == null || id.isEmpty()) {
			List<Book> books = bookService.getBookAll(key, type);
			request.setAttribute("books", books);
			return "admin/admin_book_list";
		}
		Book book = bookService.getBook(Integer.valueOf(id));
		if (book == null) {
			List<Book> books = bookService.getBookAll(key, type);
			request.setAttribute("books", books);
			return "admin/admin_book_list";
		}
		if (bookname == null || bookname.isEmpty()) {
			request.setAttribute("book", book);
			return "admin/admin_book_edit";
		}
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) (request);
		MultipartFile file = multipartRequest.getFile("file");
		if (!file.getOriginalFilename().isEmpty()) {
			String path = request.getServletContext().getRealPath("/") + "page/static/images/picture/";
			String name = String.valueOf(System.currentTimeMillis() + "_" + file.getOriginalFilename());
			File destFile = new File(path, name);
			try {
				file.transferTo(destFile);
			} catch (IllegalStateException | IOException e) {
				e.printStackTrace();
			}
			pictureUrl = "images/picture/" + name;
		}
		book.setBookname(bookname);
		book.setAuthor(author);
		book.setIntroduce(introduce);
		book.setSurplus(surplus);
		book.setType(type);
		book.setPicture(pictureUrl);
		bookService.updateBook(book);
		List<Book> books = bookService.getBookAll(null, null);
		request.setAttribute("books", books);
		return "admin/admin_book_list";
	}

	@RequestMapping("admin_book_delete")
	public String adminBookDelete(HttpServletRequest request) {
		List<Booktype> booktypes = bookTypeService.getBooktypeAll();
		request.setAttribute("booktypes", booktypes);

		String id = request.getParameter("id");
		String key = request.getParameter("key");
		String type = request.getParameter("type");
		if (id != null && !id.isEmpty()) {
			Book book = bookService.getBook(Integer.valueOf(id));
			if (book != null) {
				bookService.deleteBook(book.getId());
			}
		}
		List<Book> books = bookService.getBookAll(key, type);
		request.setAttribute("books", books);
		return "admin/admin_book_list";
	}

	@RequestMapping("admin_borrow")
	public String adminDcShow(HttpServletRequest request) {
		List<Booktype> booktypes = bookTypeService.getBooktypeAll();
		request.setAttribute("booktypes", booktypes);
		List<Bookborrow> bookborrows = bookBorrowService.getBookborrowAll();
		List<Borrow> borrows = new ArrayList<>();
		for (Bookborrow bookborrow : bookborrows) {
			Borrow borrow = new Borrow();
			User user = userService.getUser(bookborrow.getUserid());
			Book book = bookService.getBook(bookborrow.getBookid());
			if (user == null) {
				continue;
			}
			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 "admin/admin_borrow";
	}

}
最近下载更多
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日
n2352441  LV4 3月3日
最近浏览更多
momomo228 2小时前
暂无贡献等级
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日
暂无贡献等级
krispeng  LV13 10月2日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友