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

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.TreeMap;

import org.springframework.beans.factory.annotation.Autowired;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.platomix.userprofile.enums.DsEnum;
import com.platomix.userprofile.model.UserprofileTag;
import com.platomix.userprofile.service.DynamicExecuteService;
import com.platomix.userprofile.service.UserprofileTagService;

import cn.hutool.core.lang.Tuple;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

/**
 * @author wookvn
 *
 *	分桶TAG生成
 */
public class BucketTagManager {

	@Autowired
	private DynamicExecuteService dynamicExecuteService;
	
	@Autowired
	private UserprofileTagService tagService;

	/**
	 * 生成APP使用标签
	 */
	public void genAppUse() {
		String[] appArr = new String[] {
			"抖音","快手","企鹅电竞","腾讯视频","其他视频","西瓜视频","火山小视频","哔哩哔哩动画","优酷视频","微视","爱奇艺视频","爱奇艺","虎牙直播","NOW","斗鱼TV","爱奇艺PPS影音","YY","腾讯云直播","UC影音","斗鱼直播","来疯直播秀","奇秀直播","风行视频","联通手机电视","芒果TV","韩剧TV","皮皮影视","搜狐视频","央视频","响巢看看"
		};
		List<BucketParam> bucketList = new ArrayList<BucketParam>();
		int sortNum = 0;
		for(String str : appArr) {
			String formula = "APP_NAME='"+str+"'";
			bucketList.add(new BucketParam(str+"_0.1G-0.5G", null, null, formula + " and ONE_FLUX > 100 and ONE_FLUX<=512",  ++sortNum));
			bucketList.add(new BucketParam(str+"_0.5G-2G", null, null, formula + " and ONE_FLUX > 512 and ONE_FLUX<=2096",  ++sortNum));
			bucketList.add(new BucketParam(str+"_2G-5G", null, null, formula + " and ONE_FLUX > 2096 and ONE_FLUX<=5120",  ++sortNum));
			bucketList.add(new BucketParam(str+"_5G-10G", null, null, formula + " and ONE_FLUX > 5120 and ONE_FLUX<=10240",  ++sortNum));
			bucketList.add(new BucketParam(str+"_10G-20G", null, null, formula + " and ONE_FLUX > 10240 and ONE_FLUX<=20960",  ++sortNum));
			bucketList.add(new BucketParam(str+"_20G以上", null, null, formula + " and ONE_FLUX > 20960",  ++sortNum));
		}
		bucketList.add(new BucketParam("其它", null, null, null, ++sortNum));
		UserprofileTag baseTagInfo = new UserprofileTag(null, null, null,
				"A", "大网标签", null, "APP月使用天数", "event_app_use", "1", 2,
				"array", "number", "APP流量的使用情况");
		_genBucketTag(baseTagInfo, bucketList, 16);
	}
	
	/**
	 * 生成APP月使用天数标签
	 */
	public void genAppUseDays() {
		String[] appArr = new String[] {
			"抖音","快手","企鹅电竞","腾讯视频","其他视频","西瓜视频","火山小视频","哔哩哔哩动画","优酷视频","微视","爱奇艺视频","爱奇艺","虎牙直播","NOW","斗鱼TV","爱奇艺PPS影音","YY","腾讯云直播","UC影音","斗鱼直播","来疯直播秀","奇秀直播","风行视频","联通手机电视","芒果TV","韩剧TV","皮皮影视","搜狐视频","央视频","响巢看看"
		};
		List<BucketParam> bucketList = new ArrayList<BucketParam>();
		int sortNum = 0;
		for(String str : appArr) {
			String formula = "APP_NAME='"+str+"'";
			bucketList.add(new BucketParam(str+"_0天", null, null, formula + " and DAYS <= 0",  ++sortNum));
			bucketList.add(new BucketParam(str+"_1-5天", null, null, formula + " and DAYS >= 1 and DAYS <= 5",  ++sortNum));
			bucketList.add(new BucketParam(str+"_6-15天", null, null, formula + " and DAYS >= 6 and DAYS <= 15",  ++sortNum));
			bucketList.add(new BucketParam(str+"_16-31天", null, null, formula + " and DAYS >= 16 and DAYS <= 31",  ++sortNum));
		}
		bucketList.add(new BucketParam("其它", null, null, null, ++sortNum));
		UserprofileTag baseTagInfo = new UserprofileTag(null, null, null,
				"A", "大网标签", null, "APP月使用天数", "event_app_use_days", "1", 2,
				"array","number", "");
		_genBucketTag(baseTagInfo, bucketList);
	}
	
