package com.sino.publicclass.sql;

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

import oracle.sql.BLOB;

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

public class ConstructUpdateSQL implements ConstructSQL {
	private String tableName = "";
	private String columns = "";
	private String condition = "";
	private String updateSQL = "";

	public ConstructUpdateSQL() {
	}

	public ConstructUpdateSQL(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 (this.columns.equals("")) {
				this.columns = columnName + "="
						+ StringTools.convertSqlStr(StringValue);
			} else {
				this.columns += "," + columnName + "="
						+ StringTools.convertSqlStr(StringValue);
			}
		}
	}

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

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

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

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

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

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

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

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

	@Override
	public void setColumnDate(String columnName, String StringValue,
			String formot) {
		if (this.columns.equals("")) {
			this.columns = columnName + "=" + "to_date('" + StringValue + "','"
					+ formot + "' )";
		} else {
			this.columns += "," + columnName + "=" + "to_date('" + 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!");
		}
		if (!this.condition.equals("")) {
			this.condition = " where " + this.condition;
		}
		this.updateSQL = "update " + this.tableName + " set " + this.columns
				+ this.condition;
		return updateSQL;
	}

	@Override
	public void setCondition(String condition) {
		this.condition = condition;
	}

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

	}
	
	@Override
	public void setColumnTimestamp(String columnName, Date date) {
		SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		if (date == null)
			return;
		if (this.columns.equals("")) {
			this.columns = columnName;
				this.columns += columnName+"='"+sf.format(date)+"'";
		} else {
				this.columns += "," + columnName+"='"+sf.format(date)+"'";
		}
		/*if (date == null)
			return;
		String dateStr = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS")
				.format(date);

		if (this.columns.equals("")) {
			this.columns = columnName + "=" + "to_date('" + dateStr
					+ "','syyyy-mm-dd hh24:mi:ss.ff' )";
		} else {
			this.columns += "," + columnName + "=" + "to_date('" + dateStr
					+ "','syyyy-mm-dd hh24:mi:ss.ff' )";
		}*/
	}
	

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