#include "CivTableTemp.h"
#include "CivDbConn.h"

CivTableTemp::CivTableTemp()
{
	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(32)"));
	mFieldTyepMap.insert(std::pair<FieldType, std::string>(CivTableTemp::FILED_VARCHAR64, "varchar(64)"));
	mFieldTyepMap.insert(std::pair<FieldType, std::string>(CivTableTemp::FILED_VARCHAR128, "varchar(128)"));
    mFieldTyepMap.insert(std::pair<FieldType, std::string>(CivTableTemp::FILED_VARCHAR256,"varchar(256)"));
    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);
}

Str CivTableTemp::createSql()
{
	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;
}

bool CivTableTemp::updateToDb(CivDbConn* dbConn)
{
    if (dbConn == nullptr)
        return false;

    Str sql = createSql();
    return true;
}

CivJunctionTableTemp::CivJunctionTableTemp(Str tableName)
{
    setName(PIPENODE);

    TableFileds fileds;
     addProperty(fileds.ID ,CivTableTemp::FILED_VARCHAR);// "id";
     addProperty(fileds.geom ,CivTableTemp::FILED_VARCHAR);// "geom";
     addProperty(fileds.thePoint ,CivTableTemp::FILED_VARCHAR);// "本点号";
     addProperty(fileds.xCoord ,CivTableTemp::FILED_VARCHAR);// "横坐标";
     addProperty(fileds.yCoord ,CivTableTemp::FILED_VARCHAR);// "纵坐标";
     addProperty(fileds.elev ,CivTableTemp::FILED_VARCHAR);// "高程";
     addProperty(fileds.demand ,CivTableTemp::FILED_VARCHAR);// "需水量";
     addProperty(fileds.pattern ,CivTableTemp::FILED_VARCHAR);// "需水量模式";
     addProperty(fileds.head ,CivTableTemp::FILED_VARCHAR);// "总水头";
     addProperty(fileds.freeHead ,CivTableTemp::FILED_VARCHAR);// "自由水头";
     addProperty(fileds.actualDemand ,CivTableTemp::FILED_VARCHAR);// "实际需水量";
     addProperty(fileds.code ,CivTableTemp::FILED_VARCHAR);// "code";
     addProperty(fileds.createUser ,CivTableTemp::FILED_VARCHAR);// "create_user";
     addProperty(fileds.modifyUser ,CivTableTemp::FILED_VARCHAR);// "modify_user";
     addProperty(fileds.createTime ,CivTableTemp::FILED_VARCHAR);// "create_time";
     addProperty(fileds. modifyTime ,CivTableTemp::FILED_VARCHAR);// "modify_time";
     addProperty(fileds.symbolAngle ,CivTableTemp::FILED_VARCHAR);// "symbol_angle";
     addProperty(fileds.compType ,CivTableTemp::FILED_VARCHAR);// "组分类型";
     addProperty(fileds.pressure ,CivTableTemp::FILED_VARCHAR);// "压力";
     addProperty(fileds.quality ,CivTableTemp::FILED_VARCHAR);// "水质";
};

