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
#ifndef RECIPE_MANAGER_H
#define RECIPE_MANAGER_H
 
#include <string>
#include <vector>
#include <mutex>
#include <unordered_map>
#include "Database.h"
#include "CParam.h"
 
 
// µ¥¸öÉ豸Åä·½Ó³ÉäÐÅÏ¢
struct DeviceRecipe {
    int nDeviceID;               // É豸ID
    int nRecipeID;               // ×ÓÅä·½ID
    std::string strRecipeName;   // ×ÓÅä·½Ãû³Æ
    std::string strDeviceName;   // É豸Ãû³Æ 
};
 
// Åä·½ÐÅÏ¢
struct RecipeInfo {
    std::string strPPID;         // Åä·½ID
    std::string strDescription;  // Åä·½ÃèÊö
    std::string strCreateTime;   // ´´½¨Ê±¼ä
    std::vector<DeviceRecipe> vecDeviceList;  // ¹ØÁªµÄÉ豸ÐÅÏ¢Áбí
    std::vector<CParam*> m_params;            // ³öվʱ¼Ç¼²ÎÊý
    void RecipeInfo::addIntParam(const char* pszName, const char* pszId, const char* pszUnit, int value)
    {
        CParam* pParam = new CParam(pszName, pszId, pszUnit, value);
        m_params.push_back(pParam);
    }
 
    void RecipeInfo::addDoubleParam(const char* pszName, const char* pszId, const char* pszUnit, double value)
    {
        CParam* pParam = new CParam(pszName, pszId, pszUnit, value);
        m_params.push_back(pParam);
    }
 
    std::vector<CParam*>& RecipeInfo::getParams()
    {
        return m_params;
    }
};
 
using RecipeMap = std::unordered_map<std::string, RecipeInfo>; // °´ PPID Ó³ÉäµÄÅä·½±í
 
class RecipeManager {
public:
    // »ñÈ¡µ¥Àý
    static RecipeManager& getInstance();
 
    // ³õʼ»¯Åä·½Êý¾Ý¿â
    bool initRecipeTable();
 
    // Ïú»Ù±í»ò¹Ø±ÕÁ¬½Ó
    void termRecipeTable();
    bool destroyRecipeTable();
 
    // ¼ì²é PPID ÊÇ·ñ´æÔÚ
    bool ppidExists(const std::string& ppid);
 
    // ¼ì²éÉ豸ÊÇ·ñ´æÔÚÓÚÖ¸¶¨ PPID µÄÅä·½ÖÐ
    bool deviceExists(const std::string& ppid, int nDeviceID);
 
    // Ìí¼ÓÒ»¸öÅä·½¼°ÆäÉ豸ӳÉä
    bool addRecipe(const RecipeInfo& recipe);
 
    // Ìí¼ÓÉ豸µ½Ö¸¶¨Åä·½
    bool addRecipeDevice(const std::string& ppid, const DeviceRecipe& device);
 
    // É¾³ýÖ¸¶¨ PPID µÄÉ豸Åä·½
    bool deleteRecipeDeviceByID(const std::string& ppid, int nDeviceID);
 
    // É¾³ýÖ¸¶¨ PPID µÄÉ豸Åä·½£¨Í¨¹ýÉ豸Ãû³Æ£©
    bool deleteRecipeDeviceByName(const std::string& ppid, const std::string& strDeviceName);
 
    // ²éѯËùÓÐÅä·½
    std::vector<RecipeInfo> getAllRecipes();
 
    // ¸ù¾Ý PPID »òÃèÊö²éѯÅä·½
    std::vector<RecipeInfo> getRecipesByKeyword(const std::string& keyword);
 
    // »ñÈ¡ËùÓРPPID
    std::vector<std::string> getAllPPID() const;
 
    // °´ PPID ²éѯÅä·½
    RecipeInfo getRecipeByPPID(const std::string& ppid);
 
    // ¸ù¾Ý PPID ºÍÉ豸ID »ñÈ¡É豸Åä·½ID
    int getDeviceRecipeIDByID(const std::string& ppid, int nDeviceID);
 
    // ¸ù¾Ý PPID ºÍÉ豸Ãû³Æ »ñÈ¡É豸Åä·½ID
    int getDeviceRecipeIDByName(const std::string& ppid, const std::string& strDeviceName);
 
    // É¾³ýÖ¸¶¨ PPID µÄÅä·½
    bool deleteRecipeByPPID(const std::string& ppid);
 
    // ¸üÐÂÖ¸¶¨ PPID µÄÅä·½
    bool updateRecipe(const RecipeInfo& recipe);
 
    // ¸üРPPID£¨Í¨¹ý¾É PPID ºÍРPPID£©
    bool updatePPID(const std::string& oldPPID, const std::string& newPPID);
 
    // ¸üÐÂÅä·½ÃèÊö£¨Í¨¹ý PPID£©
    bool updateDescription(const std::string& ppid, const std::string& newDescription);
 
    // ¸üÐÂÉ豸Åä·½ID£¨Í¨¹ý PPID ºÍÉ豸ID£©
    bool updateDeviceRecipeIDByID(const std::string& ppid, int nDeviceID, int nNewRecipeID);
 
    // ¸üÐÂÉ豸Åä·½ID£¨Í¨¹ý PPID ºÍÉ豸Ãû³Æ£©
    bool updateDeviceRecipeIDByName(const std::string& ppid, const std::string& strDeviceName, int nNewRecipeID);
 
    // Ä£Äâ²åÈëÊý¾Ý£¨²âÊÔÓã©
    void insertMockData();
 
    // ¶ÁÈ¡Åä·½Îļþ£¨CSV »ò JSON£©
    bool readRecipeFile(const std::string& filename);
 
    // ±£´æÅä·½µ½Îļþ
    bool saveRecipeFile(const std::string& filename);
 
private:
    RecipeManager();
    ~RecipeManager();
 
    RecipeManager(const RecipeManager&) = delete;
    RecipeManager& operator=(const RecipeManager&) = delete;
 
private:
    BL::Database* m_pDB;
    static std::recursive_mutex m_mutex;
};
 
#endif // RECIPE_MANAGER_H