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

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

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 UserOrderStateTagHandler extends AbstractTagHandler {
	
	final static String ZT_TABLE_NAME = "ads.ZT_USER_ORDER_STATE";
	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, String> tagMap = tagList.stream().collect(Collectors.toMap(UserprofileTag::getTagId, UserprofileTag::getTagName, (t, t1) -> t));

//		String lrMobileSql = mobileLeft == null || mobileRight == null ? "" : String.format(" and USERID>='%s' and USERID<'%s'", mobileLeft, mobileRight);
		
		// 获取月份开始日期
		String monthStart = month.substring(0, 4) + "-" + month.substring(4) + "-01";
		LocalDate monthStartDate = LocalDate.parse(monthStart, DateTimeFormatter.ISO_LOCAL_DATE);
		String nextMonth = monthStartDate.plusMonths(1).format(DateTimeFormatter.ofPattern("yyyy-MM-dd"));
		
		StrBuilder sqlBuilder = new StrBuilder();
		sqlBuilder.appendLn(super.buildTableSql(ZT_TABLE_NAME, "THEMONTH_BL", "USERID_BL"));
		sqlBuilder.append("select DEVICE_NUMBER as USERID_BL,").append("'"+month+"'").appendLn(" as THEMONTH_BL,")
				  .appendLn("toDate('1970-01-01') as cTime,")
				  .appendLn("'B112_19_003' as EVENT_USER_OSTATE,")
				  .appendLn("'未订过' as EVENT_USER_OSTATE_C,")
				  .appendLn("toDate('1970-01-01') as EVENT_USER_OSTATE_V from (")
				  .appendLn(" select DISTINCT DEVICE_NUMBER from")
				  .appendLn(" (")
				  .appendLn("	select DEVICE_NUMBER from ads.XXH_ALL_MONTH")
				  .appendLn("	union all")
				  .appendLn("   select DEVICE_NUMBER from ads.XXH_KUAISHOU_MONTH xkm")
				  .appendLn(" )")
				  .appendLn(")s")
				  .appendLn("where s.DEVICE_NUMBER not in (")
				  .appendLn("select USERID from ads.ztemp_relation_users  --ads.ETL_TD_ORDER_RELATIONS")
				  .appendLn(")")
				  .appendLn("union all")
				  .appendLn("SELECT '"+month+"' as THEMONTH_BL,USERID as USERID_BL,")
				  .appendLn("arrayJoin(groupArray(1)(CANCELTIME1)) AS cTime,")
				  .appendLn("if(cTime>='"+nextMonth+"' ,'B112_19_001','B112_19_002') as EVENT_USER_OSTATE,")
				  .appendLn("if(cTime>='"+nextMonth+"' ,'在订','退订') as EVENT_USER_OSTATE_C,")
				  .appendLn("cTime as EVENT_USER_OSTATE_V")
				  .appendLn("FROM (")
				  .appendLn("	SELECT USERID,if(CANCELTIME='1970-01-01', toDate('"+nextMonth+"'),CANCELTIME) as CANCELTIME1")
				  .appendLn("	FROM ads.ETL_TD_ORDER_RELATIONS_YEAR")
				  .appendLn("	where THEMONTH ='"+month+"'")
				  .appendLn("	ORDER BY CANCELTIME1 DESC")
				  .appendLn(") GROUP BY USERID");
		log.info("\n" + sqlBuilder.toString());
		dynamicExecuteService.execute(DsEnum.clickhouse, sqlBuilder.toString(), "用户订购状态标签生成");
	}
	
	@Override
	public boolean executeInBatchs() {
		return false;
	}
	@Override
	public long calcMobileRangeStep() {
		return _STEP;
	}

}
