package com.service;


import com.jfinal.plugin.activerecord.Page;
import com.model.BookHot;
import com.model.Org;

import java.util.Date;
import java.util.List;

/**
 * 图书
 * 2018-10-23
 * zj
 */
/*
     1> 增加一本图书通过扫描条形码和ISBN去资源库带出本书相关信息
     2> 保存书本信息需要保存三张表 t_book,t_book_plus,t_book_detail,其中t_book_plus为书本附加信息（可不填写，但需要保存增加的书本记录）
     3> 同时增加新书时，需要判断该书是否重复，重复时保存复本
 */
public class HotBookService {

	public static final HotBookService me = new HotBookService();
	private BookHot dao = new BookHot().dao();

	public BookHot findById(int id){
		return dao.findById(id);
	}


	public Page<BookHot> paginate(List<Org> orgList, int schooltype, String booktypeid, int pageNumber,int pagesize){
		String select = "SELECT t_book_hot.*,t_booktype.name as booktypename  ";
		StringBuilder where = new StringBuilder(" FROM t_book_hot inner join t_booktype on t_booktype.id=t_book_hot.booktypeid  where t_book_hot.schooltype=?  ");
		Object[] arg =null;
		if (booktypeid.equals("ALL")){
			arg = new Object[]{schooltype};
		}else {
			where.append("and t_book_hot.booktypeid=?");
			arg = new Object[]{schooltype,booktypeid};
		}
		where.append(" and orgid in (");
		for (Org org : orgList) {
			where.append(org.getId()).append(",");
		}
		where.append(" -1) ");


		return dao.paginate(pageNumber, pagesize,select,where.toString(),arg);
	}

	public void deleteById(int id) {
		dao.deleteById(id);
	}

	public void addOrUpdate(BookHot bean,   int operatorid ){
		if(bean.getId() ==null){
			bean.setPublishertime(new Date()).setOperatorid(operatorid).save();
		}else {
			bean.update();
		}
	}


	public void deleteHotBook(List<BookHot> bookHotList){
		for (BookHot bookhot : bookHotList) {
			bookhot.delete();
		}
	}

}
