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

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.platomix.userprofile.handler.AbstractTagHandler;
import com.platomix.userprofile.model.UserprofileTag;
import com.platomix.userprofile.service.UserprofileTagService;
import com.platomix.userprofile.utils.StrBuilder;

import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * @author wookvn
 *
 *	订购偏好预测 - 临时表
 */
@Slf4j
@Service
@Deprecated
public class ForecastOrderTagTempManager extends AbstractTagHandler {

	final static String ZT_TABLE_NAME = "ads.ZT_ORDER_PREFERENCE_temp1";
	
	@Autowired
	private UserprofileTagService userprofileTagService;
	
	@Override
	public void invoke(String month, String mobileLeft, String mobileRight) {
		List<UserprofileTag> tagList = userprofileTagService.list();
		Map<String, List<UserprofileTag>> tagListMap = tagList.stream().collect(Collectors.groupingBy(t -> t.getEnglishName().toLowerCase()));
		
		String lrMobileSql = mobileLeft == null || mobileRight == null ? "" : String.format(" and DEVICE_NUMBER>='%s' and DEVICE_NUMBER<'%s'", mobileLeft, mobileRight);
		StrBuilder sqlBuilder = new StrBuilder();
		sqlBuilder.appendLn(super.buildTableSql(ZT_TABLE_NAME, null, "tDeepCross_AQY.USERID"));
		//爱奇艺
		sqlBuilder.appendLn(_genOrderPreferenceSql(new ForecastParam(month, "AQY", "爱奇艺", "DeepCross", " select * from ", lrMobileSql), tagListMap));
		//B站
		sqlBuilder.appendLn(_genOrderPreferenceSql(new ForecastParam(month, "BZ", "哔哩", "DeepCross", lrMobileSql), tagListMap));
		sqlBuilder.appendLn("on tDeepCross_AQY.USERID=tDeepCross_BZ.USERID");
		//快手
		sqlBuilder.appendLn(_genOrderPreferenceSql(new ForecastParam(month, "KS", "快手", "DeepCross", lrMobileSql), tagListMap));
		sqlBuilder.appendLn("on tDeepCross_AQY.USERID=tDeepCross_KS.USERID");
		//腾讯
		sqlBuilder.appendLn(_genOrderPreferenceSql(new ForecastParam(month, "TX", "腾讯", "DeepCross", lrMobileSql), tagListMap));
		sqlBuilder.appendLn("on tDeepCross_AQY.USERID=tDeepCross_TX.USERID");
		//优酷
		sqlBuilder.appendLn(_genOrderPreferenceSql(new ForecastParam(month, "YK", "优酷", "DeepCross", lrMobileSql), tagListMap));
		sqlBuilder.appendLn("on tDeepCross_AQY.USERID=tDeepCross_YK.USERID");
		//芒果
		sqlBuilder.appendLn(_genOrderPreferenceSql(new ForecastParam(month, "MG", "芒果", "DeepCross", lrMobileSql), tagListMap));
		sqlBuilder.appendLn("on tDeepCross_AQY.USERID=tDeepCross_MG.USERID");
		
		//爱奇艺2
		sqlBuilder.appendLn(_genOrderPreferenceSql(new ForecastParam(month, "AQY", "爱奇艺", "XGBoost", lrMobileSql), tagListMap));
		sqlBuilder.appendLn("on tDeepCross_AQY.USERID=tXGBoost_AQY.USERID");
		//B站
		sqlBuilder.appendLn(_genOrderPreferenceSql(new ForecastParam(month, "BZ", "哔哩", "XGBoost", lrMobileSql), tagListMap));
		sqlBuilder.appendLn("on tDeepCross_AQY.USERID=tXGBoost_BZ.USERID");
		//快手
		sqlBuilder.appendLn(_genOrderPreferenceSql(new ForecastParam(month, "KS", "快手", "XGBoost", lrMobileSql), tagListMap));
		sqlBuilder.appendLn("on tDeepCross_AQY.USERID=tXGBoost_KS.USERID");
		//腾讯
		sqlBuilder.appendLn(_genOrderPreferenceSql(new ForecastParam(month, "TX", "腾讯", "XGBoost", lrMobileSql), tagListMap));
		sqlBuilder.appendLn("on tDeepCross_AQY.USERID=tXGBoost_TX.USERID");
		//优酷
		sqlBuilder.appendLn(_genOrderPreferenceSql(new ForecastParam(month, "YK", "优酷", "XGBoost", lrMobileSql), tagListMap));
		sqlBuilder.appendLn("on tDeepCross_AQY.USERID=tXGBoost_YK.USERID");
		//芒果
		sqlBuilder.appendLn(_genOrderPreferenceSql(new ForecastParam(month, "MG", "芒果", "XGBoost", lrMobileSql), tagListMap));
		sqlBuilder.appendLn("on tDeepCross_AQY.USERID=tXGBoost_MG.USERID");
		log.info("\n" + sqlBuilder.toString());
//		dynamicExecuteService.execute(DsEnum.clickhouse, sqlBuilder.toString(), "订购偏好预测[临时表]生成");
	}
	
