lixioayao 6 years ago
parent
commit
98aef0986e

+ 17 - 0
common_tool.h

@@ -87,6 +87,23 @@ public:
 	{
 		return type_id_to_u64(card_id_to_type(cardid),card_id_to_id(cardid));
 	}
+
+    ///采煤机
+    static bool is_coal(int32_t type)
+    {
+        return CT_COAL_CUTTER == type;
+    }
+
+    /// 掘进机
+    static bool is_driving(int32_t type)
+    {
+        return CT_HEADING_MACHINE == type;
+    }
+
+    static bool is_coal_or_driving(int32_t type)
+    {
+        return CT_COAL_CUTTER == type || CT_HEADING_MACHINE == type;
+    }
 };
 
 class tool_map

+ 39 - 38
db/db_api/CDBCommon.h

@@ -1,18 +1,18 @@
 /**
 * @brief
-鏁版嵁搴撳叕鍏卞ご鏂囦欢
+数据库公共头文件
 
 * @version
 V 1.0.0
 
 * @author
-鐜嬬泭淇�
+王益俊
 
 * @date
-鍒涘缓鏃堕棿:  2018-04-19\n
+创建时间:  2018-04-19\n
 
 * @note
-2018-04-19  鍒濇�鍒涘缓銆俓n
+2018-04-19  初次创建。\n
 
 * @warning
 
@@ -27,72 +27,72 @@ V 1.0.0
 
 namespace YADB
 {
-    //----------------------------------------------------------------------------
-	//                                 鏁版嵁搴撻敊璇�畾涔�
 	//----------------------------------------------------------------------------
-	const int DB_ERR_NO_ERROR               = 0;//鏃犻敊璇�
-	const int DB_ERR_NOT_CONNECT_DB         = -1;//鏈�繛鎺ユ暟鎹�簱
-	const int DB_ERR_EXCUTE_QUERY           = -1;//鎵ц�鏌ヨ�澶辫触
-	const int DB_ERR_QUERY_RES_NOT_INITED   = -1;//鏌ヨ�缁撴灉闆嗚繕鏈�垵濮嬪寲
+	//                                 数据库错误定义
+	//----------------------------------------------------------------------------
+	const int DB_ERR_NO_ERROR               = 0;//无错误
+	const int DB_ERR_NOT_CONNECT_DB         = -1;//未连接数据库
+	const int DB_ERR_EXCUTE_QUERY           = -1;//执行查询失败
+	const int DB_ERR_QUERY_RES_NOT_INITED   = -1;//查询结果集还未初始化
 
 	//----------------------------------------------------------------------------
-	//                                 鍏跺畠瀹氫箟
+	//                                 其它定义
 	//----------------------------------------------------------------------------
-	const int MAX_ASYNC_EXEC_FAILED_COUNT   = 3;//鏈€澶у紓姝ユ墽琛屽け璐ユ�鏁�
-	const int MAX_ASYNC_QUEQUE_CAPACITY     = 32 * 1024;//寮傛�鎵ц�闃熷垪鏈€澶у�閲�
+	const int MAX_ASYNC_EXEC_FAILED_COUNT   = 3;//最大异步执行失败次数
+	const int MAX_ASYNC_QUEQUE_CAPACITY     = 32 * 1024;//异步执行队列最大容量
 
 	/**
 	* @brief
-	鏁版嵁搴撹繛鎺ユ暟鏋氫妇
+	数据库连接数枚举
 	*/
 	enum _DB_CONNECT_COUNT_
 	{
-		DCC_MIN_COUNT = 1,//鏈€灏忚繛鎺ユ暟
-		DCC_MAX_COUNT = 60,//鏈€澶ц繛鎺ユ暟
+		DCC_MIN_COUNT = 1,//最小连接数
+		DCC_MAX_COUNT = 60,//最大连接数
 	};
 
 	/**
 	* @brief
-	鏁版嵁搴撹繛鎺ヨ�缃�粨鏋勪綋
+	数据库连接设置结构体
 	*/
