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

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
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.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
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
 *
 *	APP偏好标签
 */
@Slf4j
@Service
public class AppPreferenceTagHandler extends AbstractTagHandler {

	final static String ZT_TABLE_NAME = "ads.ZT_USER_APP_PREFERENCE";
	
	@Autowired
	private UserprofileTagService userprofileTagService;
	
	@Override
	public void invoke(String month, String mobileLeft, String mobileRight) {
		// 查询所有标签
		LambdaQueryWrapper<UserprofileTag> queryWrapper = Wrappers.<UserprofileTag>lambdaQuery().eq(UserprofileTag::getLevel2Name, "APP偏好");
		List<UserprofileTag> tagList = userprofileTagService.list(queryWrapper);
		
		LocalDate monthStartDate = LocalDate.parse(month+"01", DateTimeFormatter.BASIC_ISO_DATE);
		String threeMonthAgo = monthStartDate.minusMonths(2).format(YYYMM_DATE_FORMATTER);
		
//		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_APP_PREF", "USERID_APP_PREF"));
		sqlBuilder.appendLn("with NULLIF(APP_NAME, '') as APPNAME")
				    .append("select DEVICE_NUMBER as USERID_APP_PREF,").append("'"+month+"'").appendLn(" as THEMONTH_APP_PREF,");
		
		//格式化数据
		List<String> tagIdList = tagList.stream().map(UserprofileTag::getTagId).collect(Collectors.toList());
		Map<String, String> tagName2IdMap = tagList.stream().collect(Collectors.toMap(UserprofileTag::getTagName, UserprofileTag::getTagId, (t, t1) -> t));
		String tagIdListStr = JSON.toJSONString(tagIdList).replaceAll("\"", "\'"), 
				tagName2IdMapStr = JSON.toJSONString(tagName2IdMap);
		
		sqlBuilder.appendLn("arrayMap(x -> JSONExtractString('"+tagName2IdMapStr+"', x), TAG_APP_PREF_C) TAG_APP_PREF,")
				  .appendLn("groupUniqArray(if(has("+tagIdListStr+", APPNAME) = 1, APPNAME, '其他')) as TAG_APP_PREF_C,")
				  .appendLn("TAG_APP_PREF_C as TAG_APP_PREF_V")
				  .appendLn("from ads.XXH_ALL_APP_MONTH")
				  .appendLn("where MONTH_ID >= '"+threeMonthAgo+"' and MONTH_ID <= '"+month+"' and ONE_FLUX > 1024")
				  .appendLn("group by DEVICE_NUMBER");
		log.info("\n" + sqlBuilder.toString());
		dynamicExecuteService.execute(DsEnum.clickhouse, sqlBuilder.toString(), "信息化标签生成");
	}
	
	@Override
	public boolean executeInBatchs() {
		return false;
	}

}