	/**
	 * 
	 * @param param
	 * @return
	 */
	private String _genOrderPreferenceSql(ForecastParam param, Map<String, List<UserprofileTag>> tagListMap) {
		StrBuilder sqlBuilder = new StrBuilder();
		sqlBuilder.appendLn(param.getJoinStr()).appendLn("(select USERID,");
//		String level2Name = String.format("订购预测_%s_%s", param.getTpName(), 
//				"DeepCross".equalsIgnoreCase(param.getTableAlsStr()) ? "dec" : "xgb");
		String englishName = String.format("als_order_%s_%s", param.getLongTp(), 
				"DeepCross".equalsIgnoreCase(param.getTableAlsStr()) ? "dec" : "xgb");
		
		String fragmentSql = genBucketSql(englishName, "CANCEL_P1", "CANCEL_P2", tagListMap.get(englishName));
//		String fragmentSql = userprofileTagService.genSelectSql(level2Name, "CANCEL_P1", "CANCEL_P2", null,"", "tagValue");
		sqlBuilder.appendLn(fragmentSql);
		
		String valueStr  = "XGBoost".equalsIgnoreCase(param.getTableAlsStr()) ? "PRO_RESULT" : "`1_PRO_RESULT`",
			   value1Str = "XGBoost".equalsIgnoreCase(param.getTableAlsStr()) ? "RESULT" : "result",
			   tableName = String.format("ads.ADS_ORDER_PRE_%s_%s_%s", param.getTableAlsStr(), param.getTp(), value1Str),
			   tableAlias = String.format("t%s_%s", param.getTableAlsStr(), param.getTp());
		sqlBuilder.appendLn("from")
		          .appendLn("(")
				  .appendLn("select DEVICE_NUMBER as  USERID,intDiv("+valueStr+"*100,10)*10 as CANCEL_P1,"+valueStr+"*100 as CANCEL_P2")
				  .appendLn("from " + tableName)
				  .append("where 1 = 1").appendLn(param.getLrMobileSql())
				  .appendLn(") t")
				  .appendLn(") " + tableAlias);
		return sqlBuilder.toString();
	}

	@Override
	public boolean executeInBatchs() {
		return true;
	}
	
	@Data
	@NoArgsConstructor
	static class ForecastParam{
		private String month;
		private String tp;
		private String tpName;
		private String tableAlsStr;
		private String joinStr = " full outer join ";
		private String lrMobileSql;
		
		final static Map<String, String> nameMap = new HashMap<>();
		static {
			nameMap.put("AQY", "aiqiyi");
			nameMap.put("KS", "kuaishou");
			nameMap.put("TX", "tencent");
			nameMap.put("MG", "mangtv");
			nameMap.put("YK", "youku");
			nameMap.put("BZ", "bilibili");
		}
		/**
		 * @param month
		 * @param tp
		 * @param tpName
		 * @param tableAlsStr
		 * @param lrMobileSql
		 */
		public ForecastParam(String month, String tp, String tpName, String tableAlsStr, String lrMobileSql) {
			super();
			this.month = month;
			this.tp = tp;
			this.tpName = tpName;
			this.tableAlsStr = tableAlsStr;
			this.lrMobileSql = lrMobileSql;
		}
		/**
		 * @param month
		 * @param tp
		 * @param tpName
		 * @param tableAlsStr
		 * @param joinStr
		 * @param lrMobileSql
		 */
		public ForecastParam(String month, String tp, String tpName, String tableAlsStr, String joinStr,
				String lrMobileSql) {
			super();
			this.month = month;
			this.tp = tp;
			this.tpName = tpName;
			this.tableAlsStr = tableAlsStr;
			this.joinStr = joinStr;
			this.lrMobileSql = lrMobileSql;
		}
		
		public String getLongTp() {
			return nameMap.get(tp);
		}
		
	}
}