/**
管线表模板
*/
CivPipeTableTemp::CivPipeTableTemp() :CivTableTemp()
{
    setName(PIPELINE);

    TableFileds fileds;
     addProperty(fileds.ID ,CivTableTemp::FILED_VARCHAR);// "id";
     addProperty(fileds.geom ,CivTableTemp::FILED_VARCHAR);// "geom";
     addProperty(fileds.snNo ,CivTableTemp::FILED_VARCHAR);// "编号";
     addProperty(fileds.thePoint ,CivTableTemp::FILED_VARCHAR);// "上点号";
     addProperty(fileds.lastPoint ,CivTableTemp::FILED_VARCHAR);// "本点号";
     addProperty(fileds.length ,CivTableTemp::FILED_VARCHAR);// "管长";
     addProperty(fileds.material ,CivTableTemp::FILED_VARCHAR);// "管材";
     addProperty(fileds.diameter ,CivTableTemp::FILED_VARCHAR);// "管径";
     addProperty(fileds.friction ,CivTableTemp::FILED_VARCHAR);// "摩阻系数";
     addProperty(fileds.localLoss ,CivTableTemp::FILED_VARCHAR);// "局部损失系";
     addProperty(fileds.initStatus ,CivTableTemp::FILED_VARCHAR);// "初始状态";
     addProperty(fileds.buryingTime ,CivTableTemp::FILED_VARCHAR);// "埋设时间";
     addProperty(fileds.flow ,CivTableTemp::FILED_VARCHAR);// "流量";
     addProperty(fileds.velocity ,CivTableTemp::FILED_VARCHAR);// "流速";
     addProperty(fileds.unitHeadLoss ,CivTableTemp::FILED_VARCHAR);// "单位水头损";
     addProperty(fileds.headLoss ,CivTableTemp::FILED_VARCHAR);// "水头总损失";
     addProperty(fileds.unitPressLoss ,CivTableTemp::FILED_VARCHAR);// "单位压力损";
     addProperty(fileds.pressLoss ,CivTableTemp::FILED_VARCHAR);// "压力总损失";
     addProperty(fileds.lastHead ,CivTableTemp::FILED_VARCHAR);// "上点水头";
     addProperty(fileds.theHead ,CivTableTemp::FILED_VARCHAR);// "本点水头";
     addProperty(fileds.lastPress ,CivTableTemp::FILED_VARCHAR);// "上点压力";
     addProperty(fileds.thePress ,CivTableTemp::FILED_VARCHAR);// "本点压力";
     addProperty(fileds.localHeadLoss ,CivTableTemp::FILED_VARCHAR);// "局部水头损";
     addProperty(fileds.fracHeadLoss ,CivTableTemp::FILED_VARCHAR);// "摩擦水头损";
     addProperty(fileds.status ,CivTableTemp::FILED_VARCHAR);// "状态";
     addProperty(fileds.guid ,CivTableTemp::FILED_VARCHAR);// "guid";
     addProperty(fileds.code ,CivTableTemp::FILED_VARCHAR);// "code";
     addProperty(fileds.fromCode ,CivTableTemp::FILED_VARCHAR);// "from_code";
     addProperty(fileds.fromLayer ,CivTableTemp::FILED_VARCHAR);// "from_layer";
     addProperty(fileds.toCode ,CivTableTemp::FILED_VARCHAR);// "to_code";
     addProperty(fileds.toLayer ,CivTableTemp::FILED_VARCHAR);// "to_layer";
     addProperty(fileds.createUser ,CivTableTemp::FILED_VARCHAR);// "create_user";
     addProperty(fileds.modifyUser ,CivTableTemp::FILED_VARCHAR);// "modify_user";
     addProperty(fileds.createTime ,CivTableTemp::FILED_VARCHAR);// "create_time";
     addProperty(fileds.modifyTime ,CivTableTemp::FILED_VARCHAR);// "modify_time";
     addProperty(fileds.remarks ,CivTableTemp::FILED_VARCHAR);// "备注";
     addProperty(fileds.quality ,CivTableTemp::FILED_VARCHAR);// "水质";
}

/**
水泵
*/
CivPumpTableTemp::CivPumpTableTemp() :CivTableTemp()
{
    setName(PUMP);

    TableFileds fileds;
    addProperty(fileds.ID, CivTableTemp::FILED_VARCHAR);// "id";
    addProperty(fileds.geom, CivTableTemp::FILED_VARCHAR);// "geom";
    addProperty(fileds.thePoint, CivTableTemp::FILED_VARCHAR);// "本点号";
    addProperty(fileds.xCoord, CivTableTemp::FILED_VARCHAR);// "横坐标";
    addProperty(fileds.yCoord, CivTableTemp::FILED_VARCHAR);// "纵坐标";
    addProperty(fileds.startPoint, CivTableTemp::FILED_VARCHAR);// "起始节点";
    addProperty(fileds.endPoint, CivTableTemp::FILED_VARCHAR);// "终止节点";
    addProperty(fileds.curve, CivTableTemp::FILED_VARCHAR);// "水泵曲线";
    addProperty(fileds.ratio, CivTableTemp::FILED_VARCHAR);// "转速比";
    addProperty(fileds.flow, CivTableTemp::FILED_VARCHAR);// "流量";
    addProperty(fileds.headCurve, CivTableTemp::FILED_VARCHAR);// "扬程曲线";
    addProperty(fileds.status, CivTableTemp::FILED_VARCHAR);// "状态";
    addProperty(fileds.code, CivTableTemp::FILED_VARCHAR);// "code";
    addProperty(fileds.createUser, CivTableTemp::FILED_VARCHAR);// "create_user";
    addProperty(fileds.modifyUser, CivTableTemp::FILED_VARCHAR);// "modify_user";
    addProperty(fileds.createTime, CivTableTemp::FILED_VARCHAR);// "create_time";
    addProperty(fileds.modifyTime, CivTableTemp::FILED_VARCHAR);// "modify_time";
    addProperty(fileds.SymbolAngle, CivTableTemp::FILED_VARCHAR);// "symbol_angle";
    addProperty(fileds.compType, CivTableTemp::FILED_VARCHAR);// "组分类型";
    addProperty(fileds.power, CivTableTemp::FILED_VARCHAR);// "功率";
}

