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

import java.util.concurrent.TimeUnit;

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 XxhLongTagHandler extends AbstractTagHandler {
	final static String XXH_TABLE_NAME = "ads.XXH_ALL_MONTH_LONG";
	final static String ZT_TABLE_NAME = "ads.ZT_USER_TAG_XXH_LONG";

	@Autowired
	private UserprofileTagService userprofileTagService;

	@Override
	public void invoke(String month, String mobileLeft, String mobileRight) {
		String lrMobileSql = mobileLeft == null || mobileRight == null ? "" : String.format(" and DEVICE_NUMBER>='%s' and DEVICE_NUMBER<'%s'", mobileLeft, mobileRight);
		StrBuilder sqlBuilder = new StrBuilder();
		sqlBuilder.appendLn(super.buildTableSql(ZT_TABLE_NAME, null, "DEVICE_NUMBER"));
		sqlBuilder.appendLn("select DEVICE_NUMBER ,any(PROV_DESC) PROV_DESC,");
		sqlBuilder.appendLn("any(SERVICE_TYPE) SERVICE_TYPE,any(CUST_SEX) CUST_SEX,any(CERT_AGE) CERT_AGE,any(PRODUCT_CLASS_DESC) PRODUCT_CLASS_DESC ");
		sqlBuilder.appendLn("from ");
		sqlBuilder.appendLn("(select  MONTH_ID, DEVICE_NUMBER ,if(s1.PROV_DESC=='',b.PROVINCE,s1.PROV_DESC) as PROV_DESC,SERVICE_TYPE ,CUST_SEX,CERT_AGE,PRODUCT_CLASS_DESC from ( ");
		sqlBuilder.appendLn("	select MONTH_ID, DEVICE_NUMBER ,PROV_DESC,SERVICE_TYPE ,CUST_SEX,CERT_AGE,PRODUCT_CLASS_DESC from ");
		sqlBuilder.appendLn("	ads.XXH_ALL_MONTH ");
		sqlBuilder.appendLn(String.format("	where  MONTH_ID >='202101' and  DEVICE_NUMBER not in (select DEVICE_NUMBER from ads.XXH_KUAISHOU_MONTH where 1=1 '%s')  '%s' ",lrMobileSql,lrMobileSql));
		sqlBuilder.appendLn("  union all ");
		sqlBuilder.appendLn(" select MONTH_ID,DEVICE_NUMBER,  PROV_DESC, NULL as SERVICE_TYPE,if(CUST_SEX==1,'男','女') as CUST_SEX, ");
		sqlBuilder.appendLn("  multiIf((2021-CERT_AGE)>=2010,'10后',(2021-CERT_AGE)>=2000,'00后',(2021-CERT_AGE)>=1990,'90后',(2021-CERT_AGE)>=1980,'80后', ");
		sqlBuilder.appendLn("  (2021-CERT_AGE)>=1970,'70后',(2021-CERT_AGE)<1970,'70前','未知') as CERT_AGE, ");
		sqlBuilder.appendLn("  PRODUCT_CLASS_DESC ");
		sqlBuilder.appendLn("  from ads.XXH_KUAISHOU_MONTH where 1=1 " + lrMobileSql);
		sqlBuilder.appendLn("  ) s1 ");
		sqlBuilder.appendLn("  left join  ads.ETL_VW_TB_CALLER_AREACODE b on substring(s1.DEVICE_NUMBER , 1, 7) = b.CALLERHEAD ");
		sqlBuilder.appendLn("	order by MONTH_ID desc ");
		sqlBuilder.appendLn(") group by DEVICE_NUMBER ");
		log.info("\n" + sqlBuilder.toString());
		dynamicExecuteService.execute(DsEnum.clickhouse, sqlBuilder.toString(), "信息化标签生成");
	}

	
	@Override
	public boolean executeInBatchs() { return true; }

	/**
	 * before invoke 执行
	 * @return
	 */
	@Override
	public void beforeInvoke(String month) {
		dynamicExecuteService.execute(DsEnum.clickhouse, String.format("drop table if exists %s",XXH_TABLE_NAME), "删除long标签原始数据");
		dynamicExecuteService.execute(DsEnum.clickhouse, String.format("drop table if exists %s",ZT_TABLE_NAME), "删除long标签缓存数据");
		try {
			TimeUnit.SECONDS.sleep(10);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

	/**
	 * after invoke 执行
	 * @return
	 */
	@Override
	public void afterInvoke(String month){
		try {
			TimeUnit.SECONDS.sleep(10);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}

		StrBuilder sqlBuilder = new StrBuilder();
		sqlBuilder.appendLn(super.buildTableSql(XXH_TABLE_NAME, null, "DEVICE_NUMBER_XXH_L"));
		sqlBuilder.appendLn("select DEVICE_NUMBER as DEVICE_NUMBER_XXH_L,");
		//性别
		String sexSql = userprofileTagService.genSelectSql("性别", "CUST_SEX", null, null);
		sqlBuilder.append(sexSql).appendLn(",");

		//省份
		String provinceSql = userprofileTagService.genSelectSql("省份", "PROV_DESC", null, null);
		sqlBuilder.append(provinceSql).appendLn(",");

		//网络类型
		String netSql = userprofileTagService.genSelectSql("网络类型", "PROV_DESC", null, null);
		sqlBuilder.append(netSql).appendLn(",");

		//年龄阶段
		String ageSql = userprofileTagService.genSelectSql("年龄阶段", "CERT_AGE", null, null);
		sqlBuilder.appendLn(ageSql);

		sqlBuilder.appendLn(String.format("from %s ",XXH_TABLE_NAME));
		log.info("\n" + sqlBuilder.toString());
		dynamicExecuteService.execute(DsEnum.clickhouse, sqlBuilder.toString(), "信息化标签生成");

	}

}
