package com.sino.publicclass.sql;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

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

import oracle.sql.BLOB;

import com.sino.publicclass.base.CException;
import com.sino.publicclass.util.StaticVar;
import com.sino.publicclass.util.StringTools;

public class ConstructInsertSQL implements ConstructSQL {
	private String tableName = "";
	private String columns = "";
	private String values = "";
	private String insertSQL = "";

	public ConstructInsertSQL() {
	}

	public ConstructInsertSQL(String tableName) {
		this.tableName = tableName;
	}

	@Override
	public void setTableName(String tableName) {
		this.tableName = tableName;
	}

	@Override
	public void setColumn(String columnName, String StringValue) {
		if (StringValue != null) {
			if (!(StringValue.equals("") || StringValue.equals(null) || StringValue
					.equals("null"))) {
				if (StringValue.equals("-1")) {
					StringValue = "";
				}
				if (this.columns.equals("")) {
					this.columns = columnName;
					this.values = StringTools.convertSqlStr(StringValue);
				} else {
					this.columns += "," + columnName;
					this.values += "," + StringTools.convertSqlStr(StringValue);
				}
			}
		}
	}

	@Override
	public void setColumnSql(String columnName, String sqlString) {
		if (sqlString != null) {
			if (this.columns.equals("")) {
				this.columns = columnName;
				this.values = sqlString;
			} else {
				this.columns += "," + columnName;
				this.values += "," + sqlString;
			}
		}
	}

	@Override
	public void setColumn(String columnName, int intValue) {
		if (this.columns.equals("")) {
			this.columns = columnName;
			this.values += intValue;
		} else {
			this.columns += "," + columnName;
			this.values += "," + intValue;
		}
	}

	public void setColumn(String columnName, Now time) {
		if (this.columns.equals("")) {
			this.columns = columnName;
			this.values += time;
		} else {
			this.columns += "," + columnName;
			this.values += "," + time;
		}
	}
	
	@Override
	public void setColumn(String columnName, double doubleValue) {
		if (this.columns.equals("")) {
			this.columns = columnName;
			this.values += doubleValue;
		} else {
			this.columns += "," + columnName;
			this.values += "," + doubleValue;
		}
	}

	@Override
	public void setColumn(String columnName, float floatValue) {
		if (this.columns.equals("")) {
			this.columns = columnName;
			this.values += floatValue;
		} else {
			this.columns += "," + columnName;
			this.values += "," + floatValue;
		}
	}

	@Override
	public void setCondition(String condition) {
	}

	@Override
	public void setColumnSelect(String columnName, String StringValue) {
		if (StringValue != null) {
			if (!StringValue.equals("-1")) { 
				if (this.columns.equals("")) {
					this.columns = columnName;
					this.values = StringTools.convertSqlStr(StringValue);
				} else {
					this.columns += "," + columnName;
					this.values += "," + StringTools.convertSqlStr(StringValue);
				}
			}
		}
	}

	public void setColumnFull(String columnName, String StringValue) {
		if (StringValue != null) {
			if (this.columns.equals("")) {
				this.columns = columnName;
				this.values = StringTools.convertSqlStr(StringValue);
			} else {
				this.columns += "," + columnName;
				this.values += "," + StringTools.convertSqlStr(StringValue);
			}
		}
	}

	public void setColumnFull(String columnName, int intValue) {
		if (this.columns.equals("")) {
			this.columns = columnName;
			this.values += intValue;
		} else {
			this.columns += "," + columnName;
			this.values += "," + intValue;
		}
	}

	@Override
	public void setColumnSelect(String columnName, int intValue) {
		if (intValue != -1) {
			if (this.columns.equals("")) {
				this.columns = columnName;
				this.values += intValue;
			} else {
				this.columns += "," + columnName;
				this.values += "," + intValue;
			}
		}
	}

	@Override
	public void setColumnDate(String columnName) {
		if (this.columns.equals("")) {
			this.columns = columnName;
			this.values += " SYSDATE ";
		} else {
			this.columns += "," + columnName;
			this.values += "," + "SYSDATE";
		}
	}

	@Override
	public void setColumnDate(String columnName, String StringValue) {
		if (!StringValue.equals(StaticVar.DONOTUPDATE)) {
			if (this.columns.equals("")) {
				this.columns = columnName;
				this.values += " SYSDATE ";
			} else {
				this.columns += "," + columnName;
				this.values += "," + "SYSDATE";
			}
		}
	}

	/*public void setColumnDate(String columnName, String StringValue,
			String formot) {
		if (this.columns.equals("")) {
			this.columns = columnName;
			this.values += "to_date('" + StringValue + "','" + formot + "' )";
		} else {
			this.columns += "," + columnName;
			this.values += "," + "to_date('" + StringValue + "','" + formot
					+ "' )";
		}
	}*/
	
	@Override
	public void setColumnDate(String columnName, String StringValue,
			String formot) {
		if (this.columns.equals("")) {
			this.columns = columnName;
			this.values += "to_date('" + StringValue + "','" + formot + "' )";
		} else {
			this.columns += "," + columnName;
			this.values += "," + "date_format('" + StringValue + "','" + formot
					+ "' )";
		}
	}

	public void setColumn(String columnName, Date date) {
		if (date == null)
			return;
		String dateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss")
				.format(date);
		setColumnDate(columnName, dateStr, TIME_FORMAT);
	}

	@Override
	public String getSQL() throws CException {
		if (this.tableName.equals("")) {
			throw new CException("tableName has not define!");
		}
		if (this.columns.equals("")) {
			throw new CException("column has not define!");
		}
		this.insertSQL = "insert into " + this.tableName + " (" + this.columns
				+ ") values (" + values + ") ";
		return insertSQL;
	}

	@Override
	public void setColumn(String columnName, long longValue) {
		if (this.columns.equals("")) {
			this.columns = columnName;
			this.values += longValue;
		} else {
			this.columns += "," + columnName;
			this.values += "," + longValue;
		}

	}

	@Override
	public void setColumnTimestamp(String columnName, Date date) {
		if (date == null)
			return;
		if (this.columns.equals("")) {
			this.columns = columnName;
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
			try {
				date = sf.parse(sf.format(date));
				this.columns += "," + columnName;
				this.values += "," + date;
			} catch (ParseException e) {
				e.printStackTrace();
			}
			this.values += date;
		} else {
			SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
			try {
				date = sf.parse(sf.format(date));
				this.columns += "," + columnName;
				this.values += "," + "'"+sf.format(date)+"'";
			} catch (ParseException e) {
				e.printStackTrace();
			}
		}
		/*String dateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(date);
		if (this.columns.equals("")) {
			this.columns = columnName;
			this.values += "to_date('" + dateStr
					+ "','syyyy-mm-dd hh24:mi:ss.ff' )";
		} else {
			this.columns += "," + columnName;
			this.values += "," + "to_date('" + dateStr
					+ "','syyyy-mm-dd hh24:mi:ss.ff' )";
		}*/
	}

	@Override
	public void setColumn(String columnName, BLOB blob) {
		// TODO Auto-generated method stub
		
	}

}