/**
   水池
*/
CivTankTableTemp::CivTankTableTemp() :CivTableTemp()
{
    setName(TANK);

    TableFileds fileds;
    addProperty(fileds.ID  ,CivTableTemp::FILED_VARCHAR);// "id";
    addProperty(fileds.geom  ,CivTableTemp::FILED_VARCHAR);// "geom";
    addProperty(fileds.thePoint  ,CivTableTemp::FILED_VARCHAR);// "本点号";
    addProperty(fileds.xCoord  ,CivTableTemp::FILED_VARCHAR);// "横坐标";
    addProperty(fileds.yCoord  ,CivTableTemp::FILED_VARCHAR);// "纵坐标";
    addProperty(fileds.elev  ,CivTableTemp::FILED_VARCHAR);// "高程";
    addProperty(fileds.diametor  ,CivTableTemp::FILED_VARCHAR);// "直径";
    addProperty(fileds.baseLevel  ,CivTableTemp::FILED_VARCHAR);// "基础水位";
    addProperty(fileds.initLevel  ,CivTableTemp::FILED_VARCHAR);// "初始水位";
    addProperty(fileds.highLevel  ,CivTableTemp::FILED_VARCHAR);// "最高水位";
    addProperty(fileds.lowLevel  ,CivTableTemp::FILED_VARCHAR);// "最低水位";
    addProperty(fileds.flowIn  ,CivTableTemp::FILED_VARCHAR);// "输入流量";
    addProperty(fileds.flowOut  ,CivTableTemp::FILED_VARCHAR);// "输出流量";
    addProperty(fileds.leveHeight  ,CivTableTemp::FILED_VARCHAR);// "液位高度";
    addProperty(fileds.capacity  ,CivTableTemp::FILED_VARCHAR);// "容量";
    addProperty(fileds.percentage  ,CivTableTemp::FILED_VARCHAR);// "装满水池百";
    addProperty(fileds.status  ,CivTableTemp::FILED_VARCHAR);// "状态";
    addProperty(fileds.code  ,CivTableTemp::FILED_VARCHAR);// "code";
    addProperty(fileds.createUser  ,CivTableTemp::FILED_VARCHAR);// "create_user";
    addProperty(fileds.modifyUser  ,CivTableTemp::FILED_VARCHAR);// "modify_user";
    addProperty(fileds.createTime  ,CivTableTemp::FILED_VARCHAR);// "create_time";
    addProperty(fileds.modifyTime  ,CivTableTemp::FILED_VARCHAR);// "modify_time";
    addProperty(fileds.SymbolAngle  ,CivTableTemp::FILED_VARCHAR);// "symbol_angle";
    addProperty(fileds.compType  ,CivTableTemp::FILED_VARCHAR);// "组分类型";
}

/**
  水库
*/
CivResourcesTableTemp::CivResourcesTableTemp() :CivTableTemp()
{
    setName(RESIVOIR);

    TableFileds fileds;

    addProperty(fileds.ID  ,CivTableTemp::FILED_VARCHAR);// "id";
    addProperty(fileds.geom  ,CivTableTemp::FILED_VARCHAR);// "geom";
    addProperty(fileds.thePoint  ,CivTableTemp::FILED_VARCHAR);// "本点号";
    addProperty(fileds.xCoord  ,CivTableTemp::FILED_VARCHAR);// "横坐标";
    addProperty(fileds.yCoord  ,CivTableTemp::FILED_VARCHAR);// "纵坐标";
    addProperty(fileds.headPattern  ,CivTableTemp::FILED_VARCHAR);// "水头模式曲";
    addProperty(fileds.elev  ,CivTableTemp::FILED_VARCHAR);// "高程";
    addProperty(fileds.head  ,CivTableTemp::FILED_VARCHAR);// "水头";
    addProperty(fileds.flow  ,CivTableTemp::FILED_VARCHAR);// "流出流量";
    addProperty(fileds.code  ,CivTableTemp::FILED_VARCHAR);// "code";
    addProperty(fileds.createUser  ,CivTableTemp::FILED_VARCHAR);// "create_user";
    addProperty(fileds.modifyUser  ,CivTableTemp::FILED_VARCHAR);// "modify_user";
    addProperty(fileds.createTime  ,CivTableTemp::FILED_VARCHAR);// "create_time";
    addProperty(fileds.modifyTime  ,CivTableTemp::FILED_VARCHAR);// "modify_time";
    addProperty(fileds.SymbolAngle  ,CivTableTemp::FILED_VARCHAR);// "symbol_angle";
    addProperty(fileds.compType  ,CivTableTemp::FILED_VARCHAR);// "组分类型";
}

