package com.service;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.model.*;
import com.tools.ISBNTools;
import com.utils.CommonUtils;
import com.utils.DateUtils;
import org.apache.commons.lang3.StringUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.*;

public class SelectCountService {
    public static final SelectCountService me = new SelectCountService();
    private Book bookDao = new Book().dao();
    private Org orgDao = new Org().dao();
    private Readertype readertypeDao = new Readertype().dao();
    private Dayreport dayDao = new Dayreport().dao();
    private  OrgService orgService =  OrgService.me;


    //馆藏检索
    public Page<Record> collectionSearch(int pageNumber,int pagesize,Integer orgid,String countent) {
        String orgidStr = String.valueOf(orgid);
        Record databaseRecord = Db.findFirst("select * from t_database where status = 1 and orgid = ?",orgid);
        String databasename = "";
        if(CommonUtils.isNotNullOrNotEmpty(databaseRecord)){
            databasename = databaseRecord.getStr("databasename");
        } else if(orgidStr.length() > 6){
            databaseRecord = Db.findFirst("select * from t_database where status = 1 and orgid like ?",orgidStr.substring(0,4)+"%");
            if(CommonUtils.isNotNullOrNotEmpty(databaseRecord)){
                databasename = databaseRecord.getStr("databasename");
            }
        }
        String select = "SELECT a.isbn,a.title,a.author,a.publisher,c.orgname,a.id as bookid,count(*) as gcs,SUM(b.status = 0) as kjs ";
        StringBuilder where = new StringBuilder(" from t_book a INNER JOIN t_org c ON a.orgid = c.id " +
                "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!=null){
            String parentcodes = CommonUtils.codeAddOne(orgid,6);
            where.append("AND (c.parentcodes LIKE ? OR c.id = ?) ");
            list.add("%"+parentcodes+"%");
            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("%"+countent+"%");
            list.add("%"+countent+"%");
            list.add("%"+countent+"%");
        }
        where.append(" GROUP BY a.id");
        Page<Record> page;
        if("".equals(databasename)){
            page =  Db.paginate(pageNumber, pagesize,select,where.toString(), CommonUtils.listToArray(list));
        } else {
            page =  Db.use(databasename).paginate(pageNumber, pagesize,select,where.toString(), CommonUtils.listToArray(list));
        }
        return page;
    }
    //机构联动
    public List linkAge(Map map,Integer orgid,Integer onlyedu){
        List<Record> org = null;
        if (orgid!=null) {
//            org = orgDao.find("SELECT id as orgid,orgname,orgtypeid from t_org WHERE parentid = ?",orgid);
            String sql = "SELECT id as orgid,orgname,orgtypeid from t_org WHERE parentid = ?";
            if(CommonUtils.isNotNullOrNotEmpty(onlyedu)) {
                sql = sql + " and orgtypeid!=5";
            }
            Record orgdatabaseRecord = Db.findFirst("select * from t_database where status = 1 and orgid = ?",orgid);
            if(CommonUtils.isNotNullOrNotEmpty(orgdatabaseRecord)){
                org = Db.use(orgdatabaseRecord.getStr("databasename")).find(sql,orgid);
            } else {
                org = Db.find(sql,orgid);
            }

//            if(org.size() == 0){
//                org = orgDao.find("SELECT id as orgid,orgname,orgtypeid from t_org WHERE id = ?",orgid);
//                return org;
//            }
            return org;
        }else {
            Object d = map.get("orgid");
            String sql = "SELECT id as orgid,orgname,orgtypeid from t_org WHERE parentid = ?";
            if(CommonUtils.isNotNullOrNotEmpty(onlyedu)) {
                sql = sql + " and orgtypeid!=5";
            }
            org = Db.find(sql,d);
            return org;
        }
    }

    //馆藏高级检索
    public Page<Record> collectionSenSearch(int pageNumber,int pagesize,Integer orgid,Book bean) {
        String select = "SELECT a.isbn,a.title,a.author,a.publisher,c.orgname,a.id as bookid,count(*) as gcs,SUM(b.status = 0) as kjs ";
        StringBuilder where = new StringBuilder(" from t_book a LEFT JOIN t_org c ON a.orgid = c.id " +
                "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!=null){
            String parentcodes = CommonUtils.codeAddOne(orgid,6);
            where.append("AND (c.parentcodes LIKE ? OR c.id = ?)");
            list.add("%"+parentcodes+"%");
            list.add(orgid);
        }
        if(StringUtils.isNotBlank(bean.getTitle())){
            where.append(" and a.title like ?");
            list.add("%"+bean.getTitle()+"%");
        }
        if(StringUtils.isNotBlank(bean.getAuthor())){
            where.append(" and a.author like ?");
            list.add("%"+bean.getAuthor()+"%");
        }
        if(StringUtils.isNotBlank(bean.getIsbn())){
            where.append(" and a.isbn like ?");
            list.add("%"+ISBNTools.fmtisbn(bean.getIsbn())+"%");
        }
        if(StringUtils.isNotBlank(bean.getPublisher())){
            where.append(" and a.publisher like ? ");
            list.add("%"+bean.getPublisher()+"%");
        }
        where.append(" GROUP BY a.id");
        Page<Record> page =  Db.paginate(pageNumber, pagesize,select,where.toString(), CommonUtils.listToArray(list));
//        for(Record r : page.getList()){
//            Integer bookid = r.getInt("bookid");
//            Record gcs = Db.findFirst("SELECT COUNT(id) AS gcs from t_book_detail WHERE `status` != 3 AND `status` != 4 AND bookid = ?",bookid);
//            Record kjs = Db.findFirst("SELECT COUNT(id) AS kjs from t_book_detail WHERE `status` = 0 AND bookid = ?",bookid);
//            int g = gcs.getInt("gcs");
//            int k = kjs.getInt("kjs");
//            r.set("gcs",g);
//            r.set("kjs",k);
//        }
        return page;
    }
    //读者查询省市端
    public Page<Record> readerSearchProvinces(int pageNumber,int pagesize, Integer orgid){
        Record orgRecord = Db.findById("t_org",orgid);
        String databasename = "";
        if(CommonUtils.isNotNullOrNotEmpty(orgRecord)){
            Record orgdatabaseRecord = Db.findFirst("select * from t_database where status = 1 and orgid = ?",orgid);
            if(CommonUtils.isNotNullOrNotEmpty(orgdatabaseRecord)){
                databasename = orgdatabaseRecord.getStr("databasename");
            }
        }
        String select = "SELECT a.orgname,a.id as orgid,a.orgtypeid,c.category ";
        StringBuilder where = new StringBuilder(" FROM t_org  a LEFT JOIN t_schoolcategory c ON a.schoolcategoryid = c.id " +
                "WHERE isclose=0 and a.parentid = ?");
        Page<Record> page;
        if("".equals(databasename)){
            page = Db.paginate(pageNumber, pagesize,select,where.toString(),orgid);
        } else {
            page = Db.use(databasename).paginate(pageNumber, pagesize,select,where.toString(),orgid);
        }
//        if(page.getTotalRow() ==0 || page.getList().isEmpty()){
//            String select1 = "SELECT a.orgname,a.id as orgid,a.orgtypeid,c.category ";
//            StringBuilder where1 = new StringBuilder(" FROM t_org  a LEFT JOIN t_schoolcategory c ON a.schoolcategoryid = c.id " +
//                    "WHERE a.id = ?");
//            page = Db.paginate(pageNumber, pagesize,select1,where1.toString(),orgid);
//        }
        List list = new ArrayList();
        for(Record r : page.getList()){
            int id = r.getInt("orgid");
            Record databaseRecord = Db.findFirst("select * from t_database where status = 1 and orgid = ?",id);
            if(CommonUtils.isNotNullOrNotEmpty(databaseRecord)){
                databasename = databaseRecord.getStr("databasename");
            }
            String parentcodes = CommonUtils.codeAddOne(id,6);
            Record rs;
            Record r1;
            if("".equals(databasename)){
                rs = Db.findFirst("SELECT COUNT(r.orgid) as dzs " +
                        "FROM t_reader r right join t_org o on r.orgid = o.id WHERE r.status != 4  AND (o.parentcodes LIKE ? or o.id = ?)","%"+parentcodes+"%",id);
                r1 = Db.findFirst("SELECT (IFNULL(gzstudents,0)+IFNULL(czstudents,0)+IFNULL(students,0)) as students " +
                        "FROM t_org WHERE isclose = 0 AND (parentcodes LIKE ? or id = ?)","%"+parentcodes+"%",id);
            } else {
                rs = Db.use(databasename).findFirst("SELECT COUNT(r.orgid) as dzs " +
                        "FROM t_reader r right join t_org o on r.orgid = o.id WHERE r.status != 4  AND (o.parentcodes LIKE ? or o.id = ?)","%"+parentcodes+"%",id);
                r1 = Db.use(databasename).findFirst("SELECT (IFNULL(gzstudents,0)+IFNULL(czstudents,0)+IFNULL(students,0)) as students " +
                        "FROM t_org WHERE isclose = 0 AND (parentcodes LIKE ? or id = ?)","%"+parentcodes+"%",id);
            }
            Integer dzs = 0;
            String students = null;
            if(rs!=null){
                dzs = rs.get("dzs")==null ? 0 : rs.getInt("dzs");
                if(CommonUtils.isNullOrEmpty(r1) || CommonUtils.isNullOrEmpty(r1.get("students"))){
                    students = "0";
                } else {
                    students = r1.getStr("students");
                }
            }
            r.set("students",students);
            r.set("dzs",dzs);
            list.add(r);
        }
        Page<Record> repage =new Page(list, page.getPageNumber(),page.getPageSize(),page.getTotalPage(),page.getTotalRow());
        return repage;
    }
    //读者检索学校
    public Page<Record> readerSearch(int pageNumber, int pageSize,Integer orgid, Reader bean) {
            String orgstr = String.valueOf(orgid);
            String databasename = "";
            if(orgstr.length() > 6){
                System.err.println(orgstr.substring(0,4));
                Record orgdatabaseRecord = Db.findFirst("select * from t_database where status = 1 and orgid like ?",orgstr.substring(0,4)+"%");
                if(CommonUtils.isNotNullOrNotEmpty(orgdatabaseRecord)){
                    databasename = orgdatabaseRecord.getStr("databasename");
                }
            }
            String parentcodes = CommonUtils.codeAddOne(orgid, 6);
            String select = "SELECT a.id AS readerid,a.realname,a.cardnumber,b.name as typename,c.orgname,c.id ";
            StringBuilder where = new StringBuilder(" FROM t_reader a LEFT JOIN t_dict b ON a.readertypeid = b.id " +
                    "LEFT JOIN t_org c ON a.orgid = c.id where (c.parentcodes LIKE ? or c.id = ?) AND a.status !=4 ");
            List list = new ArrayList();
            list.add("%"+parentcodes+"%");
            list.add(orgid);
            //检索条件
            if(StringUtils.isNotBlank(bean.getRealname())){
                where.append(" and a.realname like ? ");
                list.add("%"+bean.getRealname()+"%");
            }
            if(StringUtils.isNotBlank(bean.getCardnumber())){
                where.append(" and a.cardnumber like ? ");
                list.add("%"+bean.getCardnumber()+"%");
            }
            if(bean.getReadertypeid()!=null){
                where.append(" and a.readertypeid = ? ");
                list.add(bean.getReadertypeid());
            }
            Page<Record> page;
            if("".equals(databasename)){
                page = Db.paginate(pageNumber, pageSize,select,where.toString(),CommonUtils.listToArray(list));
            } else {
                page = Db.use(databasename).paginate(pageNumber, pageSize,select,where.toString(),CommonUtils.listToArray(list));
            }
            for(Record r : page.getList()){
                Integer readerid = r.getInt("readerid");
                Record rs;
                if("".equals(databasename)){
                    rs = Db.findFirst("SELECT c.title,b.createtime FROM t_book_detail a LEFT JOIN t_jieyue b ON a.id = b.bookdetailid " +
                            "LEFT JOIN t_book c ON c.id = a.bookid WHERE b.readerid = ? ORDER BY b.createtime DESC LIMIT 0,1",readerid);
                } else {
                    rs = Db.use(databasename).findFirst("SELECT c.title,b.createtime FROM t_book_detail a LEFT JOIN t_jieyue b ON a.id = b.bookdetailid " +
                            "LEFT JOIN t_book c ON c.id = a.bookid WHERE b.readerid = ? ORDER BY b.createtime DESC LIMIT 0,1",readerid);
                }
                if(rs != null){
                    String title = rs.getStr("title");
                    String createtime = rs.getStr("createtime");
                    r.set("readerid",readerid);
                    r.set("realname",r.getStr("realname"));
                    r.set("typename",r.getStr("typename"));
                    r.set("orgname",r.getStr("orgname"));
                    r.set("title",title);
                    r.set("createtime",createtime);
                }
            }
        return page;
    }
    //读者类型
    public List readertypeList(){
        List<Record> readertypes = Db.find("SELECT id,name as typename from t_dict where type=8");
        return readertypes;
    }
    //读者个人借阅记录明细
    public Page<Book> readerJieyue(int pageNumber,int pagesize,int readerid,String kssj,String jssj){
        String select = "SELECT c.isbn,c.title,c.author,c.publisher,c.createtime,a.createtime,a.returntime ";
        StringBuilder where = new StringBuilder(" FROM t_jieyue a LEFT JOIN t_book_detail b ON a.bookdetailid = b.id " +
                "LEFT JOIN t_book c ON b.bookid = c.id WHERE a.flag = 0 AND a.readerid = ? ");
        List list = new ArrayList();
        list.add(readerid);
        if(StringUtils.isNotBlank(kssj)){
            where.append(" AND a.createtime > '"+kssj+" 00:00:00' ");
        }
        if(StringUtils.isNotBlank(jssj)){
            where.append(" AND a.createtime < '"+jssj+" 23:59:59'");
        }
        where.append(" ORDER BY a.createtime DESC");
        Page<Book> page = bookDao.paginate(pageNumber, pagesize,select,where.toString(), CommonUtils.listToArray(list));
        return page;
    }
    //学校数据统计
    public Page<Record>  booksNumList(int pageNumber,int pageSize,Integer areatypeid,Integer schoolcategoryid,String schoolids,Integer orgid,String databasename){
       String select = "SELECT b.orgname,a.orgid,c.category,a.lss,a.xss,ifnull(a.dzs,0) as dzs,a.tsglys,ifnull(a.tszs,0) as tszs,ifnull(a.tszcs,0) as tszcs,ifnull((a.tszcs - a.tszcs_bz),0) as qscs," +
               "ifnull((a.tszcs / a.xss),0) as sjcs,ifnull((a.tszcs / a.tszs),0) as tsfbl, ifnull(a.qkzs,0) as qkzs  ";
        StringBuilder where = new StringBuilder(" FROM t_dayreport a " +
                "LEFT JOIN t_org b ON a.orgid = b.id LEFT JOIN t_schoolcategory c on b.schoolcategoryid = c.id " +
                "WHERE DATEDIFF(a.createtime,NOW())=0 AND b.isclose = 0 ");
        List list = new ArrayList();
        if(orgid != null){
            String parentcodes = CommonUtils.codeAddOne(orgid,6);
            where.append(" AND (b.parentcodes LIKE ? OR b.id = ?)");
            list.add("%"+parentcodes+"%");
            list.add(orgid);
        }else {
            where.append(" AND a.orgid IN "+schoolids);
        }
        if(areatypeid!=null){
            where.append(" AND b.areatypeid = ? ");
            list.add(areatypeid);
        }
        if(schoolcategoryid!=null){
            where.append(" AND b.schoolcategoryid = ? ");
            list.add(schoolcategoryid);
        }


        OrgService orgService = OrgService.me;
        /*Page<Dayreport> dayreportPage = null;
        try {
            List<Standardbooknum> standardbooknumList=standardBookNumService.getStandardbooknum(orgid);
            Org org =orgService.findById(orgid);
            dayreportPage = me.schoolstandardBoookNumList(pageNumber,pageSize,areatypeid,schoolcategoryid,orgid,org.getOrgtypeid(),standardbooknumList);
        } catch (Exception e) {
            e.printStackTrace();
        }*/
        Page<Record> page;
        if("".equals(databasename)){
            page=  Db.paginate(pageNumber, pageSize,select,where.toString(), CommonUtils.listToArray(list));
        } else {
            page=  Db.use(databasename).paginate(pageNumber, pageSize,select,where.toString(), CommonUtils.listToArray(list));
        }
       /* int index = 0;
        for (Record dayreport :page.getList()) {
            BigDecimal quekouBooknum= new BigDecimal(dayreportPage.getList().get(index).getInt("quekouBooknum"));
            dayreport.set("quekouBooknum",quekouBooknum);
            index ++;
        }*/
        return page;
    }


