package com.service.intelligentdevice;

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.model.ReadingWorks;
import com.utils.CommonUtils;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

public class ReadingWorksService {
    public static final ReadingWorksService me = new ReadingWorksService();
    private ReadingWorks dao = new ReadingWorks().dao();

    public Page<Record> findReadingWorksList(int pageNumber,int pagesize,Integer equipmentid, Integer orgid,String readerName,String title,String starttime,String endtime) {
        String select = "select w.id,DATE_FORMAT(w.createDate,'%Y-%m-%d') createDate,readerName,title,libtypename,author,ListenCount,serverFileName ";
        StringBuilder where = new StringBuilder("from t_reading_works w left join t_reading_info i on w.readinginfoid = i.id where 1=1 ");
        List list = new ArrayList();
        if(CommonUtils.isNotNullOrNotEmpty(equipmentid)) {
            where.append(" and i.equipmentid = ?");
            list.add(equipmentid);
        }
        if(CommonUtils.isNotNullOrNotEmpty(orgid)) {
            where.append(" and w.orgid = ?");
            list.add(orgid);
        }
        if(CommonUtils.isNotNullOrNotEmpty(readerName)) {
            where.append(" and w.readerName like '%"+readerName+"%'");
        }
        if(CommonUtils.isNotNullOrNotEmpty(title)) {
            where.append(" and w.title like '%"+title+"%'");
        }
        if(CommonUtils.isNotNullOrNotEmpty(starttime)) {
            where.append(" and w.createDate >= '"+starttime+" 00:00:00'");
        }
        if(CommonUtils.isNotNullOrNotEmpty(endtime)) {
            where.append(" and w.createDate <= '"+endtime+" 23:59:59'");
        }
        where.append(" order by w.createDate desc");
        return Db.paginate(pageNumber, pagesize,select,where.toString(),CommonUtils.listToArray(list));
    }

    public List readingbookTop20 (String begindate,String enddate,int orgid ){
        return Db.find("SELECT  worksName,readerName,libtypename,ListenCount FROM t_reading_works where createDate>=? and createDate<=? and orgid = ? ORDER BY ListenCount DESC LIMIT 20",begindate,enddate,orgid);
    }

    public List readingreaderTop20 (String begindate,String enddate,int orgid ){
        return Db.find("SELECT  readerName, COUNT(*) AS booknum  FROM t_reading_works   where createDate>=? and createDate<=? and orgid = ? GROUP BY readerName ORDER BY booknum DESC LIMIT 20",begindate,enddate,orgid);
    }

    public List readingresourceTop20 (String begindate,String enddate,int orgid ){
        return Db.find(" SELECT  worksName,libtypename, COUNT(*) AS num  FROM t_reading_works  where createDate>=? and createDate<=?  and orgid = ? GROUP BY worksName,libtypename ORDER BY num DESC LIMIT 20",begindate,enddate,orgid);
    }

    public List readingReprotData (String begindate,String enddate,int orgid ){
        try {
            List resultList = new ArrayList();
            Calendar start = Calendar.getInstance();
            Calendar end = Calendar.getInstance();
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            start.setTime(format.parse(begindate));
            end.setTime(format.parse(enddate));
            List<String> dateList = new ArrayList();
            if(start.getTimeInMillis() == end.getTimeInMillis()){
                dateList.add(format.format(start.getTime()));
            }else {
                while (start.before(end)) {
                    dateList.add(format.format(start.getTime()));
                    start.add(Calendar.DATE, 1);
                }
                dateList.add(format.format(end.getTime()));
            }

            List<Record> recordList = Db.find("SELECT DATE_FORMAT(createDate,'%Y-%m-%d') AS tempdate ,COUNT(*) AS num  FROM t_reading_works  where createDate>=? and createDate<=? and orgid = ? GROUP BY tempdate ORDER BY tempdate LIMIT 20", begindate, enddate, orgid);

            List useNumList = new ArrayList();
            for (String time : dateList) {
                boolean a = false;
                for(Record record : recordList){
                    if(time.equals(record.getStr("tempdate"))){
                        useNumList.add(record.get("num"));
                        a = true;
                        break;
                    }
                }
                if(a == false){
                    useNumList.add(0);
                }

            }
            resultList.add(dateList);
            resultList.add(useNumList);

            List readerNumList = new ArrayList();
            recordList = Db.find("SELECT DATE_FORMAT(createDate,'%Y-%m-%d') AS tempdate ,COUNT(DISTINCT readerName) AS num FROM t_reading_works where createDate>=? and createDate<=? and orgid = ? GROUP BY tempdate ORDER BY tempdate  LIMIT 20", begindate, enddate, orgid);
            for (String time : dateList) {
                boolean a = false;
                for(Record record : recordList){
                    if(time.equals(record.getStr("tempdate"))){
                        readerNumList.add(record.get("num"));
                        a = true;
                    }else{
                        //
                    }
                }
                if(a == false){
                    readerNumList.add(0);
                }
            }
            resultList.add(readerNumList);
            List resourceNumList = new ArrayList();
            recordList = Db.find("SELECT DATE_FORMAT(createDate,'%Y-%m-%d') AS tempdate ,COUNT(DISTINCT worksName) AS num FROM t_reading_works where createDate>=? and createDate<=? and orgid = ? GROUP BY tempdate ORDER BY tempdate  LIMIT 20", begindate, enddate, orgid);
            for (String time : dateList) {
                boolean a = false;
                for(Record record : recordList){
                    if(time.equals(record.getStr("tempdate"))){
                        resourceNumList.add(record.get("num"));
                        a = true;
                    }else{
                        //
                    }
                }
                if(a == false){
                    resourceNumList.add(0);
                }
            }
            resultList.add(resourceNumList);
            System.out.println(resultList);
            return resultList;
        }catch(Exception e){
            e.printStackTrace();
            return null;
        }
    }

}