-    struct _DB_CONN_SETTING_
+	struct _DB_CONN_SETTING_
 	{
-		unsigned int Port;//绔�彛
-		int TimeOut;//杩炴帴鏁版嵁搴撹秴鏃�(鍗曚綅锛氱�)
-		std::string Host;//鏁版嵁搴撲富鏈哄湴鍧€
-		std::string User;//鐢ㄦ埛鍚�
-		std::string PWD;//瀵嗙爜
-		std::string DBName;//鏁版嵁搴撳悕
-		std::string CharSet;//瀛楃�闆�
-		std::string stmtSQL;//棰勫�鐞哠QL
-		_DB_CONN_SETTING_()
-		{
-			Port    = 3306;
-			TimeOut = 0;
+		unsigned int Port;//端口
+		int TimeOut;//连接数据库超时(单位:秒)
+		std::string Host;//数据库主机地址
+		std::string User;//用户名
+		std::string PWD;//密码
+		std::string DBName;//数据库名
+		std::string CharSet;//字符集
+		std::string stmtSQL;//预处理SQL
+		_DB_CONN_SETTING_()
+		{
+			Port    = 3306;
+			TimeOut = 0;
 		}
 	};
 
 	/**
 	* @brief
-	鏁版嵁搴撹繛鎺ユ睜璁剧疆缁撴瀯浣�
+	数据库连接池设置结构体
 	*/
 	struct _DB_POOL_SETTING_ : public _DB_CONN_SETTING_
 	{
-		int PoolSize;//杩炴帴姹犲ぇ灏�
+		int PoolSize;//连接池大小
 		_DB_POOL_SETTING_()
 		{
 			PoolSize = 0;
 		}
 	};
 
-    /**
-    * @brief
-    寮傛�鎵ц�缁撴瀯浣�
-    */
+	/**
+	* @brief
+	异步执行结构体
+	*/
 	struct _ASYNC_SQL_
 	{
-		int FailedCount;//鎵ц�澶辫触娆℃暟
-		std::string SQL; //SQ璇�彞
+		int FailedCount;//执行失败次数
+		std::string SQL; //SQ语句
 		_ASYNC_SQL_()
 		{
 			FailedCount = 0;
@@ -108,3 +108,4 @@ namespace YADB
 
 
 
+

+ 390 - 0
db/db_api/CDBConnPool.cpp

@@ -1,3 +1,4 @@
+<<<<<<< HEAD
 #include "CDBConnPool.h"
 #include <boost/bind.hpp>
 #include <boost/lockfree/queue.hpp>
@@ -373,3 +374,392 @@ namespace YADB
 		return nRet;
 	}
 }
+=======
+#include "CDBConnPool.h"
+#include <boost/bind.hpp>
+
+namespace YADB
+{
+	boost::lockfree::queue<_ASYNC_SQL_*, boost::lockfree::capacity<MAX_ASYNC_QUEQUE_CAPACITY>> __AsyncQueue;//异步执行无锁队列
+
+	CDBConnPool::CDBConnPool()
+	{
+		__pAsyncDBConn = 0;
+	}
+
+	CDBConnPool::~CDBConnPool()
+	{
+		Close();
+	}
+
+	CDBConnect * CDBConnPool::__CreateIdleConn( std::string& ConnErr, bool IsTemp )
+	{
+		//std::string ConnErr;
+
+		_DB_CONN_SETTING_ ConnSetting = static_cast< _DB_CONN_SETTING_ >(__Setting);
+		CDBConnect* pConn = new CDBConnect( IsTemp );
+		if ( !pConn->Connect( ConnSetting, ConnErr ) )
+		{
+			delete pConn;
+			pConn = 0;
+			return 0;
+		}
+
+		//如果设置了预处理SQL要准备预处理
+		if ( !ConnSetting.stmtSQL.empty() )
+		{
+			if ( !pConn->Preparestmt( ConnSetting.stmtSQL.c_str(), ConnErr ) )
+			{
+				delete pConn;
+				pConn = 0;
+				return 0;
+			}
+		}
+
+		__IdleConnList.push_back( pConn );
+		return pConn;
+	}
+
+	bool CDBConnPool::Create( const _DB_POOL_SETTING_& Setting, std::string& szError )
+	{
+		return Create(Setting,true,szError);
+	}
+
+	bool CDBConnPool::Create( const _DB_POOL_SETTING_& Setting,bool bAsync, std::string& szError )
+	{
+		std::unique_lock<std::mutex> lock( __mtx );
+
+		if ( Setting.PoolSize < DCC_MIN_COUNT )
+		{
+			szError = "PoolSize is too small!";
+			return false;
+		}
+
+		if ( Setting.PoolSize > DCC_MAX_COUNT )
+		{
+			szError = "PoolSize is too big!";
+			return false;
+		}
+
+		__Setting = Setting;
+		//检查连接池中是否已有连接数量
+		if ((int)__IdleConnList.size() < __Setting.PoolSize)
+		{
+			for ( int i = 0; i < __Setting.PoolSize; i++ )
+			{
+				CDBConnect* pConn = __CreateIdleConn( szError );
+				if ( !pConn )
+				{
+					return false;
+				}
+			}	
+		}
+		//是否已创建异步线程
+		if (bAsync || !__Running)
+		{
+			//创建异步执行线程
+			__CreateAsyncThrdConn();
+
+			//启动异步执行线程
+			__StartAsyncThrd();
+		}		
+		return true;
+	}
+
+	void CDBConnPool::Close()
+	{
+		std::unique_lock<std::mutex> lock( __mtx );
+
+		//停止异步执行线程
+		__StopAsyncThrd();
+
+		//??????????步执行线程连接
+		__DestroyAsyncThrdConn();
+
+		//把所有列表中的连接对象都关闭删除并清除列表
+		CDBConnect* pConn = 0;
+		std::list<CDBConnect*>::iterator lit_conn;
+		for ( lit_conn = __BusyConnList.begin(); lit_conn != __BusyConnList.end(); lit_conn++ )
+		{
+			pConn = *lit_conn;
+			pConn->Close();
+			delete pConn;
+			pConn = 0;
+		}
+		__BusyConnList.clear();
+
+		for ( lit_conn = __IdleConnList.begin(); lit_conn != __IdleConnList.end(); lit_conn++ )
+		{
+			pConn = *lit_conn;
+			pConn->Close();
+			delete pConn;
+			pConn = 0;
+		}
+		__IdleConnList.clear();
+	}
+
+	CDBConnect * CDBConnPool::GetDBConnect( std::string& Error )
+	{
+		std::unique_lock<std::mutex> lock( __mtx );
+
+		CDBConnect* pConn = 0;
+
+		if ( __IdleConnList.size() > 0 )
+		{
+			pConn = *(__IdleConnList.begin());
+			__IdleConnList.pop_front();
+			__BusyConnList.push_back( pConn );
+		}
+		else
+		{
+			//如果已经没有空闲连接,只要当前连接池数量没有超过最大连接数就创建一个临时连接
+			//这个判断无意义
+			if ( __IdleConnList.size() < DCC_MAX_COUNT )
+			{
+				pConn = __CreateIdleConn( Error, true );
+				if ( !pConn )
+				{
+					Error = "Error,failed connect to database!";
+					return 0;
+				}
+
+				__IdleConnList.pop_front();
+				__BusyConnList.push_back( pConn );
+			}
+			else
+			{
+				Error = "Error,db connect count beyond the max connect count!";
+				return 0;
+			}
+		}
+
+		//验证看数据库连接是否还有效
+		if ( pConn )
+		{
+			if ( pConn->ConnctionTest( Error ) != 0 )
+			{
+				//重连一次
+				_DB_CONN_SETTING_ ConnSetting = static_cast< _DB_CONN_SETTING_ >(__Setting);
+				pConn->Close();
+				int nRet = pConn->Connect( ConnSetting, Error );
+				if ( nRet < 0 )
+				{
+					GiveBack( pConn );
+					Error = "Error,failed connect to database!";
+					return 0;
+				}
+			}
+		}
+
+		return pConn;
+	}
+
+	void CDBConnPool::GiveBack( CDBConnect * pConn )
+	{
+		std::unique_lock<std::mutex> lock( __mtx );
+
+		if ( 0 == pConn )
+		{
+			return;
+		}
+
+		__BusyConnList.remove( pConn );
+
+		//如果是临时连接,直接删除不再放入到空闲连接列表中
+		if ( pConn->IsTemp() )
+		{
+			delete pConn;
+			pConn = 0;
+		}
+		else
+		{
+			__IdleConnList.push_back( pConn );
+		}
+	}
+
+	void CDBConnPool::_AsyncThreadFunc( CDBConnPool* pOwner )
+	{
+		std::string Error;
+		while( pOwner->__Running )
+		{
+			_ASYNC_SQL_* pData = 0;
+
+			while ( __AsyncQueue.pop( pData ) )
+			{
+				if ( pData )
+				{
+					if ( __pAsyncDBConn )
+					{
+						my_ulonglong llRes = 0;
+						llRes = __pAsyncDBConn->ExecuteRealSql( pData->SQL.c_str(), Error ); 
+						if ( (my_ulonglong)-1 == llRes )
+						{
+							//Execute failed, write log...
+							printf( "Error,调用ExcuteRealSql失败,Err=%s\n", Error.c_str() );
+							//如果失败了看是不是数据库断开连接了,尝试重新连接一次
+							if ( __pAsyncDBConn->ConnctionTest( Error ) != 0 )
+							{
+								_DB_CONN_SETTING_ ConnSetting = static_cast< _DB_CONN_SETTING_ >(__Setting);
+								__pAsyncDBConn->Close();
+								int nRet = __pAsyncDBConn->Connect( ConnSetting, Error );
+								if ( nRet < 0 )
+								{
+									Error = "Error,failed connect to database!";
+									//Connect failed, write log...
+									printf( "Error,failed connect to database,Err=%s\n", Error.c_str() );
+									//如果连接失败了休息一下
+									boost::this_thread::sleep( boost::posix_time::milliseconds( 100 ) );
+								}
+							}
+
+							//如果执行失败,失败次数加一,失败次数小于最大失败次数放到队尾下次再执行
+							pData->FailedCount++;
+							if ( pData->FailedCount < MAX_ASYNC_EXEC_FAILED_COUNT )
+							{
+								_ASYNC_SQL_* pNewData = new _ASYNC_SQL_();
+								pNewData->FailedCount = pData->FailedCount;
+								pNewData->SQL         = pData->SQL;
+								__AsyncQueue.push( pNewData );
+							}
+						}
+					}
+
+					delete pData;
+					pData = 0;
+				}
+			}
+
+			boost::this_thread::sleep_for( boost::chrono::milliseconds( 1 ) );
+		}
+
+		//线程退出
+		__IsExited = true;
+	}
+
+	void CDBConnPool::__StopAsyncThrd()
+	{
+		if ( !__Running )
+		{
+			return;
+		}
+
+		//等待异步执行线程退出
+		__Running = false;
+
+		while ( !__IsExited )
+		{
+			boost::this_thread::sleep(boost::posix_time::millisec(1));
+		}
+
+		//把异步执行无锁队列中每个元素释放
+		_ASYNC_SQL_* pData = 0;
+		while ( __AsyncQueue.pop( pData ) )
+		{
+			if (pData)
+			{
+				delete pData;
+				pData = 0;
+			}
+		}
+	}
+
+	void CDBConnPool::__StartAsyncThrd()
+	{
+		__Running = true;
+		boost::thread thrd( boost::bind( &CDBConnPool::_AsyncThreadFunc, this, this ) );
+		thrd.detach();
+	}
+
+	void CDBConnPool::__CreateAsyncThrdConn()
+	{
+		std::string ConnErr;
+		//先断开之前的连接
+		this->__DestroyAsyncThrdConn();
+
+		_DB_CONN_SETTING_ ConnSetting = static_cast< _DB_CONN_SETTING_ >(__Setting);
+		CDBConnect* pConn = new CDBConnect();
+		if ( !pConn->Connect( ConnSetting, ConnErr ) )
+		{
+			delete pConn;
+			pConn = 0;
+			return;
+		}
+
+		__pAsyncDBConn = pConn;
+	}
+
+	void CDBConnPool::__DestroyAsyncThrdConn()
+	{
+		if ( __pAsyncDBConn )
+		{
+			__pAsyncDBConn->Close();
+			delete __pAsyncDBConn;
+			__pAsyncDBConn = 0;
+		}
+	}
+
+	bool CDBConnPool::PushAsync( const std::string& strSQL )
+	{
+		_ASYNC_SQL_* pData = new _ASYNC_SQL_;
+		if ( !pData )
+		{
+			return false;
+		}
+
+		pData->SQL = strSQL;
+		return __AsyncQueue.push( pData );
+	}
+
+	bool CDBConnPool::Query( const char *szSql, CDBResultSet& DBRes,std::string& Error )
+	{
+		CDBConnect *pConn = GetDBConnect( Error );
+		if ( 0 == pConn )
+		{
+			return false;
+		}
+
+		MYSQL_RES* pRes = pConn->Query( szSql, Error );
+		GiveBack( pConn );
+		return DBRes.Bind( pRes, Error );
+	}
+
+	MYSQL_RES* CDBConnPool::Query( const char *szSql, std::string& Error)
+	{
+		CDBConnect *pConn = GetDBConnect(Error);
+		if( 0 == pConn){
+			return nullptr;
+		}
+
+		MYSQL_RES* pRes = pConn->Query(szSql,Error);
+		GiveBack(pConn);
+		return pRes;
+	}
+	my_ulonglong CDBConnPool::ExecuteSql( const char *szSql, std::string& Error )
+	{
+		CDBConnect *pConn = GetDBConnect( Error );
+		if ( 0 == pConn )
+		{
+			return -1;
+		}
+		my_ulonglong nRet = pConn->ExecuteSql( szSql, Error );
+		GiveBack( pConn );
+		return nRet;
+		//return pConn->ExecuteSql( szSql, Error );
+	}
+
+	my_ulonglong CDBConnPool::ExecuteSqlID( const char * szSql, std::string & Error )
+	{
+		CDBConnect *pConn = GetDBConnect( Error );
+		if ( 0 == pConn )
+		{
+			return -1;
+		}
+
+		my_ulonglong nRet = pConn->ExecuteSqlID( szSql, Error );
+
+		GiveBack( pConn );
+		return nRet;
+	}
+}
+
+>>>>>>> 10da641ebe4222984b0724854edeb8088ac7c5a7

+ 120 - 139
db/db_api/CDBConnPool.h

@@ -1,65 +1,65 @@
-#ifndef CDBConnPool_h__
-#define CDBConnPool_h__
-
-
+#ifndef CDBConnPool_h__
+#define CDBConnPool_h__
+
+
 /**
 * @brief
-数据库连接池类
+脢媒戮脻驴芒脕卢陆脫鲁脴脌脿
 
 * @version
 V 1.0.0
 
 * @author
-王益俊
+脥玫脪忙驴隆
 
 * @date
-创建时间:  2018-04-17\n
+麓麓陆篓脢卤录盲:  2018-04-17\n
 
 * @note
-2018-04-17  创建类。\n
+2018-04-17  麓麓陆篓脌脿隆拢\n
 
 * @warning
 
 * @bug
 
 */
-
-#pragma once
-
-#include <boost/atomic.hpp>
-#include <boost/thread/thread.hpp>
-#include <boost/serialization/singleton.hpp>
-
-#include <list>
-#include <string>
-#include <mutex>
-
-#include "CDBCommon.h"
-#include "CDBConnect.h"
-
-namespace YADB
-{
-	class CDBConnPool
-	{
-	private:
-		std::list<CDBConnect*> __BusyConnList;//正在使用的数据库连接列表
-		std::list<CDBConnect*> __IdleConnList;//没有使用的数据库连接列表
-		_DB_POOL_SETTING_ __Setting;//数据库连接池设置
-		std::mutex __mtx;//互斥量
-		boost::atomic<bool> __Running{ true };//线程是否运行的标识		
-		boost::atomic<bool> __IsExited{ false };//线程是否已退出的标识
-		CDBConnect* __pAsyncDBConn;//异步执行线程用的数据库连接
-	private:
+
+#pragma once
+
+#include <boost/atomic.hpp>
+#include <boost/thread/thread.hpp>
+#include <boost/lockfree/queue.hpp>
+
+#include <list>
+#include <string>
+#include <mutex>
+
+#include "CDBCommon.h"
+#include "CDBConnect.h"
+
+namespace YADB
+{
+	class CDBConnPool
+	{
+	private:
+		std::list<CDBConnect*> __BusyConnList;//脮媒脭脷脢鹿脫脙碌脛脢媒戮脻驴芒脕卢陆脫脕脨卤铆
+		std::list<CDBConnect*> __IdleConnList;//脙禄脫脨脢鹿脫脙碌脛脢媒戮脻驴芒脕卢陆脫脕脨卤铆
+		_DB_POOL_SETTING_ __Setting;//脢媒戮脻驴芒脕卢陆脫鲁脴脡猫脰脙
+		std::mutex __mtx;//禄楼鲁芒脕驴
+		boost::atomic<bool> __Running{ true };//脧脽鲁脤脢脟路帽脭脣脨脨碌脛卤锚脢露		
+		boost::atomic<bool> __IsExited{ false };//脧脽鲁脤脢脟路帽脪脩脥脣鲁枚碌脛卤锚脢露
+		CDBConnect* __pAsyncDBConn;//脪矛虏陆脰麓脨脨脧脽鲁脤脫脙碌脛脢媒戮脻驴芒脕卢陆脫
+	private:
 		/**
 
 		* @brief
-		创建一个空闲连接函数。
-		* @param  [out] std::string& Error  错误信息\n
-		* @param  [in] bool IsTemp  创建的连接是否是临时的\n
+		麓麓陆篓脪禄赂枚驴脮脧脨脕卢陆脫潞炉脢媒隆拢
+		* @param  [out] std::string& Error  麓铆脦贸脨脜脧垄\n
+		* @param  [in] bool IsTemp  麓麓陆篓碌脛脕卢陆脫脢脟路帽脢脟脕脵脢卤碌脛\n
 
-		* @return 返回创建的连接
-		* @return  >0   成功\n
-		* @return  ==0  失败\n
+		* @return 路碌禄脴麓麓陆篓碌脛脕卢陆脫
+		* @return  >0   鲁脡鹿娄\n
+		* @return  ==0  脢搂掳脺\n
 
 		* @note
 
@@ -67,45 +67,29 @@ namespace YADB
 		* @bug
 
 		*/
-		CDBConnect* __CreateIdleConn( std::string& Error, bool IsTemp = false );
-		/**
-		* @brief
-		创建异步执行线程所用连接函数。
-
-		* @param  无\n
-
-		* @return 无\n
-
-		* @note
-
-		* @warning
-
-		* @bug
-		*/
-		void __CreateAsyncThrdConn();
+		CDBConnect* __CreateIdleConn( std::string& Error, bool IsTemp = false );
 		/**
 		* @brief
-		??????创建异步执行线程所用连接函数。
+		麓麓陆篓脪矛虏陆脰麓脨脨脧脽鲁脤脣霉脫脙脕卢陆脫潞炉脢媒隆拢
 
-		* @param  \n
+		* @param  脦脼\n
 
-		* @return \n
+		* @return 脦脼\n
 
 		* @note
 
 		* @warning
 
 		* @bug
-
 		*/
-		void __DestroyAsyncThrdConn();
+		void __CreateAsyncThrdConn();
 		/**
 		* @brief
-		启动异步执行线程函数。
+		??????麓麓陆篓脪矛虏陆脰麓脨脨脧脽鲁脤脣霉脫脙脕卢陆脫潞炉脢媒隆拢
 
-		* @param  \n
+		* @param  脦脼\n
 
-		* @return 
+		* @return 脦脼\n
 
 		* @note
 
@@ -114,14 +98,14 @@ namespace YADB
 		* @bug
 
 		*/
-		void __StartAsyncThrd();
+		void __DestroyAsyncThrdConn();
 		/**
 		* @brief
-		停止异步执行线程函数。
+		脝么露炉脪矛虏陆脰麓脨脨脧脽鲁脤潞炉脢媒隆拢
 
-		* @param  \n
+		* @param  脦脼\n
 
-		* @return 
+		* @return 脦脼
 
 		* @note
 
@@ -130,15 +114,14 @@ namespace YADB
 		* @bug
 
 		*/
-		void __StopAsyncThrd();
-	protected:
+		void __StartAsyncThrd();
 		/**
 		* @brief
-		异步执行线程函数。
+		脥拢脰鹿脪矛虏陆脰麓脨脨脧脽鲁脤潞炉脢媒隆拢
 
-		* @param  [in] CDBConnPool* pOwner  线程所属对象\n
+		* @param  脦脼\n
 
-		* @return 无\n
+		* @return 脦脼
 
 		* @note
 
@@ -147,20 +130,15 @@ namespace YADB
 		* @bug
 
 		*/
-		void _AsyncThreadFunc( CDBConnPool* pOwner );//线程函数
-	public:
-		CDBConnPool();
-		~CDBConnPool();
+		void __StopAsyncThrd();
+	protected:
 		/**
 		* @brief
-		创建数据库连接池函数。
+		脪矛虏陆脰麓脨脨脧脽鲁脤潞炉脢媒隆拢
 
-		* @param  [in] const _DB_POOL_SETTING_& Setting  数据库连接池设置\n
-		* @param  [out] std::string& Error  错误信息\n
+		* @param  [in] CDBConnPool* pOwner  脧脽鲁脤脣霉脢么露脭脧贸\n
 
-		* @return 返回创建数据库连接池是否成功
-		* @return  true   成功
-		* @return  false  失败
+		* @return 脦脼\n
 
 		* @note
 
@@ -169,14 +147,20 @@ namespace YADB
 		* @bug
 
 		*/
-		bool Create( const _DB_POOL_SETTING_& Setting, std::string& Error );
+		void _AsyncThreadFunc( CDBConnPool* pOwner );//脧脽鲁脤潞炉脢媒
+	public:
+		CDBConnPool();
+		~CDBConnPool();
 		/**
 		* @brief
-		关闭线程池函数。
+		麓麓陆篓脢媒戮脻驴芒脕卢陆脫鲁脴潞炉脢媒隆拢
 
-		* @param  无\n
+		* @param  [in] const _DB_POOL_SETTING_& Setting  脢媒戮脻驴芒脕卢陆脫鲁脴脡猫脰脙\n
+		* @param  [out] std::string& Error  麓铆脦贸脨脜脧垄\n
 
-		* @return  无\n
+		* @return 路碌禄脴麓麓陆篓脢媒戮脻驴芒脕卢陆脫鲁脴脢脟路帽鲁脡鹿娄
+		* @return  true   鲁脡鹿娄
+		* @return  false  脢搂掳脺
 
 		* @note
 
@@ -185,16 +169,15 @@ namespace YADB
 		* @bug
 
 		*/
-		void Close();
+		bool Create( const _DB_POOL_SETTING_& Setting, std::string& Error );
+		bool Create( const _DB_POOL_SETTING_& Setting,bool bAsync, std::string& szError );
 		/**
 		* @brief
-		从连接池中获得一个数据库连接函数。
+		鹿脴卤脮脧脽鲁脤鲁脴潞炉脢媒隆拢
 
-		* @param  [out] std::string& Error  错误信息\n
+		* @param  脦脼\n
 
-		* @return 返回获得一个数据库连接是否成功
-		* @return  0 失败\n
-		* @return  !=0 获得的数据库连接\n
+		* @return  脦脼\n
 
 		* @note
 
@@ -203,14 +186,16 @@ namespace YADB
 		* @bug
 
 		*/
-		CDBConnect* GetDBConnect( std::string& Error );
+		void Close();
 		/**
 		* @brief
-		归还一个数据库连接到连接池中函数。
+		麓脫脕卢陆脫鲁脴脰脨禄帽碌脙脪禄赂枚脢媒戮脻驴芒脕卢陆脫潞炉脢媒隆拢
 
-		* @param  [in] CDBConnect* pConn  数据库连接\n
+		* @param  [out] std::string& Error  麓铆脦贸脨脜脧垄\n
 
-		* @return  无\n
+		* @return 路碌禄脴禄帽碌脙脪禄赂枚脢媒戮脻驴芒脕卢陆脫脢脟路帽鲁脡鹿娄
+		* @return  0 脢搂掳脺\n
+		* @return  !=0 禄帽碌脙碌脛脢媒戮脻驴芒脕卢陆脫\n
 
 		* @note
 
@@ -219,16 +204,14 @@ namespace YADB
 		* @bug
 
 		*/
-		void GiveBack( CDBConnect* pConn );
+		CDBConnect* GetDBConnect( std::string& Error );
 		/**
 		* @brief
-		把SQL语句加入异步执行队列函数。
+		鹿茅禄鹿脪禄赂枚脢媒戮脻驴芒脕卢陆脫碌陆脕卢陆脫鲁脴脰脨潞炉脢媒隆拢
 
-		* @param  [in] const std::string& strSQL  要执行的SQL语句\n
+		* @param  [in] CDBConnect* pConn  脢媒戮脻驴芒脕卢陆脫\n
 
-		* @return  加入异步执行队列是否成功\n
-		* @return  true成功\n
-		* @return  false失败\n
+		* @return  脦脼\n
 
 		* @note
 
@@ -237,18 +220,16 @@ namespace YADB
 		* @bug
 
 		*/
-		bool PushAsync( const std::string& strSQL );
+		void GiveBack( CDBConnect* pConn );
 		/**
 		* @brief
-		执行SQL语句返回结果集函数。
+		掳脩SQL脫茂戮盲录脫脠毛脪矛虏陆脰麓脨脨露脫脕脨潞炉脢媒隆拢
 
-		* @param [in] const char *szSql  SQL语句\n
-		* @param [out] CDBResultSet& DBRes  结果集\n
-		* @param [out] std::string& Error  错误信息\n
+		* @param  [in] const std::string& strSQL  脪陋脰麓脨脨碌脛SQL脫茂戮盲\n
 
-		* @return 返回查询是否成功
-		* @return  true  成功\n
-		* @return  false  失败\n
+		* @return  录脫脠毛脪矛虏陆脰麓脨脨露脫脕脨脢脟路帽鲁脡鹿娄\n
+		* @return  true鲁脡鹿娄\n
+		* @return  false脢搂掳脺\n
 
 		* @note
 
@@ -257,15 +238,18 @@ namespace YADB
 		* @bug
 
 		*/
-		bool Query( const char *szSql, CDBResultSet& DBRes, std::string& Error );
+		bool PushAsync( const std::string& strSQL );
 		/**
 		* @brief
-		执行SQL语句返回原生结果集函数。
+		脰麓脨脨SQL脫茂戮盲路碌禄脴陆谩鹿没录炉潞炉脢媒隆拢
 
-		* @param [in] const char *szSql  SQL语句\n
-		* @param [out] std::string& Error  错误信息\n
+		* @param [in] const char *szSql  SQL脫茂戮盲\n
+		* @param [out] CDBResultSet& DBRes  陆谩鹿没录炉\n
+		* @param [out] std::string& Error  麓铆脦贸脨脜脧垄\n
 
-		* @return 返回查询的结果集
+		* @return 路碌禄脴虏茅脩炉脢脟路帽鲁脡鹿娄
+		* @return  true  鲁脡鹿娄\n
+		* @return  false  脢搂掳脺\n
 
 		* @note
 
@@ -274,17 +258,18 @@ namespace YADB
 		* @bug
 
 		*/
-		MYSQL_RES* Query( const char *szSql, std::string& Error );
+		bool Query( const char *szSql, CDBResultSet& DBRes, std::string& Error );
+		MYSQL_RES* Query( const char *szSql, std::string& Error );
 		/**
 		* @brief
-		执行SQL语句函数。
+		脰麓脨脨SQL脫茂戮盲潞炉脢媒隆拢
 
-		* @param [in] const char *szSql  SQL语句\n
-		* @param [out] std::string& Error  错误信息\n
+		* @param [in] const char *szSql  SQL脫茂戮盲\n
+		* @param [out] std::string& Error  麓铆脦贸脨脜脧垄\n
 
-		* @return 返回影响到的记录数量
-		* @return  -1  失败\n
-		* @return  >=0  成功\n
+		* @return 路碌禄脴脫掳脧矛碌陆碌脛录脟脗录脢媒脕驴
+		* @return  -1  脢搂掳脺\n
+		* @return  >=0  鲁脡鹿娄\n
 
 		* @note
 
@@ -293,17 +278,17 @@ namespace YADB
 		* @bug
 
 		*/
-		my_ulonglong ExecuteSql( const char *szSql, std::string& Error );
+		my_ulonglong ExecuteSql( const char *szSql, std::string& Error );
 		/**
 		* @brief
-		执行SQL语句并获得ID的函数。
+		脰麓脨脨SQL脫茂戮盲虏垄禄帽碌脙ID碌脛潞炉脢媒隆拢
 
-		* @param [in] const char *szSql  SQL语句\n
-		* @param [out] std::string& Error  错误信息\n
+		* @param [in] const char *szSql  SQL脫茂戮盲\n
+		* @param [out] std::string& Error  麓铆脦贸脨脜脧垄\n
 
-		* @return 返回获得的ID
-		* @return  <=0  失败\n
-		* @return  >0   成功\n
+		* @return 路碌禄脴禄帽碌脙碌脛ID
+		* @return  <=0  脢搂掳脺\n
+		* @return  >0   鲁脡鹿娄\n
 
 		* @note
 
@@ -312,12 +297,8 @@ namespace YADB
 		* @bug
 
 		*/
-		my_ulonglong ExecuteSqlID( const char *szSql, std::string& Error );
-	};
-}
-
-//单件相关定义
-typedef boost::serialization::singleton<YADB::CDBConnPool> singleton_CDBConnPool;
-#define sDBConnPool singleton_CDBConnPool::get_mutable_instance()
-
+		my_ulonglong ExecuteSqlID( const char *szSql, std::string& Error );
+	};
+}
 #endif // CDBConnPool_h__
+

+ 25 - 23
db/db_api/CDBConnect.cpp

@@ -18,10 +18,10 @@ namespace YADB
 
 	bool CDBConnect::Connect( const _DB_CONN_SETTING_ & DBSetting, std::string & Error )
 	{
-		//鍒涘缓
+		//创建
 		__pConn = mysql_init( NULL );
 
-		//瓒呮椂璁剧疆
+		//超时设置
 		if ( mysql_options( __pConn, MYSQL_OPT_CONNECT_TIMEOUT, &(DBSetting.TimeOut) ) != 0 )
 		{
 			Error = "Failed to call mysql_options,";
@@ -30,7 +30,7 @@ namespace YADB
 			return false;
 		}
 
-		//璁剧疆瀛楃�闆�
+		//设置字符集
 		if ( mysql_set_character_set( __pConn, DBSetting.CharSet.c_str() ) != 0 )
 		{
 			Error = "Failed to call mysql_set_character_set,";
@@ -39,7 +39,7 @@ namespace YADB
 			return false;
 		}
 
-		//杩炴帴鏁版嵁搴�
+		//连接数据库
 		if ( !mysql_real_connect( __pConn, DBSetting.Host.c_str(), DBSetting.User.c_str(), DBSetting.PWD.c_str(), DBSetting.DBName.c_str(), 0, NULL, 0 ) )
 		{
 			Error = "Failed to connect database,";
@@ -47,7 +47,7 @@ namespace YADB
 			Error += GetLastError();
 			return false;
 		}
-		//渚濋潬 mysql_ping 鏃犳硶鍋氬埌閲嶈繛
+		//依靠 mysql_ping 无法做到重连
 		char value = 1;
 		mysql_options(__pConn, MYSQL_OPT_RECONNECT, &value);
 
@@ -182,24 +182,6 @@ namespace YADB
 
 		return mysql_affected_rows( __pConn );
 	}
-	my_ulonglong CDBConnect::ExecuteSqlID( const char * szSql, std::string & Error )
-	{
-		if ( 0 == __pConn )
-		{
-			Error = "Error,not connected to database!";
-			return DB_ERR_NOT_CONNECT_DB;
-		}
-
-		if ( mysql_real_query( __pConn, szSql, strlen( szSql ) ) )
-		{
-			Error = "Failed to execute SQL!";
-			Error += " LastError=";
-			Error += GetLastError();
-			return DB_ERR_EXCUTE_QUERY;
-		}
-
-		return mysql_insert_id( __pConn );
-	}
 
 	my_ulonglong CDBConnect::ExecuteRealSql( const char * szSql, std::string & Error )
 	{
@@ -308,4 +290,24 @@ namespace YADB
 			__pstmt = 0;
 		}
 	}
