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.utils.CommonUtils;

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

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

    /**
     * 藏书借阅排行榜
     */
    public Page<Record> libraryRanking(int pageNumber, int pagesize, String kssj, String jssj, Integer orgid){
        String select = "SELECT b.isbn,b.title,b.author,b.publisher, count(j.id) as jieyuenums ";
        StringBuilder where = new StringBuilder(" FROM t_book b LEFT JOIN t_book_detail d  on b.id=d.bookid " +
                " LEFT JOIN t_jieyue j ON d.id = j.bookdetailid LEFT JOIN t_org o ON b.orgid = o.id " +
                "where j.createtime >= ? AND j.createtime <=? ");
        List list = new ArrayList();
        list.add(kssj+" 00:00:00");
        list.add(jssj+" 23:59:59");
        if(orgid != 0){
            where.append(" AND b.orgid = ? ");
            list.add(orgid);
        }
        where.append(" group by b.isbn,b.title,b.author,b.publisher ORDER BY jieyuenums DESC");
        Page<Record> page = Db.paginate(pageNumber, pagesize,select,where.toString(),CommonUtils.listToArray(list));
        return page;
    }
    /**
     * 读者借阅排行榜
     */
    public Page<Record> readerRanking(int pageNumber,int pagesize, String kssj, String jssj,Integer orgid){
        String select = "SELECT a.realname,b.name as typename,c.deptname,COUNT(d.id) as jieyuenums,e.orgname ";
        StringBuilder where = new StringBuilder(" FROM t_reader a LEFT JOIN t_dict b ON a.readertypeid = b.id " +
                "LEFT JOIN t_org_dept c ON a.orgdeptid = c.id LEFT JOIN t_jieyue d ON a.id = d.readerid " +
                "LEFT JOIN t_org e ON a.orgid = e.id WHERE d.createtime >=? AND d.createtime <=?");
        List list = new ArrayList();
        list.add(kssj+" 00:00:00");
        list.add(jssj+" 23:59:59");
        if(orgid != 0){
            where.append(" AND e.id = ? ");
            list.add(orgid);
        }
        where.append(" GROUP BY d.readerid ORDER BY jieyuenums DESC ");
        Page<Record> page = Db.paginate(pageNumber, pagesize,select,where.toString(),CommonUtils.listToArray(list));
        return page;
    }
    /**
     * 生均借阅排行榜
     */
    public Page<Record> studentRanking(int pageNumber,int pagesize, String kssj, String jssj,Integer orgid){
        String select = "SELECT a.id,a.orgname,(a.students+a.czstudents+a.gzstudents) as students,COUNT(distinct b.id) as readers,COUNT(e.id) as jieyuenums," +
                "IFNULL((COUNT(e.id)/(a.students+a.czstudents+a.gzstudents)),0) as rjjycs  ";
        StringBuilder where = new StringBuilder(" FROM t_org a LEFT JOIN t_reader b ON a.id = b.orgid " +
                "LEFT JOIN t_jieyue e ON b.id = e.readerid " +
                "WHERE b.status != 4 AND a.orgtypeid = 5 AND e.createtime >=? AND e.createtime <=?");
        List list = new ArrayList();
        list.add(kssj+" 00:00:00");
        list.add(jssj+" 23:59:59");

        StringBuffer totalRowSql  = new StringBuffer("SELECT COUNT(*) " +
                "FROM (SELECT a.id FROM t_org a LEFT JOIN t_reader b ON a.id = b.orgid " +
                "LEFT JOIN t_jieyue e ON b.id = e.readerid WHERE b. STATUS != 4 " +
                "AND a.orgtypeid = 5 AND e.createtime >= ? " +
                "AND e.createtime <= ? ");
        if(orgid != 0){
            where.append(" AND a.id = ? ");
            totalRowSql.append(" AND a.id = ? ");
            list.add(orgid);
        }
        totalRowSql.append(" GROUP BY a.id) aa ");
        where.append(" GROUP BY a.id ORDER BY rjjycs DESC");
        Page<Record> page = Db.paginateByFullSql(pageNumber, pagesize,totalRowSql.toString(),select+where.toString(),CommonUtils.listToArray(list));
        for(Record r : page.getList()){
            Integer id = r.getInt("id");
            Integer students = r.getInt("students");
            Record rs = Db.findFirst("SELECT COUNT(b.id) as librarys FROM t_book a " +
                    "LEFT JOIN t_book_detail b ON a.id = b.bookid WHERE a.orgid = ? and b.bookstatus=1 and b.status!=3 ",id);
            Integer librarys = rs.getInt("librarys");
            double sjcs = 0;
            if(students != 0){
                sjcs = Double.valueOf(librarys+"").doubleValue() / Double.valueOf(students+"").doubleValue();
            }
            r.set("librarys",librarys);
            r.set("sjcs",sjcs);
        }
        return page;
    }
}
