package com.prolog.rdc.tms.util;

import java.util.List;
import java.util.Map;

import org.apache.poi.ss.formula.functions.T;

import com.prolog.common.util.ToolKits;
import com.prolog.framework.core.exception.ParameterException;
import com.prolog.framework.core.restriction.Criteria;
import com.prolog.framework.core.restriction.Restriction;
import com.prolog.framework.core.restriction.Restrictions;
import com.prolog.framework.dao.mapper.BaseMapper;
import com.prolog.rdc.tms.constant.StatusConstant;

/**
 * 
 *
 * 公共方法
 *
 * @className :PublicMethods
 * @author :fanzw
 * @creationTime :2021/12/22
 * @description :
 * @modifier :fanzw
 * @modifyTime :2021/12/22
 * @reviseRemarks:
 *
 * @version :1.0.0
 *
 */
public class PublicMethods {

	/**
	 * 参照数据翻译
	 * 
	 * @param list
	 * @param character
	 * @return
	 */
	public static String change(List<Map<String, Object>> list, String character) {
		if (list == null || list.size() == 0 || character == null || character.equals("")) {
			return character;
		}

		String massage = null;
		for (Map<String, Object> map : list) {
			if ((map.get("value")).equals(character)) {
				massage = map.get("text").toString();
				break;
			}
		}

		return massage;
	}

	/**
	 * 参照数据反向翻译
	 *
	 * @param list
	 * @param character
	 * @return
	 */
	public static String reverseChange(List<Map<String, Object>> list, String character) {
		if (list == null || list.size() == 0 || character == null || character.equals("")) {
			return character;
		}

		String massage = null;
		for (Map<String, Object> map : list) {
			if ((map.get("text")).equals(character)) {
				massage = map.get("value").toString();
				break;
			}
		}

		return massage;
	}

	/**
	 * 判断指定信息是否存在
	 * @param clazz
	 * @param paramName1
	 * @param paramValue1
	 * @param paramName2
	 * @param paramValue2
	 * @param paramName3
	 * @param paramValue3
	 * @param paramName4
	 * @param paramValue4
	 * @param mapper
	 * @param message
	 */
	public static void isThereAnyinformation(Class<?> clazz, String paramName1, String paramValue1, String paramName2, Long paramValue2, String paramName3, Object paramValue3, String paramName4, Object paramValue4, BaseMapper mapper, String message) {
		Criteria criteria = Criteria.forClass(clazz);
		Restriction r1 = Restrictions.eq(paramName1, paramValue1);
		Restriction r2 = Restrictions.eq(paramName2, paramValue2);
		Restriction r3 = Restrictions.eq(paramName3, paramValue3);
		Restriction r4 = Restrictions.eq(paramName4, paramValue4);
		criteria.setRestriction(Restrictions.and(r1, r2, r3, r4));
		List<T> list = mapper.findByCriteria(criteria);
		if (ToolKits.isNotEmpty(list)) {
			throw new ParameterException(message);
		}
	}

	/**
	 * 判断指定信息是否存在
	 * 
	 * @param clazz
	 * @param paramName1
	 * @param paramValue1
	 * @param paramName2
	 * @param paramValue2
	 * @param paramName3
	 * @param paramValue3
	 * @param mapper
	 * @param message
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void isThereAnyinformation(Class<?> clazz, String paramName1, String paramValue1, String paramName2, Long paramValue2, String paramName3, Object paramValue3, BaseMapper mapper, String message) {
		Criteria criteria = Criteria.forClass(clazz);
		Restriction r1 = Restrictions.eq(paramName1, paramValue1);
		Restriction r2 = Restrictions.eq(paramName2, paramValue2);
		Restriction r3 = Restrictions.eq(paramName3, paramValue3);
		criteria.setRestriction(Restrictions.and(r1, r2, r3));
		List<T> list = mapper.findByCriteria(criteria);
		if (ToolKits.isNotEmpty(list)) {
			throw new ParameterException(message);
		}
	}

	/**
	 * 判断指定信息是否存在
	 * 
	 * @param clazz
	 * @param paramName1
	 * @param paramValue1
	 * @param paramName2
	 * @param paramValue2
	 * @param mapper
	 * @param message
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static void isThereAnyinformation(Class<?> clazz, String paramName1, String paramValue1, String paramName2, Long paramValue2, BaseMapper mapper, String message) {
		Criteria criteria = Criteria.forClass(clazz);
		Restriction r1 = Restrictions.eq(paramName1, paramValue1);
		Restriction r2 = Restrictions.eq(paramName2, paramValue2);
		criteria.setRestriction(Restrictions.and(r1, r2));
		List<T> list = mapper.findByCriteria(criteria);
		if (ToolKits.isNotEmpty(list)) {
			throw new ParameterException(message);
		}
	}

	/**
	 * 根据map查询记录数
	 * 
	 * @param mapper
	 * @param map
	 * @param clazz
	 * @return
	 */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static long findCountByMap(BaseMapper mapper, Map map, Class<?> clazz) {
		return mapper.findCountByMap(map, clazz);
	}

	/**
	 * 根据map判断是否存在
	 * 
	 * @param mapper
	 * @param map
	 * @param clazz
	 * @return
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static void doesItExistByMap(BaseMapper mapper, Map map, Class<?> clazz, String message) {
		List<T> list = mapper.findByMap(map, clazz);
		if (ToolKits.isNotEmpty(list)) {
			throw new ParameterException(message);
		}
	}

	/**
	 * 格式转换
	 * 
	 * @param str
	 * @return
	 */
	public static Integer FormatConversion(String str) {
		if (!StatusConstant.WHETHER_STRING_YES.equals(str) && !StatusConstant.WHETHER_STRING_NO.equals(str)) {
			throw new RuntimeException("格式不正确[是/否]");
		}
		return StatusConstant.WHETHER_STRING_YES.equals(str) ? 1 : 0;
	}

	/**
	 * 根据map更新指定字段
	 * 
	 * @param field
	 * @param value
	 * @param map
	 * @param mapper
	 * @param clazz
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static void updateMapByCriteria(String field, Long value, Map map, BaseMapper mapper, Class<?> clazz) {
		Criteria criteria = Criteria.forClass(clazz);
		Restriction r1 = Restrictions.eq(field, value);
		criteria.setRestriction(r1);
		mapper.updateMapByCriteria(map, criteria);
	}
}
