/************************************************************************/
/*  Copyright (C), 2016-2020, [IT], 保留所有权利;
/*  模 块 名：;
/*  描    述：当前项目数据库操作实现;
/*
/*  版    本：[V];	
/*  作    者：[IT];
/*  日    期：[11/13/2016];
/*
/*
/*  注    意：;
/*
/*  修改记录：[IT];
/*  修改日期：;
/*  修改版本：;
/*  修改内容：;
/************************************************************************/
#ifndef __DATA_IMPL__
#define __DATA_IMPL__

#pragma once

#include <vector>
#include "TableInfo.h"

class CDataImpl
{
public:
	CDataImpl(void);
	virtual ~CDataImpl(void);

	// 私有变量;
private:
	sqlite3		*m_psqlite3;

	// 用户函数;
public:
	BOOL Open();
	void Close();

	// begin;
	int TransactionBegin()
	{
		char* psqlite_error = NULL;
		INT nRet = sqlite3_exec(m_psqlite3, "begin;", 0, 0, &psqlite_error);
		if ( nRet != SQLITE_OK)
		{
			if ( psqlite_error ) 
			{ 
				sqlite3_free(psqlite_error); 
				psqlite_error = NULL;
			}
			return nRet;
		}

		return SQLITE_OK;
	}

	// commit;
	int TransactionCommit()
	{
		char* psqlite_error = NULL;
		INT nRet = sqlite3_exec(m_psqlite3, "commit;", 0, 0, &psqlite_error);
		if ( nRet != SQLITE_OK)
		{
			if ( psqlite_error ) 
			{ 
				sqlite3_free(psqlite_error); 
				psqlite_error = NULL;
			}
			return nRet;
		}
		return SQLITE_OK;
	}

	// 执行语句;
	BOOL ExecteSQL(IN LPCSTR lpSQL);
	// 查询表是否存在;
	BOOL QueryTable(std::string table);

public:
	INT InsertOrder(const TB_ORDER &order);
	INT InsertOrder(std::string strOrder, std::string strDeltaEType, std::string strDeltaEValue, int nMode);
	INT InsertSN(const TB_SN &sn_data);
	INT InsertSN(std::string strOrder, std::string strLine, std::string strSN, std::string strStartTime, std::string strFinishTime, int nElapsed, int nTestResult, DOUBLE dDeltaAVGEValue, std::string strDetails );

	INT QueryOrder(std::string strOrder, TB_ORDER &order);
	INT QueryOrders(std::vector<TB_ORDER> &vtOrders, std::string strBeginTime, std::string EndTime);
	INT QuerySNData(std::string strSN, TB_SN &sn_data);
	INT QuerySNData(std::string strOrder, std::vector<TB_SN> &vtSNdata);
	INT QuerySNData(std::vector<TB_SN> &vtSNData, std::string strBeginTime, std::string EndTime);

	BOOL UpdateOrder(TB_ORDER &order);
	BOOL UpdateOrder(std::string strOrder, std::string strDeltaEType, std::string strDeltaEValue, int nMode);
	BOOL UpdateSNData(TB_SN &sn_data);
	BOOL UpdateSNData(std::string strOrder,std::string strSN, std::string strLine, std::string strTestTime, std::string strFinishedTime, int nElapsed, int nTestResult, double dAVGDeltaEVlaue, std::string strDetails);

	BOOL DeleteOrder(std::string strOrder);//同时删除所有关联的SN;
	BOOL DeleteSNData(std::string strSN);
};

#endif