/**
 * 
 */
package com.platomix.userprofile.service;

import java.util.List;
import java.util.stream.Stream;

import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.platomix.userprofile.mapper.UserprofileTagMapper;
import com.platomix.userprofile.model.UserprofileTag;
import com.platomix.userprofile.utils.ReflectUtils;
import com.platomix.userprofile.utils.StrBuilder;

/**
 * @author wookvn
 *
 */
@Service
public class UserprofileTagService extends ServiceImpl<UserprofileTagMapper, UserprofileTag> {

	final static String[] OTHER = new String[] {"其他", "其它","#其他", "未知"};
	
	/**
	 * 根据中文名称生成SQL
	 * @param level2Name
	 * @param sourceFieldName
	 * @param sourceFieldValue   
	 * @param targetFieldName    - 生成大宽表的字段名前缀(默认使用englishName大写)
	 * @return
	 */
	public String genSelectSql(String level2Name, String sourceFieldName, String sourceFieldValue, String targetFieldName) {
		return genSelectSql(level2Name, sourceFieldName, sourceFieldValue, targetFieldName,"", null);
	}

	/**
	 * 根据中文名称生成SQL
	 * @param level2Name
	 * @param sourceFieldName
	 * @param sourceFieldValue
	 * @param targetFieldName    - 生成大宽表的字段名前缀(默认使用englishName大写)
	 * @param separator   分隔符
	 * @return
	 */
	public String genSelectSql(String level2Name, String sourceFieldName, String sourceFieldValue, String targetFieldName,String separator) {
		return genSelectSql(level2Name, sourceFieldName, sourceFieldValue, targetFieldName,separator, null);
	}

	/**
	 * 根据中文名称生成SQL
	 * @param level2Name
	 * @param sourceFieldName
	 * @param sourceFieldValue   
	 * @param targetFieldName    - 生成大宽表的字段名前缀(默认使用englishName大写)
	 * @param separator   分隔符，如渠道、产品需要2个字段作为标签名
	 * @param useTagNv
	 * @return
	 */
	public String genSelectSql(String level2Name, String sourceFieldName, String sourceFieldValue, String targetFieldName,String separator, String useTagNv) {
		LambdaQueryWrapper<UserprofileTag> queryWrapper = Wrappers.<UserprofileTag>lambdaQuery().eq(UserprofileTag::getLevel2Name, level2Name).orderByAsc(UserprofileTag::getId);
		List<UserprofileTag> dataList = baseMapper.selectList(queryWrapper);
		StrBuilder tagSqlBuilder = new StrBuilder("(case "), tagCSqlBuilder = new StrBuilder("(case ");
		targetFieldName = StringUtils.isEmpty(targetFieldName) ? dataList.get(0).getEnglishName().toUpperCase() : targetFieldName.toUpperCase();
		String otherTagId = null, otherTagName = null;
		for(UserprofileTag tagItem : dataList) {
			String tagId = tagItem.getTagId(), tagName = tagItem.getTagName(),
					//分隔符切分后的值；取指定列的值
					spValue=null,tvName=null;
			if(!StringUtils.isEmpty(separator)){
				spValue=tagName.split(separator)[1];
			}
			if(Stream.of(OTHER).anyMatch(f -> f.equals(tagItem.getTagName()))) {
				otherTagId = tagId;  otherTagName = tagName; continue;
			}
			if(!StringUtils.isEmpty(useTagNv)) {
				tvName = ReflectUtils.invokeGetter(tagItem, useTagNv);
			}
			String eqValue=StringUtils.isEmpty(separator)?(StringUtils.isEmpty(tvName)?tagName:tvName):spValue;
			tagSqlBuilder.appendLn(String.format("when %s = '%s' then '%s'", sourceFieldName, eqValue, tagId));
			tagCSqlBuilder.appendLn(String.format("when %s = '%s' then '%s'", sourceFieldName, eqValue,tagName));
		}
		//如果标签中包含<其它>,则设置，否则默认值。
		if(otherTagId == null) {
			tagSqlBuilder.appendLn(String.format("else 'Axx' end) as %s,", targetFieldName));
			tagCSqlBuilder.appendLn(String.format("else '未知' end) as %s_C,", targetFieldName));
		}else {
			tagSqlBuilder.appendLn(String.format("else '%s' end) as %s,", otherTagId, targetFieldName));
			tagCSqlBuilder.appendLn(String.format("else '%s' end) as %s_C,", otherTagName, targetFieldName));
		}
		String tagVSql = String.format("%s as %s_V", StringUtils.isEmpty(sourceFieldValue) ? sourceFieldName : sourceFieldValue, targetFieldName);
		return tagSqlBuilder.toString() + tagCSqlBuilder.toString() + tagVSql;
	}

}