/**
阀门
*/
CivValveTableTemp::CivValveTableTemp() :CivTableTemp()
{
    setName(VALVE);

    TableFileds fileds;
    addProperty(fileds.id  ,CivTableTemp::FILED_VARCHAR);// "本点号";
    addProperty(fileds.geom  ,CivTableTemp::FILED_VARCHAR);// "geom";
    addProperty(fileds.xCoord  ,CivTableTemp::FILED_VARCHAR);// "横坐标";
    addProperty(fileds.yCoord  ,CivTableTemp::FILED_VARCHAR);// "纵坐标";
    addProperty(fileds.startPoint  ,CivTableTemp::FILED_VARCHAR);// "起始节点";
    addProperty(fileds.endPoint  ,CivTableTemp::FILED_VARCHAR);// "终止节点";
    addProperty(fileds.diameter  ,CivTableTemp::FILED_VARCHAR);// "直径";
    addProperty(fileds.status  ,CivTableTemp::FILED_VARCHAR);// "状态";
    addProperty(fileds.type  ,CivTableTemp::FILED_VARCHAR);// "阀门类型";
    addProperty(fileds.setting  ,CivTableTemp::FILED_VARCHAR);// "阀门设置";
    addProperty(fileds.loss  ,CivTableTemp::FILED_VARCHAR);// "损失系数";
    addProperty(fileds.flow  ,CivTableTemp::FILED_VARCHAR);// "流量 ";
    addProperty(fileds.veclocity  ,CivTableTemp::FILED_VARCHAR);// "流速";
    addProperty(fileds.headLoss  ,CivTableTemp::FILED_VARCHAR);// "水头损失";
    addProperty(fileds.code  ,CivTableTemp::FILED_VARCHAR);// "code";
    addProperty(fileds.createUser  ,CivTableTemp::FILED_VARCHAR);// "create_user";
    addProperty(fileds.modityUser  ,CivTableTemp::FILED_VARCHAR);// "modify_user";
    addProperty(fileds.createTime  ,CivTableTemp::FILED_VARCHAR);// "create_time";
    addProperty(fileds.modifyTime  ,CivTableTemp::FILED_VARCHAR);// "modify_time";
    addProperty(fileds.symbolAngle  ,CivTableTemp::FILED_VARCHAR);// "symbol_angle";
    addProperty(fileds.compType  ,CivTableTemp::FILED_VARCHAR);// "组分类型";
}

/**
   节点模拟结果存储表
*/
CivNodeResultTableTemp::CivNodeResultTableTemp() :CivTableTemp()
{

    setName(NODERESULTTABLE);

    NodeResultTable nodeTable;
    addProperty(nodeTable.Number, CivTableTemp::FILED_VARCHAR);
    addProperty(nodeTable.dPressure, CivTableTemp::FILED_DOUBLE);
    addProperty(nodeTable.dHead, CivTableTemp::FILED_DOUBLE);
    addProperty(nodeTable.dElevation, CivTableTemp::FILED_DOUBLE);
    addProperty(nodeTable.dDemand, CivTableTemp::FILED_DOUBLE);
    addProperty(nodeTable.dTankVolume, CivTableTemp::FILED_DOUBLE);
    addProperty(nodeTable.dTankMaxVolume, CivTableTemp::FILED_DOUBLE);
    addProperty(nodeTable.dTankLevel, CivTableTemp::FILED_DOUBLE);
    addProperty(nodeTable.dQuality, CivTableTemp::FILED_DOUBLE);
    addProperty(nodeTable.dDate, CivTableTemp::FILED_DATE);
    addProperty(nodeTable.dInterval, CivTableTemp::FILED_INT);
    addProperty(nodeTable.dModifyTime, CivTableTemp::FILED_DATETIME);
}

/**
	模拟结果线表
*/
CivPipeResultTableTemp::CivPipeResultTableTemp() :CivTableTemp()
{

    setName(PIPERESULTTABLE);

    TableField 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);
}

/**
	 模式表
*/
CivPatternTableTemp::CivPatternTableTemp() :CivTableTemp()
{
    setName(PATTERNTABLE);

    TableField fileds;
    addProperty(fileds.modelName, CivTableTemp::FILED_VARCHAR);
    addProperty(fileds.modelData, CivTableTemp::FILED_VARCHAR64);
    addProperty(fileds.modelType, CivTableTemp::FILED_INTEGER);
}

/**
	参数表
*/
CivParametorTableTemp::CivParametorTableTemp() :CivTableTemp() 
{
    setName(OPTIONTTABLE);
    TableField 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);
}

/**
   曲线表
*/
CivCurveTableTemp::CivCurveTableTemp() :CivTableTemp()
{
    setName(CURVETABLE);
    TableField fileds;
    addProperty(fileds.CurveName, CivTableTemp::FILED_VARCHAR64);
    addProperty(fileds.CurveType, CivTableTemp::FILED_INTEGER);
    addProperty(fileds.CurveData, CivTableTemp::FILED_VARCHAR256);
}