+
+	my_ulonglong CDBConnect::ExecuteSqlID( const char * szSql, std::string & Error )
+	{
+		if ( 0 == __pConn )
+		{
+			Error = "Error,not connected to database!";
+			return DB_ERR_NOT_CONNECT_DB;
+		}
+
+		if ( mysql_real_query( __pConn, szSql, strlen( szSql ) ) )
+		{
+			Error = "Failed to execute SQL!";
+			Error += " LastError=";
+			Error += GetLastError();
+			return DB_ERR_EXCUTE_QUERY;
+		}
+
+		return mysql_insert_id( __pConn );
+	}
 }
+

+ 174 - 173
db/db_api/CDBConnect.h

@@ -1,18 +1,18 @@
 /**
 * @brief
-鏁版嵁搴撹繛鎺ョ被
+数据库连接类
 
 * @version
 V 1.0.0
 
 * @author
-鐜嬬泭淇�
+王益俊
 
 * @date
-鍒涘缓鏃堕棿:  2018-04-17\n
+创建时间:  2018-04-17\n
 
 * @note
-2018-04-17  鍒涘缓绫汇€俓n
+2018-04-17  创建类。\n
 
 * @warning
 
@@ -32,23 +32,23 @@ namespace YADB
 	class CDBConnect
 	{
 	private:
-    	MYSQL * __pConn;//鏁版嵁搴撹繛鎺�
-    	CDBResultSet __RessultSet;//缁撴灉闆�
-    	bool __IsTemp;//鏄�惁鏄�复鏃剁殑(濡傛灉鏄�复鏃朵娇鐢ㄧ殑,褰掕繕鍒拌繛鎺ユ睜涓�椂浼氳嚜鍔ㄥ垹闄�,浠ヤ繚鎸佹€讳綋杩炴帴鏁颁笉鍙�)
-    	MYSQL_STMT *__pstmt;//棰勫�鐞�
+		MYSQL * __pConn;//数据库连接
+		CDBResultSet __RessultSet;//结果集
+		bool __IsTemp;//是否是临时的(如果是临时使用的,归还到连接池中时会自动删除,以保持总体连接数不变)
+		MYSQL_STMT *__pstmt;//预处理
 	public:
 		CDBConnect( bool IsTemp = false );
 		~CDBConnect();
 		/**
 		* @brief
-		杩炴帴鏁版嵁搴撳嚱鏁般€�
+		连接数据库函数。
 
-		* @param  [in] const _DB_POOL_SETTING_& Setting  鏁版嵁搴撹繛鎺ヨ�缃甛n
-		* @param  [out] std::string& Error  閿欒�淇℃伅\n
+		* @param  [in] const _DB_POOL_SETTING_& Setting  数据库连接设置\n
+		* @param  [out] std::string& Error  错误信息\n
 
-		* @return 杩斿洖杩炴帴鏁版嵁搴撴槸鍚︽垚鍔�
-		* @return  true   鎴愬姛\n
-		* @return  false  澶辫触\n
+		* @return 返回连接数据库是否成功
+		* @return  true   成功\n
+		* @return  false  失败\n
 
 		* @note
 
@@ -59,142 +59,142 @@ namespace YADB
 		*/
 		bool Connect( const _DB_CONN_SETTING_& DBSetting, std::string& Error );
 		/**
-	    * @brief
-	    鍏抽棴杩炴帴鍑芥暟銆�
+		* @brief
+		关闭连接函数。
 
-	    * @param  鏃燶n
+		* @param  无\n
 
-	    * @return  鏃燶n
+		* @return  无\n
 
-	    * @note
+		* @note
 
-	    * @warning
+		* @warning
 
-	    * @bug
+		* @bug
 
-	    */
+		*/
 		void Close();
 		/**
-	    * @brief
-	    娴嬭瘯杩炴帴鍑芥暟銆�
+		* @brief
+		测试连接函数。
 
-	    * @param [out] std::string& Error 閿欒�淇℃伅\n
+		* @param [out] std::string& Error 错误信息\n
 
-	    * @return 杩斿洖娴嬭瘯杩炴帴鏄�惁鎴愬姛
-	    * @return  0  杩炴帴娴嬭瘯鎴愬姛\n
-	    * @return  !=0  杩炴帴娴嬭瘯澶辫触\n
+		* @return 返回测试连接是否成功
+		* @return  0  连接测试成功\n
+		* @return  !=0  连接测试失败\n
 
-	    * @note
+		* @note
 
-	    * @warning
+		* @warning
 
-	    * @bug
+		* @bug
 
-	    */
+		*/
 		int ConnctionTest( std::string& Error );
 		/**
-	    * @brief
-	    璁剧疆鑷�姩鎻愪氦鐨勫嚱鏁般€�
+		* @brief
+		设置自动提交的函数。
 
-	    * @param [in] bool Mode 鏄�惁鏄�惁鑷�姩鎻愪氦\n
-	    * @param [out] std::string& Error 閿欒�淇℃伅\n
+		* @param [in] bool Mode 是否是否自动提交\n
+		* @param [out] std::string& Error 错误信息\n
 
-	    * @return 杩斿洖璁剧疆鑷�姩鎻愪氦鏄�惁鎴愬姛
-	    * @return  0  鎴愬姛\n
-	    * @return  !=0  澶辫触\n
+		* @return 返回设置自动提交是否成功
+		* @return  0  成功\n
+		* @return  !=0  失败\n
 
-	    * @note
+		* @note
 
-	    * @warning
+		* @warning
 
-	    * @bug
+		* @bug
 
-	    */
+		*/
 		int AutoCommit( bool Mode, std::string& Error );
 		/**
-	    * @brief
-	    鎻愪氦鍑芥暟銆�
+		* @brief
+		提交函数。
 
-	    * @param [out] std::string& Error 閿欒�淇℃伅\n
+		* @param [out] std::string& Error 错误信息\n
 
-	    * @return 杩斿洖鎻愪氦鏄�惁鎴愬姛
-	    * @return  0  鎴愬姛\n
-	    * @return  !=0  澶辫触\n
+		* @return 返回提交是否成功
+		* @return  0  成功\n
+		* @return  !=0  失败\n
 
-	    * @note
+		* @note
 
-	    * @warning
+		* @warning
 
-	    * @bug
+		* @bug
 
-	    */
+		*/
 		int Commit( std::string& Error  );
 		/**
-	    * @brief
-	    鍥炴粴鍑芥暟銆�
+		* @brief
+		回滚函数。
 
-	    * @param [out] std::string& Error  閿欒�淇℃伅\n
+		* @param [out] std::string& Error  错误信息\n
 
-	    * @return 杩斿洖鍥炴粴鏄�惁鎴愬姛
-	    * @return  0  鎴愬姛\n
-	    * @return  !=0  澶辫触\n
+		* @return 返回回滚是否成功
+		* @return  0  成功\n
+		* @return  !=0  失败\n
 
-	    * @note
+		* @note
 
-	    * @warning
+		* @warning
 
-	    * @bug
+		* @bug
 
-	    */
+		*/
 		int RollBack( std::string& Error );
 		/**
-	    * @brief
-	    鎵ц�SQL璇�彞杩斿洖缁撴灉闆嗗嚱鏁般€�
+		* @brief
+		执行SQL语句返回结果集函数。
 
-	    * @param [in] const char *szSql  SQL璇�彞\n
-	    * @param [out] std::string& Error  閿欒�淇℃伅\n
+		* @param [in] const char *szSql  SQL语句\n
+		* @param [out] std::string& Error  错误信息\n
 
-	    * @return 杩斿洖缁撴灉闆�
-	    * @return  0  澶辫触\n
-	    * @return  !=0  鎴愬姛\n
+		* @return 返回结果集
+		* @return  0  失败\n
+		* @return  !=0  成功\n
 
-	    * @note
+		* @note
 
-	    * @warning
+		* @warning
 
-	    * @bug
+		* @bug
 
-	    */
+		*/
 		CDBResultSet* ExecuteQuery( const char *szSql, std::string& Error );
 		/**
-	    * @brief
-	    鎵ц�SQL璇�彞杩斿洖mysql鍘熷�缁撴灉闆嗗嚱鏁般€�
+		* @brief
+		执行SQL语句返回mysql原始结果集函数。
 
-	    * @param [in] const char *szSql  SQL璇�彞\n
-	    * @param [out] std::string& Error  閿欒�淇℃伅\n
+		* @param [in] const char *szSql  SQL语句\n
+		* @param [out] std::string& Error  错误信息\n
 
-	    * @return 杩斿洖鐨刴ysql鍘熷�缁撴灉闆�
-	    * @return  ==0  澶辫触\n
-	    * @return  !=0  鎴愬姛\n
+		* @return 返回的mysql原始结果集
+		* @return  ==0  失败\n
+		* @return  !=0  成功\n
 
-	    * @note
+		* @note
 
-	    * @warning
+		* @warning
 
-	    * @bug
+		* @bug
 
-	    */
+		*/
 		MYSQL_RES* Query( const char *szSql, std::string& Error );
 		/**
 		* @brief
-		鎵ц�SQL璇�彞鍑芥暟銆�
+		执行SQL语句函数。
 
-		* @param [in] const char *szSql  SQL璇�彞\n
-		* @param [out] std::string& Error  閿欒�淇℃伅\n
+		* @param [in] const char *szSql  SQL语句\n
+		* @param [out] std::string& Error  错误信息\n
 
-		* @return 杩斿洖褰卞搷鍒扮殑璁板綍鏁伴噺
-		* @return  -1  澶辫触\n
-		* @return  >=0  鎴愬姛\n
+		* @return 返回影响到的记录数量
+		* @return  -1  失败\n
+		* @return  >=0  成功\n
 
 		* @note
 
@@ -205,84 +205,65 @@ namespace YADB
 		*/
 		my_ulonglong ExecuteSql( const char *szSql, std::string& Error );
 		/**
-	    * @brief
-	    鎵ц�SQL璇�彞鍑芥暟銆�
+		* @brief
+		执行SQL语句函数。
 
-	    * @param [in] const char *szSql  SQL璇�彞\n
-	    * @param [out] std::string& Error  閿欒�淇℃伅\n
+		* @param [in] const char *szSql  SQL语句\n
+		* @param [out] std::string& Error  错误信息\n
 
-	    * @return 杩斿洖褰卞搷鍒扮殑璁板綍鏁伴噺
-	    * @return  -1  澶辫触\n
-	    * @return  >=0  鎴愬姛\n
+		* @return 返回影响到的记录数量
+		* @return  -1  失败\n
+		* @return  >=0  成功\n
 
-	    * @note
+		* @note
 
-	    * @warning
+		* @warning
 
-	    * @bug
+		* @bug
 
-	    */
+		*/
 		my_ulonglong ExecuteRealSql( const char *szSql, std::string& Error );
