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

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.Iterator;
import java.util.SortedSet;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;

import com.platomix.userprofile.service.DynamicExecuteService;
import com.platomix.userprofile.service.MobileRangeService;

/**
 * @author wookvn
 *
 */
public abstract class AbstractTagHandler implements ITagHandler{
	
	@Autowired
	private MobileRangeService mobileRangeService;
	
	@Autowired
	protected DynamicExecuteService dynamicExecuteService;
	
	protected final static DateTimeFormatter YYYMM_DATE_FORMATTER = DateTimeFormatter.ofPattern("yyyyMM");
	
	/**
	 * 执行入口
	 * @param month     月份 
	 * @param isInsert  是否插入
	 */
	public void handle(String month) {
		//invoke前执行
		beforeInvoke(month);

		if(executeInBatchs()) {
//			invoke(month, "0", "131");
//			invoke(month, "131", "132");
//			invoke(month, "132", "150");
//			invoke(month, "150", "156");
//			invoke(month, "156", "166");
//			invoke(month, "166", "176");
//			invoke(month, "176", "1768");
//			invoke(month, "1768", "1855");
//			invoke(month, "1855", "186");
//			invoke(month, "186", "1865");
//			invoke(month, "1865", "999");
			SortedSet<String> mobileList = mobileRangeService.getList(calcMobileRangeStep());
			if(mobileList.isEmpty()) {
				throw new RuntimeException("未查询到有效的手机号区间范围");
			}else {
				String mobileLeft = mobileList.first();
				for(Iterator<String> it = mobileList.iterator(); it.hasNext(); ) {
					String mobileRight = it.next();
					if(mobileLeft.equals(mobileRight)) {
						continue;
					}
					invoke(month, mobileLeft, mobileRight);
					mobileLeft = mobileRight;
				}
			}
		}else {
			invoke(month, null, null);
		}
		//invoke后执行
		afterInvoke(month);
	}

	/**
	 * 执行入口 (方便测试少量数据)
	 * @param month     月份
	 * @param mobileLeft  左号段  131
	 * @param mobileRight  右号段  131001
	 */
	public void handle(String month, String mobileLeft, String mobileRight) {
		invoke(month, mobileLeft, mobileRight);
	}

	/**
	 * 处理多个月份
	 * @param month
	 * @param isInsert
	 * @param number
	 */
	public void handleMultiMonth(String month, int number) {
		LocalDate monthDate = LocalDate.parse(month+"01", DateTimeFormatter.BASIC_ISO_DATE);
		for(int i = 0; i < number; i++) {
			String monthStr = monthDate.plusMonths(number).format(YYYMM_DATE_FORMATTER);
			handle(monthStr);
		}
	}

	/**
	 * 构建创建表或者插入表SQL
	 * @param tableName
	 * @param partitionBy
	 * @param orderBy
	 * @return
	 */
	protected String buildTableSql(String tableName, String partitionBy, String orderBy) {
		boolean isExists = dynamicExecuteService.checkTableExistsOfClickhouse(tableName);
		if(isExists) {
			return String.format("insert into %s", tableName);
		}else {
			String partition = StringUtils.isEmpty(partitionBy) ? "" : "PARTITION BY " + partitionBy;
			return String.format("create table %s ENGINE = MergeTree %s ORDER BY %s as", tableName, partition, orderBy);
		}
	}
}