package com.service.schoolhome;

import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.model.Book;
import com.model.Collection;
import com.model.Comment;
import com.service.OrgService;
import com.tools.ISBNTools;
import com.utils.CommonUtils;
import org.apache.commons.lang3.StringUtils;

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

public class BookSearchService {
    public static final BookSearchService me = new BookSearchService();
    private Collection collectionDao = new Collection().dao();
    private Comment commentDao = new Comment().dao();

    public List<Record> popularSearch(int orgid){
        String select = "SELECT b.title,a.bookid ";
        StringBuffer where = new StringBuffer(" FROM t_popular_search a " +
                "LEFT JOIN t_book b ON a.bookid = b.id WHERE a.orgid = ? ORDER BY a.number LIMIT 0,5");
        List<Record> r = Db.find(select+where.toString(),orgid);
        return r;
    }
    public Page<Record> bookSearch(int pageNumber,int pagesize,Integer orgid,String countent) {

        String select = "SELECT a.title,a.author,DATE_FORMAT(a.publisher,'%Y-%m-%d') as publisher,a.id as bookid,a.typecode,a.imgurl ";
        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 ");
        List list = new ArrayList();
        if(orgid>0){
            where.append(" AND a.orgid = ? ");
            list.add(orgid);
        }
        if(StringUtils.isNotBlank(countent)){
            where.append(" and (a.title like ? or a.isbn like ? or a.author like ? or a.publisher like ?) ");
            list.add("%"+countent+"%");
            list.add("%"+ISBNTools.fmtisbn(countent)+"%");
            list.add("%"+countent+"%");
            list.add("%"+countent+"%");
        }
        where.append(" GROUP BY a.id");
        Page<Record> page =  Db.paginate(pageNumber, pagesize,select,where.toString(), CommonUtils.listToArray(list));
        return page;
    }

    public Page<Record> bookSenSearch(int pageNumber, int pagesize, Integer orgid, String title,String author,String isbn,String keyword,String publisher,String typecode,String stay) {
        String select = "SELECT a.title,a.author,DATE_FORMAT(a.publisher,'%Y-%m-%d') as publisher,a.id as bookid,a.typecode,a.imgurl ";
        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 ");
        List list = new ArrayList();
        if(orgid > 0){
            where.append(" AND a.orgid = ? ");
            list.add(orgid);
        }
        if(StringUtils.isNotBlank(title)){
            where.append(" and a.title like ?");
            list.add("%"+title+"%");
        }
        if(StringUtils.isNotBlank(author)){
            where.append(" and a.author like ?");
            list.add("%"+author+"%");
        }
        if(StringUtils.isNotBlank(isbn)){
            where.append(" and a.isbn like ?");
            list.add("%"+ ISBNTools.fmtisbn(isbn)+"%");
        }
        if(StringUtils.isNotBlank(publisher)){
            where.append(" and a.publisher like ? ");
            list.add("%"+publisher+"%");
        }
        if(StringUtils.isNotBlank(keyword)){
            where.append(" and a.keyword like ? ");
            list.add("%"+keyword+"%");
        }
        if(StringUtils.isNotBlank(typecode)){
            where.append(" and a.typecode like ? ");
            list.add("%"+typecode+"%");
        }
        if(StringUtils.isNotBlank(stay)){
            where.append(" and b.status = 0 ");
        }
        where.append(" GROUP BY a.id");
        Page<Record> page =  Db.paginate(pageNumber, pagesize,select,where.toString(), CommonUtils.listToArray(list));
        return page;
    }

    public Record bookInfo(Integer bookid){
        Record r = Db.findFirst("SELECT a.title,a.author,DATE_FORMAT(a.publisher,'%Y-%m-%d') as publisher,a.id as bookid,a.typecode,a.publishdate,a.imgurl," +
                "SUM(CASE WHEN b.status=0 THEN 1 ELSE 0 END) AS zgs,SUM(CASE WHEN b.status=1 THEN 1 ELSE 0 END) AS jcs " +
                "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.id =?",bookid);
        //记录热门搜索
        Integer orgid = PropKit.getInt("orgid");
        if(orgid>0){
            Record r1 = Db.findFirst("SELECT COUNT(*) as num FROM t_popular_search WHERE bookid = ?",bookid);
            int n = r1.getInt("num");
            if(n>0){
                Db.update("UPDATE t_popular_search SET number = number+1 WHERE bookid = ?",bookid);
            }else {
                Db.update("INSERT INTO t_popular_search (bookid,number,orgid) VALUES (?,1,?)",bookid,orgid);
            }
        }
        return r;
    }
    public boolean collection(Integer bookid,Integer orgid,Integer readerid,Integer flag){
        Collection c = collectionDao.findFirst("SELECT id FROM t_collection WHERE bookid = ? AND readerid = ? AND flag = ?",bookid,readerid,flag);
        if(c != null){
            return false;
        }else {
            Collection collection = new Collection();
            collection.setBookid(bookid).setFlag(flag).setOrgid(orgid).setReaderid(readerid).setCreatetime(new Date()).save();
            return true;
        }
    }

    public Page<Record> comment(int pageNumber, int pagesize, String bookno,Integer flag){
        String select = "SELECT a.score,a.content,DATE_FORMAT(a.createtime,'%Y-%m-%d') as createtime,b.realname ";
        StringBuffer where = new StringBuffer(" FROM t_comment a LEFT JOIN t_reader b ON a.readerid = b.id WHERE a.bookno = ? AND a.flag = ?");

        Page<Record> c = Db.paginate(pageNumber,pagesize,select,where.toString(),bookno,flag);
        return c;
    }

    public Double readerScore(String bookno,Integer flag){
        Record r = Db.findFirst("SELECT AVG(score) as score FROM t_comment WHERE bookno = ? AND flag = ? ",bookno,flag);
        Double score = r.getDouble("score");
        return score;
    }
}
