From 684b55231257011c11caacc5ae72b0f0977c6114 Mon Sep 17 00:00:00 2001
From: chenshijun <chenshijun@aiotlink.com>
Date: 星期五, 15 三月 2019 15:14:22 +0800
Subject: [PATCH] 整合代码

---
 QiaoJiaSystem/DataManagerServer/vss/dao/CamDevSqliteDao.h |  294 ++++++++++++++++++++++++++++++++++++----------------------
 1 files changed, 184 insertions(+), 110 deletions(-)

diff --git a/QiaoJiaSystem/DataManagerServer/vss/dao/CamDevSqliteDao.h b/QiaoJiaSystem/DataManagerServer/vss/dao/CamDevSqliteDao.h
index 8657c45..93aba70 100755
--- a/QiaoJiaSystem/DataManagerServer/vss/dao/CamDevSqliteDao.h
+++ b/QiaoJiaSystem/DataManagerServer/vss/dao/CamDevSqliteDao.h
@@ -4,147 +4,221 @@
 
 #include "../model/CamDev.h"
 #include "BaseSqliteDao.h"
+#include <qsqlfield.h>
 
-#define CamDev_TABLE_NAME "CamDev"
+#define CamDev_TABLE_NAME "cam_dev"
 
 /**
  * 鎽勫儚澶磋澶囩鐞嗙被
  */
 class CamDevSqliteDao : public BaseSqliteDao {
 private:
-	/** 鎽勫儚澶磋澶囨瀯閫犲嚱鏁� */
-	CamDevSqliteDao() {}
+    /** 鎽勫儚澶磋澶囨瀯閫犲嚱鏁� */
+    CamDevSqliteDao() {}
 public:
-	/** 鎽勫儚澶磋澶囧崟渚嬫ā寮� */
+    /** 鎽勫儚澶磋澶囧崟渚嬫ā寮� */
     static CamDevSqliteDao* instance() {
         static CamDevSqliteDao instance;
         return &instance;
     }
-    
+
     /** 娣诲姞鎽勫儚澶磋澶� keyValuesToAdd 闇�瑕佹坊鍔犵殑鍒楀悕鍜屽垪鍊煎map*/
-    mysqlpp::SimpleResult addCamDev(std::map<std::string, std::string>& keyValuesToAdd) {
-        return add(keyValuesToAdd, CamDev_TABLE_NAME);
+    Json::Value addCamDev(std::map<std::string, std::string>& keyValuesToAdd) {
+        Json::Value responseJsonValue;
+        responseJsonValue["message"] = "娣诲姞澶辫触锛�";
+        responseJsonValue["success"] = "false";
+
+        std::map<std::string, std::string> whereKeyValues;
+        whereKeyValues["cam_dev_id"] = keyValuesToAdd["cam_dev_id"];
+        if (this->findCamDevList(whereKeyValues)["data"].size() > 0) {
+            responseJsonValue["message"] = "cam_dev_id exist锛�";
+            return responseJsonValue;
+        }
+
+        QMutexLocker mutexLocker(m_mutexVisit);//TODO
+        QSqlTableModel pModel(NULL, *m_db);
+
+        pModel.setTable(CamDev_TABLE_NAME);
+        pModel.setEditStrategy(QSqlTableModel::OnManualSubmit);
+
+        QSqlRecord rec = pModel.record();
+        for (auto keyValueToAdd : keyValuesToAdd) {
+            rec.setValue(QString::fromUtf8(keyValueToAdd.first.c_str()), QString::fromUtf8(keyValueToAdd.second.c_str()));
+        }
+        rec.setGenerated("id", false);
+
+        pModel.insertRecord(-1, rec);//TODO
+        m_db->transaction();//寮�濮嬩簨鍔℃搷浣�
+        auto id1 = pModel.query().lastInsertId();
+        if (pModel.submitAll()) {
+            m_db->commit();//鎻愪氦
+            responseJsonValue["id"] = pModel.query().lastInsertId().toString().toStdString();
+            responseJsonValue["success"] = "true";
+            responseJsonValue["message"] = "娣诲姞鎴愬姛锛�";
+        } else {
+            m_db->rollback();//鍥炴粴
+            ERR("addCamDev err ,Database Error: " << pModel.lastError().text().toStdString());
+        }
+        return responseJsonValue;
     }
-    
-    /** 鍒犻櫎鎽勫儚澶磋澶� whereKey 鍒楀悕锛泈hereValue 鍒楀�� */
-    bool deleteByColumn(string whereKey, string whereValue) {
+
+    /** 鍒犻櫎鏈湴鍥芥爣閰嶇疆 whereKey 鍒楀悕锛泈hereValue 鍒楀�� */
+    Json::Value deleteByColumn(string whereKey, string whereValue) {
+
+
         std::map<std::string, std::string> whereKeyValues;
         whereKeyValues[whereKey] = whereValue;
         return deleteCamDev(whereKeyValues);
     }
 
-    /** 鍒犻櫎鎽勫儚澶磋澶� whereColumnNameValues 鍒楀悕鍜屽垪鍊煎鏉′欢 */
-    bool deleteCamDev(std::map<std::string, std::string>& whereKeyValues) {
-        return del(CamDev_TABLE_NAME, whereKeyValues);
+    /** 鍒犻櫎鎽勫儚澶磋澶� whereKey 鍒楀悕锛泈hereValue 鍒楀�� */
+    Json::Value deleteCamDev(std::map<std::string, std::string>& whereKeyValues) {
+
+        Json::Value responseJsonValue;
+        responseJsonValue["message"] = "鍒犻櫎澶辫触锛�";
+        responseJsonValue["success"] = "false";
+        QMutexLocker mutexLocker(m_mutexVisit);//TODO
+        QSqlTableModel pModel(NULL, *m_db);
+        pModel.setTable(CamDev_TABLE_NAME);
+        if (whereKeyValues.size() > 0) {
+            auto filter = QObject::tr((string(" 1=1 ")+getWhereColumnNameValuePair(whereKeyValues)).c_str());
+            pModel.setFilter(filter);
+        }
+        pModel.setEditStrategy(QSqlTableModel::OnManualSubmit);//OnManualSubmit OnFieldChange
+        pModel.select();
+
+        int rowCount = pModel.rowCount();
+
+        if (rowCount >= 1) {
+            for (int i = 0; i < rowCount; i++) {
+                pModel.removeRow(i);//TODO
+            }
+        } else {
+            return responseJsonValue;
+        }
+
+        m_db->transaction();//寮�濮嬩簨鍔℃搷浣�
+
+        if (pModel.submitAll()) {
+            m_db->commit();//鎻愪氦
+            responseJsonValue["success"] = "true";
+            responseJsonValue["message"] = "鍒犻櫎鎴愬姛锛�";
+        } else {
+            m_db->rollback();//鍥炴粴
+            ERR("deleteLDeviceTable ,pModel_load Error: " << pModel.lastError().text().toStdString());
+        }
+
+        return responseJsonValue;
     }
-    
+
+    /** 鏇存柊鏈湴鍥芥爣閰嶇疆 keyValuesToUpdate 闇�瑕佹洿鏂扮殑鍒楀悕鍜屽垪鍊煎锛� whereKeyValues 鍒楀悕鍜屽垪鍊兼潯浠� */
+    Json::Value updateCamDev(std::map<std::string, std::string>& keyValuesToUpdate,
+                             std::map<std::string, std::string>& whereKeyValues) {
+        Json::Value responseJsonValue;
+        responseJsonValue["message"] = "鏇存柊澶辫触锛�";
+        responseJsonValue["success"] = "false";
+        QMutexLocker mutexLocker(m_mutexVisit);//TODO
+        QSqlTableModel pModel(NULL, *m_db);
+        pModel.setTable(CamDev_TABLE_NAME);
+        if (whereKeyValues.size() > 0) {
+            auto filter = QObject::tr((string(" 1=1 ")+getWhereColumnNameValuePair(whereKeyValues)).c_str());
+            pModel.setFilter(filter);
+        }
+        pModel.setEditStrategy(QSqlTableModel::OnManualSubmit);//OnManualSubmit OnFieldChange
+        pModel.select();
+
+        /*QSqlRecord rec =  pModel.record();
+        rec.setValue("dst_path",QString::fromStdString(dst_path));
+        rec.setValue("total",total);
+        rec.setValue("finish_flag",1);
+        rec.setValue("update_time",QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));*/
+
+        int rowCount = pModel.rowCount();
+        if (rowCount > 0) {
+            QSqlRecord rec = pModel.record(0);
+            for (auto keyValueToUpdate : keyValuesToUpdate) {
+                rec.setValue(keyValueToUpdate.first.c_str(), keyValueToUpdate.second.c_str());
+            }
+//            rec.setValue("dst_path", QString::fromStdString(dst_path));
+//            rec.setValue("total", total);
+//            rec.setValue("finish_flag", 1);
+            rec.setValue("UpdateTime", QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"));
+
+            pModel.setRecord(0, rec);
+        } else {
+            return responseJsonValue;
+        }
+
+        m_db->transaction();//寮�濮嬩簨鍔℃搷浣�
+
+        if (pModel.submitAll()) {
+            m_db->commit();//鎻愪氦
+            responseJsonValue["success"] = "true";
+            responseJsonValue["message"] = "鏇存柊鎴愬姛锛�";
+        } else {
+            m_db->rollback();//鍥炴粴
+            ERR("updateLDeviceTable ,pModel_load Error: " << pModel.lastError().text().toStdString());
+        }
+        return responseJsonValue;
+    }
+
     /** 鏇存柊鎽勫儚澶磋澶� keyValuesToUpdate 闇�瑕佹洿鏂扮殑鍒楀悕鍜屽垪鍊煎锛� whereKeyValues 鍒楀悕鍜屽垪鍊兼潯浠� */
-	bool updateCamDev(std::map<std::string, std::string>& keyValuesToUpdate, 
-									std::map<std::string, std::string>& whereKeyValues) {
-		return update(keyValuesToUpdate, CamDev_TABLE_NAME, whereKeyValues);
-	}
-	
-    /** 鏇存柊鍒涘缓浜岀骇璁惧琛� keyValuesToUpdate 闇�瑕佹洿鏂扮殑鍒楀悕鍜屽垪鍊煎锛� whereKeyValues 鍒楀悕鍜屽垪鍊兼潯浠� */
-    bool updateCamDev(std::map<std::string, std::string>& keyValuesToUpdate,
+    Json::Value updateCamDev(std::map<std::string, std::string>& keyValuesToUpdate,
                              string whereKey,
                              string whereValue) {
         std::map<std::string, std::string> whereKeyValues;
         whereKeyValues[whereKey] = whereValue;
-        return update(keyValuesToUpdate, CamDev_TABLE_NAME, whereKeyValues);
+        return updateCamDev(keyValuesToUpdate, whereKeyValues);
     }
-	
-	/** 鏌ヨ鎽勫儚澶磋澶囧垪琛�  querySql 瑕佹煡璇㈢殑sql璇彞 */
-	vector<CamDev> findCamDevList(string querySql) {
-        vector<CamDev> camDevVec;
-        vector<map<string, string>> rowDatList = findList(querySql);
-        camDevVec.reserve(rowDatList.size());
-        for (auto rowData : rowDatList) {
-            camDevVec.emplace_back(mapToModel(rowData));
-        }
-        return camDevVec;
-	}
-	
+
     /**鏌ヨ鎽勫儚澶磋澶囧垪琛� whereKeyValues 鍒楀悕鍜屽垪鍊煎鏉′欢 */
-    vector<CamDev> findCamDevList(std::map<std::string, std::string>& whereKeyValues) {
-        return findCamDevList(string("select * from ") + CamDev_TABLE_NAME + " where 1=1 " + getWhereColumnNameValuePair(whereKeyValues));
+    Json::Value findCamDevList(std::map<std::string, std::string>& whereKeyValues) {
+
+        Json::Value responseJsonValue;
+        responseJsonValue["message"] = "鏌ヨ澶辫触锛�";
+        responseJsonValue["success"] = "false";
+        responseJsonValue["data"] = Json::arrayValue;
+
+        QMutexLocker mutexLocker(m_mutexVisit);//TODO
+        std::list<Record_Cut_Video_info> lst;
+        QSqlTableModel pModel(NULL, *m_db);
+        pModel.setTable(CamDev_TABLE_NAME);;
+        if (whereKeyValues.size() > 0) {
+            auto filter = QObject::tr((string(" 1=1 ")+getWhereColumnNameValuePair(whereKeyValues)).c_str());
+            pModel.setFilter(filter);
+        }
+        pModel.setEditStrategy(QSqlTableModel::OnManualSubmit);//OnManualSubmit OnFieldChange
+        pModel.select();
+
+        int rowCount = pModel.rowCount();
+        if (rowCount > 0) {
+            Json::Value rowDataList = Json::arrayValue;
+            for (int i = 0; i < rowCount; ++i) {
+                Json::Value rowData = Json::objectValue;
+                Record_Cut_Video_info lRec;
+                QSqlRecord rec = pModel.record(i);
+                for (int i = 0; i < rec.count(); i++) {
+                    QSqlField field = rec.field(i);
+                    rowData[field.name().toStdString()] = field.value().toString().toStdString();
+                }
+                rowDataList.append(rowData);
+            }
+            responseJsonValue["data"] = rowDataList;
+        }
+
+        responseJsonValue["message"] = "鏌ヨ鎴愬姛锛�";
+        responseJsonValue["success"] = "true";
+        return responseJsonValue;
     }
-    
-    /**鏌ヨ鎵�鏈夋憚鍍忓ご璁惧鍒楄〃 */
-    vector<CamDev> findAllCamDevList() {
-        return findCamDevList(string("select * from ") + CamDev_TABLE_NAME + " where 1=1 ");
+
+    /**鏌ヨ鎵�鏈夋湰鍦板浗鏍囬厤缃垪琛� */
+    Json::Value findAllCamDevList() {
+        std::map<std::string, std::string> whereKeyValues;
+        return findCamDevList(whereKeyValues);
     }
-	
-	/** 鏌ヨmap鍒楄〃 querySql 瑕佹煡璇㈢殑sql璇彞 */
-	vector<map<string, string>> findMapList(string querySql) {
-		return findList(querySql);
-	}
-    
-    /** 鎵цsql璇彞 */
-    bool execute(string sql) {
-        return exec(move(sql));
-    }
-    
-    /** map杞琺odel绫� */
-    CamDev mapToModel(map<string, string>& rowData) {
-        CamDev camDev;
-        string idValue = rowData[CamDev_id];
-        if (idValue.length() != 0 && idValue != "NULL") {
-            camDev.id = std::stoi(idValue);
-        }
-        string cam_dev_idValue = rowData[CamDev_cam_dev_id];
-        if (cam_dev_idValue.length() != 0 && cam_dev_idValue != "NULL") {
-            camDev.cam_dev_id = cam_dev_idValue;
-        }
-        string nameValue = rowData[CamDev_name];
-        if (nameValue.length() != 0 && nameValue != "NULL") {
-            camDev.name = nameValue;
-        }
-        string addrValue = rowData[CamDev_addr];
-        if (addrValue.length() != 0 && addrValue != "NULL") {
-            camDev.addr = addrValue;
-        }
-        string longitudeValue = rowData[CamDev_longitude];
-        if (longitudeValue.length() != 0 && longitudeValue != "NULL") {
-            camDev.longitude = longitudeValue;
-        }
-        string latitudeValue = rowData[CamDev_latitude];
-        if (latitudeValue.length() != 0 && latitudeValue != "NULL") {
-            camDev.latitude = latitudeValue;
-        }
-        string ipValue = rowData[CamDev_ip];
-        if (ipValue.length() != 0 && ipValue != "NULL") {
-            camDev.ip = ipValue;
-        }
-        string portValue = rowData[CamDev_port];
-        if (portValue.length() != 0 && portValue != "NULL") {
-            camDev.port = std::stoi(portValue);
-        }
-        string usernameValue = rowData[CamDev_username];
-        if (usernameValue.length() != 0 && usernameValue != "NULL") {
-            camDev.username = usernameValue;
-        }
-        string passwordValue = rowData[CamDev_password];
-        if (passwordValue.length() != 0 && passwordValue != "NULL") {
-            camDev.password = passwordValue;
-        }
-        string brandValue = rowData[CamDev_brand];
-        if (brandValue.length() != 0 && brandValue != "NULL") {
-            camDev.brand = brandValue;
-        }
-        string reservedValue = rowData[CamDev_reserved];
-        if (reservedValue.length() != 0 && reservedValue != "NULL") {
-            camDev.reserved = reservedValue;
-        }
-        string typeValue = rowData[CamDev_type];
-        if (typeValue.length() != 0 && typeValue != "NULL") {
-            camDev.type = typeValue;
-        }
-        return camDev;
-    }
-   
+
 };
 
 
 #endif //CAMDEV_MANAGER_H
-
 

--
Gitblit v1.8.0