package com.service;

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

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

public class RegionService {
    public static final RegionService me = new RegionService();
    private Dayreport dao = new Dayreport().dao();

    public Record summaryList(String schoolids,int orgid){
        //查询昨天统计数据  DATEDIFF(createtime,NOW())=-1
//        Date date = new Date();
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
//        sdf.format(date);
        Record summary = Db.findFirst("SELECT COUNT(orgid) as xxs,SUM(dzs) as dzs,SUM(lss) as lss,SUM(xss) as xss,SUM(tsglys) as tsglys," +
        " SUM(tszs) as tszs,SUM(tszcs) as tszcs,SUM(tszcs_bz) as tszcs_bz,SUM(qkzs) as qkzs," +
        " SUM(tszcs)/SUM(xss) as tssjcs,SUM(tszcs) - SUM(tszcs_bz) as qscs,SUM(tszcs) / SUM(tszs) as pjfb,SUM(tszcs) / SUM(xss) as tsbzl,SUM(sjdz) as sjdz " +
        " from t_dayreport as a left join t_org as b on a.orgid = b.id WHERE DATEDIFF(a.createtime,NOW())=0 and b.isclose =0 and  orgid in "+schoolids);

        List<Record> list = Db.find("select * from t_database where status = 1 and parentorgid = ?",orgid);
        for(Record record : list) {
            int suborgid = record.getInt("orgid");
            String databasename = record.getStr("databasename");

            Record reportRecord = Db.use(databasename).findFirst("" +
                    "SELECT COUNT(orgid) as xxs," + //
                    "ifnull(SUM(dzs),0) as dzs," + //
                    "ifnull(SUM(lss),0) as lss," + //
                    "ifnull(SUM(xss),0) as xss," + //
                    "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," + //
                    " ifnull(SUM(tszcs)/SUM(xss),0) as tssjcs," + //
                    "ifnull(SUM(tszcs) - SUM(tszcs_bz),0) as qscs," +
                    "ifnull(SUM(tszcs) / SUM(tszs),0) as pjfb," +
                    "ifnull(SUM(tszcs) / SUM(xss),0) as tsbzl," +
                    "ifnull(SUM(sjdz),0) as sjdz " + //
                    " from t_dayreport as a left join t_org as b on a.orgid = b.id WHERE DATEDIFF(a.createtime,NOW())=0 and b.isclose =0 and  b.parentcodes like '%"+suborgid+"%'");
            summary.set("dzs",CommonUtils.isNullOrEmpty(summary.get("dzs"))?0:summary.getInt("dzs")+reportRecord.getInt("dzs"));
            summary.set("lss",CommonUtils.isNullOrEmpty(summary.get("lss"))?0:summary.getInt("lss")+reportRecord.getInt("lss"));
            summary.set("xss",CommonUtils.isNullOrEmpty(summary.get("xss"))?0:summary.getInt("xss")+reportRecord.getInt("xss"));
            summary.set("tsglys",CommonUtils.isNullOrEmpty(summary.get("tsglys"))?0:summary.getInt("tsglys")+reportRecord.getInt("tsglys"));
            summary.set("tszs",CommonUtils.isNullOrEmpty(summary.get("tszs"))?0:summary.getInt("tszs")+reportRecord.getInt("tszs"));
            summary.set("tszcs",CommonUtils.isNullOrEmpty(summary.get("tszcs"))?0:summary.getInt("tszcs")+reportRecord.getInt("tszcs"));
            summary.set("tszcs_bz",CommonUtils.isNullOrEmpty(summary.get("tszcs_bz"))?0:summary.getInt("tszcs_bz")+reportRecord.getInt("tszcs_bz"));
            summary.set("qkzs",CommonUtils.isNullOrEmpty(summary.get("qkzs"))?0:summary.getInt("qkzs")+reportRecord.getInt("qkzs"));
            summary.set("sjdz",CommonUtils.isNullOrEmpty(summary.get("sjdz"))?0:summary.getInt("sjdz")+reportRecord.getInt("sjdz"));
            summary.set("xxs",CommonUtils.isNullOrEmpty(summary.get("xxs"))?0:summary.getInt("xxs")+reportRecord.getInt("xxs"));
            summary.set("tssjcs",CommonUtils.isNullOrEmpty(summary.get("tssjcs"))?0:summary.getInt("tssjcs")+reportRecord.getInt("tssjcs"));
        }
        DecimalFormat df = new DecimalFormat("0.00");
        if(CommonUtils.isNotNullOrNotEmpty(summary.get("tszcs")) && CommonUtils.isNotNullOrNotEmpty(summary.get("tszs"))){
            String pjfb = df.format((float)summary.getInt("tszcs")/summary.getInt("tszs"));
            summary.set("pjfb",pjfb);
        }
        if(CommonUtils.isNotNullOrNotEmpty(summary.get("tszcs")) && CommonUtils.isNotNullOrNotEmpty(summary.get("xss"))){
            String tsbzl = df.format((float)summary.getInt("tszcs")/summary.getInt("xss"));
            summary.set("tsbzl",tsbzl);
        }

        return summary;
    }
    //今日统计
    public Map dayBriefingList(String schoolids,int orgid){
        //当天借书数量
        Record r_jssl = Db.findFirst("SELECT COUNT(b.id) as jssl from t_reader a LEFT JOIN t_jieyue b ON a.id = b.readerid " +
                "WHERE  b.flag = 0 AND DATEDIFF(b.createtime,NOW())=0 AND a.orgid IN "+schoolids);
        int jssl = r_jssl.getInt("jssl");
        //当天还书数量
        Record r_hssl = Db.findFirst("SELECT COUNT(b.id) as hssl from t_reader a LEFT JOIN t_jieyue b ON a.id = b.readerid " +
                "WHERE b.`status` = 1 AND b.flag = 0 AND DATEDIFF(b.returntime,NOW())=0 AND a.orgid IN "+schoolids);
        int hssl = r_hssl.getInt("hssl");
        //新增图书数量
        Record r_xzsl = Db.findFirst("SELECT COUNT(a.id) as xzsl from t_book_detail a LEFT JOIN t_book b ON a.bookid = b.id " +
                "WHERE DATEDIFF(a.shangjiatime,NOW())=0 AND b.orgid in"+schoolids);
        int xzsl = r_xzsl.getInt("xzsl");
        //注销图书数量
        Record r_zxsl = Db.findFirst("SELECT COUNT(id) as zxsl FROM t_book_zhuxiao " +
                "WHERE flag = 0 AND DATEDIFF(createtime,NOW())=0 AND orgid in"+schoolids);
        int zxsl = r_zxsl.getInt("zxsl");

        //人均借阅册数 = 总借阅册数 / 借阅总人数
        Record r_zjycs = Db.findFirst("SELECT COUNT(b.id) as jycs from t_reader a LEFT JOIN t_jieyue b ON a.id = b.readerid " +
                "WHERE DATEDIFF(b.createtime,NOW())=0 AND a.orgid IN"+schoolids);
        Record r_zjyrs = Db.findFirst("SELECT COUNT(DISTINCT a.id) as jyrs from t_jieyue b LEFT JOIN t_reader a ON a.id = b.readerid " +
                "WHERE DATEDIFF(b.createtime,NOW())=0 AND b.`status` = 0 AND a.orgid IN"+schoolids);
        int zjycs = r_zjycs.getInt("jycs");
        int zjyrs = r_zjyrs.getInt("jyrs");
        //图书借阅率 = 图书借阅总册数 / 馆藏图书总册数
        Record r_gczcs = Db.findFirst("SELECT COUNT(a.id) as gczcs from t_book_detail a LEFT JOIN t_book b ON a.bookid = b.id " +
                "WHERE a.status != 3 AND a.bookstatus = 1 AND b.orgid IN "+schoolids);
        int gczcs = r_gczcs.getInt("gczcs");
        //读者到馆率 = 借阅总人数 / 读者总人数
        Record r_dzzrs = Db.findFirst("select COUNT(id) as dzzrs from t_reader WHERE orgid IN "+schoolids);
        int dzzrs = r_dzzrs.getInt("dzzrs");
        List<Record> list = Db.find("select * from t_database where status = 1 and parentorgid = ?",orgid);
        for(Record record : list) {
            int suborgid = record.getInt("orgid");
            String databasename = record.getStr("databasename");
            //当天借书数量
            Record r_jsslRecord = Db.use(databasename).findFirst("SELECT COUNT(b.id) as jssl from t_reader a LEFT JOIN t_jieyue b ON a.id = b.readerid left join t_org o on a.orgid = o.id " +
                    "WHERE  b.flag = 0 AND DATEDIFF(b.createtime,NOW())=0 AND o.parentcodes like '%"+suborgid+"%'");
            jssl += CommonUtils.isNotNullOrNotEmpty(r_jsslRecord.get("jssl"))?r_jsslRecord.getInt("jssl"):0;
            //当天还书数量
            Record r_hsslRecord = Db.use(databasename).findFirst("SELECT COUNT(b.id) as hssl from t_reader a LEFT JOIN t_jieyue b ON a.id = b.readerid left join t_org o on a.orgid = o.id " +
                    "WHERE b.`status` = 1 AND b.flag = 0 AND DATEDIFF(b.returntime,NOW())=0 AND o.parentcodes like '%"+suborgid+"%'");
            hssl += CommonUtils.isNotNullOrNotEmpty(r_hsslRecord.get("hssl"))?r_hsslRecord.getInt("hssl"):0;
            //新增图书数量
            Record r_xzslRecord = Db.use(databasename).findFirst("SELECT COUNT(a.id) as xzsl from t_book_detail a LEFT JOIN t_book b ON a.bookid = b.id left join t_org o on b.orgid = o.id " +
                    "WHERE DATEDIFF(a.shangjiatime,NOW())=0 AND o.parentcodes like '%"+suborgid+"%'");
            xzsl += CommonUtils.isNotNullOrNotEmpty(r_xzslRecord.get("xzsl"))?r_xzslRecord.getInt("xzsl"):0;
            //注销图书数量
            Record r_zxslRecord = Db.use(databasename).findFirst("SELECT COUNT(z.id) as zxsl FROM t_book_zhuxiao z left join t_org o on z.orgid = o.id " +
                    "WHERE z.flag = 0 AND DATEDIFF(z.createtime,NOW())=0 AND o.parentcodes like '%"+suborgid+"%'");
            zxsl = CommonUtils.isNotNullOrNotEmpty(r_zxslRecord.get("zxsl"))?r_zxslRecord.getInt("zxsl"):0;

            //人均借阅册数 = 总借阅册数 / 借阅总人数
            Record r_zjycsRecord = Db.use(databasename).findFirst("SELECT COUNT(b.id) as jycs from t_reader a LEFT JOIN t_jieyue b ON a.id = b.readerid left join t_org o on a.orgid = o.id " +
                    "WHERE DATEDIFF(b.createtime,NOW())=0 AND o.parentcodes like '%"+suborgid+"%'");
            Record r_zjyrsRecord = Db.use(databasename).findFirst("SELECT COUNT(DISTINCT a.id) as jyrs from t_jieyue b LEFT JOIN t_reader a ON a.id = b.readerid left join t_org o on a.orgid = o.id " +
                    "WHERE DATEDIFF(b.createtime,NOW())=0 AND b.`status` = 0 AND o.parentcodes like '%"+suborgid+"%'");
            zjycs += CommonUtils.isNotNullOrNotEmpty(r_zjycsRecord.get("jycs"))?r_zjycsRecord.getInt("jycs"):0;
            zjyrs += CommonUtils.isNotNullOrNotEmpty(r_zjyrsRecord.get("jyrs"))?r_zjyrsRecord.getInt("jyrs"):0;
            //图书借阅率 = 图书借阅总册数 / 馆藏图书总册数
            Record r_gczcsRecord = Db.use(databasename).findFirst("SELECT COUNT(a.id) as gczcs from t_book_detail a LEFT JOIN t_book b ON a.bookid = b.id left join t_org o on b.orgid = o.id " +
                    "WHERE a.status != 3 AND a.bookstatus = 1 AND o.parentcodes like '%"+suborgid+"%'");
            gczcs += CommonUtils.isNotNullOrNotEmpty(r_gczcsRecord.get("gczcs"))?r_gczcsRecord.getInt("gczcs"):0;
            //读者到馆率 = 借阅总人数 / 读者总人数
            Record r_dzzrsRecord = Db.use(databasename).findFirst("select COUNT(a.id) as dzzrs from t_reader a left join t_org o on a.orgid = o.id WHERE  o.parentcodes like '%"+suborgid+"%'");
            dzzrs += CommonUtils.isNotNullOrNotEmpty(r_dzzrsRecord.get("dzzrs"))?r_dzzrsRecord.getInt("dzzrs"):0;
        }
        float jyl = (float)zjycs / (float)gczcs;
        float dgl = (float) zjyrs / (float)dzzrs;
        Map map = new HashMap();
        map.put("jssl",jssl);//借书数量
        map.put("hssl",hssl);//还书数量
        map.put("xzsl",xzsl);//新增数量
        map.put("zxsl",zxsl);//注销数量
        map.put("jyl",jyl);//借阅率
        map.put("dgl",dgl);//到馆率
        return map;
    }
    //流通统计
    public Map circulationList(String schoolids,String ksrq,String jsrq){

        //图书借出数量，期刊借出数量，当前借出数量
        Record r = Db.findFirst("SELECT ifnull(SUM(CASE WHEN b.flag = 0 THEN 1 ELSE 0 END),0) as tsjcsl," +
                "ifnull(SUM(CASE WHEN b.flag = 1 THEN 1 ELSE 0 END),0) as qkjcsl from t_reader a LEFT JOIN t_jieyue b ON a.id = b.readerid " +
                "WHERE b.`status` = 0 and a.orgid IN "+schoolids);
        int tsjcsl = r.getInt("tsjcsl");
        int qkjcsl = r.getInt("qkjcsl");
        int dqjcsl = qkjcsl+tsjcsl;

        //kssj为前一个月，jssj为当天
        if(!StringUtils.isNotBlank(ksrq) || !StringUtils.isNotBlank(jsrq)){
            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();
            ksrq = format.format(d);
            jsrq = format.format(date);
        }

        Record r1 = Db.findFirst("SELECT COUNT(a.id) as dzdgrc FROM t_jieyue a LEFT JOIN t_reader b ON a.readerid = b.id " +
                "WHERE a.createtime >= ? AND a.createtime < ? AND b.orgid IN "+schoolids,
                ksrq+" 00:00:00",jsrq+" 23:59:59");
        Record r2 = Db.findFirst("select COUNT(id) as dzzrs from t_reader " +
                "WHERE createtime >= ? AND createtime < ? AND orgid IN "+schoolids,
                ksrq+" 00:00:00",jsrq+" 23:59:59");
        Record r3 = Db.findFirst("SELECT COUNT(a.id) as dzjytszcs FROM t_jieyue a LEFT JOIN t_reader b ON a.readerid = b.id " +
                "WHERE a.flag = 0 AND a.createtime >= ? AND a.createtime < ? AND b.orgid IN "+schoolids,
                ksrq+" 00:00:00",jsrq+" 23:59:59");
        Record r4 = Db.findFirst("SELECT COUNT(a.id) as tsgczcs from t_book_detail a LEFT JOIN t_book b ON a.bookid = b.id " +
                "WHERE a.createtime >= ? AND a.createtime < ? AND a.status != 3 AND a.bookstatus = 1 AND b.orgid IN "+schoolids,
                ksrq+" 00:00:00",jsrq+" 23:59:59");
        Record r5 = Db.findFirst("SELECT COUNT(b.id) AS qkgczcs FROM t_periodical_zhengding a LEFT JOIN t_periodical_detail b ON a.id = b.libraryid " +
                "WHERE b.createtime >= ? AND b.createtime < ? AND b.status != 3 AND b.bookstatus = 1 AND a.orgid IN "+schoolids,
                ksrq+" 00:00:00",jsrq+" 23:59:59");
        Record r6 = Db.findFirst("SELECT ifnull(SUM(students+czstudents+gzstudents),0) as students,id FROM t_org WHERE id IN "+schoolids);

        int dzdgrc = r1.getInt("dzdgrc");
        int dzzrs = r2.getInt("dzzrs");
        int dzjytszcs = r3.getInt("dzjytszcs");
        int tsgczcs = r4.getInt("tsgczcs");
        int qkgczcs = r5.getInt("qkgczcs");
        int students = r6.getInt("students");
        int gczcs = tsgczcs + qkgczcs;
        float dzdgl = 0;
        if(dzzrs != 0){
            dzdgl = (float) dzdgrc / (float)dzzrs;
        }
        float cslyl = 0;
        if(tsgczcs != 0){
            cslyl = (float)dzjytszcs / (float)tsgczcs;
        }
        float wxlyl = 0;
        if(gczcs != 0){
            wxlyl = (float)dzdgrc / (float)gczcs;
        }
        float rjjycs = 0;
        if(students != 0){
            rjjycs = (float)dzdgrc / (float)students;
        }
        float dzydl = 0;
        if(dzdgrc != 0){
            dzydl = (float)gczcs / (float) dzdgrc;
        }

        Map map = new HashMap();
        map.put("dzdgrc",dzdgrc);
        map.put("dqjcsl",dqjcsl);//当前借出数量
        map.put("tsjcsl",tsjcsl);//图书借出数量
        map.put("qkjcsl",qkjcsl);//期刊借出数量
        map.put("dzdgl",dzdgl);//读者到馆率
        map.put("cslyl",cslyl);//藏书利用率
        map.put("wxlyl",wxlyl);//文献利用率
        map.put("rjjycs",rjjycs);//人均借阅册数
        map.put("dzydl",dzydl);//读者阅读率
        return  map;
    }
}