    public Record  booksNumReportTotalList(Integer areatypeid,Integer schoolcategoryid,String schoolids,Integer orgid){
        Record orgRecord = Db.findById("t_org",orgid);
        Record reRecord = new Record();
        int xss = 0;
        int dzs = 0;
        int tszcs = 0;
        int tsfbl = 0;
        int qkzs = 0;
        List<Record> orglist;
        String databasename = "";
        if(CommonUtils.isNotNullOrNotEmpty(orgRecord) && orgRecord.getInt("orgtypeid") < 5){
            Record orgdatabaseRecord = Db.findFirst("select * from t_database where status = 1 and orgid = ?",orgid);
            if(CommonUtils.isNotNullOrNotEmpty(orgdatabaseRecord)){
                databasename = orgdatabaseRecord.getStr("databasename");
                orglist = Db.use(databasename).find("select * from t_org where parentid = ?",orgid);
            } else{
                orglist = Db.find("select * from t_org where parentid = ?",orgid);
            }
        } else {
            orglist = Db.find("select * from t_org where id = ?",orgid);
        }
        for(Record record : orglist) {
            Record databaseRecord = Db.findFirst("select * from t_database where status = 1 and orgid = ?",record.getInt("id"));
            if(CommonUtils.isNotNullOrNotEmpty(databaseRecord)){
                databasename = databaseRecord.getStr("databasename");
            }
            String select = "SELECT ifnull(SUM(a.xss),0) as xss,ifnull(SUM(a.dzs),0) as dzs,ifnull(SUM(a.tszcs),0) as tszcs," +
                    "case when  IFNULL(SUM(a.xss),0) !=0 then   SUM(a.tszcs)/SUM(a.xss) else 0 end  as tssjcs ," +
                    "case when  IFNULL(SUM(a.tszs),0) !=0 then   SUM(a.tszcs) / SUM(a.tszs)  else 0 end as tsfbl, ifnull(SUM(a.qkzs),0) as qkzs ";
            StringBuilder where = new StringBuilder(" FROM t_dayreport a " +
                    "LEFT JOIN t_org b ON a.orgid = b.id LEFT JOIN t_schoolcategory c on b.schoolcategoryid = c.id " +
                    "WHERE DATEDIFF(a.createtime,NOW())=0 AND b.isclose = 0 ");
            List list = new ArrayList();
            if(orgid != null){
                String parentcodes = CommonUtils.codeAddOne(record.getInt("id"),6);
                where.append(" AND (b.parentcodes LIKE ? OR b.id = ?)");
                list.add("%"+parentcodes+"%");
                list.add(record.getInt("id"));
            }else {
                where.append(" AND a.orgid IN "+schoolids);
            }
            if(areatypeid!=null){
                where.append(" AND b.areatypeid = ? ");
                list.add(areatypeid);
            }
            if(schoolcategoryid!=null){
                where.append(" AND b.schoolcategoryid = ? ");
                list.add(schoolcategoryid);
            }
            Record qRecord;
            if("".equals(databasename)){
                qRecord =  Db.findFirst(select+where.toString(), CommonUtils.listToArray(list));
            } else {
                qRecord =  Db.use(databasename).findFirst(select+where.toString(), CommonUtils.listToArray(list));
            }
            xss += qRecord.getInt("xss");
            dzs += qRecord.getInt("dzs");
            tszcs += qRecord.getInt("tszcs");
            tsfbl += qRecord.getInt("tsfbl");
            qkzs += qRecord.getInt("qkzs");
        }
        reRecord.set("xss",xss);
        reRecord.set("dzs",dzs);
        reRecord.set("tszcs",tszcs);
        reRecord.set("tsfbl",tsfbl);
        reRecord.set("qkzs",qkzs);
        if(tszcs == 0 || xss == 0){
            reRecord.set("tssjcs",0);
        } else {
            DecimalFormat df = new DecimalFormat("0.00");
            double f = (float)tszcs/xss;
            reRecord.set("tssjcs",df.format(f));
        }
        return reRecord;
    }


    //藏书量统计
    public Record booksNumList(Integer areatypeid,Integer orgid,Integer schoolcategoryid,int orgtypeid,String databasename){
        StringBuffer sqlBuf =new StringBuffer( "  SELECT IFNULL(SUM(lss),0) as lss, IFNULL(SUM(xss),0) as xss , IFNULL(SUM(dzs),0) as dzs, IFNULL(SUM(tsglys),0) as tsglys," +
                " IFNULL(SUM(tszs),0) as tszs, IFNULL(SUM(tszcs),0) as tszcs, IFNULL(SUM(tszcs_bz),0) as tszcs_bz, IFNULL(SUM(qkzs),0) as qkzs , " +
                " case when  IFNULL(SUM(a.xss),0) !=0 then   SUM(a.tszcs)/SUM(a.xss) else 0 end  as tssjcs  FROM t_dayreport a " +
                " INNER JOIN t_org b ON a.orgid = b.id " +
                " LEFT JOIN t_schoolcategory c on b.schoolcategoryid = c.id " +
                "where a.createtime=curdate() ");
        List list = new ArrayList();
        if(orgtypeid == 5){
            sqlBuf.append("and a.orgid = '"+orgid+"'");
        } else {
            sqlBuf.append("and b.parentcodes like '%"+orgid+"%'");
        }
        if(areatypeid!=null){
            sqlBuf.append(" AND b.areatypeid = ? ");
            list.add(areatypeid);
        }
        if(schoolcategoryid!=null){
            sqlBuf.append(" AND b.schoolcategoryid = ? ");
            list.add(schoolcategoryid);
        }
        if("".equals(databasename)){
            return Db.findFirst(sqlBuf.toString(),list.toArray(new Object[0]));
        } else {
            return Db.use(databasename).findFirst(sqlBuf.toString(),list.toArray(new Object[0]));
        }
    }


    //学校数据统计合计
    public Dayreport  booksNumTotalList(Integer areatypeid,Integer schoolcategoryid,Integer orgid,String schoolids){
        String select = "SELECT SUM(a.lss) as lss,SUM(a.xss) as xss,SUM(a.dzs) as dzs,SUM(a.tsglys) as tsglys,SUM(a.tszs) as tszs," +
                "SUM(a.tszcs) as tszcs,(SUM(a.tszcs) - SUM(a.tszcs_bz)) as qscs,(SUM(a.tszcs) / SUM(a.xss)) as sjcs," +
                "(SUM(a.tszs) / SUM(a.tszcs)) as tsfbl,SUM(a.qkzs) as qkzs ";
        StringBuilder where = new StringBuilder(" FROM t_dayreport a " +
                "LEFT JOIN t_org b ON a.orgid = b.id LEFT JOIN t_schoolcategory c on b.schoolcategoryid = c.id " +
                "WHERE DATEDIFF(a.createtime,NOW())=0 AND b.isclose = 0 ");
        List list = new ArrayList();
        if(orgid != null){
            String parentcodes = CommonUtils.codeAddOne(orgid,6);
            where.append(" AND (b.parentcodes LIKE ? OR b.id = ?)");
            list.add("%"+parentcodes+"%");
            list.add(orgid);
        }else {
            where.append("and a.orgid IN "+schoolids);
        }
        if(areatypeid!=null){
            where.append(" AND b.areatypeid = ? ");
            list.add(areatypeid);
        }
        if(schoolcategoryid!=null){
            where.append(" AND b.schoolcategoryid = ? ");
            list.add(schoolcategoryid);
        }
        Dayreport total = dayDao.findFirst(select+where,CommonUtils.listToArray(list));
        return total;
    }
    //学校馆藏图书明细
    public Page<Record> booksNumDetailedList(int pageNumber,int pageSize,Integer orgid,String title,String databasename){
        String select = "SELECT a.id as bookid,a.isbn,a.title,a.author,a.publisher,a.publishdate,a.bookno,a.price,COUNT(b.id) as booknum ";
        StringBuilder where = new StringBuilder(" FROM t_book a INNER JOIN t_book_detail b ON a.id = b.bookid WHERE a.orgid = ? and b.bookstatus = 1 AND b.status != 3 ");
        List list = new ArrayList();
        list.add(orgid);
        if(StringUtils.isNotBlank(title)){
            where.append(" AND (a.title LIKE ? OR a.author LIKE ? OR a.publisher LIKE ?) ");
            list.add("%"+title+"%");
            list.add("%"+title+"%");
            list.add("%"+title+"%");
        }
        where.append(" GROUP BY bookid ");
        if(CommonUtils.isNullOrEmpty(databasename) || "".equals(databasename)){
            return Db.paginate(pageNumber, pageSize,select,where.toString(), CommonUtils.listToArray(list));
        } else {
            return Db.use(databasename).paginate(pageNumber, pageSize,select,where.toString(), CommonUtils.listToArray(list));
        }
    }
    //学校期刊明细
    public Page<Book> qBooksNumDetailedList(int pageNumber,int pageSize,Integer orgid,String title){
        String select = "SELECT a.id as bookid,a.issn,a.kanming as title,b.bookno,a.bianjibu,a.tongyikanhao,a.youfadaihao,b.price ";
        StringBuilder where = new StringBuilder(" FROM t_periodical_zhengding a LEFT JOIN t_periodical_detail b ON a.id = b.periodicalid " +
                "left join t_org o on o.id=a.orgid ");
        Org org = orgService.findById(orgid);
        List list = new ArrayList();
        if(org.getOrgtypeid() == 5){
            where.append("WHERE a.orgid = ? and b.status !=3 ");
            list.add(orgid);
        }else{
            where.append("WHERE o.parentcodes like ? and b.status!=3 ");
            list.add(org.getParentcodes()+orgid+"%");
        }

        if(StringUtils.isNotBlank(title)){
            where.append(" AND (a.kanming LIKE ? OR a.futiming LIKE ? OR a.bianjibu LIKE ?) ");
            list.add("%"+title+"%");
            list.add("%"+title+"%");
            list.add("%"+title+"%");
        }
        where.append(" GROUP BY a.kanming,a.issn,b.bookno ");
        return bookDao.paginate(pageNumber, pageSize,select,where.toString(), CommonUtils.listToArray(list));
    }

