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

import java.nio.charset.Charset;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;

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

import com.platomix.userprofile.service.DynamicExecuteService;
import com.platomix.userprofile.utils.TagKit;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IoUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author wookvn
 *
 *         退订预测 帮助类
 */
@Slf4j
@Component
public class ForecastCancelTagManager {

	@Autowired
	protected DynamicExecuteService dynamicExecuteService;

	/**
	 * @param month
	 * @param isCancel
	 * @param isDropMonth
	 */
	public void tagCancelDataC4(String month, int isCancel, boolean isDropMonth) {
		String dateStr = month.substring(0, 4) + "-" + month.substring(4) + "-01";
		Date date = DateUtil.parse(dateStr, "yyyy-MM-dd");
		String pr1fullMonth = DateUtil.format(DateUtil.offsetMonth(date, 1), "yyyy-MM-dd"),
				p1month = DateUtil.format(DateUtil.offsetMonth(date, 0), "yyyyMM"),
				p2month = DateUtil.format(DateUtil.offsetMonth(date, -1), "yyyyMM"),
				p3month = DateUtil.format(DateUtil.offsetMonth(date, -2), "yyyyMM"),
				p4month = DateUtil.format(DateUtil.offsetMonth(date, -3), "yyyyMM"),
				p5month = DateUtil.format(DateUtil.offsetMonth(date, -4), "yyyyMM"),
				theDay = DateUtil.format(DateUtil.offsetMonth(date, 1), "yyyyMM01"), 
				per3Day = p4month + "01";
		if (isDropMonth) {
			_dropPartition("ads.ALS_XXH_CANCEL_NEW_C4", month);
			_dropPartition("ads.ALS_XXH_CANCEL_NEW_C4_lowDim", month);
			_dropPartition("ads.ALS_XXH_CANCEL_RESULT_C4", month);
			_dropPartition("ads.FIN_XXH_CANCEL_NEW_C4_RESULT", month);
		}
		String sqlCancel = "";
		if (isCancel == 1) {
			sqlCancel = " and IS_CANCEL=1 ";
		}
		if (isCancel == 0) {
			sqlCancel = " and IS_CANCEL=0 ";
		}
		if (isCancel == -1) {
			sqlCancel = " ";
		}
		
		Map<String, String> params = new HashMap<>();
		params.put("month", month);
		params.put("dateStr", dateStr);
		params.put("pr1full_month", pr1fullMonth);
		params.put("p1month", p1month);
		params.put("p2month", p2month);
		params.put("p3month", p3month);
		params.put("p4month", p4month);
		params.put("p5month", p5month);
		params.put("theDay", theDay);
		params.put("per3Day", per3Day);
		params.put("sql_Cancel", sqlCancel);
		
		String sqlText = IoUtil.read(Thread.currentThread().getContextClassLoader().getResourceAsStream("sql/ALS_XXH_CANCEL_NEW_C4.sql"), Charset.forName("utf-8"));
		String sql = TagKit.sqlMatchReplace(sqlText, params);
		log.info("\n" + sql);
		dynamicExecuteService.execute(DsEnum.clickhouse, sql, "ALS_XXH_CANCEL_NEW_C4数据生成");
	}
	
	/**
	 * 
	 * @param month
	 */
	public void tagCancelDataC4V2(String month) {
		Map<String, String> params = new HashMap<>();
		params.put("month", month);
		String sqlText = IoUtil.read(Thread.currentThread().getContextClassLoader().getResourceAsStream("sql/ALS_XXH_CANCEL_NEW_C4_lowDim.sql"), Charset.forName("utf-8"));
		String sql = TagKit.sqlMatchReplace(sqlText, params);
		log.info("\n" + sql);
		dynamicExecuteService.execute(DsEnum.clickhouse, sql, "ALS_XXH_CANCEL_NEW_C4_lowDim数据生成");
	}
	
	private void _dropPartition(String tableName, String partitionMonth) {
		String dropSql = "alter table " + tableName + " drop partition '" + partitionMonth + "'";
//		dynamicExecuteService.execute(DsEnum.clickhouse, dropSql, String.format("drop table [%s] partition[%s] 完成", tableName, partitionMonth));
		try {
			TimeUnit.SECONDS.sleep(3);
		} catch (InterruptedException e) {
		}
	}
}