-		/**
-		* @brief
-		鎵ц�SQL璇�彞骞惰幏寰楄嚜鍔ㄧ敓鎴愮殑ID鍑芥暟銆�
-
-		* @param [in] const char *szSql  SQL璇�彞\n
-		* @param [out] std::string& Error  閿欒�淇℃伅\n
-
-		* @return 杩斿洖鑷�姩鐢熸垚鐨処D
-		* @return  <1  澶辫触\n
-		* @return  >=1  鎴愬姛\n
-
-		* @note
-
-		* @warning
-
-		* @bug
-
-		*/
-		my_ulonglong ExecuteSqlID( const char *szSql, std::string& Error );
 		/**
-	    * @brief
-	    鑾峰緱鏈€鍚庝竴娆¢敊璇�俊鎭�嚱鏁般€�
+		* @brief
+		获得最后一次错误信息函数。
 
-	    * @param 鏃燶n
+		* @param 无\n
 
-	    * @return 杩斿洖鏈€鍚庝竴娆¢敊璇�
+		* @return 返回最后一次错误
 
-	    * @note
+		* @note
 
-	    * @warning
+		* @warning
 
-	    * @bug
+		* @bug
 
-	    */
+		*/
 		const char* GetLastError();
 		/**
-	    * @brief
-	    鑾峰緱鏈€鍚庝竴娆℃彃鍏ョ殑ID鍑芥暟銆�
+		* @brief
+		获得最后一次插入的ID函数。
 
-	    * @param [out] std::string& Error  閿欒�淇℃伅\n
+		* @param [out] std::string& Error  错误信息\n
 
-	    * @return 杩斿洖鏈€鍚庝竴娆℃彃鍏ョ殑ID
-	    * @return  -1  澶辫触\n
-	    * @return  >=0  鎴愬姛\n
+		* @return 返回最后一次插入的ID
+		* @return  -1  失败\n
+		* @return  >=0  成功\n
 
-	    * @note
+		* @note
 
-	    * @warning
+		* @warning
 
-	    * @bug
+		* @bug
 
-	    */
+		*/
 		my_ulonglong GetLastInsertID( std::string& Error );
 		/**
 		* @brief
-		鑾峰緱鏄�惁鏄�复鏃剁殑杩炴帴鍑芥暟銆�
+		获得是否是临时的连接函数。
 
-		* @param 鏃燶n
+		* @param 无\n
 
-		* @return 杩斿洖鏄�惁鏄�复鏃剁殑
+		* @return 返回是否是临时的
 
 		* @note
 
@@ -293,59 +274,79 @@ namespace YADB
 		*/
 		bool IsTemp();
 		/**
-	    * @brief
-	    鍑嗗�stmt鍑芥暟銆�
+		* @brief
+		准备stmt函数。
 
-	    * @param const char *szSql 棰勫�鐞哠QL璇�彞\n
-	    * @param [out] std::string& Error  閿欒�淇℃伅\n
+		* @param const char *szSql 预处理SQL语句\n
+		* @param [out] std::string& Error  错误信息\n
 
-	    * @return 杩斿洖鍑嗗�stmt鏄�惁鎴愬姛
-	    * @return  false  澶辫触\n
-	    * @return  true  鎴愬姛\n
+		* @return 返回准备stmt是否成功
+		* @return  false  失败\n
+		* @return  true  成功\n
 
-	    * @note
+		* @note
 
-	    * @warning
+		* @warning
 
-	    * @bug
+		* @bug
 
-	    */
+		*/
 		bool Preparestmt( const char *szSql, std::string& Error );
 		/**
-	    * @brief
-	    鎵ц�stmt鍑芥暟銆�
+		* @brief
+		执行stmt函数。
 
-	    * @param [in] MYSQL_BIND *stBinds  瑕佹墽琛岀殑BIND\n
-	    * @param [in] uint64_t *piId  鎵ц�鍚庤幏寰楃殑ID\n
-	    * @param [out] std::string& Error  閿欒�淇℃伅\n
+		* @param [in] MYSQL_BIND *stBinds  要执行的BIND\n
+		* @param [in] uint64_t *piId  执行后获得的ID\n
+		* @param [out] std::string& Error  错误信息\n
 
-	    * @return 杩斿洖鎵ц�stmt鎴愬姛杩樻槸澶辫触
-	    * @return  false  澶辫触\n
-	    * @return  true  鎴愬姛\n
+		* @return 返回执行stmt成功还是失败
+		* @return  false  失败\n
+		* @return  true  成功\n
 
-	    * @note
+		* @note
 
-	    * @warning
+		* @warning
 
-	    * @bug
+		* @bug
 
-	    */
+		*/
 		bool stmtExcute( MYSQL_BIND *stBinds, uint64_t *piId, std::string& Error );
 		/**
-	    * @brief
-	    鍏抽棴stmt鍑芥暟銆�
+		* @brief
+		关闭stmt函数。
 
-	    * @param 鏃燶n
+		* @param 无\n
 
-	    * @return 鏃燶n
+		* @return 无\n
 
-	    * @note
+		* @note
 
-	    * @warning
+		* @warning
 
-	    * @bug
+		* @bug
 
-	    */
+		*/
 		void stmtClose();
