#ifndef RECIPE_MANAGER_H #define RECIPE_MANAGER_H #include #include #include #include #include "Database.h" #include "CParam.h" // 单个设备配方映射信息 struct DeviceRecipe { int nDeviceID; // 设备ID int nRecipeID; // 子配方ID std::string strRecipeName; // 设备名称 std::string strDeviceName; // 子配方名称 std::vector paramsRawData; // 原始参数数据 std::vector m_params; // 参数对象列表 }; // 配方信息 struct RecipeInfo { std::string strPPID; // 配方ID std::string strDescription; // 配方描述 std::string strCreateTime; // 创建时间 std::vector vecDeviceList; // 关联的设备信息列表 }; using RecipeMap = std::unordered_map; // 按 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 getAllRecipes(); // 根据 PPID 或描述查询配方 std::vector getRecipesByKeyword(const std::string& keyword); // 获取所有 PPID std::vector 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& strDeviceName, int nID, const std::string& strName, const std::string& strPara); bool updateDeviceRecipeName(const std::string& strDeviceName, int nID, const std::string& strNewName); bool updateDeviceRecipePara(const std::string& strDeviceName, int nID, const std::string& strNewPara); std::string getDeviceRecipeName(const std::string& strDeviceName, int nID); std::string getDeviceRecipePara(const std::string& strDeviceName, int nID); bool deleteDeviceRecipe(const std::string& strDeviceName, int nID); std::vector> getDeviceRecipes(const std::string& strDeviceName); // 模拟插入数据(测试用) 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