package com.service;


import com.config.Constants;
import com.jfinal.aop.Before;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.tx.Tx;
import com.model.Book;
import com.model.BookYubian;
import com.tools.ISBNTools;
import com.utils.CommonUtils;
import com.utils.MarcUtil;
import org.apache.commons.lang3.StringUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.Map;

/**
 * 图书
 * 2018-10-23
 * wush
 */
public class BookYubianService {

	public static final BookYubianService me = new BookYubianService();

	BookYubian dao =new BookYubian().dao();

	/**
	 * 通过机构id和isbn获取预编图书列表
	 * @param orgid
	 * @param isbnCode
	 */
	public List<Record> findByISBN(int orgid,String isbnCode){
		List<Record> bookList = Db.find("select '' as 'bean.id','' as 'beanPlus.id',isbn 'bean.isbn',title 'bean.title',bltitle 'beanPlus.bltitle',subtitle 'beanPlus.subtitle',author 'bean.author',otherauthor 'beanPlus.otherauthor'\n" +
				",workmodeid 'beanPlus.workmodeid',guobieid 'beanPlus.guobieid',publisher 'bean.publisher',publishaddress 'bean.publishaddress',publishdate 'bean.publishdate',edition 'bean.edition'\n" +
				",price 'bean.price',sizeid 'beanPlus.sizeid',languageid 'beanPlus.languageid',totalpages 'beanPlus.totalpages',bindingid 'beanPlus.bindingid'\n" +
				",congshuming 'beanPlus.congshuming',congshuauthor 'beanPlus.congshuauthor',newsource 'bean.newsource',keyword 'bean.keyword',fuzhu 'beanPlus.fuzhu'\n" +
				",typecode 'bean.typecode',shucihao 'bean.shucihao',juanci 'beanPlus.juanci',ceci 'beanPlus.ceci' from t_book_yubian where (isbn=? or isbn=?) and orgid =?",isbnCode,isbnCode.replace("-",""),orgid);
		return bookList;
	}

	/**
	 * 通过机构id和正题名获取预编图书列表
	 * @param orgid
	 * @param ztmStr
	 */
	public List<Record> findByZTM(int orgid,String ztmStr){
		List<Record> bookList = Db.find("select isbn 'bean.isbn',title 'bean.title',bltitle 'beanPlus.bltitle',subtitle 'beanPlus.subtitle',author 'bean.author',otherauthor 'beanPlus.otherauthor'" +
				",workmodeid 'beanPlus.workmodeid',guobieid 'beanPlus.guobieid',publisher 'bean.publisher',publishaddress 'bean.publishaddress',publishdate 'bean.publishdate',edition 'bean.edition'" +
				",price 'bean.price',sizeid 'beanPlus.sizeid',languageid 'beanPlus.languageid',totalpages 'beanPlus.totalpages',bindingid 'beanPlus.bindingid'" +
				",congshuming 'beanPlus.congshuming',congshuauthor 'beanPlus.congshuauthor',newsource 'bean.newsource',keyword 'bean.keyword',fuzhu 'beanPlus.fuzhu'" +
				",typecode 'bean.typecode',shucihao 'bean.shucihao',juanci 'beanPlus.juanci',ceci 'beanPlus.ceci' from t_book_yubian where title like ? and orgid =?","%"+ztmStr+"%",orgid);
		return bookList;
	}

	public Page<Record> paginate(int pageNumber,int pagesize, String timesortd, String timesorta, BookYubian bean, String bookstatus){
		String select = "SELECT * ";
		StringBuilder where = new StringBuilder(" from t_book_yubian  where 1=1 ");


		List<Object> paramList =new ArrayList<Object>();
		if (bean.getIsbn() !=null && !bean.getIsbn().equals("")){
			where.append(" and isbn = ? ");
			paramList.add(ISBNTools.fmtisbn(bean.getIsbn()));
		}

		if (bean.getTitle() !=null && !bean.getTitle().equals("")){
			where.append(" and title like '%"+bean.getTitle()+"%' ");
		}

		if (bean.getAuthor() !=null && !bean.getAuthor().equals("")){
			where.append(" and author like '%"+bean.getAuthor()+"%' ");
		}
		if (bean.getPublisher() !=null && !bean.getPublisher().equals("")){
			where.append(" and publisher like '%"+bean.getPublisher()+"%' ");
		}
		if (bean.getBookno() !=null && !bean.getBookno().equals("")){
			where.append(" and bookno =? ");
			paramList.add(bean.getBookno());
		}
		if (bean.getTypeid() !=null){
			where.append(" and typeid =? ");
			paramList.add(bean.getTypeid());
		}
		if (bean.getOrgid() !=null ){
			where.append(" and orgid =? ");
			paramList.add(bean.getOrgid());
		}
		where.append(" order by id desc");
		return Db.paginate(pageNumber, pagesize,select,where.toString(),paramList.toArray(new Object[0]));
	}

	public void deleteByID(Integer id){
		dao.deleteById(id);
	}

	public void saveOrUpdate(BookYubian bookYubian){
		if (bookYubian.getId() ==null){
			bookYubian.save();
		}else {
			bookYubian.update();
		}
	}

	public BookYubian findBookYubianByID(Integer id){
		return	dao.findById(id);
	}

	@Before(Tx.class)
	public void uploadMarc(List<String> bookLineStrList,List<Record> expRecordList,Map<String,String> dictExtMap,int orgid) throws UnsupportedEncodingException {

	}
}
