mrDarker
2025-09-04 f5d3efdcec2d59e4adf6a3ae534082023e2df345
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
#ifndef RECIPE_MANAGER_H
#define RECIPE_MANAGER_H
 
#include <string>
#include <vector>
#include <mutex>
#include <unordered_map>
#include "Database.h"
 
// 单个设备配方映射信息
struct DeviceRecipe {
    int nDeviceID;               // 设备ID
    int nRecipeID;               // 子配方ID
    std::string strDeviceName;   // 设备名称 
    std::string strRecipeName;   // 子配方名称
};
 
// 配方信息
struct RecipeInfo {
    std::string strPPID;         // 配方ID
    std::string strDescription;  // 配方描述
    std::string strCreateTime;   // 创建时间
    std::vector<DeviceRecipe> vecDeviceList;  // 关联的设备信息列表
};
 
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;
 
    // 按 ID 查询 PPID
    std::string getPPIDById(int nId);
 
    // 按 PPID 查询 ID
    int getIdByPPID(const std::string& ppid);
 
    // 按 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);
 
    bool addDeviceRecipe(const std::string& deviceName, int nRecipeID, const std::string& strRecipeName);
    bool updateDeviceRecipe(const std::string& deviceName, int nRecipeID, const std::string& newName);
    std::string getDeviceRecipeName(const std::string& deviceName, int nRecipeID);
    bool deleteDeviceRecipe(const std::string& deviceName, int nRecipeID);
    std::vector<std::pair<int, std::string>> getDeviceRecipes(const std::string& deviceName);
 
    // 模拟插入数据(测试用)
    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