/**
 * 
 */
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 ActivityJoinTagHandler extends AbstractTagHandler {

	final static String ZT_TABLE_NAME = "ads.ZT_USER_ACTIVITY_JOIN";
	
	@Autowired
	private UserprofileTagService userprofileTagService;
	
	@Override
	public void invoke(String month, String mobileLeft, String mobileRight) {
//		String lrMobileSql = mobileLeft == null || mobileRight == null ? "" : String.format(" and USERID>='%s' and USERID<'%s'", mobileLeft, mobileRight);
		LocalDate monthStartDate = LocalDate.parse(month+"01", DateTimeFormatter.BASIC_ISO_DATE);
		String nextMonth = monthStartDate.plusMonths(2).format(YYYMM_DATE_FORMATTER);
		
		StrBuilder sqlBuilder = new StrBuilder();
		sqlBuilder.appendLn(super.buildTableSql(ZT_TABLE_NAME, "THEMONTH_ACT", "USERID_ACT"));
		sqlBuilder.append("select MEMBER_PHONE as USERID_ACT,").append("'"+month+"'").appendLn(" as THEMONTH_ACT,");

		String activitySql = userprofileTagService.genSelectSql("参与活动", "title", "ACTIVITY_UUID", null);
		sqlBuilder.appendLn(activitySql);
		
		sqlBuilder.appendLn("from (")
				  .appendLn("  select ACTIVITY_UUID,MEMBER_PHONE,title from ads.ACTIVITY_MEMBER_JOIN_RECORD r")
				  .appendLn("	inner join ads.DIC_BLADE_ACTIVITY a on r.ACTIVITY_UUID =a.activity_uuid")
				  .appendLn("	where r.CREATE_TIME>='"+month+"' and r.CREATE_TIME<'"+nextMonth+"'")
				  .appendLn("	and  match(MEMBER_PHONE,'\\d{11}')")
				  .appendLn(")");
		log.info("\n" + sqlBuilder.toString());
		dynamicExecuteService.execute(DsEnum.clickhouse, sqlBuilder.toString(), "信息化标签生成");
	}
	
	@Override
	public boolean executeInBatchs() {
		return false;
	}

}