    //学校总括登记
//    public Page<Record> schoolOmnibusList(int pageNumber,int pagesize,Integer orgid,String kssj,String jssj){
//        String select = "SELECT c.createtime,c.batchname,COUNT(DISTINCT b.qzhongcihao,a.typeid) as zch,COUNT(d.id) as zcs,SUM(a.price) as price, " +
//                "COUNT(DISTINCT case when e.parentid = 1 then b.qzhongcihao else NULL END ) as  a, " +
//                "COUNT(DISTINCT case when e.parentid = 2 then b.qzhongcihao else NULL END ) as  b, " +
//                "COUNT(DISTINCT case when e.parentid = 3 then b.qzhongcihao else NULL END ) as  c, " +
//                "COUNT(DISTINCT case when e.parentid = 4 then b.qzhongcihao else NULL END ) as  d, " +
//                "COUNT(DISTINCT case when e.parentid = 5 then b.qzhongcihao else NULL END ) as  e, " +
//                "COUNT(case when e.parentid = 1 then d.id else NULL END ) as  a1, " +
//                "COUNT(case when e.parentid = 2 then d.id else NULL END ) as  b1, " +
//                "COUNT(case when e.parentid = 3 then d.id else NULL END ) as  c1, " +
//                "COUNT(case when e.parentid = 4 then d.id else NULL END ) as  d1, " +
//                "COUNT(case when e.parentid = 5 then d.id else NULL END ) as  e1";
//        StringBuilder where = new StringBuilder(" from t_book a INNER JOIN t_book_plus b ON a.id = b.bookid " +
//                "INNER JOIN t_bookpici c ON b.piciid = c.id INNER JOIN t_book_detail d ON a.id = d.bookid " +
//                "INNER JOIN t_booktype e ON a.typeid = e.id where a.orgid = ? ");
//        List list = new ArrayList();
//        list.add(orgid);
//        if(StringUtils.isNotBlank(kssj)){
//            where.append(" AND c.createtime > ? ");
//            list.add(kssj);
//        }
//        if(StringUtils.isNotBlank(jssj)){
//            where.append(" AND c.createtime < ? ");
//            list.add(jssj);
//        }
//        where.append(" group by c.batchname");
//        return Db.paginate(pageNumber, pagesize,select,where.toString(), CommonUtils.listToArray(list));
//    }
    //学校流通记录（单个学校）
    public Page<Record> schoolJieyueRecordList(int pageNumber,int pageSize,Integer orgid,String kssj,String jssj){
        String select = "SELECT c.isbn,c.title,d.realname,d.cardnumber,a.createtime,a.returntime,a.continuenum ";
        StringBuilder where = new StringBuilder("FROM t_jieyue a LEFT JOIN t_book_detail b ON a.bookdetailid = b.id " +
                "LEFT JOIN t_book c ON b.bookid = c.id LEFT JOIN t_reader d ON a.readerid = d.id WHERE c.orgid = ? ");
        List list = new ArrayList();
        list.add(orgid);
        //如果没有选择时间，则默认显示近一个月数据
        if(!StringUtils.isNotBlank(kssj) && !StringUtils.isNotBlank(jssj)){
            where.append(" AND a.createtime>DATE_SUB(CURDATE(), INTERVAL 1 MONTH) ");
        }
        if(StringUtils.isNotBlank(kssj)){
            kssj+=" 00:00:00";
            where.append(" AND a.createtime >= ?");
            list.add(kssj);
        }
        if(StringUtils.isNotBlank(jssj)){
            jssj+=" 23:59:59";
            where.append(" AND a.createtime < ? ");
            list.add(jssj);
        }
        Page<Record> page = Db.paginate(pageNumber, pageSize,select,where.toString(), CommonUtils.listToArray(list));
        return page;
    }
    //图书新增记录
    public Page<Record> bookNewlyAddedList(int pageNumber,int pageSize,Integer orgid,String kssj,String jssj,String countent){
        String select = "SELECT a.isbn,a.title,a.author,a.publisher,a.createtime,p.dictid newsource,COUNT(b.bookid) as booknum,a.price * COUNT(b.bookid) as price ";
        StringBuilder where = new StringBuilder(" from t_book a inner JOIN t_book_detail b ON a.id = b.bookid inner join t_bookpici p on b.piciid=p.id " +
                " WHERE b.`status` != 3  and a.orgid = ? ");
        List list = new ArrayList();
        list.add(orgid);
        if(StringUtils.isNotBlank(countent)){
            where.append(" AND a.title LIKE ? OR a.author LIKE ? ");
            list.add("%"+countent+"%");
            list.add("%"+countent+"%");
        }
        //如果没有选择时间，则默认显示近三个月数据
        if(StringUtils.isBlank(kssj) && StringUtils.isBlank(jssj)){
            where.append(" AND a.createtime>DATE_SUB(CURDATE(), INTERVAL 1 MONTH) ");
        }
        if(StringUtils.isNotBlank(kssj)){
            where.append(" AND a.createtime >= ? ");
            kssj = kssj + " 00:00:00";
            list.add(kssj);
        }
        if(StringUtils.isNotBlank(jssj)){
            where.append(" AND a.createtime <= ? ");
            jssj = jssj + " 23:59:59";
            list.add(jssj);
        }
        where.append(" GROUP BY b.bookid ");
        Page<Record> page = Db.paginate(pageNumber, pageSize,select,where.toString(), CommonUtils.listToArray(list));
        return page;
    }
    //图书注销记录
    public Page<Record> bookCancelList(int pageNumber,int pageSize,Integer orgid,String kssj,String jssj,String countent){
        String select = "SELECT a.isbn,a.title,a.author,a.publisher,c.createtime,c.reason,b.barcode ";
        StringBuilder where = new StringBuilder(" from t_book a LEFT JOIN t_book_detail b ON a.id = b.bookid " +
                " LEFT JOIN t_book_zhuxiao c ON b.id = c.bookdetailid WHERE b.`status` = 3 and a.orgid = ? ");
        List list = new ArrayList();
        list.add(orgid);
        if(StringUtils.isNotBlank(countent)){
            where.append(" AND a.title LIKE ? OR a.author LIKE ? ");
            list.add("%"+countent+"%");
            list.add("%"+countent+"%");
        }
        //如果没有选择时间，则默认显示近三个月数据
        if(StringUtils.isBlank(kssj) && StringUtils.isBlank(jssj)){
            where.append(" AND DATE_SUB(CURDATE(), INTERVAL 30 DAY) < DATE(b.destroytime) ");
        }
        if(StringUtils.isNotBlank(kssj)){
            where.append(" AND c.createtime >= ? ");
            kssj = kssj + " 00:00:00";
            list.add(kssj);
        }
        if(StringUtils.isNotBlank(jssj)){
            where.append(" AND c.createtime <= ? ");
            jssj = jssj + " 23:59:59";
            list.add(jssj);
        }
        where.append(" GROUP BY b.id ");
        Page<Record> page = Db.paginate(pageNumber, pageSize,select,where.toString(), CommonUtils.listToArray(list));
        return page;
    }
    //藏书分类统计省市县端
    public Page<Record> bookTypeListProvinces(int pageNumber,int pageSize,Integer orgid,Integer areatypeid,Integer schoolcategoryid,Integer schoolid){
        String select = "SELECT a.orgname,a.id as orgid,b.category,a.orgtypeid ";
        StringBuilder where = new StringBuilder("FROM t_org a LEFT JOIN t_schoolcategory b ON a.schoolcategoryid = b.id " +
                " WHERE a.parentid =? AND a.isclose = 0 ");
        List value = new ArrayList();
        value.add(orgid);
        if(schoolid!=null){
            where.append(" AND a.id = ? ");
            value.add(schoolid);
        }
        if(areatypeid!=null){
            where.append(" AND a.areatypeid = ? ");
            value.add(areatypeid);
        }
        if(schoolcategoryid!=null){
            where.append(" AND a.schoolcategoryid = ? ");
            value.add(schoolcategoryid);
        }
        Record orgdatabaseRecord = Db.findFirst("select * from t_database where status = 1 and orgid = ?",orgid);
        String databasename = "";
        Page<Record> page;
        if(CommonUtils.isNotNullOrNotEmpty(orgdatabaseRecord)){
            databasename = orgdatabaseRecord.getStr("databasename");
            page = Db.use(databasename).paginate(pageNumber, pageSize,select,where.toString(),CommonUtils.listToArray(value));
        } else {
            page = Db.paginate(pageNumber, pageSize,select,where.toString(),CommonUtils.listToArray(value));
        }
//        if(page.getTotalRow() ==0 || page.getList().isEmpty()){
//            String select1 = "SELECT a.orgname,a.id as orgid,b.category,a.orgtypeid ";
//            StringBuilder where1 = new StringBuilder("FROM t_org a LEFT JOIN t_schoolcategory b ON a.schoolcategoryid = b.id " +
//                    " WHERE a.id =?");
//            List value1 = new ArrayList();
//            value1.add(orgid);
//            if(areatypeid!=null){
//                where1.append(" AND a.areatypeid = ? ");
//                value1.add(areatypeid);
//            }
//            if(schoolcategoryid!=null){
//                where1.append(" AND a.schoolcategoryid = ? ");
//                value1.add(schoolcategoryid);
//            }
//            page = Db.paginate(pageNumber, pageSize,select1,where1.toString(),CommonUtils.listToArray(value1));
//        }
        for(Record r : page.getList()){
            int id = r.getInt("orgid");
            Record databaseRecord = Db.findFirst("select * from t_database where status = 1 and orgid = ?",id);
            String parentcodes = CommonUtils.codeAddOne(id,6);
            String selects = "SELECT COUNT(d.id) as zcs," +
                    "COUNT(case when e.parentid = 1 then d.id else NULL END ) as  cs1," +
                    "COUNT(case when e.parentid = 2 then d.id else NULL END ) as  cs2," +
                    "COUNT(case when e.parentid = 3 then d.id else NULL END ) as  cs3," +
                    "COUNT(case when e.parentid = 4 then d.id else NULL END ) as  cs4," +
                    "COUNT(case when e.parentid = 5 then d.id else NULL END ) as  cs5 ";
            StringBuilder wheres = new StringBuilder("from t_book a " +
                    "LEFT JOIN t_book_plus b ON a.id = b.bookid LEFT JOIN t_book_detail d ON a.id = d.bookid " +
                    "LEFT JOIN t_booktype e ON a.typeid = e.id LEFT JOIN t_org f ON a.orgid = f.id " +
                    "LEFT JOIN t_schoolcategory g ON f.schoolcategoryid = g.id " +
                    "where (f.parentcodes LIKE ? or f.id = ?) AND d.status != 3 AND d.bookstatus = 1 ");
            List list = new ArrayList();
            list.add("%"+parentcodes+"%");
            list.add(id);

            List<Record> total_l;
            if(CommonUtils.isNotNullOrNotEmpty(databaseRecord)){
                databasename = databaseRecord.getStr("databasename");
            }
            if(!databasename.equals("")){
                total_l = Db.use(databasename).find(selects+wheres.toString(),CommonUtils.listToArray(list));
            } else {
                total_l = Db.find(selects+wheres.toString(),CommonUtils.listToArray(list));
            }
            DecimalFormat df = new DecimalFormat("0.00%");
            for(Record l : total_l){
                int zcs = l.getInt("zcs");
                int cs1 = l.getInt("cs1");
                int cs2 = l.getInt("cs2");
                int cs3 = l.getInt("cs3");
                int cs4 = l.getInt("cs4");
                int cs5 = l.getInt("cs5");
                String zb1 = "";
                String zb2 = "";
                String zb3 = "";
                String zb4 = "";
                String zb5 = "";
                if(zcs != 0){
                    zb1 = df.format((float) cs1 / (float) zcs);
                    zb2 = df.format((float) cs2 / (float) zcs);
                    zb3 = df.format((float) cs3 / (float) zcs);
                    zb4 = df.format((float) cs4 / (float) zcs);
                    zb5 = df.format((float) cs5 / (float) zcs);
                }
                r.set("zcs",zcs);
                r.set("cs1",cs1);
                r.set("cs2",cs2);
                r.set("cs3",cs3);
                r.set("cs4",cs4);
                r.set("cs5",cs5);
                r.set("hjzb1",zb1);
                r.set("hjzb2",zb2);
                r.set("hjzb3",zb3);
                r.set("hjzb4",zb4);
                r.set("hjzb5",zb5);
            }
        }
        return page;
    }
    //藏书分类统计合计省市县端
    public Record bookTypeTotalListProvinces(Integer orgid,Integer areatypeid,Integer schoolcategoryid,Integer schoolid){
        String select = "SELECT a.orgname,a.id as orgid,b.category,a.orgtypeid ";
        StringBuilder where = new StringBuilder("FROM t_org a LEFT JOIN t_schoolcategory b ON a.schoolcategoryid = b.id " +
                " WHERE a.parentid =? AND a.isclose = 0 ");
        List value = new ArrayList();
        value.add(orgid);
        if(schoolid!=null){
            where.append(" AND a.id = ? ");
            value.add(schoolid);
        }
        if(areatypeid!=null){
            where.append(" AND a.areatypeid = ? ");
            value.add(areatypeid);
        }
        if(schoolcategoryid!=null){
            where.append(" AND a.schoolcategoryid = ? ");
            value.add(schoolcategoryid);
        }
        Record orgdatabaseRecord = Db.findFirst("select * from t_database where status = 1 and orgid = ?",orgid);
        String databasename = "";
        List<Record> orglist;
        if(CommonUtils.isNotNullOrNotEmpty(orgdatabaseRecord)){
            databasename = orgdatabaseRecord.getStr("databasename");
            orglist = Db.use(databasename).find(select+where.toString(),CommonUtils.listToArray(value));
        } else {
            orglist = Db.find(select+where.toString(),CommonUtils.listToArray(value));
        }
//        if(page.getTotalRow() ==0 || page.getList().isEmpty()){
//            String select1 = "SELECT a.orgname,a.id as orgid,b.category,a.orgtypeid ";
//            StringBuilder where1 = new StringBuilder("FROM t_org a LEFT JOIN t_schoolcategory b ON a.schoolcategoryid = b.id " +
//                    " WHERE a.id =?");
//            List value1 = new ArrayList();
//            value1.add(orgid);
//            if(areatypeid!=null){
//                where1.append(" AND a.areatypeid = ? ");
//                value1.add(areatypeid);
//            }
//            if(schoolcategoryid!=null){
//                where1.append(" AND a.schoolcategoryid = ? ");
//                value1.add(schoolcategoryid);
//            }
//            page = Db.paginate(pageNumber, pageSize,select1,where1.toString(),CommonUtils.listToArray(value1));
//        }

        int zcs = 0;
        int cs1 = 0;
        int cs2 = 0;
        int cs3 = 0;
        int cs4 = 0;
        int cs5 = 0;
        for(Record r : orglist){
            int id = r.getInt("orgid");
            Record databaseRecord = Db.findFirst("select * from t_database where status = 1 and orgid = ?",id);
            String parentcodes = CommonUtils.codeAddOne(id,6);
            String selects = "SELECT COUNT(d.id) as zcs," +
                    "COUNT(case when e.parentid = 1 then d.id else NULL END ) as  cs1," +
                    "COUNT(case when e.parentid = 2 then d.id else NULL END ) as  cs2," +
                    "COUNT(case when e.parentid = 3 then d.id else NULL END ) as  cs3," +
                    "COUNT(case when e.parentid = 4 then d.id else NULL END ) as  cs4," +
                    "COUNT(case when e.parentid = 5 then d.id else NULL END ) as  cs5 ";
            StringBuilder wheres = new StringBuilder("from t_book a " +
                    "LEFT JOIN t_book_plus b ON a.id = b.bookid LEFT JOIN t_book_detail d ON a.id = d.bookid " +
                    "LEFT JOIN t_booktype e ON a.typeid = e.id LEFT JOIN t_org f ON a.orgid = f.id " +
                    "LEFT JOIN t_schoolcategory g ON f.schoolcategoryid = g.id " +
                    "where (f.parentcodes LIKE ? or f.id = ?) AND d.status != 3 AND d.bookstatus = 1 ");
            List list = new ArrayList();
            list.add("%"+parentcodes+"%");
            list.add(id);

            List<Record> total_l;
            if(CommonUtils.isNotNullOrNotEmpty(databaseRecord)){
                databasename = databaseRecord.getStr("databasename");
            }
            if(!databasename.equals("")){
                total_l = Db.use(databasename).find(selects+wheres.toString(),CommonUtils.listToArray(list));
            } else {
                total_l = Db.find(selects+wheres.toString(),CommonUtils.listToArray(list));
            }
            for(Record l : total_l){
                zcs += l.getInt("zcs");
                cs1 += l.getInt("cs1");
                cs2 += l.getInt("cs2");
                cs3 += l.getInt("cs3");
                cs4 += l.getInt("cs4");
                cs5 += l.getInt("cs5");
            }
        }
        DecimalFormat df = new DecimalFormat("0.00%");

        String zb1 = "";
        String zb2 = "";
        String zb3 = "";
        String zb4 = "";
        String zb5 = "";
        if(zcs != 0){
            zb1 = df.format((float) cs1 / (float) zcs);
            zb2 = df.format((float) cs2 / (float) zcs);
            zb3 = df.format((float) cs3 / (float) zcs);
            zb4 = df.format((float) cs4 / (float) zcs);
            zb5 = df.format((float) cs5 / (float) zcs);
        }
        Record total = new Record();
        total.set("zcs",zcs);
        total.set("cs1",cs1);
        total.set("cs2",cs2);
        total.set("cs3",cs3);
        total.set("cs4",cs4);
        total.set("cs5",cs5);
        total.set("hjzb1",zb1);
        total.set("hjzb2",zb2);
        total.set("hjzb3",zb3);
        total.set("hjzb4",zb4);
        total.set("hjzb5",zb5);
//        String parentcodes = CommonUtils.codeAddOne(orgid,6);
//        String selects = "SELECT COUNT(d.id) as zcs," +
//                "COUNT(case when e.parentid = 1 then d.id else NULL END ) as  cs1," +
//                "COUNT(case when e.parentid = 2 then d.id else NULL END ) as  cs2," +
//                "COUNT(case when e.parentid = 3 then d.id else NULL END ) as  cs3," +
//                "COUNT(case when e.parentid = 4 then d.id else NULL END ) as  cs4," +
//                "COUNT(case when e.parentid = 5 then d.id else NULL END ) as  cs5," +
//                "(SELECT orgname FROM t_org WHERE id = 1) as orgname  ";
//        StringBuilder wheres = new StringBuilder("from t_book a " +
//                "LEFT JOIN t_book_plus b ON a.id = b.bookid LEFT JOIN t_book_detail d ON a.id = d.bookid " +
//                "LEFT JOIN t_booktype e ON a.typeid = e.id LEFT JOIN t_org f ON a.orgid = f.id " +
//                "where (f.parentcodes LIKE ? or f.id = ?) AND d.status != 3 AND d.bookstatus = 1 AND f.isclose = 0 ");
//        List list = new ArrayList();
//        list.add("%"+parentcodes+"%");
//        list.add(orgid);
//
//        if(schoolid!=null){
//            wheres.append(" AND f.id = ? ");
//            list.add(schoolid);
//        }
//
//        if(areatypeid!=null){
//            wheres.append(" AND f.areatypeid = ? ");
//            list.add(areatypeid);
//        }
//        if(schoolcategoryid!=null){
//            wheres.append(" AND f.schoolcategoryid = ? ");
//            list.add(schoolcategoryid);
//        }
//        Record total = Db.findFirst(selects+wheres,CommonUtils.listToArray(list));
//        DecimalFormat df = new DecimalFormat("0.00%");
//        int t_zcs = total.getInt("zcs");
//        int t_cs1 = total.getInt("cs1");
//        int t_cs2 = total.getInt("cs2");
//        int t_cs3 = total.getInt("cs3");
//        int t_cs4 = total.getInt("cs4");
//        int t_cs5 = total.getInt("cs5");
//        String zb1 = "";
//        String zb2 = "";
//        String zb3 = "";
//        String zb4 = "";
//        String zb5 = "";
//        if(t_zcs != 0){
//            zb1 = df.format((float) t_cs1 / (float) t_zcs);
//            zb2 = df.format((float) t_cs2 / (float) t_zcs);
//            zb3 = df.format((float) t_cs3 / (float) t_zcs);
//            zb4 = df.format((float) t_cs4 / (float) t_zcs);
//            zb5 = df.format((float) t_cs5 / (float) t_zcs);
//        }
        return total;
    }

