#include "CivTableTemp.h"

CivTableTemp::CivTableTemp(Str tableName, TableType tableType)
	:mTableName(tableName),mTableType(tableType)
{
	mFieldTyepMap.insert(std::pair<FieldType, std::string>(CivTableTemp::FILED_INT, "int"));
	mFieldTyepMap.insert(std::pair<FieldType, std::string>(CivTableTemp::FILED_INTEGER,"integer"));
	mFieldTyepMap.insert(std::pair<FieldType, std::string>(CivTableTemp::FILED_DOUBLE, "double precision"));
	mFieldTyepMap.insert(std::pair<FieldType, std::string>(CivTableTemp::FILED_VARCHAR, "varchar(20)"));
	mFieldTyepMap.insert(std::pair<FieldType, std::string>(CivTableTemp::FILED_VARCHAR64, "varchar(64)"));
	mFieldTyepMap.insert(std::pair<FieldType, std::string>(CivTableTemp::FILED_VARCHAR128, "varchar(20)"));
	mFieldTyepMap.insert(std::pair<FieldType, std::string>(CivTableTemp::FILED_DATETIME, "timestamp"));
	mFieldTyepMap.insert(std::pair<FieldType, std::string>(CivTableTemp::FILED_JSON, "json"));
	mFieldTyepMap.insert(std::pair<FieldType, std::string>(CivTableTemp::FILED_DATE, "date"));
}

CivTableTemp::~CivTableTemp()
{

}

void CivTableTemp::addProperty(Str fileName, FieldType filedType)
{
	FieldProperty prop = { fileName ,mFieldTyepMap.find(filedType)->second};
	mPropertys.push_back(prop);
}

void CivTableTemp::addPropertys(FiledPropertys props)
{
	mPropertys.insert(mPropertys.end(), props.begin(), props.end());
}

Str CivTableTemp::createSql()
{
    switch (mTableType)
    {
    case NODE_TABLE:
    {
        NodeFieldName filedName;
        addProperty(filedName.Number, CivTableTemp::FILED_VARCHAR);
        addProperty(filedName.dPressure, CivTableTemp::FILED_DOUBLE);
        addProperty(filedName.dHead, CivTableTemp::FILED_DOUBLE);
        addProperty(filedName.dElevation, CivTableTemp::FILED_DOUBLE);
        addProperty(filedName.dDemand, CivTableTemp::FILED_DOUBLE);
        addProperty(filedName.dTankVolume, CivTableTemp::FILED_DOUBLE);
        addProperty(filedName.dTankMaxVolume, CivTableTemp::FILED_DOUBLE);
        addProperty(filedName.dTankLevel, CivTableTemp::FILED_DOUBLE);
        addProperty(filedName.dQuality, CivTableTemp::FILED_DOUBLE);
        addProperty(filedName.dDate, CivTableTemp::FILED_DATE);
        addProperty(filedName.dInterval, CivTableTemp::FILED_INT);
        addProperty(filedName.dModifyTime, CivTableTemp::FILED_DATETIME);
    }
    break;
    case PIPE_TABLE:
    {
        LinkFiledName pipeFileds;
        addProperty(pipeFileds.szNo, CivTableTemp::FILED_VARCHAR);
        addProperty(pipeFileds.dFlow, CivTableTemp::FILED_DOUBLE);
        addProperty(pipeFileds.nFlowDirect, CivTableTemp::FILED_INT);
        addProperty(pipeFileds.dVelocity, CivTableTemp::FILED_DOUBLE);
        addProperty(pipeFileds.dHeadloss, CivTableTemp::FILED_DOUBLE);
        addProperty(pipeFileds.dUnitHeadloss, CivTableTemp::FILED_DOUBLE);
        addProperty(pipeFileds.dFromNodHeadloss, CivTableTemp::FILED_DOUBLE);
        addProperty(pipeFileds.dToNodHeadloss, CivTableTemp::FILED_DOUBLE);
        addProperty(pipeFileds.dLocalHeadloss, CivTableTemp::FILED_DOUBLE);
        addProperty(pipeFileds.dFrictionHeadloss, CivTableTemp::FILED_DOUBLE);
        addProperty(pipeFileds.szStatus, CivTableTemp::FILED_VARCHAR);
        addProperty(pipeFileds.dQuality, CivTableTemp::FILED_DOUBLE);
        addProperty(pipeFileds.dDate, CivTableTemp::FILED_DATE);
        addProperty(pipeFileds.dInterval, CivTableTemp::FILED_INT);
        addProperty(pipeFileds.dModifyTime, CivTableTemp::FILED_DATETIME);
    }
    break;
    case PATTERN_TABLE:
    {
        PatternFields fileds;
        addProperty(fileds.Name, CivTableTemp::FILED_VARCHAR);
        addProperty(fileds.Value, CivTableTemp::FILED_VARCHAR64);
        addProperty(fileds.NetName, CivTableTemp::FILED_VARCHAR);
    }
    break;
    case CURVE_TABLE:
    {
        CurvesFields fileds;
        addProperty(fileds.Label, CivTableTemp::FILED_VARCHAR);
        addProperty(fileds.Type, CivTableTemp::FILED_VARCHAR);
        addProperty(fileds.XCoord, CivTableTemp::FILED_DOUBLE);
        addProperty(fileds.YCoord, CivTableTemp::FILED_DOUBLE);
    }
    break;
    case PARAMETER_TABLE:
    {
        ParameterFileds fileds;
        addProperty(fileds.name, CivTableTemp::FILED_VARCHAR);
        addProperty(fileds.val, CivTableTemp::FILED_VARCHAR);
        addProperty(fileds.type, CivTableTemp::FILED_VARCHAR);
        addProperty(fileds.netName, CivTableTemp::FILED_VARCHAR64);
    }
    break;
    default:
        break;
    }
	if (mPropertys.size() <= 0)
		return Str();

    char sql[512] = "create table ";

	strcat_s(sql, mTableName.c_str());
	strcat_s(sql, "(id serial primary key");
	size_t total = mPropertys.size();
	for (int i = 0; i < total; i++)
	{
		FieldProperty property = mPropertys[i];
		strcat_s(sql, ",");
		strcat_s(sql, property.mFiledName.c_str());
		strcat_s(sql, "  ");
		strcat_s(sql, property.mFieldType.c_str());
	}
	strcat_s(sql, ")");

	return sql;
}