package com.controller.api;


import com.config.BaseController;
import com.config.ControllerInterface;
import com.jfinal.aop.Clear;
import com.jfinal.kit.HttpKit;
import com.jfinal.kit.PropKit;
import com.jfinal.log.Log;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.DbKit;
import com.jfinal.plugin.activerecord.Record;
import com.jfinal.plugin.activerecord.dialect.SqlServerDialect;
import com.jfinal.plugin.druid.DruidPlugin;
import com.utils.CommonUtils;

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

@Clear
@ControllerInterface(path="/api/gzplatReport")
public class GzPlatReportController extends BaseController {
    public Log logger = Log.getLog(GzPlatReportController.class);

    public void report(){
//        Calendar calendar = Calendar.getInstance();
//        String datepara = getPara("datepara");
//        String datestr = "";
//        if(CommonUtils.isNullOrEmpty(datepara)){
//            int day_of_month = calendar.get(Calendar.DAY_OF_MONTH);
//            if(day_of_month != 1){
//                return;
//            }
//            calendar.add(Calendar.MONTH,-1);
//            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM");
//            datestr = sdf.format(calendar.getTime());
//        } else {
//            datestr = datepara;
//        }
//
//        boolean check = true;
//        String areacode = PropKit.get("areacode","");
//        if(!areacode.equals("gzplat")){
//            check = false;
//        }
//        String jdbcUtil = "jdbc:mysql://121.37.166.0/library?characterEncoding=utf8&zeroDateTimeBehavior=convertToNull";
//        String username =  "root";
//        String password = "eZ4HnKwvAFIbSFCN";
//        DruidPlugin c3p0Plugin = new DruidPlugin(jdbcUtil, username, password);
//
//        DbKit.removeConfig("gzplatdb");
//        c3p0Plugin.stop();
//        ActiveRecordPlugin arp = new ActiveRecordPlugin("gzplatdb", c3p0Plugin);
//        arp.setDialect(new SqlServerDialect());
//        c3p0Plugin.start();
//        arp.start();
//
//        List<Record> list = Db.find("select o.id orgid,o.orgname,(select orgname from t_org where id = o.parentid) areaname ,(select orgname from t_org where id = (select parentid from t_org where id = o.parentid)) cityname,o.schoolcategoryid from t_org o where orgtypeid = 5 and isclose= 0 and status = 1");
//        for(Record record : list) {
//            int orgid = record.getInt("orgid");
//            Record checkRecord = Db.use("gzplatdb").findFirst("select * from t_gzplat_report where orgid = ? and reporttime =?",orgid,datestr);
//            if(CommonUtils.isNotNullOrNotEmpty(checkRecord)){
//                if(!check){
//                    Db.use("gzplatdb").deleteById("t_gzplat_report",checkRecord.getInt("id"));
//                } else {
//                    continue;
//                }
//            }
//            Record studentNumberRecord = Db.findFirst("select count(*) studentnumber from t_reader where orgid = ? and status = 1",orgid);
//            record.setColumns(studentNumberRecord);
//            Record classNumberRecord = Db.findFirst("select count(*) classnumber from t_org_dept where orgid = ?",orgid);
//            record.setColumns(classNumberRecord);
//            Record bookNumberRecord = Db.findFirst("select count(*) booknumber from t_book_detail d left join t_book b on d.bookid=b.id where b.orgid = ? and d.bookstatus = 1 and d.status < 3",orgid);
//            record.setColumns(bookNumberRecord);
//            Record jieyueRecord = Db.findFirst("select count(*) jieyuenumber from t_jieyue j where j.orgid = ? and DATE_FORMAT(j.createtime,'%Y-%m') =?",orgid,datestr);
//            record.setColumns(jieyueRecord);
//            record.set("createtime",new Date());
//            record.set("reporttime",datestr);
//            Db.use("gzplatdb").save("t_gzplat_report",record);
//        }
        success();
    }

    public void findList(){
        String datepara = getPara("datepara");
        List relist = Db.find("select * from t_gzplat_report where reporttime = ?",datepara);
        success(relist);
    }
}
