package com.hzya.frame.sys.entity;


import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.util.List;

public class TemplateKeyValue {


	/** 字段 */
	private String fields;

	/** 值 */
	private String values;

	/** 类型 Vachar,number,decimal,datetime */
	private String fieldsType;
	/** 1开始 2结束 */
	private String valuePosition;
	public String getFields() {
		return fields;
	}
	public Object getValueByType(String a) {
		StringBuffer stringBuffer = new StringBuffer();
		if(this.fieldsType != null && "4".equals(this.fieldsType)){
			List<String> valuesList = JSONArray.parseArray(values,String.class);
			if(valuesList!= null && valuesList.size() > 1){
				stringBuffer.append(" AND DATE_FORMAT("+ a+this.fields+ " , '%Y-%m-%d') >= str_to_date('"+valuesList.get(0)+"','%Y-%m-%d')");
				stringBuffer.append(" AND DATE_FORMAT("+ a+this.fields+ " , '%Y-%m-%d') <= str_to_date('"+valuesList.get(1)+"','%Y-%m-%d')");
			}else if(valuesList!= null && valuesList.size() == 1){
				if(this.valuePosition != null && "1".equals(this.valuePosition)){
					stringBuffer.append(" AND DATE_FORMAT("+ a+this.fields+ " , '%Y-%m-%d') >= str_to_date('"+valuesList.get(0)+"','%Y-%m-%d')");
				}else {
					stringBuffer.append(" AND DATE_FORMAT("+ a+this.fields+ " , '%Y-%m-%d') <= str_to_date('"+valuesList.get(0)+"','%Y-%m-%d')");
				}
			}
		}else if (this.fieldsType != null && "5".equals(this.fieldsType)){
			List<String> valuesList = JSONArray.parseArray(values,String.class);
			if(valuesList!= null && valuesList.size() > 0){
				stringBuffer.append(" and "+ a+this.fields + " in (");
				for (int i = 0; i < valuesList.size(); i++) {
					if(i ==0 ){
						stringBuffer.append(" '" + valuesList.get(i) + "'");
					}else {
						stringBuffer.append(", '" + valuesList.get(i) + "'");
					}
				}
				stringBuffer.append(" ) ");
			}
		}else {
			stringBuffer.append(" and "+ a+this.fields + " like '%" +this.values + "%'");

		}
		return stringBuffer;
	}


	public void setFields(String fields) {
		this.fields = fields;
	}

	public Object getValues() {
		return values;
	}

	public void setValues(String values) {
		this.values = values;
	}

	public String getFieldsType() {
		return fieldsType;
	}

	public void setFieldsType(String fieldsType) {
		this.fieldsType = fieldsType;
	}

	public String getValuePosition() {
		return valuePosition;
	}

	public void setValuePosition(String valuePosition) {
		this.valuePosition = valuePosition;
	}
}