    //藏书分类统计按22大类
    public List<Record> bookTypeSmallList(String schoolids,Integer orgid,Integer areatypeid,Integer schoolcategoryid){
        String select = "SELECT left(bt.id,1) as gname,COUNT(d.id) as zcs,COUNT(DISTINCT a.shucihao,a.typecode) as zs,SUM(a.price) as price  ";
        StringBuilder where = new StringBuilder(" from t_book a " +
                "LEFT JOIN t_book_plus b ON a.id = b.bookid LEFT JOIN t_book_detail d ON a.id = d.bookid " +
                "LEFT join t_booktype bt on bt.id=a.typeid  LEFT JOIN t_org f ON a.orgid = f.id " +
                "LEFT JOIN t_schoolcategory g ON f.schoolcategoryid = g.id where d.bookstatus = 1 AND d.status !=3 ");
        List list = new ArrayList();
        if(orgid!=null){
            where.append("and a.orgid = ? ");
            list.add(orgid);
        }else {
            where.append("and a.orgid IN "+schoolids);
        }
        if(areatypeid!=null){
            where.append(" AND f.areatypeid = ? ");
            list.add(areatypeid);
        }
        if(schoolcategoryid!=null){
            where.append(" AND f.schoolcategoryid = ? ");
            list.add(schoolcategoryid);
        }
        where.append(" group by gname ");
        List<Record> total = Db.find(select+where,CommonUtils.listToArray(list));
        return total;
    }
    //藏书分类统计合计按22大类
//    public Record bookTypeSmallTotalList(String schoolids,Integer orgid,Integer areatypeid,Integer schoolcategoryid){
//        String select = "SELECT left(bt.id,1) as gname,COUNT(d.id) as zcs,COUNT(DISTINCT a.shucihao,a.typeid) as zs,SUM(a.price) as price ";
//        StringBuilder where = new StringBuilder(" from t_book a " +
//                "LEFT JOIN t_book_plus b ON a.id = b.bookid LEFT JOIN t_book_detail d ON a.id = d.bookid " +
//                "LEFT join t_booktype bt on bt.id=a.typeid  LEFT JOIN t_org f ON a.orgid = f.id " +
//                "LEFT JOIN t_schoolcategory g ON f.schoolcategoryid = g.id where 1=1 ");
//        List list = new ArrayList();
//        if(orgid!=null){
//            where.append("and a.orgid = ? ");
//            list.add(orgid);
//        }else {
//            where.append("and a.orgid IN "+schoolids);
//        }
//        if(areatypeid!=null){
//            where.append(" AND f.areatypeid = ? ");
//            list.add(areatypeid);
//        }
//        if(schoolcategoryid!=null){
//            where.append(" AND f.schoolcategoryid = ? ");
//            list.add(schoolcategoryid);
//        }
//        Record total = Db.findFirst(select+where,CommonUtils.listToArray(list));
//        return total;
//    }
    //学校馆藏流通记录
    public Page<Record> schoolsJieyueRecordList(int pageNumber,int pageSize,Integer orgid,Integer month,String kssj,String jssj,Integer areatypeid,
                                                Integer schoolcategoryid,String schoolids,String status){
        String select = "SELECT c.isbn,c.title,d.realname,d.cardnumber,a.createtime,a.returntime,a.continuenum,f.orgname ";
        StringBuilder where = new StringBuilder(" FROM t_jieyue a INNER JOIN t_book_detail b ON a.bookdetailid = b.id " +
                "INNER JOIN t_book c ON b.bookid = c.id LEFT JOIN t_reader d ON a.readerid = d.id " +
                "INNER JOIN t_org f ON c.orgid = f.id WHERE 1=1 ");
        List list = new ArrayList();
        if(orgid!=null){
            where.append("and c.orgid = ? ");
            list.add(orgid);
        }else {
            where.append("and c.orgid IN "+schoolids);
        }
        if(month!=null){
            if(month == 1){
                where.append(" AND a.createtime>DATE_SUB(CURDATE(), INTERVAL 1 MONTH) ");
            }
            if (month == 6){
                where.append(" AND a.createtime>DATE_SUB(CURDATE(), INTERVAL 6 MONTH) ");
            }
            if (month == 12){
                where.append(" AND a.createtime>DATE_SUB(CURDATE(), INTERVAL 12 MONTH) ");
            }
        }

        if(StringUtils.isNotBlank(kssj)){
            where.append(" AND a.createtime > ? ");
            list.add(kssj+" 00:00:00");
        }
        if(StringUtils.isNotBlank(jssj)){
            where.append(" AND a.createtime < ? ");
            list.add(jssj+" 23:59:59");
        }

        if(StringUtils.isNotBlank(status)){
            where.append(" AND a.status in ( "+status +" )");
        }
        if(areatypeid!=null){
            where.append(" AND f.areatypeid = ? ");
            list.add(areatypeid);
        }
        if(schoolcategoryid!=null){
            where.append(" AND f.schoolcategoryid = ? ");
            list.add(schoolcategoryid);
        }
        Page<Record> page = Db.paginate(pageNumber, pageSize,select,where.toString(), CommonUtils.listToArray(list));
        return page;
    }

    /**
     * 学校馆藏流通记录 省市查看汇总     *
     * @param month
     * @param kssj
     * @param jssj
     * @param areatypeid
     * @param schoolcategoryid
     * @param schoolids
     * @param wxlx
     * @return
     */
    public List<Record> schoolsJieyueRecordList(Integer month,String kssj,String jssj,Integer areatypeid,  Integer schoolcategoryid,String schoolids,Integer wxlx){


        StringBuilder sql = new StringBuilder("SELECT IFNULL(count(*),0) as outcnt,IFNULL(SUM(a.status in (1,2)),0) as incnt " +
                " FROM t_jieyue a INNER JOIN t_book_detail b ON a.bookdetailid = b.id " +
                "  INNER JOIN t_book c ON b.bookid = c.id " +
                "    INNER JOIN t_org f ON c.orgid = f.id  ");

        List list = new ArrayList();
        sql.append(" where c.orgid IN "+schoolids);

        if(month!=null){
            if(month == 1){
                sql.append(" AND a.createtime>DATE_SUB(CURDATE(), INTERVAL 1 MONTH) ");
            }
            if (month == 6){
                sql.append(" AND a.createtime>DATE_SUB(CURDATE(), INTERVAL 6 MONTH) ");
            }
            if (month == 12){
                sql.append(" AND a.createtime>DATE_SUB(CURDATE(), INTERVAL 12 MONTH) ");
            }
        }
        if(StringUtils.isNotBlank(kssj)){
            sql.append(" AND a.createtime >= ?");
            list.add(kssj+" 00:00:00");
        }
        if(StringUtils.isNotBlank(jssj)){
            sql.append(" AND a.createtime <= ? ");
            list.add(jssj+" 23:59:59");
        }
        if(areatypeid!=null){
            sql.append(" AND f.areatypeid = ? ");
            list.add(areatypeid);
        }
        if(schoolcategoryid!=null){
            sql.append(" AND f.schoolcategoryid = ? ");
            list.add(schoolcategoryid);
        }

        return Db.find(sql.toString(), CommonUtils.listToArray(list));

    }

    public Page<Record> schoolsJieyueReportRecordList(int pageNumber, int pageSize,Integer month,String kssj,String jssj,Integer areatypeid,  Integer schoolcategoryid,String schoolids,Integer wxlx){
        String select ="SELECT f.id as orgid,f.orgname,f.orgtypeid, IFNULL(count(a.id),0) as outcnt,IFNULL(SUM(a.status in (1,2) ),0) as incnt ";
        StringBuilder sql = new StringBuilder(" FROM t_jieyue a INNER JOIN t_book_detail b ON a.bookdetailid = b.id " +
                "  INNER JOIN t_book c ON b.bookid = c.id " +
                "  INNER JOIN t_org f ON c.orgid = f.id  ");

        List list = new ArrayList();
        sql.append(" where f.isclose = 0 AND c.orgid IN "+schoolids);

        if(month!=null){
            if(month == 1){
                sql.append(" AND a.createtime>DATE_SUB(CURDATE(), INTERVAL 1 MONTH) ");
            }
            if (month == 6){
                sql.append(" AND a.createtime>DATE_SUB(CURDATE(), INTERVAL 6 MONTH) ");
            }
            if (month == 12){
                sql.append(" AND a.createtime>DATE_SUB(CURDATE(), INTERVAL 12 MONTH) ");
            }
        }

        if(StringUtils.isNotBlank(kssj)){
            sql.append(" AND a.createtime >= ?");
            list.add(kssj+" 00:00:00");
        }
        if(StringUtils.isNotBlank(jssj)){
            sql.append(" AND a.createtime <= ? ");
            list.add(jssj+" 23:59:59");
        }
        if(areatypeid!=null){
            sql.append(" AND f.areatypeid = ? ");
            list.add(areatypeid);
        }
        if(schoolcategoryid!=null){
            sql.append(" AND f.schoolcategoryid = ? ");
            list.add(schoolcategoryid);
        }

        sql.append("group by f.id,f.orgname");
        Page<Record> page = Db.paginate(pageNumber, pageSize,true,select,sql.toString(),CommonUtils.listToArray(list));

//        List list2 = Db.find(select+sql.toString(), CommonUtils.listToArray(list));
//        Page page = new Page(list2,1,list2.size(),1,list2.size());
        return page;

    }


