/**
 * 
 */
package com.platomix.userprofile;

import cn.hutool.core.date.DateUtil;
import com.platomix.userprofile.handler.AbstractTagHandler;
import com.platomix.userprofile.handler.core.*;
import com.platomix.userprofile.handler.support.ForecastCancelTagManager;
import com.platomix.userprofile.handler.support.UserEventManager;
import com.platomix.userprofile.handler.support.UserTagManager;
import com.platomix.userprofile.handler.support.XxhTagManager;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.cache.annotation.EnableCaching;
import org.springframework.context.ConfigurableApplicationContext;

import com.platomix.userprofile.handler.FlatTagHandler;

import java.lang.reflect.Array;
import java.time.LocalDate;
import java.util.Arrays;
import java.util.Date;
import java.util.stream.Stream;

/**
 * @author wookvn
 */
@EnableCaching
@SpringBootApplication
public class Application {
	//全量运行时的handler  ！！注意！！ 一定要保证 collectAllTagHandler是最后一个
	static final String[] classHandlers=new String[]
			{ "xxhLongTagHandler", "xxhTagHandler", "xxhAppTagHandler", "xxhOrderTagHandler",
					"userOrderStateTagHandler", "orderSumDateTagHandler", "appPreferenceTagHandler",
					"complainTagHandler","forecastCancelTagHandler", "forecastOrderTagHandler",
					"collectAllTagHandler"};
	//运行时需要取前2个月的handler
	static final String[] classPerHandlers=new String[]
			{ "xxhTagHandler", "xxhAppTagHandler","appPreferenceTagHandler"};
	/**
	 * # arg[0]=调用类别:
	 * 		zt_tag(生成标签) dic_tag（更新mysql字典表） xxh_etl（更新原始信息化etl数据到xxh表）
	 * 		z_cancel_data（准备退订数据） join_all（只合并宽表）  single_handler（单独调用 指定handler）
	 * # arg[1]=月份: month
	 * # arg[2]=HandlerName: [比如：java -jar xxx.jar xxhAppTagHandler（使用类名的小写开头的字符串）]
	 *
	 */
	public static void main(String[] args) {
		ConfigurableApplicationContext applicationContext = SpringApplication.run(Application.class, args);

//		AbstractTagHandler tagHandler = applicationContext.getBean("orderSumDateTagHandler", AbstractTagHandler.class);
//		tagHandler.handle("202111");

		//批量
//		tagHandler.handleMultiMonth("202111", false, 3);
//		//Flat表操作
//		FlatTagHandler tagHandler = applicationContext.getBean(FlatTagHandler.class);
//		tagHandler.tempDataToFlatView("202111");
		//运行程序
		StartProg(applicationContext,args);
		applicationContext.close();
	}

	/**
	 * 根据args参数
	 * @param args
	 */
	static void StartProg(ConfigurableApplicationContext applicationContext ,String[] args){
		//arg0 调用类别
		String sendTp = "";
		//arg1 月份
		String permonth = DateUtil.format(DateUtil.offsetMonth(new Date(), -1), "yyyyMM");
		String p_permonth =  DateUtil.format(DateUtil.offsetMonth(new Date(), -2), "yyyyMM");
		//arg2 HandlerName
		String handlerName = "";
		if (args != null && args.length > 0) {
			sendTp = args[0];
			if (args.length > 1) {
				permonth = args[1];
				p_permonth = DateUtil.format(DateUtil.offsetMonth(DateUtil.parse(permonth.substring(0, 4) + "-" + permonth.substring(4) + "-01", "yyyy-MM-dd"), -1), "yyyyMM");
			}
			if (args.length > 2) {
				handlerName = args[2];
			}
		}

		/**
		 * 生成指定月份全部标签（不传月份参数会默认跑前一个月的标签）
		 */
		if (sendTp.equals("zt_tag")) {
			for (Integer i = 0; i < classHandlers.length; i++) {
				AbstractTagHandler tagHandler = applicationContext.getBean(classHandlers[i], AbstractTagHandler.class);
				if (Arrays.asList(classPerHandlers).contains(classHandlers[i])) {
					tagHandler.handle(p_permonth);
				} else {
					tagHandler.handle(permonth);
				}
			}
			//生成rfm表 todo
			//Flat表操作
			FlatTagHandler flatTagHandler = applicationContext.getBean(FlatTagHandler.class);
			flatTagHandler.tempDataToFlatView(permonth);
		}
		/**
		 * 单独调用指定handler
		 */
		if (sendTp.equals("single_handler")) {
			AbstractTagHandler tagHandler = applicationContext.getBean(handlerName, AbstractTagHandler.class);
			tagHandler.handle(permonth);
		}
		/**
		 * 更新mysql中的字典表
		 */
		if (sendTp.equals("dic_tag")) {
			UserEventManager userEventHandler = applicationContext.getBean(UserEventManager.class);
			userEventHandler.genEventPropToMysql();
			UserTagManager manager = applicationContext.getBean(UserTagManager.class);
			manager.genSomeTagToMysql();
		}
		/**
		 * 更新etl2->xxh
		 */
		if (sendTp.equals("xxh_etl")) {
			XxhTagManager xxhTagManager = applicationContext.getBean(XxhTagManager.class);
			xxhTagManager.xxhFromEtlSp(permonth);
		}
		/**
		 * 退订订ding  todo
		 */
		//退订数据准备  先调用这个准备数据，然后取TensorFlow训练，并预测 ？月结果
		if (sendTp.equals("z_cancel_data")) {
			ForecastCancelTagManager manager = applicationContext.getBean(ForecastCancelTagManager.class);
			manager.tagCancelDataC4(permonth,-1,true);
			manager.tagCancelDataC4V2(permonth);
		}

		if(sendTp.equals("join_all")){
			FlatTagHandler flatTagHandler = applicationContext.getBean(FlatTagHandler.class);
			flatTagHandler.tempDataToFlatView(permonth);
		}
	}
}