首页>代码>基于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";
	}

}
最近下载更多
SX520886  LV6 4月24日
dsffssff  LV4 2024年12月25日
zolscy  LV24 2024年11月24日
angaochong  LV5 2024年10月10日
Darchry  LV2 2024年6月29日
lilitu  LV6 2024年5月30日
2017143155  LV12 2024年5月13日
pangzhihui  LV14 2024年4月11日
weilaizhisheng  LV21 2024年3月21日
uid0901  LV2 2024年3月12日
最近浏览更多
SX520886  LV6 4月24日
2022102154  LV1 3月27日
docnnxxy688 3月22日
暂无贡献等级
PLVAE_514  LV2 3月7日
哪里的完整版  LV8 3月6日
dsffssff  LV4 2024年12月25日
MQ-EQW 2024年12月18日
暂无贡献等级
bankroll  LV5 2024年12月16日
2194192290li  LV1 2024年12月9日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友