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

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.IoUtil;
import com.platomix.userprofile.enums.DsEnum;
import com.platomix.userprofile.service.DynamicExecuteService;
import com.platomix.userprofile.service.MobileRangeService;
import com.platomix.userprofile.utils.StrBuilder;
import com.platomix.userprofile.utils.TagKit;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.nio.charset.Charset;
import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @author felix
 * 信息化数据处理 帮助类
 */
@Slf4j
@Component
public class XxhTagManager {

	final static String ETL_TABLE_NAME = "ads.XXH_ALL_MONTH_ETL2";
	@Autowired
	protected DynamicExecuteService dynamicExecuteService;

	@Autowired
	private MobileRangeService mobileRangeService;

	/**
	 * 从etl表向 xxh表转化
	 * @param month 月份
	 */
	private void xxhFromEtl(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("insert into ads.XXH_ALL_MONTH")
			    .appendLn("select distinct MONTH_ID, SEARCH_FLAG, DEVICE_NUMBER, PROV_DESC, UPPER(SERVICE_TYPE) as SERVICE_TYPE, toUInt32OrZero(INNET_MONTHS) INNET_MONTHS, CUST_SEX, CERT_AGE,")
				.appendLn("TOTAL_FLUX, TOTAL_FEE, ifNull(MON_TOTAL_FEE,'0') AS MON_TOTAL_FEE, PRODUCT_CLASS_DESC, null as MON_FEE, FLUX_M, RESOURCE_NAME,")
				.appendLn("PKG_NAME, MANU_NAME, MODEL_NAME, IS_ACCT ,TOTAL_FLUX_VIDEO, AREA as ARAE")
				.appendLn(String.format("from %s",ETL_TABLE_NAME))
				.appendLn("where MONTH_ID ='"+month+"'" + lrMobileSql);
		log.info("\n" + sqlBuilder.toString());
		dynamicExecuteService.execute(DsEnum.clickhouse, sqlBuilder.toString(), "ETL导入数据转化为XXH基础数据1");

		StrBuilder sqlBuilderApp = new StrBuilder();
		sqlBuilderApp.appendLn("insert into ads.XXH_ALL_APP_MONTH")
				.appendLn("select MONTH_ID,DEVICE_NUMBER, APP_PRE, CON_PRE, APP_NAME, Max(toUInt32OrZero(DAYS)) as DAYS,")
				.appendLn("toUInt32OrZero(TIMES) as TIMES, toUInt32OrZero(MINS) as MINS, toFloat64OrZero(ONE_FLUX) as ONE_FLUX")
				.appendLn("from ")
				.appendLn("( select MONTH_ID,DEVICE_NUMBER, APP_PRE, CON_PRE, APP_NAME, DAYS,TIMES,MINS,ifNull(ONE_FLUX,'0') as  ONE_FLUX")
				.appendLn("from  "+ETL_TABLE_NAME+" where MONTH_ID ='"+month+"'" + lrMobileSql)
				.appendLn(") s")
				.appendLn("group by MONTH_ID,DEVICE_NUMBER, APP_PRE, CON_PRE, APP_NAME, TIMES, MINS, ONE_FLUX");
		log.info("\n" + sqlBuilderApp.toString());
		dynamicExecuteService.execute(DsEnum.clickhouse, sqlBuilderApp.toString(), "ETL导入数据转化为XXH基础数据2");


	}
	
	/**
	 * 从etl表向 xxh表转化
	 * @param month 月份
	 */
	public void xxhFromEtlSp(String month) {
		SortedSet<String> mobileList = mobileRangeService.getList(MobileRangeService.DEFAULT_STEP);
		String mobileLeft = mobileList.first();
		for(Iterator<String> it = mobileList.iterator(); it.hasNext(); ) {
			String mobileRight = it.next();
			if(mobileLeft.equals(mobileRight)) {
				continue;
			}
			xxhFromEtl(month, mobileLeft, mobileRight);
			mobileLeft = mobileRight;
		}
	}

}
