xuxiuxi
2019-03-04 845c62691a5b19faf67775f712f294fa0d1789f6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
 
#ifndef CAMDEV_MANAGER_H
#define CAMDEV_MANAGER_H
 
#include "../model/CamDev.h"
#include "BaseSqliteDao.h"
 
#define CamDev_TABLE_NAME "CamDev"
 
/**
 * 摄像头设备管理类
 */
class CamDevSqliteDao : public BaseSqliteDao {
private:
    /** 摄像头设备构造函数 */
    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);
    }
    
    /** 删除摄像头设备 whereKey 列名;whereValue 列值 */
    bool 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);
    }
    
    /** 更新摄像头设备 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,
                             string whereKey,
                             string whereValue) {
        std::map<std::string, std::string> whereKeyValues;
        whereKeyValues[whereKey] = whereValue;
        return update(keyValuesToUpdate, CamDev_TABLE_NAME, 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));
    }
    
    /**查询所有摄像头设备列表 */
    vector<CamDev> findAllCamDevList() {
        return findCamDevList(string("select * from ") + CamDev_TABLE_NAME + " where 1=1 ");
    }
    
    /** 查询map列表 querySql 要查询的sql语句 */
    vector<map<string, string>> findMapList(string querySql) {
        return findList(querySql);
    }
    
    /** 执行sql语句 */
    bool execute(string sql) {
        return exec(move(sql));
    }
    
    /** map转model类 */
    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