+		/**
+		* @brief
+		执行SQL语句并获得自动生成的ID函数。
+
+		* @param [in] const char *szSql  SQL语句\n
+		* @param [out] std::string& Error  错误信息\n
+
+		* @return 返回自动生成的ID
+		* @return  <1  失败\n
+		* @return  >=1  成功\n
+
+		* @note
+
+		* @warning
+
+		* @bug
+
+		*/
+		my_ulonglong ExecuteSqlID( const char *szSql, std::string& Error );
 	};
 }
+

+ 22 - 1
module_service/area_business_car_attendance.cpp

@@ -34,7 +34,28 @@ void area_business_car_attendance::on_enter(const std::shared_ptr<area_hover>&ar
         return;
     }
 
-    save_attendance(card_ptr, area_hover_ptr);
+    //save_attendance(card_ptr, area_hover_ptr);
+    auto mine_tool_ptr = card_ptr->get_mine_tool();
+    if(!mine_tool_ptr->m_is_attendance)
+    {
+        return;
+    }
+
+    //考勤结束
+    mine_tool_ptr->m_is_attendance=false;
+    //作为一条结束考勤记录保存到数据库
+    db_tool::save_attendance(card_ptr);
+
+    //save_attendance(card_ptr, area_hover_ptr);
+
+    //    auto ptr_temp = std::make_shared<car_attendance_data>();
+    //    ptr = ptr_temp;
+
+    //    ptr_temp->m_is_attendance = true;
+    //    ptr_temp->m_attendance_start_time = std::chrono::system_clock::now();
+
+    //    //作为一条开始考勤记录保存到数据库
+    //    db_tool::save_attendance(card_ptr, ptr_temp->m_is_attendance, ptr_temp->m_attendance_start_time);
 }
 
 void area_business_car_attendance::on_hover(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data> ptr)

+ 1 - 1
module_service/area_business_car_attendance.h

@@ -20,6 +20,6 @@ public:
     //记录离开考勤区域信息,开始考勤
     void on_leave(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data> ptr);
 
-    static void save_attendance(const std::shared_ptr<card_location_base>& card_ptr, const std::shared_ptr<area_hover> area_hover_ptr){}
+    //static void save_attendance(const std::shared_ptr<card_location_base>& card_ptr, const std::shared_ptr<area_hover> area_hover_ptr){}
 };
 #endif // AREA_BUSINESS_CAR_ATTENDANCE_H

+ 1 - 15
module_service/area_business_card_enter_or_leave.cpp

