package com.sys.core;


import com.alibaba.druid.filter.stat.StatFilter;
import com.alibaba.druid.wall.WallFilter;
import com.jfinal.config.*;
import com.jfinal.core.JFinal;
import com.jfinal.ext.handler.ContextPathHandler;
import com.jfinal.ext.interceptor.SessionInViewInterceptor;
import com.jfinal.kit.PropKit;
import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
import com.jfinal.plugin.activerecord.CaseInsensitiveContainerFactory;
import com.jfinal.plugin.activerecord.dialect.MysqlDialect;
import com.jfinal.plugin.activerecord.dialect.OracleDialect;
import com.jfinal.plugin.activerecord.dialect.SqlServerDialect;
import com.jfinal.plugin.cron4j.Cron4jPlugin;
import com.jfinal.plugin.druid.DruidPlugin;
import com.jfinal.plugin.ehcache.EhCachePlugin;
import com.jfinal.template.Engine;
import com.jfinal.template.source.ClassPathSourceFactory;
import com.jfplugin.mail.MailPlugin;
import com.sys.bus.common.controller.CommonController;
import com.sys.bus.email.EmailController;
import com.sys.bus.excel.controller.ExcelController;
import com.sys.bus.file.controller.FileController;
import com.sys.bus.hr.controller.HrController;
import com.sys.bus.hr.model.Dept;
import com.sys.bus.hr.model.Person;
import com.sys.bus.jx.controller.*;
import com.sys.bus.model.UpdateRatingPermission;
import com.sys.bus.model._MappingKit;
import com.sys.bus.sys.controller.UserController;
import com.sys.bus.sys.model.DtBean;
import com.sys.bus.sys.model.User;
import com.sys.bus.sys.task.*;
import com.sys.bus.syscode.controller.SysCodeController;

import java.io.File;

public class FrameworkLoader extends JFinalConfig {
	static String path = System.getProperty("catalina.home");
	@Override
	public void configConstant(Constants me) {
		//一代读取配置方式
		//loadPropertyFile("jdbc.properties");
		//getPropertyToBoolean("config.devModel"
		//getProperty("DB.url")

		File cfg = new File(path+"/cfg/cfg.txt");
		if(cfg.exists()){
			PropKit.use(new File(path+"/cfg/cfg.txt"));
		}else{
			PropKit.use("cfg.txt");
		}

		boolean flg = PropKit.getBoolean("config.devModel", false);
		me.setDevMode(flg);
		me.setEncoding("utf-8");
		//me.setViewType(ViewType.JSP);
		me.setError404View("/_view/error.html");
	}

	@Override
	public void configRoute(Routes me) {
		
		me.add("/", IndexController.class);
		//me.add("/user", UserController.class, "/user");
		me.add("/file",FileController.class);
		me.add("/jxp",PerformanceController.class,"/_view/jx");
		me.add("/syscode",SysCodeController.class,"/_view/jx");
		me.add("/jx",JxManagerController.class,"/_view/jx");
		me.add("/login", HrController.class);
		me.add("/jxo", OrgController.class,"/_view/jx");
		//me.add("/jxr", RatingController.class,"/_view/jx");

		me.add("/jxk", KpilibController.class,"/_view/jx");
		me.add("/jxc", JxCycleController.class,"/_view/jx");
		me.add("/common", CommonController.class,"/pages");
		me.add("/excel", ExcelController.class,"/_view/jx");
		me.add("/email", EmailController.class,"/_view/jx");
		me.add("/attachment", AttachmentController.class,"/_view/jx");

		//me.add("/apprsetting", ApproverSettingController.class,"/pages");
	}

