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

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

import com.platomix.userprofile.enums.DsEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.druid.util.StringUtils;
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
public class ForecastOrderTagHandler extends AbstractTagHandler {

	final static String ZT_TABLE_NAME = "ads.ZT_ORDER_PREFERENCE";
	
	@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);

		List<ForecastParam> paramList = new ArrayList<>();
		paramList.add(new ForecastParam(month, "AQY", "爱奇艺", "爱奇艺", "DeepCross", "", lrMobileSql));
		paramList.add(new ForecastParam(month, "BZ", "哔哩", "哔哩哔哩", "DeepCross", lrMobileSql));
		paramList.add(new ForecastParam(month, "KS", "快手", "快手", "DeepCross", lrMobileSql));
		paramList.add(new ForecastParam(month, "TX", "腾讯","爱奇艺",  "DeepCross", lrMobileSql));
		paramList.add(new ForecastParam(month, "YK", "优酷","优酷视频",  "DeepCross", lrMobileSql));
		paramList.add(new ForecastParam(month, "MG", "芒果","芒果TV",  "DeepCross", lrMobileSql));
		
		paramList.add(new ForecastParam(month, "AQY", "爱奇艺","爱奇艺",  "XGBOOST", lrMobileSql));
		paramList.add(new ForecastParam(month, "BZ", "哔哩", "哔哩哔哩", "XGBOOST", lrMobileSql));
		paramList.add(new ForecastParam(month, "KS", "快手","快手",  "XGBOOST", lrMobileSql));
		paramList.add(new ForecastParam(month, "TX", "腾讯","爱奇艺",  "XGBOOST", lrMobileSql));
		paramList.add(new ForecastParam(month, "YK", "优酷","优酷视频",  "XGBOOST", lrMobileSql));
		paramList.add(new ForecastParam(month, "MG", "芒果", "芒果TV", "XGBOOST", lrMobileSql));
		
		StrBuilder sqlBuilder = new StrBuilder();
		sqlBuilder.appendLn(super.buildTableSql(ZT_TABLE_NAME, "THEMONTH_PRE", "USERID_PRE"));
		sqlBuilder.append("select ").append("'"+month+"'").appendLn(" as THEMONTH_PRE,").appendLn("multiIf(");
		for(ForecastParam param : paramList) {
			sqlBuilder.appendLn(String.format("%s.USERID != '', %s.USERID, ", param.getTableAlias(), param.getTableAlias()));
		}
		sqlBuilder.appendLn("'err') as USERID_PRE,");
		for(ForecastParam param : paramList) {
			String columnName = param.getEnglishName().toUpperCase(),
				   columnNameC = columnName+"_C", columnNameV = columnName+"_V";
			sqlBuilder.appendLn(String.format("max(%s.%s) as %s,", param.getTableAlias(), columnName, columnName));
			sqlBuilder.appendLn(String.format("max(%s.%s) as %s,", param.getTableAlias(), columnNameC, columnNameC));
			sqlBuilder.appendLn(String.format("max(%s.%s) as %s,", param.getTableAlias(), columnNameV, columnNameV));
		}
		sqlBuilder.deleteLastChar().deleteLastChar().appendLn("").appendLn("from");
		for(ForecastParam param : paramList) {
			sqlBuilder.appendLn(_genOrderPreferenceSql(param, tagListMap));
		}
		sqlBuilder.appendLn(" group by USERID_PRE");
		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.append(param.getJoinStr()).appendLn("(select USERID,");
		String englishName = param.getEnglishName();
		String fragmentSql = genBucketSql(englishName, "CANCEL_P1", "CANCEL_P2", tagListMap.get(englishName));
		sqlBuilder.appendLn(fragmentSql);
		
		String valueStr  = "XGBOOST".equalsIgnoreCase(param.getAlgName()) ? "PRO_XGBOOST" : "`PRO_DEEPCROSS`",
			   //value1Str = "XGBOOST".equalsIgnoreCase(param.getAlgName()) ? "PREDICT" : "PREDICT",
			   whereStr = String.format(" and APP_NAME='%s' ", param.getTpValue()),
				tableName = "ADS_ORDER_PRE_PREDICT_DEEP";
		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(String.format("where MONTH_ID ='%s'",param.getMonth()))
				  .appendLn(whereStr)
				  .appendLn(param.getLrMobileSql())
				  .appendLn(") t")
				  .appendLn(") " + param.getTableAlias());
		if(!StringUtils.isEmpty(param.getJoinStr())) {
			sqlBuilder.appendLn(" on tDeepCross_AQY.USERID="+param.getTableAlias()+".USERID");
		}
		return sqlBuilder.toString();
	}

	@Override
	public boolean executeInBatchs() {
		return true;
	}
	
	@Override
	public String tableName(String month) {
		return ZT_TABLE_NAME;
	}
	
	@Data
	@NoArgsConstructor
	static class ForecastParam{
		private String month;
		private String tp;
		private String tpName;
		private String tpValue;
		private String algName;
		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 tpValue
		 * @param lrMobileSql
		 */
		public ForecastParam(String month, String tp, String tpName,String tpValue, String algName, String lrMobileSql) {
			super();
			this.month = month;
			this.tp = tp;
			this.tpName = tpName;
			this.tpValue=tpValue;
			this.algName = algName;
			this.lrMobileSql = lrMobileSql;
		}
		/**
		 * @param month
		 * @param tp
		 * @param tpName
		 * @param tpValue
		 * @param joinStr
		 * @param lrMobileSql
		 */
		public ForecastParam(String month, String tp, String tpName,String tpValue, String algName, String joinStr,
				String lrMobileSql) {
			super();
			this.month = month;
			this.tp = tp;
			this.tpName = tpName;
			this.tpValue=tpValue;
			this.algName = algName;
			this.joinStr = joinStr;
			this.lrMobileSql = lrMobileSql;
		}
		
		public String getLongTp() {
			return nameMap.get(tp);
		}
		
		public String getTableAlias() {
			return String.format("t%s_%s", algName, tp);
		}

		public String getEnglishName() {
			return String.format("als_order_%s_%s", getLongTp(), 
					"DeepCross".equalsIgnoreCase(algName) ? "dec" : "xgb");
		}
		
	}
}
