/**
 * 
 */
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
 * 	
 * RMF标签
 *
 */
@Slf4j
@Service
public class RmfTagHandler extends AbstractTagHandler {

	final static String ZT_TABLE_NAME = "ads.ZT_USER_RFM";
	
	@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 USERID>='%s' and USERID<'%s'", mobileLeft, mobileRight);
		
		StrBuilder sqlBuilder = new StrBuilder();
		sqlBuilder.appendLn(super.buildTableSql(ZT_TABLE_NAME, "THEMONTH_RFM", "USERID_RFM"));
		sqlBuilder.append("select USERID as USERID_RFM,").append("'"+month+"'").appendLn(" as THEMONTH_RFM,");
		//标签范围值
		/**
		 * (case when CLASS =1 then 'A112_03_001'
			when  CLASS =2 then 'A112_03_002'
			when  CLASS =3 then 'A112_03_003'
			when  CLASS =4 then 'A112_03_004'
			when  CLASS =5 then 'A112_03_005'
			 end) TAG_RFM,
		 */
		String sql = genBucketSql("tag_rmf", "CLASS", "CLASS", tagListMap.get("tag_rmf"));
		sqlBuilder.appendLn(sql);
		sqlBuilder.appendLn("from ads.ALS_RMF_CLUSTER2_RESULT");
		log.info("\n" + sqlBuilder.toString());
		dynamicExecuteService.execute(DsEnum.clickhouse, sqlBuilder.toString(), "信息化标签生成");
	}

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