package com.service.schoolhome;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.model.Org;
import com.service.OrgService;
import com.utils.CommonUtils;
import org.apache.commons.lang3.StringUtils;

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

public class CollectionSearchService {
    public static final CollectionSearchService me = new CollectionSearchService();
    private Org orgDao = new Org().dao();


    public Page<Record> booksRetrieval(int pageNumber,int pagesize,Integer orgid,String title) throws UnsupportedEncodingException {

        String select = "SELECT a.title,a.author,a.publisher,a.bookno,DATE_FORMAT(a.createtime,'%Y-%m-%d') as createtime,a.id as bookid ";
        StringBuilder where = new StringBuilder(" FROM t_book a INNER JOIN t_book_detail b ON a.id = b.bookid " +
                " WHERE b.bookstatus = 1 AND b.`status` != 3 AND a.orgid = ? ");
        List list = new ArrayList();
        list.add(orgid);
        if(StringUtils.isNotBlank(title)){
            //URLDecoder.decode(title,"UTF-8");
            where.append(" and a.title like ? ");
            list.add("%"+title.trim()+"%");
        }
        where.append(" GROUP BY a.id");
        Page<Record> page =  Db.paginate(pageNumber, pagesize,select,where.toString(), CommonUtils.listToArray(list));
        return page;
    }

    public Page<Record> booksRetrievalDetail(int pageNumber,int pagesize,Integer bookid) {

        String select = "SELECT a.barcode,b.library,a.`status`, " +
                "CASE WHEN a.`status` = '0' THEN '在馆' " +
                "WHEN a.`status` = '1' THEN '借出' " +
                "WHEN a.`status` = '2' THEN '已预约' " +
                "WHEN a.`status` = '4' THEN '已丢失' " +
                "ELSE '其他' END AS st,ifnull(bookshelfinfoStr,'') bookshelfinfoStr ";
        StringBuilder where = new StringBuilder(" FROM t_book_detail a LEFT JOIN t_library b ON a.libraryid = b.id " +
                "WHERE bookstatus = 1 AND status != 3 AND bookid = ?");
        List list = new ArrayList();
        list.add(bookid);
        Page<Record> page =  Db.paginate(pageNumber, pagesize,select,where.toString(), CommonUtils.listToArray(list));
        return page;
    }

    public Page<Record> loanRanking(int pageNumber, int pageSize,Integer orgid){
        String select = "SELECT a.bookno,a.title,a.author,COUNT(c.id) as jieyuenums  ";
        StringBuilder where = new StringBuilder(" FROM t_book a " +
                "INNER JOIN t_book_detail b ON a.id = b.bookid INNER JOIN t_jieyue c ON b.id = c.bookdetailid " +
                "WHERE a.orgid = ? ");
        List values = new ArrayList();
        values.add(orgid);
        where.append(" GROUP BY a.id ORDER BY jieyuenums DESC");
        Page<Record> page = Db.paginate(pageNumber,pageSize,select,where.toString(),CommonUtils.listToArray(values));
        return page;
    }

    public Record libraryInfo(Integer orgid){
        String select = "SELECT * ";
        StringBuilder where = new StringBuilder(" FROM t_libraryinfo WHERE 1=1 ");
        List values = new ArrayList();
        where.append("AND orgid = ? LIMIT 0,1");
        values.add(orgid);
        return Db.findFirst(select+where.toString(),CommonUtils.listToArray(values));
    }

    public Record rulesInfo(Integer orgid){
        String select = "SELECT title,content,createtime ";
        StringBuilder where = new StringBuilder(" FROM t_rules WHERE 1=1 ");
        List values = new ArrayList();
        where.append("AND orgid = ? LIMIT 0,1");
        values.add(orgid);
        return Db.findFirst(select+where.toString(),CommonUtils.listToArray(values));
    }

    public Page<Record> newestNotice(int pageNumber, int pageSize,Integer orgid){
        String select = "SELECT id,title,DATE_FORMAT(createtime,'%Y-%m-%d') as createtime ";
        StringBuilder where = new StringBuilder(" FROM t_news WHERE `status` = 1 ");
        List values = new ArrayList();
        if(orgid!=null){
            where.append("AND orgid = ? ");
            values.add(orgid);
        }
        where.append(" ORDER BY createtime DESC");
        Page<Record> page = Db.paginate(pageNumber,pageSize,select,where.toString(),CommonUtils.listToArray(values));
        return page;
    }

    public Record newestNoticeInfo(Integer id){
        String select = "SELECT title,content,DATE_FORMAT(createtime,'%Y-%m-%d') as createtime ";
        StringBuilder where = new StringBuilder(" FROM t_news WHERE 1=1 ");
        List values = new ArrayList();
        where.append("AND id = ? ");
        values.add(id);
        return Db.findFirst(select+where.toString(),CommonUtils.listToArray(values));
    }

    public Page<Record> newBooksBulletins(int pageNumber, int pageSize,Integer orgid){
        String select = "SELECT a.id,b.title,b.publisher,b.author,a.recommendstatus,DATE_FORMAT(a.createtime,'%Y-%m-%d') as createtime ";
        StringBuilder where = new StringBuilder(" FROM t_schoolbookrecommend a LEFT JOIN t_book b ON a.bookid = b.id " +
                "WHERE a.flag = 0 AND a.deletestatus = 0 ");
        List values = new ArrayList();

        where.append("AND a.orgid = ? ");
        values.add(orgid);

        where.append(" ORDER BY a.createtime DESC");
        Page<Record> page = Db.paginate(pageNumber,pageSize,select,where.toString(),CommonUtils.listToArray(values));
        return page;
    }

    public Record newBooksDetail(Integer id){
        String select = "SELECT b.title,b.publisher,b.author,a.recommendstatus," +
                "DATE_FORMAT(a.createtime,'%Y-%m-%d') as createtime,a.authorsynopsis,a.contentsynopsis,a.recommendreason ";
        StringBuilder where = new StringBuilder(" FROM t_schoolbookrecommend a LEFT JOIN t_book b ON a.bookid = b.id " +
                "WHERE a.flag = 0 AND a.deletestatus = 0 ");
        List values = new ArrayList();
        where.append("AND a.id = ? ");
        values.add(id);
        return Db.findFirst(select+where.toString(),CommonUtils.listToArray(values));
    }

}