@@ -11,7 +11,7 @@ struct enter_or_leave_data:business_data
 {
     enter_or_leave_data()
     {
-        m_enter_time_ms = tool_time::to_ms(std::chrono::system_clock::now());
+        m_enter_time_ms = tool_time::now_to_ms();
     }
 
     ///考勤开始时间
@@ -54,13 +54,6 @@ void area_business_card_enter_or_leave::save_his_area_location_enter(const std::
     char sql[LENGTH_SQL] = {0};
 
     std::string call(card_ptr->is_vehicle()?"add_area_vehicle":"add_area_staff");
-#if 0
-    std::string call("add_area_staff");
-    if(card_ptr->is_vehicle())//车卡
-    {
-        call="add_area_vehicle";
-    }
-#endif
 
     std::string start_str = tool_time::to_str_ex(start_time_ms);
 
@@ -79,13 +72,6 @@ void area_business_card_enter_or_leave::save_his_area_location_leave(const std::
     char sql[LENGTH_SQL] = {0};
 
     std::string call(card_ptr->is_vehicle()?"add_area_vehicle":"add_area_staff");
-#if 0
-    std::string call("add_area_staff");
-    if(card_ptr->is_vehicle())//车卡
-    {
-        call="add_area_vehicle";
-    }
-#endif
 
     std::string start_str = tool_time::to_str_ex(start_time_ms);
     std::string end_str = tool_time::to_str_ex(tool_time::now_to_ms());

+ 58 - 14
module_service/area_business_geofault.cpp

@@ -10,6 +10,9 @@
 #include"common_tool.h"
 #include"log.h"
 #include"card.h"
+#include"config_file.h"
+#include"point.h"
+#include"area.h"
 
 struct geofault_data:business_data
 {
@@ -17,18 +20,25 @@ struct geofault_data:business_data
     {
         m_near_geofault_count=0;
         m_far_geofault_count=0;
+        m_is_warning=false;
     }
 
     int m_near_geofault_count;
     int m_far_geofault_count;
+    bool m_is_warning;
 };
 
 std::unordered_map<int, std::vector<point>> area_business_geofault::_area_geofault_map;
 int area_business_geofault::_geofault_count_limit;
 
 void area_business_geofault::on_enter(const std::shared_ptr<area_hover>&area_hover_ptr,
-                                                       const std::shared_ptr<card_location_base>&card_ptr,std::shared_ptr<business_data>& ptr)
+                                      const std::shared_ptr<card_location_base>&card_ptr,std::shared_ptr<business_data>& ptr)
 {
+    if(!tool_other::is_coal_or_driving(card_ptr->m_type))
+    {
+        return;
+    }
+
     auto ptr_temp = std::make_shared<geofault_data>();
     ptr = ptr_temp;
 
@@ -37,20 +47,34 @@ void area_business_geofault::on_enter(const std::shared_ptr<area_hover>&area_hov
     {
         ptr_temp->m_near_geofault_count++;
     }
+    else
+    {
+        ptr_temp->m_far_geofault_count++;
+    }
+
+    auto ev_ptr_temp = event_list::instance()->get_event_card(card_ptr->m_id, card_ptr->m_type, ET_VEHICLE_NEAR_GEOFAULT);
+    ptr_temp->m_is_warning = (nullptr != ev_ptr_temp && !ev_ptr_temp->is_end());
 }
 
 void area_business_geofault::on_hover(const std::shared_ptr<area_hover>&area_hover_ptr,
                                       const std::shared_ptr<card_location_base>&card_ptr,std::shared_ptr<business_data> ptr)
 {
+    if(!tool_other::is_coal_or_driving(card_ptr->m_type))
+    {
+        return;
+    }
+
     auto ptr_temp = static_cast<geofault_data*>(ptr.get());
 
     double dist=0;
     if(_is_near(area_hover_ptr->m_area, card_ptr->x, card_ptr->y, dist))
     {
+        ptr_temp->m_far_geofault_count=0;
         ptr_temp->m_near_geofault_count++;
     }
     else
     {
+        ptr_temp->m_near_geofault_count=0;
         ptr_temp->m_far_geofault_count++;
     }
 
@@ -59,14 +83,13 @@ void area_business_geofault::on_hover(const std::shared_ptr<area_hover>&area_hov
     {
         ptr_temp->m_near_geofault_count=_geofault_count_limit;
 
-        auto ev_ptr = event_list::instance()->get_event_card(card_ptr->m_id, card_ptr->m_type, ET_VEHICLE_NEAR_GEOFAULT);
-        if(!ev_ptr)//从没有告警状态转化为告警状态
+        if(!ptr_temp->m_is_warning)
         {
-            ptr_temp->m_far_geofault_count=0;
+            ptr_temp->m_is_warning = true;
+            uint64_t id = tool_other::type_id_to_u64(card_ptr->m_type, card_ptr->m_id);
+            event_tool::instance()->handle_event(OT_CARD, ET_VEHICLE_NEAR_GEOFAULT, id,
+                                                 CYaSetting::m_sys_setting.geofault_warn_dis, dist, true);
         }
-
-        uint64_t id = tool_other::type_id_to_u64(card_ptr->m_type, card_ptr->m_id);
-        event_tool::instance()->handle_event(OT_CARD, ET_VEHICLE_NEAR_GEOFAULT, id, CYaSetting::m_sys_setting.geofault_warn_dis, dist, true);
     }
 
     //确定正常
@@ -74,25 +97,46 @@ void area_business_geofault::on_hover(const std::shared_ptr<area_hover>&area_hov
     {
         ptr_temp->m_far_geofault_count=_geofault_count_limit;
 
-        auto ev_ptr = event_list::instance()->get_event_card(card_ptr->m_id, card_ptr->m_type, ET_VEHICLE_NEAR_GEOFAULT);
-        if(ev_ptr && !ev_ptr->is_end())
+        if(ptr_temp->m_is_warning)
         {
-            ptr_temp->m_near_geofault_count=0;
+            ptr_temp->m_is_warning = false;
 
             uint64_t id = tool_other::type_id_to_u64(card_ptr->m_type, card_ptr->m_id);
-            event_tool::instance()->handle_event(OT_CARD, ET_VEHICLE_NEAR_GEOFAULT, id, CYaSetting::m_sys_setting.geofault_warn_dis, dist, false);
+            event_tool::instance()->handle_event(OT_CARD, ET_VEHICLE_NEAR_GEOFAULT, id,
+                                                 CYaSetting::m_sys_setting.geofault_warn_dis, dist, false);
         }
     }
 }
 
-
 void area_business_geofault::on_leave(const std::shared_ptr<area_hover>&area_hover_ptr,
                                       const std::shared_ptr<card_location_base>&card_ptr,std::shared_ptr<business_data> ptr)
 {
+    if(!tool_other::is_coal_or_driving(card_ptr->m_type))
+    {
+        return;
+    }
+
     uint64_t id = tool_other::type_id_to_u64(card_ptr->m_type, card_ptr->m_id);
     event_tool::instance()->handle_event(OT_CARD, ET_VEHICLE_NEAR_GEOFAULT, id, CYaSetting::m_sys_setting.geofault_warn_dis, 1000, false);
 }
 
+void area_business_geofault::init(config_file& config)
+{
+    _geofault_count_limit = std::stoi(config.get("service.geofault_count_limit","10"));
+}
+
+std::string area_business_geofault::_points2str(std::vector<point>& points)
+{
+    std::string str;
+    for(auto&p : points)
+    {
+        char ch[64] = {0};
+        sprintf(ch, "x=%f,y=%f;", p.x, p.y);
+        str.append(ch);
+    }
+
+    return str;
+}
 
 void area_business_geofault::init_geofault_from_db()
 {
@@ -148,8 +192,8 @@ void area_business_geofault::init_geofault_from_db()
     for(const auto &p : _area_geofault_map)
     {
         auto kk = p.second;
-        std_debug("init_geofault_from_db:area_id:%d--points: %s",p.first, points2str(kk).c_str());
-        log_info("init_geofault_from_db:area_id:%d--points: %s",p.first, points2str(kk).c_str());
+        std_debug("init_geofault_from_db:area_id:%d--points: %s",p.first, _points2str(kk).c_str());
+        log_info("init_geofault_from_db:area_id:%d--points: %s",p.first, _points2str(kk).c_str());
     }
 }
 

+ 6 - 20
module_service/area_business_geofault.h

@@ -7,13 +7,13 @@
  * @date 2019-01-15
  */
 
-#include<area_business.h>
+#include"area_business.h"
 #include <unordered_map>
 #include<vector>
-#include<area.h>
-#include<point.h>
 
-#include"config_file.h"
+struct config_file;
+struct point;
+struct area;
 
 /**
  * @brief 当采煤机和掘进机有数据点过来的时候,判断当前点与地址断层坐标点的距离d。如d<设置的阈值,则告警,d>阈值则取消。
@@ -30,10 +30,7 @@ public:
     void on_hover(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data> ptr);
     void on_leave(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data> ptr);
 
-    static void init(config_file& config)
-    {
-        _geofault_count_limit = std::stoi(config.get("service.geofault_count_limit","10"));
-    }
+    static void init(config_file& config);
 
     static void init_geofault_from_db();
 
@@ -69,18 +66,7 @@ private:
         return arr;
     }
 
-    static std::string points2str(std::vector<point>& points)
-    {
-        std::string str;
-        for(auto&p : points)
-        {
-            char ch[64] = {0};
-            sprintf(ch, "x=%f,y=%f;", p.x, p.y);
-            str.append(ch);
-        }
-
-        return str;
-    }
+    static std::string _points2str(std::vector<point>& points);
 
 private:
     //area_id: points

+ 59 - 37
module_service/area_business_motionless_persion.cpp

@@ -14,76 +14,98 @@
 struct motionless_data:business_data
 {
     motionless_data()
-		:m_acc_0count(0)
-		,m_warn_flag(false)
+        :m_acc_0count(0)
+        ,m_is_warning(false)
     {
-//        m_acc_0count=0;
-//        m_acc_start_time=0;
+        //        m_acc_0count=0;
+        //        m_acc_start_time=0;
     }
 
+    ///;检测到多少次之后定位完全静止
+    //   std::atomic<int> m_acc_0count;
+    //    ///检测到多久之后告警
+    //    time_t m_acc_start_time;
 
-   ///;检测到多少次之后定位完全静止
- //   std::atomic<int> m_acc_0count;
-//    ///检测到多久之后告警
-//    time_t m_acc_start_time;
-
-	int m_acc_0count; //连续静止数量
-	bool m_warn_flag;  //告警推送标志
+    int m_acc_0count; //连续静止数量
+    bool m_is_warning;  //
 };
 
 void area_business_motionless_persion::init(config_file& config)
 {
-	//       sleep_ms = std::stoi(config.get("service.motionless_thread_sleep_ms","5000"));
-	_acc_0count_limit = std::stoi(config.get("service.motionless_acc_0count_limit","40"));
-	//        _acc_seconds_limit = std::stoi(config.get("service.motionless_acc_seconds_limit","120"));
+    //       sleep_ms = std::stoi(config.get("service.motionless_thread_sleep_ms","5000"));
+    _acc_0count_limit = std::stoi(config.get("service.motionless_acc_0count_limit","40"));
+    //        _acc_seconds_limit = std::stoi(config.get("service.motionless_acc_seconds_limit","120"));
 }
 
 void area_business_motionless_persion::on_enter(const std::shared_ptr<area_hover>&a,
-                                 const std::shared_ptr<card_location_base>&card_ptr,std::shared_ptr<business_data>&ptr)
+                                                const std::shared_ptr<card_location_base>&card_ptr,std::shared_ptr<business_data>&ptr)
 {
+    if(!card_ptr->is_person())
+    {
+        return;
+    }
+
     auto ptr_temp = std::make_shared<motionless_data>();
     ptr = ptr_temp;
+
+    auto ev_ptr_temp = event_list::instance()->get_event_card(card_ptr->m_id, card_ptr->m_type, ET_CARD_MOTIONLESS);
+    ptr_temp->m_is_warning = (nullptr != ev_ptr_temp && !ev_ptr_temp->is_end());
 }
 
 void area_business_motionless_persion::on_hover(const std::shared_ptr<area_hover>&area_hover_ptr,
-                                         const std::shared_ptr<card_location_base>&card_ptr,std::shared_ptr<business_data> ptr)
+                                                const std::shared_ptr<card_location_base>&card_ptr,std::shared_ptr<business_data> ptr)
 {
-    auto ptr_temp = static_cast<motionless_data*>(ptr.get());
+    if(!card_ptr->is_person())
+    {
+        return;
+    }
 
-	bool do_send=false;//是否需要发送
+    auto ptr_temp = static_cast<motionless_data*>(ptr.get());
 
     if(0 == static_cast<int>(card_ptr->m_acc))
     {
-        if((do_send = ++ptr_temp->m_acc_0count >= _acc_0count_limit && !ptr_temp->m_warn_flag))		//检测计数 && 是否已经发送,已经发送的话,就不发了
-		{
-			ptr_temp->m_warn_flag=true;	//如果确定发送的话,更新数据状态
-		}
+        ++ptr_temp->m_acc_0count;
     }
     else
     {
         ptr_temp->m_acc_0count=0;
-		if((do_send=ptr_temp->m_warn_flag))//如果正在告警,则取消告警
-		{
-			ptr_temp->m_warn_flag=false;
-		}
     }
 
-	if(do_send)//需要发送
-	{
-		uint64_t id = tool_other::type_id_to_u64(card_ptr->m_type, card_ptr->m_id);
-		event_tool::instance()->handle_event(OT_CARD, ET_CARD_MOTIONLESS, id, 0, 0, ptr_temp->m_warn_flag);
-	}
+    if(ptr_temp->m_acc_0count >= _acc_0count_limit)
+    {
+        ptr_temp->m_acc_0count = _acc_0count_limit;
+        if(!ptr_temp->m_is_warning)
+        {
+            ptr_temp->m_is_warning = true;
+            uint64_t id = tool_other::type_id_to_u64(card_ptr->m_type, card_ptr->m_id);
+            event_tool::instance()->handle_event(OT_CARD, ET_CARD_MOTIONLESS, id, 0, 0, true);
+        }
+    }
+    else
+    {
+        if(ptr_temp->m_is_warning)
+        {
+            ptr_temp->m_is_warning = false;
+            uint64_t id = tool_other::type_id_to_u64(card_ptr->m_type, card_ptr->m_id);
+            event_tool::instance()->handle_event(OT_CARD, ET_CARD_MOTIONLESS, id, 0, 0, false);
+        }
+    }
 }
 
 void area_business_motionless_persion::on_leave(const std::shared_ptr<area_hover>&area_hover_ptr,
-                                         const std::shared_ptr<card_location_base>&card_ptr,std::shared_ptr<business_data> ptr)
+                                                const std::shared_ptr<card_location_base>&card_ptr,std::shared_ptr<business_data> ptr)
 {
+    if(!card_ptr->is_person())
+    {
+        return;
+    }
+
     auto ptr_temp = static_cast<motionless_data*>(ptr.get());
-	if(ptr_temp->m_warn_flag)//如果正在告警,则发送取消告警的信息
-	{
-		uint64_t id = tool_other::type_id_to_u64(card_ptr->m_type, card_ptr->m_id);
-		event_tool::instance()->handle_event(OT_CARD, ET_CARD_MOTIONLESS, id, 0, 0, false);
-	}
+    if(ptr_temp->m_is_warning)//如果正在告警,则发送取消告警的信息
+    {
+        uint64_t id = tool_other::type_id_to_u64(card_ptr->m_type, card_ptr->m_id);
+        event_tool::instance()->handle_event(OT_CARD, ET_CARD_MOTIONLESS, id, 0, 0, false);
+    }
 }
 
 int area_business_motionless_persion::_acc_0count_limit=20;

+ 12 - 14
module_service/area_business_motionless_persion.h

@@ -26,17 +26,17 @@ struct config_file;
  */
 class area_business_motionless_persion : public area_business// : public i_thread, public singleton_base<module_motionless_persion>
 {
-//    void run()
-//    {
-//        auto cardlist = card_list::instance()->m_map;
-//        auto iter_m_map=cardlist.begin();
-//        for(;iter_m_map!=cardlist.end();++iter_m_map)
-//        {
-//            deal_alarm(iter_m_map->second);
-//        }
-//    }
-
-//    void deal_alarm(std::shared_ptr<card_location_base>& card_ptr);
+    //    void run()
+    //    {
+    //        auto cardlist = card_list::instance()->m_map;
+    //        auto iter_m_map=cardlist.begin();
+    //        for(;iter_m_map!=cardlist.end();++iter_m_map)
+    //        {
+    //            deal_alarm(iter_m_map->second);
+    //        }
+    //    }
+
+    //    void deal_alarm(std::shared_ptr<card_location_base>& card_ptr);
 private:
     static int _acc_0count_limit;
 
@@ -45,13 +45,11 @@ public:
     {
         return 10;
     }
-	static void init(config_file& config);
-
+    static void init(config_file& config);
 
     void on_enter(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data>&ptr);
     void on_hover(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data> ptr);
     void on_leave(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data> ptr);
-
 };
 
 #endif // MODULE_MOTIONLESS_PERSION_H

+ 111 - 72
module_service/area_business_person_attendance.cpp

@@ -32,7 +32,7 @@ area_business_person_attendance::area_business_person_attendance()
 
 //记录进入时间等信息,结束考勤,根据离开的时间和距离,判断是否记录一条新的考勤记录
 void area_business_person_attendance::on_enter(const std::shared_ptr<area_hover>&a,
-                                            const std::shared_ptr<card_location_base>&card_ptr,std::shared_ptr<business_data>&ptr)
+                                               const std::shared_ptr<card_location_base>&card_ptr,std::shared_ptr<business_data>&ptr)
 {
     if(!card_ptr->is_person())
     {
@@ -49,19 +49,19 @@ void area_business_person_attendance::on_enter(const std::shared_ptr<area_hover>
         //作为一条开始考勤记录保存到数据库
         db_tool::save_attendance(card_ptr);
 
-//        log_info("人卡考勤开始:卡id=%d,卡type=%d,分站id=%d,分站reader_type_id=%d,stat_attendance=%d",
-//                 card_ptr->m_id, card_ptr->m_type,
-//                 site_ptr->m_id,site_ptr->m_reader_type_id,mine_tool_ptr->m_is_attendance);
+        //        log_info("人卡考勤开始:卡id=%d,卡type=%d,分站id=%d,分站reader_type_id=%d,stat_attendance=%d",
+        //                 card_ptr->m_id, card_ptr->m_type,
+        //                 site_ptr->m_id,site_ptr->m_reader_type_id,mine_tool_ptr->m_is_attendance);
     }
 
-//    auto ptr_temp = std::make_shared<person_attendance_data>();
-//    ptr = ptr_temp;
+    //    auto ptr_temp = std::make_shared<person_attendance_data>();
+    //    ptr = ptr_temp;
 
-//    ptr_temp->m_is_attendance = true;
-//    ptr_temp->m_attendance_start_time = std::chrono::system_clock::now();
+    //    ptr_temp->m_is_attendance = true;
+    //    ptr_temp->m_attendance_start_time = std::chrono::system_clock::now();
 
-//    //作为一条开始考勤记录保存到数据库
-//    db_tool::save_attendance(card_ptr, ptr_temp->m_is_attendance, ptr_temp->m_attendance_start_time);
+    //    //作为一条开始考勤记录保存到数据库
+    //    db_tool::save_attendance(card_ptr, ptr_temp->m_is_attendance, ptr_temp->m_attendance_start_time);
 }
 
 void area_business_person_attendance::on_hover(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data> ptr)
@@ -70,36 +70,13 @@ void area_business_person_attendance::on_hover(const std::shared_ptr<area_hover>
 }
 //记录离开考勤区域信息,开始考勤
 void area_business_person_attendance::on_leave(const std::shared_ptr<area_hover>&a,
-                                            const std::shared_ptr<card_location_base>&card_ptr,std::shared_ptr<business_data> ptr)
+                                               const std::shared_ptr<card_location_base>&card_ptr,std::shared_ptr<business_data> ptr)
 {
     if(!card_ptr->is_person())
     {
         return;
     }
 
-    up_mine(card_ptr, false);
-
-//    if(nullptr == ptr)
-//    {
-//        log_error("结束考勤失败:nullptr == ptr");
-//        return;
-//    }
-
-//    auto ptr_temp = static_cast<person_attendance_data*>(ptr.get());
-
-//    if(ptr_temp->m_is_attendance)
-//    {
-//        ptr_temp->m_is_attendance = false;
-
-//        //作为一条开始考勤记录保存到数据库
-//        db_tool::save_attendance(card_ptr, ptr_temp->m_is_attendance, ptr_temp->m_attendance_start_time);
-//    }
-}
-
-///升井或收到web的删除卡命令  site_ptr==nullptr表示收到web的删除卡命令
-/// 保存考勤记录,发升井json,清理卡
-void area_business_person_attendance::up_mine(std::shared_ptr<card_location_base> card_ptr, bool is_web_delete)
-{
     auto mine_tool_ptr = card_ptr->get_mine_tool();
     if(!mine_tool_ptr->m_is_attendance)
     {
@@ -119,9 +96,9 @@ void area_business_person_attendance::up_mine(std::shared_ptr<card_location_base
     _to_json_card_up_one(card_ptr, datas, allocator);
     //module_meta_date_changed::clear_card(card_ptr);
 
-    auto rea_tool = card_ptr->get_area_tool();
-    rea_tool->on_leave(card_ptr);
-    card_ptr->clear();
+    //    auto rea_tool = card_ptr->get_area_tool();
+    //    rea_tool->on_leave(card_ptr);
+    //    card_ptr->clear();
 
     //升井json发给web
     if(datas.Size() > 0)
@@ -133,17 +110,23 @@ void area_business_person_attendance::up_mine(std::shared_ptr<card_location_base
         swsClientMgr.send(JSON_CMD_VALUE_PUSH, tool_json::doc_to_json(doc));
     }
 
-//    if(is_web_delete)
-//    {
-//        log_info("人卡考勤结束:卡id=%d,卡type=%d,分站id=%d,分站reader_type_id=%d,stat_attendance=%d",
-//                 card_ptr->m_id, card_ptr->m_type,
-//                 site_ptr->m_id,site_ptr->m_reader_type_id,mine_tool_ptr->m_is_attendance);
-//    }
-//    else
-//    {
-//        log_info("收到web的删除卡命令,人卡考勤结束:卡id=%d,卡type=%d, stat_attendance=%d",
-//                 card_ptr->m_id, card_ptr->m_type, mine_tool_ptr->m_is_attendance);
-//    }
+    //up_mine(card_ptr, false);
+
+    //    if(nullptr == ptr)
+    //    {
+    //        log_error("结束考勤失败:nullptr == ptr");
+    //        return;
+    //    }
+
+    //    auto ptr_temp = static_cast<person_attendance_data*>(ptr.get());
+
+    //    if(ptr_temp->m_is_attendance)
+    //    {
+    //        ptr_temp->m_is_attendance = false;
+
+    //        //作为一条开始考勤记录保存到数据库
+    //        db_tool::save_attendance(card_ptr, ptr_temp->m_is_attendance, ptr_temp->m_attendance_start_time);
+    //    }
 }
 
 /**
@@ -158,9 +141,9 @@ void area_business_person_attendance::handle_up_mine(sio::message::ptr const& da
         return;
     }
 
-    rapidjson::Document doc(rapidjson::kObjectType);
-    rapidjson::Value datas(rapidjson::kArrayType);
-    rapidjson::Document::AllocatorType& allocator=doc.GetAllocator();
+//    rapidjson::Document doc(rapidjson::kObjectType);
+//    rapidjson::Value datas(rapidjson::kArrayType);
+//    rapidjson::Document::AllocatorType& allocator=doc.GetAllocator();
 
     std::vector<sio::message::ptr>::const_iterator it_card = card_vec.begin();
     int type = 0;
@@ -168,7 +151,7 @@ void area_business_person_attendance::handle_up_mine(sio::message::ptr const& da
     for(; it_card != card_vec.end(); ++it_card)
     {
         if(!tool_map::try_get_value(s_card_id, JSON_KEY_CALL_CARD_CARD_ID, (*it_card))
-           ||!tool_map::try_get_value(type, JSON_KEY_CALL_CARD_CARD_TYPE_ID, (*it_card)))
+                ||!tool_map::try_get_value(type, JSON_KEY_CALL_CARD_CARD_TYPE_ID, (*it_card)))
         {
             log_error("手工升井,web发来的数据 card_id 或 card_type格式不对");
             continue;
@@ -181,22 +164,27 @@ void area_business_person_attendance::handle_up_mine(sio::message::ptr const& da
             std_debug("手工升井,处理,卡id=%d,卡type=%d", id, type);
             log_info("手工升井,处理,卡id=%d,卡type=%d", id, type);
 
-            auto mine_tool_ptr = card_ptr->get_mine_tool();
-            if(mine_tool_ptr->m_is_attendance)
-            {
-                //考勤结束
-                mine_tool_ptr->m_is_attendance=false;
+            module_meta_date_changed::clear_card(card_ptr);
+            //            auto rea_tool = card_ptr->get_area_tool();
+            //            rea_tool->on_leave(card_ptr);
+            //card_ptr->clear();
+
+            //            auto mine_tool_ptr = card_ptr->get_mine_tool();
+            //            if(mine_tool_ptr->m_is_attendance)
+            //            {
+            //                //考勤结束
+            //                mine_tool_ptr->m_is_attendance=false;
 
-                //作为一条结束考勤记录保存到数据库
-                db_tool::save_attendance(card_ptr);
-            }
+            //                //作为一条结束考勤记录保存到数据库
+            //                db_tool::save_attendance(card_ptr);
+            //            }
 
             //检查井下是否超员--是否需要取消
             //CMineCardManager::instance()->OnPersonUp(card_ptr);
 
-            auto rea_tool = card_ptr->get_area_tool();
-			rea_tool->on_leave(card_ptr);
-			card_ptr->clear();
+            //            auto rea_tool = card_ptr->get_area_tool();
+            //			rea_tool->on_leave(card_ptr);
+            //			card_ptr->clear();
         }
         else
         {
@@ -204,20 +192,20 @@ void area_business_person_attendance::handle_up_mine(sio::message::ptr const& da
         }
     }
 
-    //升井json发给web
-    if(datas.Size() > 0)
-    {
-        doc.AddMember(JSON_ROOT_KEY_CMD,JSON_CMD_VALUE_UP_MINE, allocator);
-        //doc.AddMember(JSON_ROOT_KEY_VERSION,INTERFACE_VERSION, allocator);
-        doc.AddMember(JSON_ROOT_KEY_DATA, datas, allocator);
+//    //升井json发给web
+//    if(datas.Size() > 0)
+//    {
+//        doc.AddMember(JSON_ROOT_KEY_CMD,JSON_CMD_VALUE_UP_MINE, allocator);
+//        //doc.AddMember(JSON_ROOT_KEY_VERSION,INTERFACE_VERSION, allocator);
+//        doc.AddMember(JSON_ROOT_KEY_DATA, datas, allocator);
 
-        swsClientMgr.send(JSON_CMD_VALUE_PUSH, tool_json::doc_to_json(doc));
-    }
+//        swsClientMgr.send(JSON_CMD_VALUE_PUSH, tool_json::doc_to_json(doc));
+//    }
 }
 
 ///升井json
 void area_business_person_attendance::_to_json_card_up_one(std::shared_ptr<card_location_base> card_ptr,
-                                   rapidjson::Value& out_datas, rapidjson::Document::AllocatorType& allocator)
+                                                           rapidjson::Value& out_datas, rapidjson::Document::AllocatorType& allocator)
 {
     if(!card_ptr->m_display)
     {
@@ -268,3 +256,54 @@ void area_business_person_attendance::_to_json_card_up_one(std::shared_ptr<card_
     out_datas.PushBack(data, allocator);
 }
 
+
+/////升井或收到web的删除卡命令  site_ptr==nullptr表示收到web的删除卡命令
+///// 保存考勤记录,发升井json,清理卡
+//void area_business_person_attendance::up_mine(std::shared_ptr<card_location_base> card_ptr, bool is_web_delete)
+//{
+//    auto mine_tool_ptr = card_ptr->get_mine_tool();
+//    if(!mine_tool_ptr->m_is_attendance)
+//    {
+//        return;
+//    }
+
+//    //考勤结束
+//    mine_tool_ptr->m_is_attendance=false;
+
+//    //作为一条结束考勤记录保存到数据库
+//    db_tool::save_attendance(card_ptr);
+
+//    rapidjson::Document doc(rapidjson::kObjectType);
+//    rapidjson::Value datas(rapidjson::kArrayType);
+//    rapidjson::Document::AllocatorType& allocator=doc.GetAllocator();
+
+//    _to_json_card_up_one(card_ptr, datas, allocator);
+//    //module_meta_date_changed::clear_card(card_ptr);
+
+//    auto rea_tool = card_ptr->get_area_tool();
+//    rea_tool->on_leave(card_ptr);
+//    card_ptr->clear();
+
+//    //升井json发给web
+//    if(datas.Size() > 0)
+//    {
+//        doc.AddMember(JSON_ROOT_KEY_CMD,JSON_CMD_VALUE_UP_MINE, allocator);
+//        //doc.AddMember(JSON_ROOT_KEY_VERSION,INTERFACE_VERSION, allocator);
+//        doc.AddMember(JSON_ROOT_KEY_DATA, datas, allocator);
+
+//        swsClientMgr.send(JSON_CMD_VALUE_PUSH, tool_json::doc_to_json(doc));
+//    }
+
+////    if(is_web_delete)
+////    {
+////        log_info("人卡考勤结束:卡id=%d,卡type=%d,分站id=%d,分站reader_type_id=%d,stat_attendance=%d",
+////                 card_ptr->m_id, card_ptr->m_type,
+////                 site_ptr->m_id,site_ptr->m_reader_type_id,mine_tool_ptr->m_is_attendance);
+////    }
+////    else
+////    {
+////        log_info("收到web的删除卡命令,人卡考勤结束:卡id=%d,卡type=%d, stat_attendance=%d",
+////                 card_ptr->m_id, card_ptr->m_type, mine_tool_ptr->m_is_attendance);
+////    }
+//}
+

+ 4 - 4
module_service/area_business_person_attendance.h

@@ -24,9 +24,9 @@ struct area_business_person_attendance:area_business
     //记录离开考勤区域信息,结束考勤
     void on_leave(const std::shared_ptr<area_hover>&a,const std::shared_ptr<card_location_base>&c,std::shared_ptr<business_data> ptr);
 
-    ///升井或收到web的删除卡命令  site_ptr==nullptr表示收到web的删除卡命令
-    /// 保存考勤记录,发升井json,清理卡
-    static void up_mine(std::shared_ptr<card_location_base> card_ptr, bool is_web_delete);
+    //    ///升井或收到web的删除卡命令  site_ptr==nullptr表示收到web的删除卡命令
+    //    /// 保存考勤记录,发升井json,清理卡
+    //    static void up_mine(std::shared_ptr<card_location_base> card_ptr, bool is_web_delete);
 
     /**
      * @brief 手工升井函数
@@ -36,7 +36,7 @@ struct area_business_person_attendance:area_business
 private:
     ///升井json
     static void _to_json_card_up_one(std::shared_ptr<card_location_base> card_ptr,
-                                       rapidjson::Value& out_datas, rapidjson::Document::AllocatorType& allocator);
+                                     rapidjson::Value& out_datas, rapidjson::Document::AllocatorType& allocator);
 };
 
 

+ 23 - 9
module_service/area_business_speed_checker.cpp

@@ -17,11 +17,13 @@ struct over_speed_data:business_data
         m_over_speed_count=0;
         m_normal_speed_count=0;
         m_limit_speed=0;
+        m_is_warning=false;
     }
 
     int m_over_speed_count;
     int m_normal_speed_count;
     double m_limit_speed;
+    bool m_is_warning;
 };
 area_business_speed_checker::area_business_speed_checker()
 {
@@ -54,6 +56,18 @@ void area_business_speed_checker::on_enter(const std::shared_ptr<area_hover>&are
     {
         ptr_temp->m_over_speed_count++;
     }
+    else
+    {
+        ptr_temp->m_normal_speed_count++;
+    }
+
+    EVENT_TYPE ev_type = ET_CARD_AREA_OVER_SPEED;
+    if(area_hover_ptr->m_area->is_mine())
+    {
+        ev_type = ET_CARD_OVER_SPEED;
+    }
+    auto ev_ptr_temp = event_list::instance()->get_event_card(card_ptr->m_id, card_ptr->m_type, ev_type);
+    ptr_temp->m_is_warning = (nullptr != ev_ptr_temp && !ev_ptr_temp->is_end());
 }
 
 void area_business_speed_checker::on_hover(const std::shared_ptr<area_hover>&area_hover_ptr,
@@ -74,10 +88,12 @@ void area_business_speed_checker::on_hover(const std::shared_ptr<area_hover>&are
     if(limit < card_ptr->m_speed)//超速
     {
         ptr_temp->m_over_speed_count++;
+        ptr_temp->m_normal_speed_count=0;
     }
     else//速度正常
     {
         ptr_temp->m_normal_speed_count++;
+        ptr_temp->m_over_speed_count=0;
     }
 
     EVENT_TYPE ev_type = ET_CARD_AREA_OVER_SPEED;
@@ -91,14 +107,13 @@ void area_business_speed_checker::on_hover(const std::shared_ptr<area_hover>&are
     {
         ptr_temp->m_over_speed_count=SPEED_COUNT_LIMIT;
 
-        auto ev_ptr_temp = event_list::instance()->get_event_card(card_ptr->m_id, card_ptr->m_type, ev_type);
-        if(!ev_ptr_temp)//从没有告警状态转化为告警状态
+        if(!ptr_temp->m_is_warning)
         {
-            ptr_temp->m_normal_speed_count=0;
-        }
+            ptr_temp->m_is_warning = true;
 
-        uint64_t id = tool_other::type_id_to_u64(card_ptr->m_type, card_ptr->m_id);
-        event_tool::instance()->handle_event(OT_CARD, ev_type, id, limit, card_ptr->m_speed, true);
+            uint64_t id = tool_other::type_id_to_u64(card_ptr->m_type, card_ptr->m_id);
+            event_tool::instance()->handle_event(OT_CARD, ev_type, id, limit, card_ptr->m_speed, true);
+        }
     }
 
     //确定正常
@@ -106,10 +121,9 @@ void area_business_speed_checker::on_hover(const std::shared_ptr<area_hover>&are
     {
         ptr_temp->m_normal_speed_count=SPEED_COUNT_LIMIT;
 
-        auto ev_ptr_temp = event_list::instance()->get_event_card(card_ptr->m_id, card_ptr->m_type, ev_type);
-        if(ev_ptr_temp && !ev_ptr_temp->is_end())
+        if(ptr_temp->m_is_warning)
         {
-            ptr_temp->m_over_speed_count=0;
+            ptr_temp->m_is_warning=false;
 
             uint64_t id = tool_other::type_id_to_u64(card_ptr->m_type, card_ptr->m_id);
             event_tool::instance()->handle_event(OT_CARD, ev_type, id, limit, card_ptr->m_speed, false);

+ 1 - 1
module_service/module_call.h

@@ -196,7 +196,7 @@ public:
      */
     void init(config_file& config)
     {
-        sleep_ms = std::stoi(config.get("service.send_call_interval","5000"));
+        sleep_ms = std::stoi(config.get("service.send_call_interval_ms","5000"));
     }
 
 private:

+ 37 - 38
module_service/module_meta_date_changed.cpp

@@ -9,6 +9,7 @@
 
 #include"ant.h"
 #include"card.h"
+#include"area.h"
 
 
 ///基础数据
@@ -103,34 +104,32 @@ void module_meta_date_changed::clear_card(std::shared_ptr<card_location_base> ca
 {
     auto rea_tool = card_ptr->get_area_tool();
     rea_tool->on_leave(card_ptr);
-    card_ptr->clear();
+    //card_ptr->clear();
 
-//    //删除
-//    //card_ptr->del_card_pos();
-//    // 升井 删除所有报警信息
-//    for(int i=0; i < CARD_EVENT_COUNT_MAX; i++ )
-//    {
-//        if (i == EVENT_TYPE::ET_CARD_LOW_POWER_SERIOUS)
-//        {
-//            continue;
-//        }
-
-//        uint64_t id = tool_other::type_id_to_u64(card_ptr->m_type, card_ptr->m_id);
-//        event_tool::instance()->handle_event(OT_CARD, static_cast<EVENT_TYPE>(i), id, 0, 0, false);
-
-////        auto ev_ptr = event_list::instance()->get_event_card(
-////                card_ptr->m_id, card_ptr->m_type, static_cast<EVENT_TYPE>(i));
-////        if(ev_ptr && !ev_ptr->is_end()) //
-////        {
-////            event_list::copy_event(card_ptr, ev_ptr);
+    //    //删除
+    //    //card_ptr->del_card_pos();
+    // 升井 删除所有报警信息
+    for(int i=0; i < CARD_EVENT_COUNT_MAX; i++ )
+    {
+        if (i == EVENT_TYPE::ET_CARD_LOW_POWER_SERIOUS)
+        {
+            continue;
+        }
 
-////            ev_ptr->m_status = ES_END;
+        uint64_t id = tool_other::type_id_to_u64(card_ptr->m_type, card_ptr->m_id);
+        event_tool::instance()->handle_event(OT_CARD, static_cast<EVENT_TYPE>(i), id, 0, 0, false);
 
-////            event_list::save_event(ev_ptr);
-////        }
-//    }
+        //        auto ev_ptr = event_list::instance()->get_event_card(
+        //                card_ptr->m_id, card_ptr->m_type, static_cast<EVENT_TYPE>(i));
+        //        if(ev_ptr && !ev_ptr->is_end()) //
+        //        {
+        //            event_list::copy_event(card_ptr, ev_ptr);
 
+        //            ev_ptr->m_status = ES_END;
 
+        //            event_list::save_event(ev_ptr);
+        //        }
+    }
 }
 
 
@@ -302,7 +301,7 @@ void module_meta_date_changed::deal_call_edit_lights_group(int id, EDIT_TYPE_ID
 
 void module_meta_date_changed::init_setting()
 {
-//pRes = getMysqlRes("select setting_id, name, type, value from dat_setting;");
+    //pRes = getMysqlRes("select setting_id, name, type, value from dat_setting;");
 }
 
 void module_meta_date_changed::remove_card(uint32_t id, int32_t type)
@@ -315,22 +314,22 @@ void module_meta_date_changed::remove_card(uint32_t id, int32_t type)
         return;
     }
 
-//    auto area_hover_ptr = card_ptr->get_area_hover();
-//    if(area_hover_ptr && 0!=area_hover_ptr->id() && 0!=area_hover_ptr->mapid())
-//    {
-//        module_area::on_leave(card_ptr->m_id, area_hover_ptr, card_ptr->m_type);
-//    }
+    //    auto area_hover_ptr = card_ptr->get_area_hover();
+    //    if(area_hover_ptr && 0!=area_hover_ptr->id() && 0!=area_hover_ptr->mapid())
+    //    {
+    //        module_area::on_leave(card_ptr->m_id, area_hover_ptr, card_ptr->m_type);
+    //    }
 
-//    if(card_ptr->is_person())
-//    {
-//        module_attendance_person::up_mine(card_ptr, nullptr);
-//    }
-//    else
-//    {
-//        module_attendance_vehicle::save_attendance(card_ptr, nullptr);
+    //    if(card_ptr->is_person())
+    //    {
+    //        module_attendance_person::up_mine(card_ptr, nullptr);
+    //    }
+    //    else
+    //    {
+    //        module_attendance_vehicle::save_attendance(card_ptr, nullptr);
 
-//        module_meta_date_changed::clear_card(card_ptr);
-//    }
+    //        module_meta_date_changed::clear_card(card_ptr);
+    //    }
 
     module_meta_date_changed::clear_card(card_ptr);
     // 避免状态重置

+ 3 - 2
module_service/module_meta_date_changed.h

@@ -8,8 +8,9 @@
 
 #include"module_singleton_base.h"
 #include"websocket/sio/sio_client.h"
-//#include"card.h"
-#include"area.h"
+
+struct card_location_base;
+struct area;
 
 
 enum EDIT_TYPE_ID

+ 1 - 0
module_service/module_mgr.cpp

@@ -11,6 +11,7 @@
 #include"area_business_geofault.h"
 #include"log.h"
 #include"common_tool.h"
+#include "config_file.h"
 
 /**
  * @brief 注册web回调函数,读配置文件,启动向web发送线程

+ 1 - 1
module_service/module_mgr.h

@@ -11,9 +11,9 @@
  */
 
 #include "module_singleton_base.h"
-#include "config_file.h"
 #include "websocket/wsClientMgr.h"
 #include "common.h"
+struct config_file;
 
 class module_mgr: public singleton_base<module_mgr>
 {

+ 1 - 1
module_service/module_web.h

@@ -45,7 +45,7 @@ public:
      */
     void init(config_file& config, std::map<std::string, YA::MSG_HANDLE_FUNC_TYPE>& MsgFuncList)
     {
-        sleep_ms = std::stoi(config.get("service.interval_send_json_alarm","10000"));
+        sleep_ms = std::stoi(config.get("service.interval_send_json_alarm_ms","10000"));
 
         MsgFuncList.insert( std::make_pair( JSON_CMD_VALUE_META_DATA_CHANGED, &module_web::accept ) );
         MsgFuncList.insert( std::make_pair( JSON_CMD_VALUE_REQUEST_ALL_DATA, &module_web::accept ) );