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

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

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

import lombok.extern.slf4j.Slf4j;

/**
 * @author wookvn
 *
 *	退订预测标签
 */
@Slf4j
@Service
public class ForecastCancelTagHandler extends AbstractTagHandler {
	
	final static String ZT_TABLE_NAME = "ads.ZT_ORDER_CANCEL";
	
	@Autowired
	private UserprofileTagService userprofileTagService;

	@Override
	public void invoke(String month, String mobileLeft, String mobileRight) {
		StrBuilder sqlBuilder = new StrBuilder();
		sqlBuilder.appendLn(super.buildTableSql(ZT_TABLE_NAME, "THEMONTH_CEL", "USERID_CEL"));
		sqlBuilder.append("select USERID as USERID_CEL,").append("'"+month+"'").appendLn(" as THEMONTH_CEL,");
		
		String cancelSql = userprofileTagService.genSelectSql("退订预测", "CANCEL_P1", "CANCEL_P2", null, "", "tagValue");
		sqlBuilder.appendLn(cancelSql);
		
		sqlBuilder.appendLn("from\n" + 
				"(\n" + 
				"	select USERID,intDiv(CANCEL_P*100,10)*10 as CANCEL_P1,CANCEL_P*100 as CANCEL_P2\n" + 
				"	from ads.ALS_XXH_CANCEL_RESULT_C4\n" + 
				"	where TARGET_MONTH ='"+month+"'\n" + 
				") t");
		log.info("\n" + sqlBuilder.toString());
		dynamicExecuteService.execute(DsEnum.clickhouse, sqlBuilder.toString(), "退订预测标签生成");
	}

}
