package xiaoqu.home.open.utils;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import javax.annotation.PostConstruct;

import org.springframework.cglib.beans.BeanMap;
import org.springframework.stereotype.Component;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;

import cn.hutool.core.bean.BeanUtil;
import xiaoqu.home.open.model.db2.FieldWebpageAuthority;

/**
 * Wrapper 条件构造 Bean到Wrapper转化
 *
 * @author wh
 */
@Component
public class WrapperUtils {
	public static Set<String> notQueryAttribute;

	public static WrapperUtils wrapperUtils;

	@PostConstruct
	public void init() {
		wrapperUtils = this;
	}

	static {
		// 不参数拼接的属性
		notQueryAttribute = new HashSet<String>();
		notQueryAttribute.add("rows");// 分页大小
		notQueryAttribute.add("page");// 当前页
		notQueryAttribute.add("sidx");// 排序字段
		notQueryAttribute.add("sord");// 排序方式
		notQueryAttribute.add("web_id");//

	}

	/**************************
	 * @param queryEntity 输入实体
	 * @return
	 **************************/
	public static <T> QueryWrapper<T> beanToWrapper(QueryWrapper<T> queryWrapper, Object queryEntity) {
		// 转化成下划线的属性Map集合
		Map<String, Object> beanToMap = BeanUtil.beanToMap(queryEntity, true, true);
		if (!beanToMap.isEmpty()) {
			for (String key : beanToMap.keySet()) {
				if (!notQueryAttribute.contains(key)) {
					if (beanToMap.get(key) instanceof String && beanToMap.get(key).toString().length() == 0) {
						// 字符串为空就不查了
						continue;
					}
					// 先排序大于小于之类的判断
					if (key.endsWith("start")) {
						queryWrapper.ge(true, key.substring(0, key.length() - 6), beanToMap.get(key));
						continue;
					} else if (key.endsWith("end")) {
						queryWrapper.le(true, key.substring(0, key.length() - 4), beanToMap.get(key));
						continue;
					} else if (key.startsWith("start")) {
						queryWrapper.ge(true, key.substring(6, key.length()), beanToMap.get(key));
						continue;
					} else if (key.startsWith("end")) {
						queryWrapper.le(true, key.substring(4, key.length()), beanToMap.get(key));
						continue;
					} else if (beanToMap.get(key) instanceof String) {
						queryWrapper.like(key, beanToMap.get(key));// 模糊查询
						continue;
					} else if (beanToMap.get(key) instanceof Integer) {
						queryWrapper.eq(key, beanToMap.get(key));
						continue;
					} else if (beanToMap.get(key) instanceof Long) {
						queryWrapper.eq(key, beanToMap.get(key));
						continue;
					} else {
						queryWrapper.eq(key, beanToMap.get(key));
						continue;
					}
				} else {
					if (key.equals("sidx")) {
						Object sord = beanToMap.get("sord");
						if (sord != null) {
							if (sord.toString().equals("asc")) {
								queryWrapper.orderByAsc(humpToLine(beanToMap.get(key) + ""));
							} else {
								queryWrapper.orderByDesc(humpToLine(beanToMap.get(key) + ""));
							}
						} else {
							queryWrapper.orderByAsc(humpToLine(beanToMap.get(key) + ""));
						}
					}
				}
			}

		}
		return queryWrapper;
	}

	/**********
	 * 如果要正常的数据就传递delFlag=false ,要所有的数据就传递true
	 *
	 * @param delFlag
	 * @return
	 */
	public static <T> QueryWrapper<T> getWrapper(Boolean delFlag) {
		QueryWrapper<T> queryWrapper = Wrappers.query();
		if (!delFlag) {
			queryWrapper.eq("soft_delete", 0);
		}
		return queryWrapper;
	}

	/**********
	 * 如果要正常的数据就传递delFlag=false ,要所有的数据就传递true
	 *
	 * @param delFlag
	 * @return
	 */
	public static <T> UpdateWrapper<T> getUpdateWrapper(Boolean delFlag) {
		UpdateWrapper<T> queryWrapper = Wrappers.update();
		if (!delFlag) {
			queryWrapper.eq("soft_delete", 0);
		}
		return queryWrapper;
	}

	private static Pattern linePattern = Pattern.compile("_(\\w)");
	private static Pattern humpPattern = Pattern.compile("[A-Z]");

	/**
	 * 驼峰转下划线
	 *
	 * @param str
	 * @return
	 */
	public static String humpToLine(String str) {
		Matcher matcher = humpPattern.matcher(str);
		StringBuffer sb = new StringBuffer();
		while (matcher.find()) {
			matcher.appendReplacement(sb, "_" + matcher.group(0).toUpperCase());
		}
		matcher.appendTail(sb);
		return sb.toString().toLowerCase();
	}

	/**
	 * 下划线转驼峰,正常输出
	 *
	 * @param str
	 * @return
	 */
	public static String lineToHump(String str) {
		Matcher matcher = linePattern.matcher(str);
		StringBuffer sb = new StringBuffer();
		while (matcher.find()) {
			matcher.appendReplacement(sb, matcher.group(1).toUpperCase());
		}
		matcher.appendTail(sb);
		return sb.toString();
	}

	public static String[] entityToColumns(List<FieldWebpageAuthority> fieldList) {
		// ID必选
		String[] fieldListColumns = new String[fieldList.size()];
		int i = 0;
		boolean flag = false;
		for (FieldWebpageAuthority fieldWebpageAuthority : fieldList) {
			fieldListColumns[i] = humpToLine(fieldWebpageAuthority.getEnName());
			if (fieldListColumns[i].equals("id")) {
				flag = true;
			}
			i++;
		}
		if (!flag) {
			String[] temfieldListColumns = new String[fieldList.size() + 1];
			for (int j = 0; j < fieldListColumns.length; j++) {
				temfieldListColumns[j] = fieldListColumns[j];
			}
			temfieldListColumns[fieldList.size()] = "id";
			return temfieldListColumns;
		}

		return fieldListColumns;
	}

	@SuppressWarnings("unchecked")
	public static <T> Map<String, Object> beanToMap(T bean) {
		BeanMap beanMap = BeanMap.create(bean);
		Map<String, Object> map = new HashMap<>();

		beanMap.forEach((key, value) -> {
			if (value != null) {
				map.put(String.valueOf(key), value);
			}
		});
		return map;
	}

	public static void main(String[] args) {
		System.out.println(humpToLine("agentId"));
	}

}
