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

import java.util.List;
import java.util.Map;

import org.springframework.util.StringUtils;

import com.platomix.userprofile.model.UserprofileTag;
import com.platomix.userprofile.service.MobileRangeService;
import com.platomix.userprofile.utils.StrBuilder;
import com.platomix.userprofile.utils.TagKit;

/**
 * @author wookvn
 *
 */
public interface ITagHandler {

	/**
	 * 是否需要分批次跑
	 * @return
	 */
	default boolean executeInBatchs() {
		return false;
	}
	
	/**
	 * 分批跑数据的手机号范围步长
	 * @return
	 */
	default long calcMobileRangeStep() {
		return MobileRangeService.DEFAULT_STEP;
	}
	
	/**
	 * 生成分桶sql
	 * @param englishName   - 二级标签英文名(用于宽表字段列名映射)
	 * @param fieldFunction - 分桶条件字段
	 * @param fieldValue    - _V设置值
	 * @param tagList       - 该二级标签下所有带分桶的标签值
	 * @param paramMap      - 带占位符
	 * @return
	 */
	default String genBucketSql(String englishName, String fieldFunction, String fieldValue, List<UserprofileTag> tagList, Map<String, String> params) {
		StrBuilder tagSqlBuilder = new StrBuilder("(case "), tagCSqlBuilder = new StrBuilder("(case ");
		String otherTagSql = null, otherTagCSql = "";
		for(UserprofileTag tagItem : tagList) {
			String sql = "";
			//公式优先级最高
			if(!StringUtils.isEmpty(tagItem.getBucketFormula())) {
				String formula = TagKit.textMatchReplace(tagItem.getBucketFormula(), params); //替换占位符
				sql = String.format("when %s then '%%s'", formula);
			}else {
				//开始存在，结束不存在
				if(!StringUtils.isEmpty(tagItem.getBucketLeft()) && StringUtils.isEmpty(tagItem.getBucketRight())) {
					sql = String.format("when %s%s then '%%s'", fieldFunction, tagItem.getBucketLeft());
				}
				//开始不存在，结束存在
				if(StringUtils.isEmpty(tagItem.getBucketLeft()) && !StringUtils.isEmpty(tagItem.getBucketRight())) {
					sql = String.format("when %s%s then '%%s'", fieldFunction, tagItem.getBucketRight());
				}
				//开始和结束都不存在
				if(StringUtils.isEmpty(tagItem.getBucketLeft()) && StringUtils.isEmpty(tagItem.getBucketRight())) {
					otherTagSql = String.format("else '%s'", tagItem.getTagId());
					otherTagCSql = String.format("else '%s'", tagItem.getTagName());
					continue;
				}
				//开始和结束都存在
				if(!StringUtils.isEmpty(tagItem.getBucketLeft()) && !StringUtils.isEmpty(tagItem.getBucketRight())) {
					sql = String.format("when %s%s and %s%s then '%%s'", 
							fieldFunction, tagItem.getBucketLeft(), fieldFunction, tagItem.getBucketRight());
				}
			}
			tagSqlBuilder.appendLn(String.format(sql, tagItem.getTagId()));
			tagCSqlBuilder.appendLn(String.format(sql, tagItem.getTagName()));
		}
		if(otherTagSql != null && otherTagCSql != null) {
			tagSqlBuilder.appendLn(otherTagSql);
			tagCSqlBuilder.appendLn(otherTagCSql);
		}
		String tagSql = tagSqlBuilder.appendLn(" end) " + englishName.toUpperCase()).append(",").toString();
		String tagCSql = tagCSqlBuilder.appendLn(" end) " + englishName.toUpperCase() + "_C,").toString();
		String tagVSql = fieldValue + " as " + englishName.toUpperCase() + "_V";
		return tagSql + tagCSql + tagVSql;
	}
	
	/**
	 * 生成分桶sql
	 * @param englishName   - 二级标签英文名(用于宽表字段列名映射)
	 * @param fieldFunction - 分桶条件字段
	 * @param fieldValue    - _V设置值
	 * @param tagList       - 该二级标签下所有带分桶的标签值
	 * @return
	 */
	default String genBucketSql(String englishName, String fieldFunction, String fieldValue, List<UserprofileTag> tagList) {
		return genBucketSql(englishName, fieldFunction, fieldValue, tagList, null);
	}
	
	/**
	 * 执行一次
	 * @param month
	 * @param isInsert
	 * @param mobileLeft
	 * @param mobileRight
	 */
	public void invoke(String month, String mobileLeft, String mobileRight);

	/**
	 * before invoke 执行
	 * @return
	 */
	default void beforeInvoke(String month) {  }

	/**
	 * after invoke 执行
	 * @return
	 */
	default void afterInvoke(String month) {  }
}
