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

	final static String ZT_TABLE_NAME = "ads.ZT_ORDER_KUAISHOU";
	
	@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_KS", "USERID_KS"));
		sqlBuilder.append("select DEVICE_NUMBER as USERID_KS,").append("'"+month+"'").appendLn(" as THEMONTH_KS,");
		//标签范围值
		/**
		 * (case when IS_ORDER=2 then 'C100_02_001'
			when  IS_ORDER=1 then 'C100_02_002'
			else 'C100_02_003'
		   end) ALS_ORDER_KUAISHOU,
		 */
		String sql = genBucketSql("als_order_kuaishou", "IS_ORDER", "PREDPROB2", tagListMap.get("als_order_kuaishou"));
		sqlBuilder.appendLn(sql);
		sqlBuilder.appendLn("from ads.XXH_KUAISHOU_YUCEJI_RESULT")
				  .appendLn("where MONTH_ID = '"+month+"'");
		log.info("\n" + sqlBuilder.toString());
		dynamicExecuteService.execute(DsEnum.clickhouse, sqlBuilder.toString(), "信息化标签生成");
	}

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

}
