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

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

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.model.UserprofileTag;
import com.platomix.userprofile.service.UserprofileTagService;
import com.platomix.userprofile.utils.StrBuilder;

import lombok.extern.slf4j.Slf4j;

/**
 * @author wookvn
 *
 */
@Slf4j
@Service
public class XxhAppTagHandler extends AbstractTagHandler {
	
	final static String ZT_TABLE_NAME = "ads.ZT_USER_TAG_XXH_APP";
	final static long _STEP=50000000L;
	@Autowired
	private UserprofileTagService userprofileTagService;
	
	@Override
	public void invoke(String month, String mobileLeft, String mobileRight) {
		// 查询所有标签
		List<UserprofileTag> tagList = userprofileTagService.list();
		Map<String, List<UserprofileTag>> tagListMap = tagList.stream().collect(Collectors.groupingBy(t -> t.getEnglishName().toLowerCase()));

		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, "MONTH_ID_APP", "DEVICE_NUMBER_APP"));
		sqlBuilder.appendLn("select '"+month+"' as MONTH_ID_APP, DEVICE_NUMBER as DEVICE_NUMBER_APP,");
		//APP使用标签
		sqlBuilder.appendLn("groupArray(EVENT_APP_USE) as `EVENT_APP_USE.E`,")
				  .appendLn("groupArray(EVENT_APP_USE_C) as `EVENT_APP_USE.C`,")
				  .appendLn("groupArray(EVENT_APP_USE_V) as `EVENT_APP_USE.V`,")
				  .appendLn("groupArray(EVENT_APP_USE_DAYS) as `EVENT_APP_USE_DAYS.E`,")
				  .appendLn("groupArray(EVENT_APP_USE_DAYS_C) as `EVENT_APP_USE_DAYS.C`,")
				  .appendLn("groupArray(EVENT_APP_USE_DAYS_V) as `EVENT_APP_USE_DAYS.V`,")
				  .appendLn("groupArray(EVENT_APP_USE_HOURS) as `EVENT_APP_USE_HOURS.E`,")
				  .appendLn("groupArray(EVENT_APP_USE_HOURS_C) as `EVENT_APP_USE_HOURS.C`,")
				  .appendLn("groupArray(EVENT_APP_USE_HOURS_V) as `EVENT_APP_USE_HOURS.V`");
		
		sqlBuilder.appendLn("from(")
				  .appendLn("select MONTH_ID, DEVICE_NUMBER,");
		
		//APP使用 标签
		String useSql = genBucketSql("event_app_use", "ONE_FLUX", "ONE_FLUX", tagListMap.get("event_app_use"));
		sqlBuilder.append(useSql).appendLn(",");
		
		//APP月使用天数 标签[20220609_aokunsang_新增]
		String daySql = genBucketSql("event_app_use_days", "DAYS", "DAYS", tagListMap.get("event_app_use_days"));
		sqlBuilder.appendLn(daySql).appendLn(",");
		
		//APP月使用小时数 标签[20220609_aokunsang_新增]
		String hourSql = genBucketSql("event_app_use_hours", "floor(MINS/60, 1)", "floor(MINS/60, 1)", tagListMap.get("event_app_use_hours"));
		sqlBuilder.appendLn(hourSql);
		
		sqlBuilder.appendLn("from (") 
				  .appendLn("select DISTINCT * from (")
				  .appendLn("select * from  ads.XXH_ALL_APP_MONTH where ONE_FLUX >100 and MONTH_ID ='"+month+"'" + lrMobileSql) 
				  .appendLn("UNION ALL") 
				  .appendLn("select * from  ads.XXH_ALL_APP_CANCEL_MONTH where ONE_FLUX >100 and MONTH_ID ='"+month+"'" + lrMobileSql) 
				  .appendLn(") a ") 
				  .appendLn(") a1 ") 
				  .appendLn(") g ") 
				  .appendLn("group by MONTH_ID,DEVICE_NUMBER");
		log.info("\n" + sqlBuilder.toString());
		dynamicExecuteService.execute(DsEnum.clickhouse, sqlBuilder.toString(), "信息化标签生成");
	}
	
	@Override
	public boolean executeInBatchs() {
		return true;
	}
	@Override
	public long calcMobileRangeStep() {
		return _STEP;
	}
}
