package com.controller.school;

import com.config.BaseController;
import com.config.ControllerInterface;
import com.jfinal.aop.Clear;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Page;
import com.jfinal.plugin.activerecord.Record;
import com.utils.CommonUtils;
import com.utils.DateUtils;

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

@ControllerInterface(path="/liutong")
public class LiutongController extends BaseController {
    @Clear
    public void index(){
        render("/schoolweb/jieyuepaihang.html");
    }

    public void teachliutongPage(){
        render("/view/query-stats/liutongList.html");
    }

    @Clear
    public void getpaihanglist(){
        try{
            int paihang = PropKit.getInt("paihang");
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.MONTH, -12);
            String begindate = getPara("begindate", DateUtils.getSimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()));
            String enddate = getPara("enddate", DateUtils.getSimpleDateFormat("yyyy-MM-dd").format(new Date()));

            String select = "SELECT a.orgid,d.orgname,COUNT(a.id) AS NUM,IFNULL(round(COUNT(a.id)/d.students),0) as shenjun FROM t_jieyue a INNER JOIN t_book_detail b ON a.bookdetailid=b.id \n" +
                    "INNER JOIN t_book c ON b.bookid =c.id \n" +
                    "INNER JOIN t_org d  ON a.orgid=d.id \n" +
                    "WHERE b.status !=3 AND  b.bookstatus = 1  AND d.orgtypeid=5  and  a.createtime>=? and a.createtime <=? \n" +
                    "GROUP BY a.orgid,d.orgname\n" +
                    "ORDER BY NUM DESC LIMIT "+paihang;
            List<Record> back = Db.find(select,begindate,enddate);
            for(Record r :back){
                Record test = Db.findFirst("select round("+r.getStr("NUM")+"/COUNT(b.id)*100,2) as jyl from t_book a INNER JOIN t_book_detail b on b.bookid = a.id where b.status !=3 AND  b.bookstatus = 1 and a.orgid='"+r.getStr("orgid")+"'");
                r.set("jyl",test.getStr("jyl"));
            }
            success(back);
        }catch(Exception e){
            e.printStackTrace();
            fail();
        }
    }

    public void getliutonglist(){
        try{
            Calendar calendar = Calendar.getInstance();
            calendar.add(Calendar.MONTH, -12);
            String begindate = getPara("begindate", DateUtils.getSimpleDateFormat("yyyy-MM-dd").format(calendar.getTime()));
            String enddate = getPara("enddate", DateUtils.getSimpleDateFormat("yyyy-MM-dd").format(new Date()));
            int orgid = this.getParaToInt("orgid",0);
            if(orgid == 0) {
                fail(1,"请求参数不完整!");
                return;
            }

            String select = "SELECT * ";
            StringBuffer where = new StringBuffer(" from (SELECT a.orgid,d.orgname,COUNT(a.id) AS NUM,IFNULL(round(COUNT(a.id)/d.students),0) as shenjun \n"+
                    "FROM t_jieyue a INNER JOIN t_book_detail b ON a.bookdetailid=b.id\n" +
                    "INNER JOIN t_book c ON b.bookid =c.id \n" +
                    "INNER JOIN t_org d  ON a.orgid=d.id \n" +
                    "WHERE b.status !=3 AND  b.bookstatus = 1  AND d.orgtypeid=5 and d.parentcodes like ? \n");
            List wherelist = new ArrayList();
            wherelist.add("%"+orgid+"%");
            if(CommonUtils.isNotNullOrNotEmpty(begindate)){
                where.append(" and a.createtime> ? \n");wherelist.add(begindate);
            }

            if(CommonUtils.isNotNullOrNotEmpty(enddate)){
                where.append(" and a.createtime<= ? \n");wherelist.add(enddate+" 23:59:59");
            }
            where.append(" GROUP BY a.orgid,d.orgname order by NUM desc) as temp");
            Page backpage = Db.paginate(this.getParaToInt("pageNumber",1), this.getParaToInt("pageSize",10),select, where.toString(),CommonUtils.listToArray(wherelist));
            List<Record> back = backpage.getList();
            for(Record r :back){
                Record test = Db.findFirst("select round("+r.getStr("NUM")+"/COUNT(b.id)*100,2) as jyl from t_book a INNER JOIN t_book_detail b on b.bookid = a.id where b.status !=3 AND  b.bookstatus = 1 and a.orgid='"+r.getStr("orgid")+"'");
                r.set("jyl",test.getStr("jyl")+"%");
            }
            success(backpage);
        }catch(Exception e){
            e.printStackTrace();
            fail();
        }
    }
}
