package com.cloudcross.ssp.common.utils;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

import com.cloudcross.ssp.common.consts.FieldValidate;

/**
 * 创建人：rongying
 * 创建时间：2015.12.11
 * 数据库表字段信息
 */

@SuppressWarnings("serial")
public class FieldInfo implements Serializable {
	
	private String columnName;//字段名

	private String isNull;//是否为空
	
	private String dataType;//数据类型
	
	private Integer maxLength;//最大长度
	
	private Integer pointNum;//小数点
	
	public String getColumnName() {
		return columnName;
	}

	public void setColumnName(String columnName) {
		this.columnName = columnName;
	}

	public String getIsNull() {
		return isNull;
	}

	public void setIsNull(String isNull) {
		this.isNull = isNull;
	}

	public String getDataType() {
		return dataType;
	}

	public void setDataType(String dataType) {
		this.dataType = dataType;
	}
	
	public Integer getMaxLength() {
		return maxLength;
	}

	public void setMaxLength(Integer maxLength) {
		this.maxLength = maxLength;
	}

	public Integer getPointNum() {
		return pointNum;
	}

	public void setPointNum(Integer pointNum) {
		this.pointNum = pointNum;
	}
	
	
	/**
	 * 校验字段
	 * @param fieldInfoMap 字段属性集合
	 * @param fieldName 字段名称
	 * @param fieldValue 字段值
	 * @param index 数据的下标，第几条
	 * @return 错误的信息
	 */
	public static Map<String,Object> validateField(Map<String,FieldInfo> fieldInfoMap,String fieldName,Object fieldValue,int index){
		Map<String,Object> map = new HashMap<String,Object>();
		if("NO".equals(fieldInfoMap.get(fieldName).getIsNull()) && (fieldValue == null || "".equals(fieldValue))){
			map = getErrorMessage(FieldValidate.FIELD_NOT_EMPTY, true, fieldName, index,"error");
		}
		try{
			if("String".equals(fieldInfoMap.get(fieldName).getDataType())){
				String.valueOf(fieldValue);
			}else if("Long".equals(fieldInfoMap.get(fieldName).getDataType())){
				Long.valueOf(fieldValue.toString());
			}
		}catch(NumberFormatException e){
			map = getErrorMessage(FieldValidate.FIELD_FORMAT_ERROR, true, fieldName, index,"error");
		}
		if(fieldValue.toString().length() > fieldInfoMap.get(fieldName).getMaxLength()){
			map = getErrorMessage(FieldValidate.FIELD_LENGTH_OVER, true, fieldName, index,"error");
		}
		return map;		
	}
	
	/**
	 * 返回错误类型信息 {"status":2,"error":[["field":id,"index":1,] "code":100,"message":"输入的参数有误"]}
	 * @param fieldValidate 枚举对应的code，value
	 * @param flag 是否显示错误的字段
	 * @param field 错误的字段
	 * @param index 错误的数据下标
	 * @param tab 成功或错误的标识
	 * @return
	 */
	public static Map<String,Object> getErrorMessage(FieldValidate fieldValidate,boolean flag,String field,int index,String tab){
		Map<String,Object> map = new HashMap<String,Object>();
		Map<String,Object> errormap = new HashMap<String,Object>();
		if("error".equals(tab)){
			map.put("status", 1);
			if(flag){
				errormap.put("field", field);
			}
			if(index != 0){
				errormap.put("index", index);
			}
		}else if("success".equals(tab)){
			map.put("status", 0);
			errormap.put("index", index);
		}
		errormap.put("code", fieldValidate.code);
		errormap.put("message", fieldValidate.value);
		map.put(tab, errormap);
		return map;
	}

}

