package com.service;

import com.config.Constants;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.model.Bookpici;
import com.model.Booktype;
import com.utils.CommonUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.*;

/**
 * 图书批次
 * 2018-10-23
 * wush
 */
public class BookPiciService {

	public static final BookPiciService me = new BookPiciService();
	private Bookpici dao = new Bookpici().dao();

	public Page<Record> paginate(int pageNumber,int pagesize, int orgid,String batchname,String realname){
		String select = "SELECT a.*,b.`name`,c.realname ";
		StringBuilder where = new StringBuilder(" from t_bookpici a " +
				"LEFT JOIN t_dict b ON a.dictid = b.id LEFT JOIN t_user c ON a.operatorid = c.id " +
				"WHERE a.orgid = ? ");
		List list = new ArrayList();
		list.add(orgid);
		if(StringUtils.isNotBlank(batchname)){
			where.append("AND a.batchname LIKE ?");
			list.add("%"+batchname+"%");
		}
		if(StringUtils.isNotBlank(realname)){
			where.append(" AND c.realname LIKE ?");
			list.add("%"+realname+"%");
		}
		where.append("ORDER BY createtime desc");
		return Db.paginate(pageNumber, pagesize,select,where.toString(),CommonUtils.listToArray(list));
	}
	public void addOrUpdate(Bookpici bean,int operatorid,int orgid){
		if(bean.getId()!=null){
			bean.update();
		}else {
			bean.setDictid(bean.getDictid()).setRemarks(bean.getRemarks()).setCreatetime(new Date()).setOperatorid(operatorid).setOrgid(orgid).save();
		}
	}
	public boolean deleteById(Bookpici bean){
		if(bean.getId()!=null){
			Record r = Db.findFirst("SELECT COUNT(b.id) as picinum FROM t_bookpici a " +
					"LEFT JOIN t_book_detail b ON a.id = b.piciid WHERE a.id = ?",bean.getId());
			if(r.getInt("picinum")>0){
				return false;
			}else {
				Db.update("DELETE FROM t_bookpici WHERE id = ?",bean.getId());
				return true;

			}
		}else {
			return false;
		}

	}

    /**
     * 根据机构和分类号获取最大书次号
     */
    public Long findMaxShucihao(int orgid,String typecode){
        Record record = Db.findFirst("select ifnull(max(shucihao),0) maxshucihao from t_book where orgid = ? and typecode =?",orgid,typecode);
        return record.getLong("maxshucihao");
    }

    public List<Record> findShucihaoListByTypeCode(int orgid,String typecode){
		String select = "select shucihao,bookno,isbn,title,publisher,author,price ";
		StringBuilder where = new StringBuilder("from t_book b where orgid =? and typecode = ? group by shucihao ORDER BY shucihao desc,createtime desc");
		List list = new ArrayList();
		list.add(orgid);
		list.add(typecode);
		return Db.find(select+where.toString(),CommonUtils.listToArray(list));
	}

	public Map<String,Integer> bookpiciNameToIDMap(int orgid){
		List<Bookpici> booktypeList=dao.find("select id,batchname from t_bookpici where orgid=?",orgid);
		Map<String,Integer> map = new HashMap<String,Integer>();
		for (Bookpici bookpici : booktypeList) {
			map.put(bookpici.getBatchname(),bookpici.getId());
		}
		return map;
	}
}