    //馆藏变化汇总统计省市县
    //要求去掉为0的新增和注销
    public Page<Record> booksChangeProvincesList(int pageNumber,int pageSize,Integer orgid,Integer month,String kssj,String jssj,Integer areatypeid,
                                                Integer schoolcategoryid){
        Record orgRecord = Db.findById("t_org",orgid);
        Page<Record> page;
        if(CommonUtils.isNotNullOrNotEmpty(orgRecord) && orgRecord.getInt("orgtypeid") == 5) {
            page = Db.paginate(pageNumber, pageSize,"select id orgid,orgname,orgtypeid ","from t_org where id = ?",orgid);
        } else {
            page = Db.paginate(pageNumber, pageSize,"select id orgid,orgname,orgtypeid ","from t_org where parentid = ?",orgid);
        }

        for(Record record : page.getList()){
            String sql = "select  " +
                    "SUM(a.createtime<? AND (a.status !=3 OR DATE_FORMAT(a.destroytime,'%Y-%m-%d') = ?) and a.bookstatus=1) as qcs," +
                    "SUM(a.destroytime>=? and a.destroytime<? AND a.status =3) as zxs," +
                    "SUM(a.createtime>=? and a.createtime<? AND a.bookstatus=1) as xzs," +
                    "SUM(a.createtime<? AND a.status !=3 and a.bookstatus=1) as qms " +
                    " from t_book_detail a inner join t_book b on a.bookid=b.id inner join t_org o on o.id=b.orgid " +
                    "where o.status = 1 and o.isclose = 0 ";
            List value = new ArrayList();
            value.add(kssj+" 00:00:00");value.add(kssj);
            value.add(kssj+" 00:00:00");value.add(jssj+" 23:59:59");
            value.add(kssj+" 00:00:00");value.add(jssj+" 23:59:59");
            value.add(jssj+" 23:59:59");
            if(record.getInt("orgtypeid") == 5) {
                sql +="and o.id = ?  ";
                value.add(record.getStr("orgid"));
            } else {
                sql +="and o.parentcodes like ?  ";
                value.add("%"+record.getStr("orgid")+"%");
            }
            Record reportRecord;
            Record databaseRecord = Db.findFirst("select * from t_database where status = 1 and orgid = ?",record.getStr("orgid"));
            if(CommonUtils.isNotNullOrNotEmpty(databaseRecord)){
                reportRecord = Db.use(databaseRecord.getStr("databasename")).findFirst(sql,CommonUtils.listToArray(value));
            } else {
                reportRecord = Db.findFirst(sql,CommonUtils.listToArray(value));
            }
            record.setColumns(reportRecord);
        }
//        Record record = Db.findFirst("select * from t_database where status = 1 and orgid = ?",orgid);
//        boolean check = false;
//        String databasename = "";
//        if(CommonUtils.isNotNullOrNotEmpty(record)){
//            databasename = record.getStr("databasename");
//            check = true;
//        }
//        Record org;
//        if(check){
//            org = Db.use(databasename).findById("t_org", orgid);
//        } else {
//            org = Db.findById("t_org", orgid);
//        }
//        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//        Date date = new Date();
//        Calendar c = Calendar.getInstance();
//        c.setTime(date);
//        if(!StringUtils.isNotBlank(kssj) && !StringUtils.isNotBlank(jssj)){
//            c.add(Calendar.MONTH, -1);
//            Date d = c.getTime();
//            kssj = format.format(d);
//            jssj = format.format(date);
//        }
//        String select = "SELECT * ";
//        StringBuilder where = new StringBuilder(" FROM (" +
//                "select o.orgname,o.id as orgid,o.orgtypeid,o.parentid,o.isclose,o.id,o.schoolcategoryid,o.parentcodes, " +
//                "SUM(a.createtime<? AND (a.status !=3 OR DATE_FORMAT(a.destroytime,'%Y-%m-%d') = ?) and a.bookstatus=1) as qcs," +
//                "SUM(a.destroytime>=? and a.destroytime<? AND a.status =3) as zxs," +
//                "SUM(a.createtime>=? and a.createtime<? AND a.bookstatus=1) as xzs," +
//                "SUM(a.createtime<? AND a.status !=3 and a.bookstatus=1) as qms " +
//                " from t_book_detail a inner join t_book b on a.bookid=b.id inner join t_org o on o.id=b.orgid " +
//                " GROUP BY o.id ) t WHERE (t.zxs>0 OR t.xzs>0) ");
//        List value = new ArrayList();
//        value.add(kssj+" 00:00:00");value.add(kssj);
//        value.add(kssj+" 00:00:00");value.add(jssj+" 23:59:59");
//        value.add(kssj+" 00:00:00");value.add(jssj+" 23:59:59");
//        value.add(jssj+" 23:59:59");
//        if(org.getInt("orgtypeid") == 5){
//            where.append(" AND t.isclose = 0 AND t.id =? ");
//            value.add(orgid);
//        }else{
//            where.append(" AND t.isclose = 0 AND t.parentcodes like ? ");
//            value.add("%"+orgid+"%");
//        }
//        if(areatypeid!=null){
//            where.append(" AND t.areatypeid = ? ");
//            value.add(areatypeid);
//        }
//        if(schoolcategoryid!=null){
//            where.append(" AND t.schoolcategoryid = ? ");
//            value.add(schoolcategoryid);
//        }
//        String selects = "SELECT orgname,id as orgid,orgtypeid,parentcodes ";
//        StringBuilder wheres = new StringBuilder("FROM t_org ");
//        if(org.getInt("orgtypeid") == 5){
//            wheres.append(" WHERE isclose = 0 AND id =? ");
//        }else{
//            wheres.append(" WHERE isclose = 0 AND parentid =? ");
//        }
//        List value = new ArrayList();
//        value.add(orgid);
//        if(areatypeid!=null){
//            wheres.append(" AND t_org.areatypeid = ? ");
//            value.add(areatypeid);
//        }
//        if(schoolcategoryid!=null){
//            wheres.append(" AND t_org.schoolcategoryid = ? ");
//            value.add(schoolcategoryid);
//        }
//        Page<Record> page = Db.paginate(pageNumber, pageSize,selects,wheres.toString(),CommonUtils.listToArray(value));
//        Page<Record> page;
//        if(check){
//            page = Db.use(databasename).paginate(pageNumber, pageSize,select,where.toString(),CommonUtils.listToArray(value));
//        } else {
//            page = Db.paginate(pageNumber, pageSize,select,where.toString(),CommonUtils.listToArray(value));
//        }
//        for(Record r : page.getList()) {
//            int id = r.getInt("orgid");
//            String orgidstr = CommonUtils.codeAddOne(id, 6);
//            Record qcsR,zxsR,xzsR,qmsR;
//            if(r.getInt("orgtypeid") == 5){
//                Db.findFirst("select " +
//                        "SUM(a.createtime<? AND o.id = ? AND (a.status !=3 OR DATE_FORMAT(a.destroytime,'%Y-%m-%d') = ?) and a.bookstatus=1) as num," +
//                        "SUM(a.destroytime>=? and a.destroytime<? AND o.id = ? AND a.status =3) as num1," +
//                        "SUM(a.createtime>=? and a.createtime<? AND o.id = ? and a.bookstatus=1) as num2," +
//                        "SUM(a.createtime<? AND o.id = ? AND a.status !=3 and a.bookstatus=1) as num3 " +
//                        " from t_book_detail a inner join t_book b on a.bookid=b.id inner join t_org o on o.id=b.orgid " +
//                        " where a.bookstatus=1");
//                qcsR = Db.findFirst("select count(a.id) as num from t_book_detail a inner join t_book b on a.bookid=b.id inner join t_org o on o.id=b.orgid " +
//                                " where a.createtime<? AND o.id = ? AND (a.status !=3 OR DATE_FORMAT(a.destroytime,'%Y-%m-%d') = ?) and a.bookstatus=1",
//                        kssj+" 00:00:00",id,kssj);
//                zxsR = Db.findFirst("select count(a.id) as num from t_book_detail a inner join t_book b on a.bookid=b.id inner join t_org o on o.id=b.orgid " +
//                                "where a.destroytime>=? and a.destroytime<? AND o.id = ? AND a.status =3",
//                        kssj+" 00:00:00",jssj+" 23:59:59",id);
//                xzsR = Db.findFirst("select count(a.id) as num from t_book_detail a inner join t_book b on a.bookid=b.id inner join t_org o on o.id=b.orgid " +
//                                "where a.createtime>=? and a.createtime<? AND o.id = ? and a.bookstatus=1",
//                        kssj+" 00:00:00",jssj+" 23:59:59",id);
//                qmsR = Db.findFirst("select count(a.id) as num from t_book_detail a inner join t_book b on a.bookid=b.id inner join t_org o on o.id=b.orgid " +
//                                "where a.createtime<? AND o.id = ? AND a.status !=3 and a.bookstatus=1",
//                        jssj+" 23:59:59",id);
//            }else{
//                qcsR = Db.findFirst("select count(a.id) as num from t_book_detail a inner join t_book b on a.bookid=b.id inner join t_org o on o.id=b.orgid " +
//                                " where a.createtime<? AND o.parentcodes like ?  AND (a.status !=3 OR DATE_FORMAT(a.destroytime,'%Y-%m-%d') = ?) and a.bookstatus=1",
//                        kssj+" 00:00:00",r.get("parentcodes")+orgidstr+"%",kssj);
//                zxsR = Db.findFirst("select count(a.id) as num from t_book_detail a inner join t_book b on a.bookid=b.id inner join t_org o on o.id=b.orgid " +
//                                "where a.destroytime>=? and a.destroytime<? AND o.parentcodes like ? AND a.status =3",
//                        kssj+" 00:00:00",jssj+" 23:59:59",r.get("parentcodes")+orgidstr+"%");
//                xzsR = Db.findFirst("select count(a.id) as num from t_book_detail a inner join t_book b on a.bookid=b.id inner join t_org o on o.id=b.orgid " +
//                                "where a.createtime>=? and a.createtime<? AND o.parentcodes like ? and a.bookstatus=1",
//                        kssj+" 00:00:00",jssj+" 23:59:59",r.get("parentcodes")+orgidstr+"%");
//                qmsR = Db.findFirst("select count(a.id) as num from t_book_detail a inner join t_book b on a.bookid=b.id inner join t_org o on o.id=b.orgid " +
//                                "where a.createtime<? AND o.parentcodes like ? AND a.status !=3 and a.bookstatus=1",
//                        jssj+" 23:59:59",r.get("parentcodes")+orgidstr+"%");
//            }
//
//
//
//            Integer qcs = qcsR.getInt("num");
//            Integer zxs = zxsR.getInt("num");
//            Integer xzs = xzsR.getInt("num");
//            Integer qms = qmsR.getInt("num");
//
//            r.set("qcs",qcs);
//            r.set("zxs",zxs);
//            r.set("xzs",xzs);
//            r.set("qms",qms);
//        }
        return page;
    }
    //馆藏变化统计合计
    //要求去掉为0的新增和注销
    public Record booksChangeSummaryTotalList(Integer orgid,String kssj,String jssj,Integer areatypeid,
                                              Integer schoolcategoryid,String schoolids){
        int qcs = 0;
        int xzs = 0;
        int zxs = 0;
        int qms = 0;
        Record orgRecord = Db.findById("t_org",orgid);
        List<Record> list;
        if(CommonUtils.isNotNullOrNotEmpty(orgRecord) && orgRecord.getInt("orgtypeid") == 5) {
            list = Db.find("select id orgid,orgname,orgtypeid from t_org where id = ?",orgid);
        } else {
            list = Db.find("select id orgid,orgname,orgtypeid from t_org where parentid = ?",orgid);
        }
        for(Record record : list){
            String sql = "select  " +
                    "ifnull(SUM(a.createtime<? AND (a.status !=3 OR DATE_FORMAT(a.destroytime,'%Y-%m-%d') = ?) and a.bookstatus=1),0) as qcs," +
                    "ifnull(SUM(a.destroytime>=? and a.destroytime<? AND a.status =3),0) as zxs," +
                    "ifnull(SUM(a.createtime>=? and a.createtime<? AND a.bookstatus=1),0) as xzs," +
                    "ifnull(SUM(a.createtime<? AND a.status !=3 and a.bookstatus=1),0) as qms " +
                    " from t_book_detail a inner join t_book b on a.bookid=b.id inner join t_org o on o.id=b.orgid " +
                    "where o.status = 1 and o.isclose = 0 ";
            List value = new ArrayList();
            value.add(kssj+" 00:00:00");value.add(kssj);
            value.add(kssj+" 00:00:00");value.add(jssj+" 23:59:59");
            value.add(kssj+" 00:00:00");value.add(jssj+" 23:59:59");
            value.add(jssj+" 23:59:59");
            if(record.getInt("orgtypeid") == 5) {
                sql +="and o.id = ?  ";
                value.add(record.getStr("orgid"));
            } else {
                sql +="and o.parentcodes like ?  ";
                value.add("%"+record.getStr("orgid")+"%");
            }
            Record reportRecord;
            Record databaseRecord = Db.findFirst("select * from t_database where status = 1 and orgid = ?",record.getStr("orgid"));
            if(CommonUtils.isNotNullOrNotEmpty(databaseRecord)){
                reportRecord = Db.use(databaseRecord.getStr("databasename")).findFirst(sql,CommonUtils.listToArray(value));
            } else {
                reportRecord = Db.findFirst(sql,CommonUtils.listToArray(value));
            }
//            record.setColumns(reportRecord);

            int qc = reportRecord.getInt("qcs");
            int zx = reportRecord.getInt("zxs");
            int xz = reportRecord.getInt("xzs");
            int qm = reportRecord.getInt("qms");
//            if(xz >0 || zx > 0){
                qcs += qc;
                xzs += xz;
                zxs += zx;
                qms += qm;
//            }
        }

//        List<Record> databaseList = Db.find("select * from t_database where status = 1 and  parentorgid = ?",orgid);
//        if(databaseList.size() == 0) {
//            databaseList = Db.find("select * from t_database where status = 1 and  orgid = ?",orgid);
//        }
//        int i = -1;
//        do {
//            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
//            Date date = new Date();
//            Calendar c = Calendar.getInstance();
//            c.setTime(date);
//            if(!StringUtils.isNotBlank(kssj) && !StringUtils.isNotBlank(jssj)){
//                c.add(Calendar.MONTH, -1);
//                Date d = c.getTime();
//                kssj = format.format(d);
//                jssj = format.format(date);
//            }
//        String select = "select SUM((SELECT COUNT(a.id) as booknum from t_book_detail a INNER JOIN t_book b ON a.bookid = b.id " +
//                "WHERE a.bookstatus=1 AND (a.status !=3 OR DATE_FORMAT(a.destroytime,'%Y-%m-%d') = '"+kssj+"') AND a.createtime< '"+kssj+" 00:00:00' AND b.orgid =t_org.id)) as qcs," +
//                "SUM((SELECT COUNT(a.id) as booknum from t_book_detail a INNER JOIN t_book b ON a.bookid = b.id " +
//                "WHERE a.`status` = 3 AND a.destroytime >= '"+kssj+" 00:00:00' AND a.destroytime < '"+jssj+" 23:59:59' AND b.orgid =t_org.id)) as zxs," +
//                "SUM((SELECT COUNT(a.id) as booknum from t_book_detail a INNER JOIN t_book b ON a.bookid = b.id " +
//                "WHERE a.bookstatus=1 AND a.createtime >= '"+kssj+" 00:00:00' AND a.createtime < '"+jssj+" 23:59:59' AND b.orgid =t_org.id)) as xzs," +
//                "SUM((SELECT COUNT(a.id) as booknum from t_book_detail a INNER JOIN t_book b ON a.bookid = b.id " +
//                "WHERE a.status !=3 AND a.createtime < '"+jssj+" 23:59:59' AND b.orgid =t_org.id))as qms ";
//        StringBuilder where = new StringBuilder(" from t_org where 1=1 ");
//        List list = new ArrayList();
//        if(orgid!=null){
//            String parentcodes = CommonUtils.codeAddOne(orgid, 6);
//            where.append(" AND isclose = 0 and (parentcodes LIKE ? or id = ?)");
//            list.add("%"+parentcodes+"%");
//            list.add(orgid);
//        }else {
//            where.append(" AND isclose = 0 and id IN "+schoolids);
//        }
//        if(areatypeid!=null){
//            where.append(" AND t_org.areatypeid = ? ");
//            list.add(areatypeid);
//        }
//        if(schoolcategoryid!=null){
//            where.append(" AND t_org.schoolcategoryid = ? ");
//            list.add(schoolcategoryid);
//        }
//            Record org = Db.findById("t_org", orgid);
//            String select = "SELECT * ";
//            StringBuilder where = new StringBuilder(" FROM (" +
//                    "select o.orgname,o.id as orgid,o.orgtypeid,o.parentid,o.isclose,o.id,o.schoolcategoryid,o.parentcodes, " +
//                    "SUM(a.createtime<? AND (a.status !=3 OR DATE_FORMAT(a.destroytime,'%Y-%m-%d') = ?) and a.bookstatus=1) as qcs," +
//                    "SUM(a.destroytime>=? and a.destroytime<? AND a.status =3) as zxs," +
//                    "SUM(a.createtime>=? and a.createtime<? AND a.bookstatus=1) as xzs," +
//                    "SUM(a.createtime<? AND a.status !=3 and a.bookstatus=1) as qms " +
//                    " from t_book_detail a inner join t_book b on a.bookid=b.id inner join t_org o on o.id=b.orgid " +
//                    " GROUP BY o.id ) t WHERE (t.zxs>0 OR t.xzs>0) ");
//            List value = new ArrayList();
//            value.add(kssj+" 00:00:00");value.add(kssj);
//            value.add(kssj+" 00:00:00");value.add(jssj+" 23:59:59");
//            value.add(kssj+" 00:00:00");value.add(jssj+" 23:59:59");
//            value.add(jssj+" 23:59:59");
//            if(org.getInt("orgtypeid") == 5){
//                where.append(" AND t.isclose = 0 AND t.id =? ");
//                value.add(orgid);
//            }else{
//                where.append(" AND t.isclose = 0 AND t.parentcodes like ? ");
//                value.add("%"+orgid+"%");
//            }
//            if(areatypeid!=null){
//                where.append(" AND t.areatypeid = ? ");
//                value.add(areatypeid);
//            }
//            if(schoolcategoryid!=null){
//                where.append(" AND t.schoolcategoryid = ? ");
//                value.add(schoolcategoryid);
//            }
//            List<Record> r = Db.find(select+where,CommonUtils.listToArray(value));
//
//            for(Record rs : r){
//                int qc = rs.getInt("qcs");
//                int zx = rs.getInt("zxs");
//                int xz = rs.getInt("xzs");
//                int qm = rs.getInt("qms");
//                if(xz >0 || zx > 0){
//                    qcs += qc;
//                    xzs += xz;
//                    zxs += zx;
//                    qms += qm;
//                }
//            }
//        }while (i < databaseList.size());
//
        Record total = new Record();
        total.set("qcs",qcs);
        total.set("xzs",xzs);
        total.set("zxs",zxs);
        total.set("qms",qms);
        //Record total = Db.findFirst(select+where,CommonUtils.listToArray(value));
        return total;
    }
    //藏书新增记录
    public Page<Record> booksNewlyAddedList(int pageNumber,int pageSize,Integer orgid,Integer month,String kssj,String jssj,Integer areatypeid,
                                               Integer schoolcategoryid,String schoolids,String countent){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        if(!StringUtils.isNotBlank(kssj) && !StringUtils.isNotBlank(jssj)){
            c.add(Calendar.MONTH, -1);
            Date d = c.getTime();
            kssj = format.format(d);
            jssj = format.format(date);
        }
        String select = "SELECT a.isbn,a.title,a.author,a.publisher,a.price,COUNT(b.id) fbs ";
        StringBuilder where = new StringBuilder("from t_book a inner JOIN t_book_detail b ON a.id = b.bookid inner JOIN t_org c ON a.orgid = c.id " +
                "WHERE b.bookstatus= 1 ");
        List list = new ArrayList();
        if(orgid!=null){
            String parentcodes = CommonUtils.codeAddOne(orgid, 6);
            where.append(" and (c.parentcodes LIKE ? or c.id = ?) ");
            list.add("%"+parentcodes+"%");
            list.add(orgid);
        }else {
            where.append("and a.orgid IN "+schoolids);
        }
        if(areatypeid!=null){
            where.append(" AND c.areatypeid = ? ");
            list.add(areatypeid);
        }
        if(schoolcategoryid!=null){
            where.append(" AND c.schoolcategoryid = ? ");
            list.add(schoolcategoryid);
        }
        if(StringUtils.isNotBlank(kssj)){
            where.append(" AND b.createtime >= ? ");
            list.add(kssj+" 00:00:00");
        }
        if(StringUtils.isNotBlank(jssj)){
            where.append(" AND b.createtime <= ? ");
            list.add(jssj+" 23：59：59");
        }
        if(StringUtils.isNotBlank(countent)){
            where.append(" AND a.title LIKE ? OR a.author LIKE ? ");
            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> booksCancelList(int pageNumber,int pageSize,Integer orgid,String kssj,String jssj,Integer areatypeid,
                                            Integer schoolcategoryid,String schoolids,String countent){
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        Calendar c = Calendar.getInstance();
        c.setTime(date);
        if(!StringUtils.isNotBlank(kssj) && !StringUtils.isNotBlank(jssj)){
            c.add(Calendar.MONTH, -1);
            Date d = c.getTime();
            kssj = format.format(d);
            jssj = format.format(date);
        }
        String select = "SELECT a.isbn,a.title,a.author,a.publisher,d.createtime,d.reason,a.price,b.barcode ";
        StringBuilder where = new StringBuilder("from t_book a LEFT JOIN t_book_detail b ON a.id = b.bookid LEFT JOIN t_org c ON a.orgid = c.id " +
                " LEFT JOIN t_book_zhuxiao d ON b.id = d.bookdetailid WHERE b.`status` = 3 ");
        List list = new ArrayList();
        if(orgid!=null){
            String parentcodes = CommonUtils.codeAddOne(orgid, 6);
            where.append(" and (c.parentcodes LIKE ? or c.id = ?) ");
            list.add("%"+parentcodes+"%");
            list.add(orgid);
        }else {
            where.append("and a.orgid IN "+schoolids);
        }
        if(areatypeid!=null){
            where.append(" AND c.areatypeid = ? ");
            list.add(areatypeid);
        }
        if(schoolcategoryid!=null){
            where.append(" AND c.schoolcategoryid = ? ");
            list.add(schoolcategoryid);
        }
        if(StringUtils.isNotBlank(kssj)){
            where.append(" AND b.destroytime >= ?");
            list.add(kssj+" 00:00:00");
        }
        if(StringUtils.isNotBlank(jssj)){
            where.append(" AND b.destroytime <= ? ");
            list.add(jssj+" 23:59:59");
        }
        if(StringUtils.isNotBlank(countent)){
            where.append(" AND (a.title LIKE ? OR a.author LIKE ?) ");
            list.add(countent);
            list.add(countent);
        }
        where.append(" GROUP BY b.id ");
        Page<Record> page = Db.paginate(pageNumber, pageSize,select,where.toString(), CommonUtils.listToArray(list));
        return page;
    }
    //生均及增量统计省市县端
    public Page<Record> incrementSummarylListProvinces(int pageNumber,int pageSize,Integer orgid,String kssj,String jssj,Integer areatypeid, Integer schoolcategoryid,Integer schoolid){
        //如果传入时间为空则默认为1个月
        if(!StringUtils.isNotBlank(kssj) && !StringUtils.isNotBlank(jssj)){
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Date date = new Date();
            Calendar c = Calendar.getInstance();
            c.setTime(date);
            c.add(Calendar.MONTH, -1);
            Date d = c.getTime();
            kssj = format.format(d);
            jssj = format.format(date);
        }

        String select = "SELECT b.orgname,b.id as orgid,a.category,b.orgtypeid ";
        StringBuilder where = new StringBuilder("FROM t_org b LEFT JOIN t_schoolcategory a ON b.schoolcategoryid = a.id " +
                "WHERE b.isclose = 0 AND b.parentid = ?");
        List value = new ArrayList();
        boolean check = false;
        String databasename = "";
        Record databaseRecord = Db.findFirst("select orgid,databasename from t_database where status = 1 and orgid = ?",orgid);
        if(CommonUtils.isNotNullOrNotEmpty(databaseRecord)){
//            value.add(databaseRecord.getInt("suborgid"));
            check = true;
            databasename = databaseRecord.getStr("databasename");

        }
        value.add(orgid);
        if(areatypeid!=null){
            where.append(" AND b.areatypeid = ? ");
            value.add(areatypeid);
        }
        if(schoolcategoryid!=null){
            where.append(" AND b.schoolcategoryid = ? ");
            value.add(schoolcategoryid);
        }
        if(schoolid!=null){
            where.append(" AND b.id = ? ");
            value.add(schoolid);
        }
        Page<Record> page;
        if(check){
            page = Db.use(databasename).paginate(pageNumber, pageSize,select,where.toString(),CommonUtils.listToArray(value));
        } else {
            page = Db.paginate(pageNumber, pageSize,select,where.toString(),CommonUtils.listToArray(value));
        }
//        if(page.getTotalRow() ==0 || page.getList().isEmpty()){
//            String select1 = "SELECT b.orgname,b.id as orgid,a.category,b.orgtypeid ";
//            StringBuilder where1 = new StringBuilder("FROM t_org b LEFT JOIN t_schoolcategory a ON b.schoolcategoryid = a.id " +
//                    "WHERE b.id = ?");
//            List value1 = new ArrayList();
//            value1.add(orgid);
//            if(areatypeid!=null){
//                where1.append(" AND b.areatypeid = ? ");
//                value1.add(areatypeid);
//            }
//            if(schoolcategoryid!=null){
//                where1.append(" AND b.schoolcategoryid = ? ");
//                value1.add(schoolcategoryid);
//            }
//            page = Db.paginate(pageNumber, pageSize,select1,where1.toString(),CommonUtils.listToArray(value1));
//        }
        for(Record r : page.getList()){
            int id = r.getInt("orgid");
            String parentcodes = CommonUtils.codeAddOne(id,6);
            //生均年度递增 = 当天时间 - 上一年最后一天
            String year = DateUtils.getYear();
            String lastDate = DateUtils.getTimeToDay(DateUtils.getLastDayOfYear(Integer.parseInt(year) - 1));
            String currentDate = DateUtils.getTimeToDay(new Date());
            String  selects = "SELECT SUM(a.students+a.czstudents+a.gzstudents) as students," +
                    "SUM((SELECT COUNT(b.id) from t_reader b WHERE b.orgid = a.id AND b.status != 4)) as dzs," +
                    "SUM((SELECT COUNT(d.id) from t_book c LEFT JOIN t_book_detail d ON c.id = d.bookid " +
                    "WHERE c.orgid = a.id AND d.bookstatus = 1 AND d.`status` !=3 AND c.createtime >= ?)) as tssl," +
                    "SUM((SELECT COUNT(d.id) from t_book c LEFT JOIN t_book_detail d ON c.id = d.bookid " +
                    "WHERE c.orgid = a.id AND d.bookstatus = 1 AND d.`status` !=3 AND c.createtime <= ? )) as lasttssl," +
                    "SUM((SELECT COUNT(d.id) from t_book c LEFT JOIN t_book_detail d ON c.id = d.bookid " +
                    "WHERE c.orgid = a.id AND d.bookstatus = 1 AND d.`status` !=3 AND c.createtime <= ? )) as currenttssl," +
                    "SUM((SELECT COUNT(d.id) from t_book c LEFT JOIN t_book_detail d ON c.id = d.bookid " +
                    "WHERE d.`status` = 1 AND c.orgid = a.id)) as jcsl";
            StringBuilder wheres = new StringBuilder(" from t_org a where (a.parentcodes LIKE ? or a.id = ?)");
            databaseRecord = Db.findFirst("select orgid,databasename from t_database where status = 1 and orgid = ?",id);
            Record rs;
            if(check ){
                rs = Db.use(databasename).findFirst(selects+wheres,kssj,lastDate+" 23:59:59",currentDate+" 23:59:59","%"+id+"%",id);
            }else if(CommonUtils.isNotNullOrNotEmpty(databaseRecord)){
                rs = Db.use(databaseRecord.getStr("databasename")).findFirst(selects+wheres,kssj,lastDate+" 23:59:59",currentDate+" 23:59:59","%"+id+"%",id);
            }else {
                rs = Db.findFirst(selects+wheres,kssj,lastDate+" 23:59:59",currentDate+" 23:59:59","%"+parentcodes+"%",id);
            }
            Integer students = 0;
            Integer lasttssl = 0;
            Integer currenttssl = 0;
            Integer jcsl = 0;
            Integer dzs = 0;
            float sjcs = 0f;
            float rjjycs =0f;
            float nddz = 0f;
            float lastsjcs = 0f;
            float currentsjcs = 0f;
            if(rs!=null){
                Integer tssl = rs.getInt("tssl");
                students = rs.getInt("students");
                lasttssl = rs.getInt("lasttssl");
                currenttssl = rs.getInt("currenttssl");
                if(tssl!=null&&students!=null){
                    sjcs = (float) tssl / (float)students;
                }
                if(lasttssl!=null&&students!=null){
                    lastsjcs = (float) lasttssl / (float)students;
                }
                if(currenttssl!=null&&students!=null){
                    currentsjcs = (float) currenttssl / (float)students;
                }
                jcsl = rs.getInt("jcsl");
                dzs = rs.getInt("dzs");
                if(jcsl!=null&&dzs!=null){
                    rjjycs = (float)jcsl / (float)dzs;
                }

                nddz = currentsjcs - lastsjcs;
                if(nddz<0){
                    nddz = 0;
                }
                r.set("sjcs",sjcs);
                r.set("rjjycs",rjjycs);
                r.set("nddz",nddz);
                r.set("tssl",tssl);
                r.set("students",students);
                r.set("jcsl",jcsl);
                r.set("dzs",dzs);
            }
        }
        return page;
    }

//    //生均及增量统计合计
    public Record incrementSummarylTotalList(Integer orgid,String kssj,String jssj,Integer areatypeid, Integer schoolcategoryid,Integer schoolid){
        List<Record> databaseList = Db.find("select * from t_database where status = 1 and  parentorgid = ?",orgid);
        if(databaseList.size() == 0) {
            databaseList = Db.find("select * from t_database where status = 1 and  orgid = ?",orgid);
        }
        Integer tssl = 0;
        Integer students = 0;
        Integer jcsl = 0;
        Integer dzs = 0;
        Integer lasttssl = 0;
        Integer currenttssl = 0;
        int i = -1;
        do {
            //如果传入时间为空则默认为1个月
            if(!StringUtils.isNotBlank(kssj) && !StringUtils.isNotBlank(jssj)){
                SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
                Date date = new Date();
                Calendar c = Calendar.getInstance();
                c.setTime(date);
                c.add(Calendar.MONTH, -1);
                Date d = c.getTime();
                kssj = format.format(d);
                jssj = format.format(date);
            }
            String parentcodes = CommonUtils.codeAddOne(orgid,6);
            //生均年度递增 = 当天时间 - 上一年最后一天
            String year = DateUtils.getYear();
            String lastDate = DateUtils.getTimeToDay(DateUtils.getLastDayOfYear(Integer.parseInt(year) - 1));
            String currentDate = DateUtils.getTimeToDay(new Date());
            String  select = "SELECT SUM(a.students+a.czstudents+a.gzstudents) as students," +
                    "SUM((SELECT COUNT(b.id) from t_reader b WHERE b.orgid = a.id AND b.status != 4)) as dzs," +
                    "SUM((SELECT COUNT(d.id) from t_book c LEFT JOIN t_book_detail d ON c.id = d.bookid " +
                    "WHERE c.orgid = a.id AND d.bookstatus = 1 AND d.`status` !=3 AND c.createtime >=? )) as tssl," +
                    "SUM((SELECT COUNT(d.id) from t_book c LEFT JOIN t_book_detail d ON c.id = d.bookid " +
                    "WHERE c.orgid = a.id AND d.bookstatus = 1 AND d.`status` !=3 AND c.createtime <=? )) as lasttssl," +
                    "SUM((SELECT COUNT(d.id) from t_book c LEFT JOIN t_book_detail d ON c.id = d.bookid " +
                    "WHERE c.orgid = a.id AND d.bookstatus = 1 AND d.`status` !=3 AND c.createtime <= ? )) as currenttssl," +
                    "SUM((SELECT COUNT(d.id) from t_book c LEFT JOIN t_book_detail d ON c.id = d.bookid " +
                    "WHERE d.`status` = 1 AND c.orgid = a.id)) as jcsl ";
            StringBuilder where = new StringBuilder(" from t_org a where (a.parentcodes LIKE ? or a.id = ?) AND a.isclose = 0 ");
            List list = new ArrayList();
            list.add(kssj);
            list.add(lastDate+" 23:59:59");
            list.add(currentDate+" 23:59:59");
            list.add("%"+parentcodes+"%");
            list.add(orgid);
            if(areatypeid!=null){
                where.append(" AND a.areatypeid = ? ");
                list.add(areatypeid);
            }
            if(schoolcategoryid!=null){
                where.append(" AND a.schoolcategoryid = ? ");
                list.add(schoolcategoryid);
            }
            if(schoolid!=null){
                where.append(" AND a.id = ? ");
                list.add(schoolid);
            }
            Record r;
            if(i >= 0 && i < databaseList.size()){
                r = Db.use(databaseList.get(i).getStr("databasename")).findFirst(select+where, CommonUtils.listToArray(list));
            } else {
                r = Db.findFirst(select+where, CommonUtils.listToArray(list));
            }
            tssl += CommonUtils.isNotNullOrNotEmpty(r.get("tssl"))?r.getInt("tssl"):0;
            students += CommonUtils.isNotNullOrNotEmpty(r.get("students"))?r.getInt("students"):0;
            jcsl += CommonUtils.isNotNullOrNotEmpty(r.get("jcsl"))?r.getInt("jcsl"):0;
            dzs += CommonUtils.isNotNullOrNotEmpty(r.get("dzs"))?r.getInt("dzs"):0;
            lasttssl += CommonUtils.isNotNullOrNotEmpty(r.get("lasttssl"))?r.getInt("lasttssl"):0;
            currenttssl += CommonUtils.isNotNullOrNotEmpty(r.get("currenttssl"))?r.getInt("currenttssl"):0;
            i ++;
            if(i >= 0 && i < databaseList.size()){
                orgid = databaseList.get(i).getInt("orgid");
            }
        }while (i < databaseList.size());
        float lastsjcs = 0f;
        float currentsjcs = 0f;
        float rjjycs = 0f;
        if(dzs != null){
            rjjycs = (float)jcsl / (float)dzs;
        }
        float sjcs = 0f;
        if(students != null){
            sjcs = (float) tssl / (float)students;
            lastsjcs = (float) lasttssl / (float)students;
            currentsjcs = (float) currenttssl / (float)students;
        }
        float nddz = currentsjcs - lastsjcs;
        Record rercord = new Record();
        rercord.set("sjcs",sjcs);
        rercord.set("rjjycs",rjjycs);
        rercord.set("nddz",nddz);
        rercord.set("tssl",tssl);
        rercord.set("students",students);
        rercord.set("jcsl",jcsl);
        rercord.set("dzs",dzs);
        rercord.set("lasttssl",lasttssl);
        rercord.set("currenttssl",currenttssl);
        return rercord;
    }

    public Record schoolName(Integer orgid){
        Record r = Db.findFirst("select orgname from t_org where id = ?",orgid);
        return r;
    }
    public Record readerName(Integer readerid){
        Record r = Db.findFirst("SELECT realname FROM t_reader WHERE id = ?",readerid);
        return r;
    }


    public Map<String,Object>  standardBoookNumList(Integer areatypeid,Integer schoolcategoryid,Integer orgid,int orgtype,List<Standardbooknum> standardbooknumList,Integer toporgid,String[] bookrate,String[] periodicalrate,String[] toolbookrate){
        StringBuilder where = new StringBuilder(" SELECT schoolcategoryid, category,IFNULL(a.xss,0) as xss, IFNULL(tszcs,0) as tszcs,IFNULL(a.qkzs,0) as qkzs, IFNULL(a.toolnum,0) as toolnum  FROM t_dayreport a " +
                " INNER JOIN t_org b ON a.orgid = b.id  " +
                " INNER JOIN t_schoolcategory c on b.schoolcategoryid = c.id  " +
                " WHERE  isclose =0 and  a.createtime = DATE_FORMAT(NOW(),'%Y-%m-%d')");
        List list = new ArrayList();
        if(orgtype== 5){
            where.append(" and b.id=?");
            list.add(orgid);
        }else {
            String parentcodes = CommonUtils.codeAddOne(orgid,6);
            where.append(" AND b.parentcodes LIKE ? ");
            list.add("%"+parentcodes+"%");
        }

        if(areatypeid!=null){
            where.append(" AND b.areatypeid = ? ");
            list.add(areatypeid);
        }
        if(schoolcategoryid!=null){
            where.append(" AND b.schoolcategoryid = ? ");
            list.add(schoolcategoryid);
        }

        if (StringUtils.isNotEmpty(bookrate[0])){
            where.append(" AND CalcStandardRate(").append(toporgid).append(",b.schoolcategoryid,1,tszcs,xss) >= ? ");
            list.add(Float.parseFloat(bookrate[0])/100);
        }

        if (StringUtils.isNotEmpty(bookrate[1])){
            where.append(" AND CalcStandardRate(").append(toporgid).append(",b.schoolcategoryid,1,tszcs,xss) <= ? ");
            list.add(Float.parseFloat(bookrate[1])/100);
        }

        if (StringUtils.isNotEmpty(periodicalrate[0])){
            where.append(" AND CalcStandardRate(").append(toporgid).append(",b.schoolcategoryid,2,qkzs,xss) >= ? ");
            list.add(Float.parseFloat(periodicalrate[0])/100);
        }

        if (StringUtils.isNotEmpty(periodicalrate[1])){
            where.append(" AND CalcStandardRate(").append(toporgid).append(",b.schoolcategoryid,2,qkzs,xss) <= ? ");
            list.add(Float.parseFloat(periodicalrate[1])/100);
        }

        if (StringUtils.isNotEmpty(toolbookrate[0])){
            where.append(" AND CalcStandardRate(").append(toporgid).append(",b.schoolcategoryid,3,toolnum,xss) >= ? ");
            list.add(Float.parseFloat(toolbookrate[0])/100);
        }

        if (StringUtils.isNotEmpty(toolbookrate[1])){
            where.append(" AND CalcStandardRate(").append(toporgid).append(",b.schoolcategoryid,3,toolnum,xss) <= ? ");
            list.add(Float.parseFloat(toolbookrate[1])/100);
        }

        Record databaseRecord = Db.findFirst("select * from t_database where status = 1 and orgid = ?",orgid);
        String databasename = "";
        if(CommonUtils.isNotNullOrNotEmpty(databaseRecord)){
            databasename = databaseRecord.getStr("databasename");
        }

        List<Record> recordList;
        if("".equals(databasename)){
            recordList= Db.find(where.toString(), CommonUtils.listToArray(list));
        } else {
            recordList= Db.use(databasename).find(where.toString(), CommonUtils.listToArray(list));
        }
        Map<String,Object> resultMap =new HashMap<String, Object>();
        if (!recordList.isEmpty()){

            resultMap.put("totalBookNum",new BigDecimal(0));
            resultMap.put("tszcs",new BigDecimal(0));
            resultMap.put("qkzs",new BigDecimal(0));
            resultMap.put("xss",new BigDecimal(0));
            resultMap.put("periodicalNum",new BigDecimal(0));
            resultMap.put("toolsBookNum",new BigDecimal(0));
            resultMap.put("toolnum",new BigDecimal(0));
            resultMap.put("bookNum",new BigDecimal(0));
            for (Record record:recordList){
                Map<String,BigDecimal>  standardMap= calculateStandard(record.get("schoolcategoryid"),standardbooknumList);
                resultMap.put("totalBookNum",((BigDecimal)resultMap.get("totalBookNum")).add(new BigDecimal(record.getInt("xss")).multiply( standardMap.get("bookNum"))));
                resultMap.put("tszcs",((BigDecimal)resultMap.get("tszcs")).add(new BigDecimal(record.getInt("tszcs"))));
                resultMap.put("qkzs",((BigDecimal)resultMap.get("qkzs")).add(new BigDecimal(record.getInt("qkzs"))));
                resultMap.put("xss",((BigDecimal)resultMap.get("xss")).add(new BigDecimal(record.getInt("xss"))));
                resultMap.put("periodicalNum",((BigDecimal)resultMap.get("periodicalNum")).add(standardMap.get("periodicalNum")));
                resultMap.put("toolsBookNum",((BigDecimal)resultMap.get("toolsBookNum")).add(standardMap.get("toolsBookNum")));
                resultMap.put("toolnum",((BigDecimal)resultMap.get("toolnum")).add(new BigDecimal(record.getInt("toolnum"))));
                resultMap.put("bookNum",((BigDecimal)resultMap.get("bookNum")).add(standardMap.get("bookNum")));
                resultMap.put("category",record.get("category"));
            }
            if(orgtype == 1){ //只有在省级访问的时候才统计每个服务器汇总数据
                List<Record> databaseList = Db.find("select * from t_database where status = 1 and parentorgid = ?",orgid);
                for(Record databaserecord : databaseList) {
                    where = new StringBuilder(" SELECT schoolcategoryid, category,IFNULL(a.xss,0) as xss, IFNULL(tszcs,0) as tszcs,IFNULL(a.qkzs,0) as qkzs, IFNULL(a.toolnum,0) as toolnum  FROM t_dayreport a " +
                            " INNER JOIN t_org b ON a.orgid = b.id  " +
                            " INNER JOIN t_schoolcategory c on b.schoolcategoryid = c.id  " +
                            " WHERE  isclose =0 and  a.createtime = DATE_FORMAT(NOW(),'%Y-%m-%d')");
                    list = new ArrayList();
                    if(orgtype== 5){
                        where.append(" and b.id=?");
                        list.add(databaserecord.getInt("orgid"));
                    }else {
                        String parentcodes = CommonUtils.codeAddOne(orgid,6);
                        where.append(" AND b.parentcodes LIKE ? ");
                        list.add("%"+databaserecord.getInt("orgid")+"%");
                    }

                    if(areatypeid!=null){
                        where.append(" AND b.areatypeid = ? ");
                        list.add(areatypeid);
                    }
                    if(schoolcategoryid!=null){
                        where.append(" AND b.schoolcategoryid = ? ");
                        list.add(schoolcategoryid);
                    }

                    if (StringUtils.isNotEmpty(bookrate[0])){
                        where.append(" AND CalcStandardRate(").append(toporgid).append(",b.schoolcategoryid,1,tszcs,xss) >= ? ");
                        list.add(Float.parseFloat(bookrate[0])/100);
                    }

                    if (StringUtils.isNotEmpty(bookrate[1])){
                        where.append(" AND CalcStandardRate(").append(toporgid).append(",b.schoolcategoryid,1,tszcs,xss) <= ? ");
                        list.add(Float.parseFloat(bookrate[1])/100);
                    }

                    if (StringUtils.isNotEmpty(periodicalrate[0])){
                        where.append(" AND CalcStandardRate(").append(toporgid).append(",b.schoolcategoryid,2,qkzs,xss) >= ? ");
                        list.add(Float.parseFloat(periodicalrate[0])/100);
                    }

                    if (StringUtils.isNotEmpty(periodicalrate[1])){
                        where.append(" AND CalcStandardRate(").append(toporgid).append(",b.schoolcategoryid,2,qkzs,xss) <= ? ");
                        list.add(Float.parseFloat(periodicalrate[1])/100);
                    }

                    if (StringUtils.isNotEmpty(toolbookrate[0])){
                        where.append(" AND CalcStandardRate(").append(toporgid).append(",b.schoolcategoryid,3,toolnum,xss) >= ? ");
                        list.add(Float.parseFloat(toolbookrate[0])/100);
                    }

                    if (StringUtils.isNotEmpty(toolbookrate[1])){
                        where.append(" AND CalcStandardRate(").append(toporgid).append(",b.schoolcategoryid,3,toolnum,xss) <= ? ");
                        list.add(Float.parseFloat(toolbookrate[1])/100);
                    }

                    recordList= Db.use(databaserecord.getStr("databasename")).find(where.toString(), CommonUtils.listToArray(list));
                    for (Record record:recordList){
                        Map<String,BigDecimal>  standardMap= calculateStandard(record.get("schoolcategoryid"),standardbooknumList);
                        resultMap.put("totalBookNum",((BigDecimal)resultMap.get("totalBookNum")).add(new BigDecimal(record.getInt("xss")).multiply( standardMap.get("bookNum"))));
                        resultMap.put("tszcs",((BigDecimal)resultMap.get("tszcs")).add(new BigDecimal(record.getInt("tszcs"))));
                        resultMap.put("qkzs",((BigDecimal)resultMap.get("qkzs")).add(new BigDecimal(record.getInt("qkzs"))));
                        resultMap.put("xss",((BigDecimal)resultMap.get("xss")).add(new BigDecimal(record.getInt("xss"))));
                        resultMap.put("periodicalNum",((BigDecimal)resultMap.get("periodicalNum")).add(standardMap.get("periodicalNum")));
                        resultMap.put("toolsBookNum",((BigDecimal)resultMap.get("toolsBookNum")).add(standardMap.get("toolsBookNum")));
                        resultMap.put("toolnum",((BigDecimal)resultMap.get("toolnum")).add(new BigDecimal(record.getInt("toolnum"))));
                        resultMap.put("bookNum",((BigDecimal)resultMap.get("bookNum")).add(standardMap.get("bookNum")));
                        resultMap.put("category",record.get("category"));
                    }
                }
            }
            BigDecimal  quekouBooknum= ((BigDecimal)resultMap.get("totalBookNum")).subtract((BigDecimal)resultMap.get("tszcs"));
            resultMap.put("quekouBooknum",quekouBooknum.intValue()<0?0:quekouBooknum.intValue() );
            BigDecimal quekouPeriodicalNum =((BigDecimal)resultMap.get("periodicalNum")).subtract((BigDecimal)resultMap.get("qkzs"));
            resultMap.put("quekouPeriodicalNum",quekouPeriodicalNum.intValue()<0?0:quekouPeriodicalNum.intValue());
            BigDecimal quekoutoolnum =((BigDecimal)resultMap.get("toolsBookNum")).subtract((BigDecimal)resultMap.get("toolnum"));
            resultMap.put("quekoutoolnum",quekoutoolnum.intValue()<0?0:quekoutoolnum.intValue());


        }

        return resultMap;
    }



    public Page<Dayreport>  schoolstandardBoookNumList(int pageNumber,int pageSize,Integer areatypeid,Integer schoolcategoryid,Integer orgid,int orgtype,List<Standardbooknum> standardbooknumList,Integer toporgid,String[] bookrate,String[] periodicalrate,String[] toolbookrate){
        String select = "SELECT b.orgname,a.orgid,c.category, b.orgtypeid,b.schoolcategoryid,ifnull(a.xss,0) as xss, ifnull(tszcs,0) as tszcs,ifnull(a.qkzs,0) as qkzs , IFNULL(a.toolnum,0) as toolnum ";
        StringBuilder where = new StringBuilder(" FROM t_dayreport a " +
                " INNER JOIN t_org b ON a.orgid = b.id LEFT JOIN t_schoolcategory c on b.schoolcategoryid = c.id " +
                "WHERE  isclose =0 and  a.createtime = DATE_FORMAT(NOW(),'%Y-%m-%d')");

        List list = new ArrayList();
        if(orgtype== 5){
            where.append(" and b.id=?");
            list.add(orgid);
        }else {
            String parentcodes = CommonUtils.codeAddOne(orgid,6);
            where.append(" AND b.parentcodes LIKE ? ");
            list.add("%"+parentcodes+"%");
        }

        if(areatypeid!=null){
            where.append(" AND b.areatypeid = ? ");
            list.add(areatypeid);
        }
        if(schoolcategoryid!=null){
            where.append(" AND b.schoolcategoryid = ? ");
            list.add(schoolcategoryid);
        }

        if (StringUtils.isNotEmpty(bookrate[0])){
            where.append(" AND CalcStandardRate(").append(toporgid).append(",b.schoolcategoryid,1,tszcs,xss) >= ? ");
            list.add(Float.parseFloat(bookrate[0])/100);
        }

        if (StringUtils.isNotEmpty(bookrate[1])){
            where.append(" AND CalcStandardRate(").append(toporgid).append(",b.schoolcategoryid,1,tszcs,xss) <= ? ");
            list.add(Float.parseFloat(bookrate[1])/100);
        }

        if (StringUtils.isNotEmpty(periodicalrate[0])){
            where.append(" AND CalcStandardRate(").append(toporgid).append(",b.schoolcategoryid,2,qkzs,xss) >= ? ");
            list.add(Float.parseFloat(periodicalrate[0])/100);
        }

        if (StringUtils.isNotEmpty(periodicalrate[1])){
            where.append(" AND CalcStandardRate(").append(toporgid).append(",b.schoolcategoryid,2,qkzs,xss) <= ? ");
            list.add(Float.parseFloat(periodicalrate[1])/100);
        }

        if (StringUtils.isNotEmpty(toolbookrate[0])){
            where.append(" AND CalcStandardRate(").append(toporgid).append(",b.schoolcategoryid,3,toolnum,xss) >= ? ");
            list.add(Float.parseFloat(toolbookrate[0])/100);
        }

        if (StringUtils.isNotEmpty(toolbookrate[1])){
            where.append(" AND CalcStandardRate(").append(toporgid).append(",b.schoolcategoryid,3,toolnum,xss) <= ? ");
            list.add(Float.parseFloat(toolbookrate[1])/100);
        }

        Page<Dayreport> page=  dayDao.paginate(pageNumber, pageSize,select,where.toString(), CommonUtils.listToArray(list));
        for (Dayreport dayreport :page.getList()) {
            Map<String,BigDecimal>  standardMap= calculateStandard((dayreport.get("schoolcategoryid") == null ? 1:dayreport.get("schoolcategoryid")),standardbooknumList);
            dayreport.put("totalBookNum",new BigDecimal(dayreport.getInt("xss")).multiply(standardMap.get("bookNum")));
            BigDecimal quekouBooknum= new BigDecimal(dayreport.getInt("totalBookNum")).subtract(new BigDecimal(dayreport.getInt("tszcs")));
            dayreport.put("quekouBooknum",quekouBooknum.intValue()<0?0:quekouBooknum.intValue());

            dayreport.put("periodicalNum",standardMap.get("periodicalNum"));
            BigDecimal quekouPeriodicalNum= new BigDecimal(dayreport.getInt("periodicalNum")).subtract(new BigDecimal(dayreport.getInt("qkzs")));
            dayreport.put("quekouPeriodicalNum",quekouPeriodicalNum.intValue()<0?0:quekouPeriodicalNum.intValue());


            dayreport.put("toolsBookNum",standardMap.get("toolsBookNum"));
            BigDecimal quekoutoolnum= new BigDecimal(dayreport.getInt("toolsBookNum")).subtract(new BigDecimal(dayreport.getInt("toolnum")));
            dayreport.put("quekoutoolnum",quekoutoolnum.intValue()<0?0:quekoutoolnum.intValue());

            dayreport.put("bookNum",standardMap.get("bookNum"));
        }
        return  page;
    }

    public Map<String,BigDecimal> calculateStandard(int schoolcategoryid,List<Standardbooknum> standardbooknumList){
        Map<String,BigDecimal> resultMap=new HashMap<String, BigDecimal>();
        for (Standardbooknum standardbooknum: standardbooknumList) {
            if (schoolcategoryid ==1){  //小学
                if (standardbooknum.getBooktype().equals("人均藏书量 (册)  （按在校学生数)")){
                    resultMap.put("bookNum",standardbooknum.getSchoolD());

                }else if (standardbooknum.getBooktype().equals("工具书，教学参考书 (种)")){
                    resultMap.put("toolsBookNum",standardbooknum.getSchoolD());

                }else if (standardbooknum.getBooktype().equals("报刊 (种)")){
                    resultMap.put("periodicalNum",standardbooknum.getSchoolD());
                }
            }else if (schoolcategoryid ==2){ //初级中学
                if (standardbooknum.getBooktype().equals("人均藏书量 (册)  （按在校学生数)")){
                    resultMap.put("bookNum",standardbooknum.getSchoolC());
                }else if (standardbooknum.getBooktype().equals("工具书，教学参考书 (种)")){
                    resultMap.put("toolsBookNum",standardbooknum.getSchoolC());
                }else if (standardbooknum.getBooktype().equals("报刊 (种)")){
                    resultMap.put("periodicalNum",standardbooknum.getSchoolC());
                }
            }else if (schoolcategoryid ==3){ //九年一贯制学校
                if (standardbooknum.getBooktype().equals("人均藏书量 (册)  （按在校学生数)")){
                    resultMap.put("bookNum",standardbooknum.getSchoolC());
                }else if (standardbooknum.getBooktype().equals("工具书，教学参考书 (种)")){
                    resultMap.put("toolsBookNum",standardbooknum.getSchoolC());
                }else if (standardbooknum.getBooktype().equals("报刊 (种)")){
                    resultMap.put("periodicalNum",standardbooknum.getSchoolC());
                }
            }else if (schoolcategoryid ==4){ //高级中学
                if (standardbooknum.getBooktype().equals("人均藏书量 (册)  （按在校学生数)")){
                    resultMap.put("bookNum",standardbooknum.getSchoolB());
                }else if (standardbooknum.getBooktype().equals("工具书，教学参考书 (种)")){
                    resultMap.put("toolsBookNum",standardbooknum.getSchoolB());
                }else if (standardbooknum.getBooktype().equals("报刊 (种)")){
                    resultMap.put("periodicalNum",standardbooknum.getSchoolB());
                }
            }else if (schoolcategoryid ==5){ //完全中学
                if (standardbooknum.getBooktype().equals("人均藏书量 (册)  （按在校学生数)")){
                    resultMap.put("bookNum",standardbooknum.getSchoolA());
                }else if (standardbooknum.getBooktype().equals("工具书，教学参考书 (种)")){
                    resultMap.put("toolsBookNum",standardbooknum.getSchoolA());
                }else if (standardbooknum.getBooktype().equals("报刊 (种)")){
                    resultMap.put("periodicalNum",standardbooknum.getSchoolA());
                }
            }else if (schoolcategoryid ==6){  //十二年一贯制学校
                if (standardbooknum.getBooktype().equals("人均藏书量 (册)  （按在校学生数)")){
                    resultMap.put("bookNum",standardbooknum.getSchoolB());
                }else if (standardbooknum.getBooktype().equals("工具书，教学参考书 (种)")){
                    resultMap.put("toolsBookNum",standardbooknum.getSchoolB());
                }else if (standardbooknum.getBooktype().equals("报刊 (种)")){
                    resultMap.put("periodicalNum",standardbooknum.getSchoolB());
                }
            }
        }
        return resultMap;
    }




    public Map<String,Object>   standardBookTypeList(Integer areatypeid,Integer schoolcategoryid,Integer orgid,int orgtype,List<Standardbooktype> standardbooktypeList, List<Standardbooknum>  standardbooknumList,Integer rate,Integer toporgid){
        StringBuilder where = new StringBuilder(" SELECT  orgname, category, schoolcategoryid, IFNULL(xss,0) as xss, IFNULL(type1num,0) as type1num, IFNULL(type2num,0) as type2num," +
                " IFNULL(type3num,0) as type3num, IFNULL(type4num,0) as type4num,IFNULL(type5num,0) as type5num  FROM t_dayreport a " +
                " INNER JOIN t_org b ON a.orgid = b.id  " +
                " INNER JOIN t_schoolcategory c on b.schoolcategoryid = c.id  " +
                " WHERE isclose =0 and a.createtime = DATE_FORMAT(NOW(),'%Y-%m-%d')");
        List list = new ArrayList();
        if(orgtype== 5){
            where.append(" and b.id=?");
            list.add(orgid);
        }else {
            String parentcodes = CommonUtils.codeAddOne(orgid,6);
            where.append(" AND b.parentcodes LIKE ? ");
            list.add("%"+parentcodes+"%");
        }

        if(areatypeid!=null){
            where.append(" AND b.areatypeid = ? ");
            list.add(areatypeid);
        }
        if(schoolcategoryid!=null){
            where.append(" AND b.schoolcategoryid = ? ");
            list.add(schoolcategoryid);
        }


        if (rate ==1){
            where.append(" AND CalcStandardBookTypeRate(").append(toporgid).append(",schoolcategoryid,1,type1num,xss)=1 ");
            where.append(" AND CalcStandardBookTypeRate(").append(toporgid).append(",schoolcategoryid,2,type2num,xss)=1 ");
            where.append(" AND CalcStandardBookTypeRate(").append(toporgid).append(",schoolcategoryid,3,type3num,xss)=1 ");
            where.append(" AND CalcStandardBookTypeRate(").append(toporgid).append(",schoolcategoryid,4,type4num,xss)=1 ");
            where.append(" AND CalcStandardBookTypeRate(").append(toporgid).append(",schoolcategoryid,5,type5num,xss)=1 ");
        }else if (rate==2){
            where.append(" AND CalcStandardBookTypeRate(").append(toporgid).append(",schoolcategoryid,1,type1num,xss)=0 ");
            where.append(" AND CalcStandardBookTypeRate(").append(toporgid).append(",schoolcategoryid,2,type2num,xss)=0 ");
            where.append(" AND CalcStandardBookTypeRate(").append(toporgid).append(",schoolcategoryid,3,type3num,xss)=0 ");
            where.append(" AND CalcStandardBookTypeRate(").append(toporgid).append(",schoolcategoryid,4,type4num,xss)=0 ");
            where.append(" AND CalcStandardBookTypeRate(").append(toporgid).append(",schoolcategoryid,5,type5num,xss)=0 ");
        }


        List<Record> recordList= Db.find(where.toString(), CommonUtils.listToArray(list));
        Map<String,Object> resultMap =new HashMap<String, Object>();
        resultMap.put("totalBookNum",new BigDecimal(0));
        resultMap.put("type1num",new BigDecimal(0));
        resultMap.put("type2num",new BigDecimal(0));
        resultMap.put("type3num",new BigDecimal(0));
        resultMap.put("type4num",new BigDecimal(0));
        resultMap.put("type5num",new BigDecimal(0));
        resultMap.put("bookNum",new BigDecimal(0));

        resultMap.put("total1num",new BigDecimal(0));
        resultMap.put("total2num",new BigDecimal(0));
        resultMap.put("total3num",new BigDecimal(0));
        resultMap.put("total4num",new BigDecimal(0));
        resultMap.put("total5num",new BigDecimal(0));

        for (Record record : recordList){
            schoolcategoryid = record.get("schoolcategoryid");
            Map<String,BigDecimal>  standardMap= calculateStandard(schoolcategoryid,standardbooknumList);
            for (Standardbooktype standardbooktype:standardbooktypeList){
                if (schoolcategoryid  ==1){
                    resultMap.put("percent"+standardbooktype.getBooktypeid(),standardbooktype.getPercenta());
                }else {
                    resultMap.put("percent"+standardbooktype.getBooktypeid(),standardbooktype.getPercentb());
                }
            }

           // resultMap.put("totalBookNum",((BigDecimal)resultMap.get("totalBookNum")).add(new BigDecimal(record.getInt("xss")).multiply( standardMap.get("bookNum"))));

            resultMap.put("type1num",((BigDecimal)resultMap.get("type1num")).add(new BigDecimal(record.getInt("type1num"))));
            resultMap.put("type2num",((BigDecimal)resultMap.get("type2num")).add(new BigDecimal(record.getInt("type2num"))));
            resultMap.put("type3num",((BigDecimal)resultMap.get("type3num")).add(new BigDecimal(record.getInt("type3num"))));
            resultMap.put("type4num",((BigDecimal)resultMap.get("type4num")).add(new BigDecimal(record.getInt("type4num"))));
            resultMap.put("type5num",((BigDecimal)resultMap.get("type5num")).add(new BigDecimal(record.getInt("type5num"))));

            resultMap.put("totalBookNum",((BigDecimal)resultMap.get("type1num")).add((BigDecimal)resultMap.get("type2num"))
                    .add((BigDecimal)resultMap.get("type3num")).add((BigDecimal)resultMap.get("type4num"))
                    .add((BigDecimal)resultMap.get("type5num")));

            BigDecimal total =new BigDecimal(record.getInt("xss")).multiply(standardMap.get("bookNum"));
            resultMap.put("total1num",((BigDecimal)resultMap.get("total1num")).add(total.multiply( (BigDecimal) resultMap.get("percent1")).divide(new BigDecimal(100))));
            resultMap.put("total2num",((BigDecimal)resultMap.get("total2num")).add(total.multiply( (BigDecimal) resultMap.get("percent2")).divide(new BigDecimal(100))));
            resultMap.put("total3num",((BigDecimal)resultMap.get("total3num")).add(total.multiply( (BigDecimal) resultMap.get("percent3")).divide(new BigDecimal(100))));
            resultMap.put("total4num",((BigDecimal)resultMap.get("total4num")).add(total.multiply( (BigDecimal) resultMap.get("percent4")).divide(new BigDecimal(100))));
            resultMap.put("total5num",((BigDecimal)resultMap.get("total5num")).add(total.multiply( (BigDecimal) resultMap.get("percent5")).divide(new BigDecimal(100))));
        }

        return resultMap;
    }


    public Page<Dayreport>   standardBookTypeList(int pageNumber,int pageSize,Integer areatypeid,Integer schoolcategoryid,Integer orgid,int orgtype,List<Standardbooktype> standardbooktypeList, List<Standardbooknum>  standardbooknumList,Integer rate,Integer toporgid){
       String select ="SELECT   orgname, category,orgtypeid, schoolcategoryid, IFNULL(xss,0) as xss, IFNULL(type1num,0) as type1num, IFNULL(type2num,0) as type2num , " +
               "    IFNULL(type3num,0) as type3num, IFNULL(type4num,0) as type4num,IFNULL(type5num,0) as type5num ";
        StringBuilder where = new StringBuilder("   FROM t_dayreport a " +
                " INNER JOIN t_org b ON a.orgid = b.id  " +
                " INNER JOIN t_schoolcategory c on b.schoolcategoryid = c.id  " +
                " WHERE  isclose =0 and a.createtime = DATE_FORMAT(NOW(),'%Y-%m-%d')");
        List list = new ArrayList();
        if(orgtype== 5){
            where.append(" and b.id=?");
            list.add(orgid);
        }else {
            String parentcodes = CommonUtils.codeAddOne(orgid,6);
            where.append(" AND b.parentcodes LIKE ? ");
            list.add("%"+parentcodes+"%");
        }

        if(areatypeid!=null){
            where.append(" AND b.areatypeid = ? ");
            list.add(areatypeid);
        }
        if(schoolcategoryid!=null){
            where.append(" AND b.schoolcategoryid = ? ");
            list.add(schoolcategoryid);
        }


        if (rate ==1){
            where.append(" AND CalcStandardBookTypeRate(").append(toporgid).append(",schoolcategoryid,1,type1num,xss)=1 ");
            where.append(" AND CalcStandardBookTypeRate(").append(toporgid).append(",schoolcategoryid,2,type2num,xss)=1 ");
            where.append(" AND CalcStandardBookTypeRate(").append(toporgid).append(",schoolcategoryid,3,type3num,xss)=1 ");
            where.append(" AND CalcStandardBookTypeRate(").append(toporgid).append(",schoolcategoryid,4,type4num,xss)=1 ");
            where.append(" AND CalcStandardBookTypeRate(").append(toporgid).append(",schoolcategoryid,5,type5num,xss)=1 ");
        }else if (rate==2){
            where.append(" AND CalcStandardBookTypeRate(").append(toporgid).append(",schoolcategoryid,1,type1num,xss)=0 ");
            where.append(" AND CalcStandardBookTypeRate(").append(toporgid).append(",schoolcategoryid,2,type2num,xss)=0 ");
            where.append(" AND CalcStandardBookTypeRate(").append(toporgid).append(",schoolcategoryid,3,type3num,xss)=0 ");
            where.append(" AND CalcStandardBookTypeRate(").append(toporgid).append(",schoolcategoryid,4,type4num,xss)=0 ");
            where.append(" AND CalcStandardBookTypeRate(").append(toporgid).append(",schoolcategoryid,5,type5num,xss)=0 ");
        }

        Page<Dayreport> page= dayDao.paginate(pageNumber,pageSize,select,where.toString(), CommonUtils.listToArray(list));
        Map<String,Object> resultMap =new HashMap<String, Object>();

        for (Dayreport record : page.getList()){
            schoolcategoryid = record.get("schoolcategoryid");
            Map<String,BigDecimal>  standardMap= calculateStandard(schoolcategoryid,standardbooknumList);
            for (Standardbooktype standardbooktype:standardbooktypeList){
                if (schoolcategoryid  ==1){
                    resultMap.put("percent"+standardbooktype.getBooktypeid(),standardbooktype.getPercenta());
                }else {
                    resultMap.put("percent"+standardbooktype.getBooktypeid(),standardbooktype.getPercentb());
                }
            }



          //  resultMap.put("totalBookNum",new BigDecimal(record.getInt("xss")).multiply(standardMap.get("bookNum")));
            BigDecimal total =new BigDecimal(record.getInt("xss")).multiply(standardMap.get("bookNum"));
            resultMap.put("total1num",total.multiply( (BigDecimal) resultMap.get("percent1")).divide(new BigDecimal(100)));
            resultMap.put("total2num",total.multiply( (BigDecimal) resultMap.get("percent2")).divide(new BigDecimal(100)));
            resultMap.put("total3num",total.multiply( (BigDecimal) resultMap.get("percent3")).divide(new BigDecimal(100)));
            resultMap.put("total4num",total.multiply( (BigDecimal) resultMap.get("percent4")).divide(new BigDecimal(100)));
            resultMap.put("total5num",total.multiply( (BigDecimal) resultMap.get("percent5")).divide(new BigDecimal(100)));

            resultMap.put("totalBookNum",new BigDecimal(record.getInt("type1num")).add(new BigDecimal(record.getInt("type2num")))
                    .add(new BigDecimal(record.getInt("type3num"))).add(new BigDecimal(record.getInt("type4num")))
                    .add(new BigDecimal(record.getInt("type5num"))));

            record.put(resultMap);
        }

        return page;
    }
}