	@Override
	public void configPlugin(Plugins plugins) {

		//me.setLoggerFactry(new Log4jLoggerfactory);
		ActiveRecordPlugin arp=null;
	    String driver=PropKit.get("DB.driver");
	    String url=PropKit.get("DB.url"); 
	    String username=PropKit.get("DB.username");
	    String password=PropKit.get("DB.password");
	    DruidPlugin druidPlugin=new DruidPlugin(url, username, password, driver,"wall");

	    //druidPlugin.addFilter(new StatFilter());
	    WallFilter wall=new WallFilter();
	    String dbType = PropKit.get("db.type");
	    wall.setDbType(dbType.toLowerCase());
	    druidPlugin.addFilter(wall);
	    
	    /*
	    druidPlugin.addFilter(new StatFilter());  
        WallFilter wall = new WallFilter();  
        wall.setDbType(PropKit.get(ConstantInit.db_type_key));  
        WallConfig config = new WallConfig();  
        config.setFunctionCheck(false); // 支持数据库函数  
        wall.setConfig(config);  
        druidPlugin.addFilter(wall); 
        */
	    /*
	    druidPlugin.set(  
                PropKit.getInt(ConstantInit.db_initialSize),  
                PropKit.getInt(ConstantInit.db_minIdle),  
                PropKit.getInt(ConstantInit.db_maxActive));  */
	    
	    plugins.add(druidPlugin);
	    arp=new ActiveRecordPlugin(druidPlugin);
		if("SqlServer".equalsIgnoreCase(PropKit.get("db.type"))){
			arp.setDialect(new SqlServerDialect());

		}
	    if("MySQL".equalsIgnoreCase(PropKit.get("db.type"))){
	    	 arp.setDialect(new MysqlDialect());
	    }
	    if("Oracle".equalsIgnoreCase(PropKit.get("db.type"))){
	    	 arp.setDialect(new OracleDialect());
	    }
	    arp.setContainerFactory(new CaseInsensitiveContainerFactory(true));
	    plugins.add(new EhCachePlugin());
	    
	    arp.setShowSql(true);
	    arp.addMapping("T_ORG_ADMIN", "id",User.class);
	    arp.addMapping("T_ORG_ADMIN","temp" ,DtBean.class);
	   // arp.addMapping("ct_jx_attachment","attachment_id" ,Attachment.class);
	    arp.addMapping("T_ORG_ADMIN", "id",Person.class);
	    arp.addMapping("T_ORG_ADMIN", "id",Dept.class);
		arp.addMapping("CT_JX_UPDATERATINGPERMISSION", "PERMISSION_ID", UpdateRatingPermission.class);
	    _MappingKit.mapping(arp);
		arp.getEngine().setSourceFactory(new ClassPathSourceFactory());
		//arp.addSqlTemplate("/sql/all.sql");
	    plugins.add(arp);
	    
	   // plugins.add(new SqlXmlPlugin());  
	    
	    //任务配置
	    /*SchedulerPlugin sp = new SchedulerPlugin();
	    Runnable task = new TestTask();
	    sp.fixedDelaySchedule(task, 10);
	    sp.fixedRateSchedule(task, 10);
	    sp.cronSchedule(task, "0/5 * * * *");
	    plugins.add(sp);*/

		File cfg = new File(path+"/cfg/mail.properties");
		if(cfg.exists()){
			plugins.add(new MailPlugin(PropKit.use(new File(path+"/cfg/mail.properties")).getProperties()));
		}else{
			plugins.add(new MailPlugin(PropKit.use("mail.properties").getProperties()));
		}
	    
	    //任务配置
	   Cron4jPlugin cp = new Cron4jPlugin();
//	   if(PropKit.getBoolean("mailTask")){
//		  // cp.addTask("1 5,23 * * *", new ConfirmTask());
//		   cp.addTask("1,6,11,16,21,26,31,36,41,46,51,56 * * * *", new EmailTask());
//	   }
//		if(PropKit.getBoolean("wxPersonTask") && "fast".equals(PropKit.get("wxPersonTaskRate"))) {
//			cp.addTask("1,3,6,8,11,13,16,18,21,23,26,28,31,33,36,38,41,43,46,48,51,53,56,58 7,8,9,10,11,12,13,14,15,16,17,18,19,20,21 * * *", new WxPersonTask());
//		}else if(PropKit.getBoolean("wxPersonTask")) {
//			cp.addTask("1,11,21,31,41,51 7,8,9,10,11,12,13,14,15,16,17,18,19,20,21 * * *", new WxPersonTask());
//		}

        //自动确认
		cp.addTask("1,21,41 * * * *", new AutoConfirmTask());
		cp.addTask("* * * * *", new AutoSsoTask());

		//微信发送消息
		if(PropKit.getBoolean("wxMsgTask")) {
			cp.addTask("* * * * *", new WxMsgTask());
		}

		//飞书人员同步
		if(PropKit.getBoolean("larkPersonTask")) {
			cp.addTask("1 7,11,15,19,23 * * *", new LarkPersonTask());
		}
		//飞书发送消息
		if(PropKit.getBoolean("larkMsgTask")) {
			cp.addTask("* * * * *", new LarkMsgTask());
		}

//		if(PropKit.get("shrResultTask")!=null && PropKit.getBoolean("shrResultTask")){
//			// cp.addTask("1 5,23 * * *", new ConfirmTask());
//			cp.addTask("2,12,22,32,42,52 * * * *", new ShrResultTask());
//		}
		//cp.addTask("1 8 * * *", new JxWfTask());
	   plugins.add(cp);
	   
	}

	@Override
	public void configInterceptor(Interceptors me) {
		me.add(new AuthInterceptor());
		me.add(new ExceptionInterceptor());
		me.add(new SessionInViewInterceptor());
	}

	@Override
	public void configHandler(Handlers me) {
		me.add(new ContextPathHandler("basePath"));
		me.add(new ActionExtentionHandler());
	}

	@Override
	public void configEngine(Engine me) {
		me.addSharedObject("ctx", JFinal.me().getContextPath());
		me.addSharedObject("StrKit", new com.jfinal.kit.StrKit());
		//me.addSharedObject("shrurl",PropKit.get("shrurl"));
		me.addSharedObject("logout",PropKit.get("logout"));
		me.addSharedObject("devDebug",PropKit.getBoolean("devDebug"));
		me.addSharedObject("wxMsg", PropKit.getBoolean("wxMsg"));
		// 配置共享模板
		me.addSharedFunction("/_view/common/_layout.html");
		//me.addSharedFunction("/_view/common/_layout1.html");
		me.addSharedFunction("/_view/common/_iframe_layout.html");
	}

}