	/**
	 * 生成APP月使用小时数标签
	 */
	public void genAppUseHours() {
		String[] appArr = new String[] {
			"抖音","快手","企鹅电竞","腾讯视频","其他视频","西瓜视频","火山小视频","哔哩哔哩动画","优酷视频","微视","爱奇艺视频","爱奇艺","虎牙直播","NOW","斗鱼TV","爱奇艺PPS影音","YY","腾讯云直播","UC影音","斗鱼直播","来疯直播秀","奇秀直播","风行视频","联通手机电视","芒果TV","韩剧TV","皮皮影视","搜狐视频","央视频","响巢看看"
		};
		List<BucketParam> bucketList = new ArrayList<BucketParam>();
		int sortNum = 0;
		for(String str : appArr) {
			String formula = "APP_NAME='"+str+"'";
			bucketList.add(new BucketParam(str+"_0-1小时", null, null, formula + " and floor(MINS/60, 1) >= 0 and floor(MINS/60, 1) < 1",  ++sortNum));
			bucketList.add(new BucketParam(str+"_1-5小时", null, null, formula + " and floor(MINS/60, 1) >= 1 and floor(MINS/60, 1) < 5",  ++sortNum));
			bucketList.add(new BucketParam(str+"_5-10小时", null, null, formula + " and floor(MINS/60, 1) >= 5 and floor(MINS/60, 1) < 10",  ++sortNum));
			bucketList.add(new BucketParam(str+"_10-20小时", null, null, formula + " and floor(MINS/60, 1) >= 10 and floor(MINS/60, 1) < 20",  ++sortNum));
			bucketList.add(new BucketParam(str+"_20-50小时", null, null, formula + " and floor(MINS/60, 1) >= 20 and floor(MINS/60, 1) < 50",  ++sortNum));
			bucketList.add(new BucketParam(str+"_50-100小时", null, null, formula + " and floor(MINS/60, 1) >= 50 and floor(MINS/60, 1) < 100",  ++sortNum));
			bucketList.add(new BucketParam(str+"_100小时以上", null, null, formula + " and floor(MINS/60, 1) > 100",  ++sortNum));
		}
		bucketList.add(new BucketParam("其它", null, null, null, ++sortNum));
		UserprofileTag baseTagInfo = new UserprofileTag(null, null, null,
				"A", "大网标签", null, "APP月使用小时数", "event_app_use_hours", "1", 2,
				"array","number", "");
		_genBucketTag(baseTagInfo, bucketList);
	}
	
	/**
	 * 生成免流流量使用量标签
	 */
	public void genFreeLimitFlowp() {
		List<BucketParam> bucketList = new ArrayList<BucketParam>();
		int sortNum = 0;
		bucketList.add(new BucketParam("0", "<=0", "", null,  ++sortNum));
		bucketList.add(new BucketParam("0-0.5G", ">0", "<512", null,  ++sortNum));
		bucketList.add(new BucketParam("0.5-2G", ">=512", "<2048", null,  ++sortNum));
		bucketList.add(new BucketParam("2-5G", ">=2048", "<5120", null,  ++sortNum));
		bucketList.add(new BucketParam("5-10G", ">=5120", "<10240", null,  ++sortNum));
		bucketList.add(new BucketParam("10-15G", ">=10240", "<15360", null,  ++sortNum));
		bucketList.add(new BucketParam("15-30G", ">=15360", "<30720", null,  ++sortNum));
		bucketList.add(new BucketParam("30G以上", ">=30720", "", null,  ++sortNum));
		bucketList.add(new BucketParam("其它", null, null, null, ++sortNum));
		UserprofileTag baseTagInfo = new UserprofileTag(null, null, null,
				"B", "产品标签", null, "免流流量使用量", "event_freelimit_flowp", "1", 1,
				"string","number", "");
		_genBucketTag(baseTagInfo, bucketList);
	}
	
	private void _genBucketTag(UserprofileTag baseTagInfo, List<BucketParam> paramList) {
		_genBucketTag(baseTagInfo, paramList, 0);
	}
	
