package com.library.book.service.impl;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.lang.RandomStringUtils;
import org.apache.poi.hssf.usermodel.HSSFCell;
import org.apache.poi.hssf.usermodel.HSSFRichTextString;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.library.book.dao.BookDAO;
import com.library.book.service.BookService;
import com.library.model.Books;
import com.library.model.Borrow;
import com.library.model.BorrowRecord;
import com.library.util.Page;
/**
* BookService接口的实现(关于BOOK有关的逻辑)
*
* @author 金成杰
*
*/
public class BookServiceImpl implements BookService {
private BookDAO bookDAO;
private String name;
private String editor;
private String press;
private String status;
private String classification;
private Page page;
private Borrow borrow;
public Borrow getBorrow() {
return borrow;
}
public void setBorrow(Borrow borrow) {
this.borrow = borrow;
}
public Page getPage() {
return page;
}
public void setPage(Page page) {
this.page = page;
}
public BookDAO getBookDAO() {
return bookDAO;
}
public void setBookDAO(BookDAO bookDAO) {
this.bookDAO = bookDAO;
}
public List<Books> findBooks(Books book, int page) throws Exception {
name = book.getName();
editor = book.getEditor();
press = book.getPress();
status = book.getStatus();
classification = book.getClassification();
String HQL = "FROM Books WHERE classification = '" + classification
+ "' ";
if (!"".equals(name) && name != null) {
HQL += " and name like'%" + name + "%' ";
}
if (!"".equals(editor) && editor != null) {
HQL += " and editor='" + editor + "'";
}
if (!"请选择".equals(press)) {
HQL += " and press='" + press + "' ";
}
if (!"请选择".equals(status)) {
HQL += " and status='" + status + "' ";
}
return bookDAO.findBooks(HQL, page);
}
public int getPageNum(int count) throws Exception {
int getPageNum = page.pageNum(count);
return getPageNum;
}
public int getCount(Books book) throws Exception {
name = book.getName();
editor = book.getEditor();
press = book.getPress();
status = book.getStatus();
classification = book.getClassification();
String HQL = "FROM Books WHERE classification = '" + classification
+ "' ";
if (!"".equals(name)) {
HQL += " and name like'%" + name + "%' ";
}
if (!"".equals(editor)) {
HQL += " and editor='" + editor + "'";
}
if (!"请选择".equals(press)) {
HQL += " and press='" + press + "' ";
}
if (!"请选择".equals(status)) {
HQL += " and status='" + status + "' ";
}
return bookDAO.getRows(HQL);
}
public void deleBook(String id) throws Exception {
bookDAO.deleBook(id);
}
public void alterBook(Books book) throws Exception {
bookDAO.alterBook(book);
}
public List<Books> findBooks(Books book) throws Exception {
name = book.getName();
editor = book.getEditor();
press = book.getPress();
status = book.getStatus();
classification = book.getClassification();
String HQL = "FROM Books WHERE classification = '" + classification
+ "' ";
if (!"".equals(name) && name != null) {
HQL += " and name like'%" + name + "%' ";
}
if (!"".equals(editor) && editor != null) {
HQL += " and editor='" + editor + "'";
}
if (!"请选择".equals(press)) {
HQL += " and press='" + press + "' ";
}
if (!"请选择".equals(status)) {
HQL += " and status='" + status + "' ";
}
return bookDAO.findAllBooks(HQL);
}
public InputStream getInputStream(Books book) {
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("sheet1");
HSSFRow row = null;
HSSFCell cell = null;
row = sheet.createRow(0);
cell = row.createCell(0);
cell.setCellValue(new HSSFRichTextString("序号"));
cell = row.createCell(1);
cell.setCellValue(new HSSFRichTextString("图书分类"));
cell = row.createCell(2);
cell.setCellValue(new HSSFRichTextString("图书名称"));
cell = row.createCell(3);
cell.setCellValue(new HSSFRichTextString("作者"));
cell = row.createCell(4);
cell.setCellValue(new HSSFRichTextString("出版社"));
cell = row.createCell(5);
cell.setCellValue(new HSSFRichTextString("价格"));
cell = row.createCell(6);
cell.setCellValue(new HSSFRichTextString("状态"));
cell = row.createCell(7);
cell.setCellValue(new HSSFRichTextString("入库时间"));
cell = row.createCell(8);
cell.setCellValue(new HSSFRichTextString("内容提要"));
List<Books> bookList = null;
try {
bookList = findBooks(book);
} catch (Exception e) {
e.printStackTrace();
}
for (int i = 0; i < bookList.size(); i++) {
Books books = bookList.get(i);
row = sheet.createRow(i + 1);
String s = (i + 1) + "";
cell = row.createCell(0);
cell.setCellValue(new HSSFRichTextString(s));
cell = row.createCell(1);
cell
.setCellValue(new HSSFRichTextString(books
.getClassification()));
cell = row.createCell(2);
cell.setCellValue(new HSSFRichTextString(books.getName()));
cell = row.createCell(3);
cell.setCellValue(new HSSFRichTextString(books.getEditor()));
cell = row.createCell(4);
cell.setCellValue(new HSSFRichTextString(books.getPress()));
cell = row.createCell(5);
cell.setCellValue(new HSSFRichTextString(books.getPrice() + ""));
cell = row.createCell(6);
cell.setCellValue(new HSSFRichTextString(books.getStatus()));
cell = row.createCell(7);
cell.setCellValue(new HSSFRichTextString(books.getInTime()
.toString()));
cell = row.createCell(8);
cell.setCellValue(new HSSFRichTextString(books.getContent()));
}
System.err.println(sheet.getRow(1).getCell(5));
String fileName = RandomStringUtils.randomAlphanumeric(10);
fileName = new StringBuffer(fileName).append(".xls").toString();
final File file = new File(fileName);
try {
// 将文件写入OutputStream
OutputStream os = new FileOutputStream(file);
wb.write(os);
os.close();
} catch (Exception e) {
System.err.println("exception");
}
InputStream is = null;
try {
is = new FileInputStream(file);
} catch (FileNotFoundException e) {
System.err.println("FileNotFoundException");
}
// 删除tomcat中的零时文件
new Thread(new Runnable() {
public void run() {
try {
Thread.sleep(15000);
} catch (InterruptedException e) {
e.printStackTrace();
}
file.delete();
}
}).start();
return is;
}
public boolean checkBookNO(String id) throws Exception {
boolean hasBookNO = false;
Books book = bookDAO.getBookMessage(id);
if (book.getBid() != null) {
hasBookNO = true;
}
return hasBookNO;
}
public void saveBorrow(Borrow borrow) throws Exception {
bookDAO.saveBorrow(borrow);
}
public List<Books> findBooks(String bid) throws Exception {
return bookDAO.findBooks(bid);
}
public void returnBooks(String bid) throws Exception {
bookDAO.returnBooks(bid);
}
public List<BorrowRecord> getBorrowRecord(String uid) throws Exception {
List<BorrowRecord> recordList = new ArrayList<BorrowRecord>();
List<Object[]> list = bookDAO.getBorrowRecord(uid);
for (Object[] objects : list) {
BorrowRecord borrowRecord = new BorrowRecord();
borrowRecord.setUserName(objects[0].toString());
borrowRecord.setBid(objects[1].toString());
borrowRecord.setBookName(objects[2].toString());
borrowRecord.setOutTime(objects[3].to
评论19
最新资源