	private void _genBucketTag(UserprofileTag baseTagInfo, List<BucketParam> paramList, int level2IdIndex) {
		if(level2IdIndex <= 0) {
			String maxIdSql = "select cast(SUBSTRING(level_2_id, 2) as signed) as level2id from ads_userprofile_tag_dev where level_1_id = '"+baseTagInfo.getLevel1Id()+"' group by level_2_id order by level2id desc limit 1";
			JSONObject jsonObject = dynamicExecuteService.get(DsEnum.master, maxIdSql);
			int maxLevel2Id = jsonObject.getIntValue("level2id");
			level2IdIndex = maxLevel2Id + 1;
		}
		List<UserprofileTag> tagList = new ArrayList<>();
		int index = 0;
		paramList.sort(Comparator.comparing(BucketParam::getSortNum));
		for(BucketParam bucketParam : paramList) {
			UserprofileTag oneTag = new UserprofileTag(String.format("%s112_%02d_%03d", baseTagInfo.getLevel1Id(), level2IdIndex, ++index), 
					bucketParam.getTagName(), null,
					baseTagInfo.getLevel1Id(), baseTagInfo.getLevel1Name(), baseTagInfo.getLevel1Id() + level2IdIndex, 
					baseTagInfo.getLevel2Name(), baseTagInfo.getEnglishName(), baseTagInfo.getIsOnline(), baseTagInfo.getShowHis(),
					baseTagInfo.getTagCType(), baseTagInfo.getSearchType(), baseTagInfo.getTagDesc());
			oneTag.setBucketLeft(bucketParam.getLeft());
			oneTag.setBucketRight(bucketParam.getRight());
			oneTag.setBucketFormula(bucketParam.getFormula());
			oneTag.setTagOrder(bucketParam.getSortNum());
			tagList.add(oneTag);
		}
		System.out.println(JSON.toJSONString(tagList));
		tagService.saveBatch(tagList);
	}
	
	/**
	 * 生成订购偏好预测标签
	 */
	public void genForecastOrder() {
		String[] suanfaArr = new String[] {"dec", "xgb"};
		Map<String, String> typeMap = new HashMap<>();
		typeMap.put("aiqiyi", "爱奇艺");
		typeMap.put("tencent", "腾讯");
		typeMap.put("bilibili", "哔哩");
		typeMap.put("kuaishou", "快手");
		typeMap.put("youku", "优酷");
		typeMap.put("mangtv", "芒果");
		
		Map<String, Tuple> bucketMap = new TreeMap<>();
		//high_高 前面加英文主要为了排序
		bucketMap.put("high_高", new Tuple(">=70", ""));
		bucketMap.put("jmiddle_中", new Tuple(">=50", "<70"));
		bucketMap.put("low_低", new Tuple(">=0", "<50"));
		bucketMap.put("weizhi_未知", new Tuple("", ""));
		
		List<UserprofileTag> tagList = new ArrayList<>();
		int index = 3;
		for(String sf : suanfaArr) {
			for(Entry<String, String> entry : typeMap.entrySet()) {
				String enName = entry.getKey(), name = entry.getValue();
				int i = 0;
				for(Entry<String, Tuple> _entry : bucketMap.entrySet()) {
					String tagName = _entry.getKey().split("_")[1], 
						   bucketLeft = _entry.getValue().get(0).toString(),
						   bucketRight = _entry.getValue().get(1).toString();
					String level2Id = "C" + index,
						   leve2Name = "订购预测_" + name + "_" + sf,
						   englishName = "als_order_"+enName+"_"+sf,
						   tagId   = String.format("C100_%02d_%03d", index, ++i);
					UserprofileTag itemTag = new UserprofileTag(tagId, tagName, null, "C", "算法标签",
							level2Id, leve2Name, englishName, "1", 0,"string","number","");
					
					itemTag.setTagOrder(i);

					//设置分桶规则
					itemTag.setBucketLeft(bucketLeft);
					itemTag.setBucketRight(bucketRight);
					tagList.add(itemTag);
				}
				index++;
			}
		}
		tagService.saveBatch(tagList);
	}

	
	@Data
	@AllArgsConstructor
	@NoArgsConstructor
	static class BucketParam {
		private String tagName;
		private String left;
		private String right;
		private String formula;
		private Integer sortNum;
	}
}
