mrDarker
6 天以前 829fe6c6bc33d53fda9c31fd45a37e1df87befff
SourceCode/Bond/Servo/RecipeManager.h
@@ -1,4 +1,4 @@
#ifndef RECIPE_MANAGER_H
#ifndef RECIPE_MANAGER_H
#define RECIPE_MANAGER_H
#include <string>
@@ -6,110 +6,128 @@
#include <mutex>
#include <unordered_map>
#include "Database.h"
#include "CParam.h"
// 单个设备配方映射信息
// 单个设备配方映射信息
struct DeviceRecipe {
    int nDeviceID;               // 设备ID
    int nRecipeID;               // 该设备对应的子配方ID
    std::string strPPID;         // 配方ID(主键)
    std::string strDeviceName;   // 设备名称
   int nDeviceID;                     // 设备ID
   int nRecipeID;                     // 子配方ID
   std::string strRecipeName;            // 设备名称
   std::string strDeviceName;            // 子配方名称
   std::vector<uint8_t> paramsRawData;     // 原始参数数据
   std::vector<CParam*> m_params;         // 参数对象列表
};
// 配方信息
// 配方信息
struct RecipeInfo {
    std::string strPPID;         // 配方ID
    std::string strDescription;  // 配方描述
    std::string strCreateTime;   // 创建时间
    std::vector<DeviceRecipe> vecDeviceList;  // 关联的设备信息列表
   std::string strPPID;         // 配方ID
   std::string strDescription;  // 配方描述
   std::string strCreateTime;   // 创建时间
   std::vector<DeviceRecipe> vecDeviceList;  // 关联的设备信息列表
};
using RecipeMap = std::unordered_map<std::string, RecipeInfo>; // 按 PPID 映射的配方表
using RecipeMap = std::unordered_map<std::string, RecipeInfo>; // 按 PPID 映射的配方表
class RecipeManager {
public:
    // 获取单例
    static RecipeManager& getInstance();
   // 获取单例
   static RecipeManager& getInstance();
    // 初始化配方数据库
    bool initRecipeTable();
   // 初始化配方数据库
   bool initRecipeTable();
    // 销毁表或关闭连接
    void termRecipeTable();
    bool destroyRecipeTable();
   // 销毁表或关闭连接
   void termRecipeTable();
   bool destroyRecipeTable();
   // 检查 PPID 是否存在
    bool ppidExists(const std::string& ppid);
   // 检查 PPID 是否存在
   bool ppidExists(const std::string& ppid);
   // 检查设备是否存在于指定 PPID 的配方中
   // 检查设备是否存在于指定 PPID 的配方中
   bool deviceExists(const std::string& ppid, int nDeviceID);
    // 添加一个配方及其设备映射
    bool addRecipe(const RecipeInfo& recipe);
   // 添加一个配方及其设备映射
   bool addRecipe(const RecipeInfo& recipe);
   // 添加设备到指定配方
    bool addRecipeDevice(const std::string& ppid, const DeviceRecipe& device);
   // 添加设备到指定配方
   bool addRecipeDevice(const std::string& ppid, const DeviceRecipe& device);
   // 删除指定 PPID 的设备配方
    bool deleteRecipeDeviceByID(const std::string& ppid, int nDeviceID);
   // 删除指定 PPID 的设备配方
   bool deleteRecipeDeviceByID(const std::string& ppid, int nDeviceID);
   // 删除指定 PPID 的设备配方(通过设备名称)
    bool deleteRecipeDeviceByName(const std::string& ppid, const std::string& strDeviceName);
   // 删除指定 PPID 的设备配方(通过设备名称)
   bool deleteRecipeDeviceByName(const std::string& ppid, const std::string& strDeviceName);
    // 查询所有配方
    std::vector<RecipeInfo> getAllRecipes();
   // 查询所有配方
   std::vector<RecipeInfo> getAllRecipes();
   // 根据 PPID 或描述查询配方
    std::vector<RecipeInfo> getRecipesByKeyword(const std::string& keyword);
   // 根据 PPID 或描述查询配方
   std::vector<RecipeInfo> getRecipesByKeyword(const std::string& keyword);
   // 获取所有 PPID
    std::vector<std::string> getAllPPID() const;
   // 获取所有 PPID
   std::vector<std::string> getAllPPID() const;
    // 按 PPID 查询配方
    RecipeInfo getRecipeByPPID(const std::string& ppid);
   // 按 ID 查询 PPID
   std::string getPPIDById(int nId);
    // 根据 PPID 和设备ID 获取设备配方ID
    int getDeviceRecipeIDByID(const std::string& ppid, int nDeviceID);
   // 按 PPID 查询 ID
   int getIdByPPID(const std::string& ppid);
    // 根据 PPID 和设备名称 获取设备配方ID
    int getDeviceRecipeIDByName(const std::string& ppid, const std::string& strDeviceName);
   // 按 PPID 查询配方
   RecipeInfo getRecipeByPPID(const std::string& ppid);
    // 删除指定 PPID 的配方
    bool deleteRecipeByPPID(const std::string& ppid);
   // 根据 PPID 和设备ID 获取设备配方ID
   int getDeviceRecipeIDByID(const std::string& ppid, int nDeviceID);
    // 更新指定 PPID 的配方
    bool updateRecipe(const RecipeInfo& recipe);
   // 根据 PPID 和设备名称 获取设备配方ID
   int getDeviceRecipeIDByName(const std::string& ppid, const std::string& strDeviceName);
   // 更新 PPID(通过旧 PPID 和新 PPID)
    bool updatePPID(const std::string& oldPPID, const std::string& newPPID);
   // 删除指定 PPID 的配方
   bool deleteRecipeByPPID(const std::string& ppid);
   // 更新配方描述(通过 PPID)
    bool updateDescription(const std::string& ppid, const std::string& newDescription);
   // 更新指定 PPID 的配方
   bool updateRecipe(const RecipeInfo& recipe);
   // 更新设备配方ID(通过 PPID 和设备ID)
    bool updateDeviceRecipeIDByID(const std::string& ppid, int nDeviceID, int nNewRecipeID);
   // 更新 PPID(通过旧 PPID 和新 PPID)
   bool updatePPID(const std::string& oldPPID, const std::string& newPPID);
   // 更新设备配方ID(通过 PPID 和设备名称)
    bool updateDeviceRecipeIDByName(const std::string& ppid, const std::string& strDeviceName, int nNewRecipeID);
   // 更新配方描述(通过 PPID)
   bool updateDescription(const std::string& ppid, const std::string& newDescription);
    // 模拟插入数据(测试用)
    void insertMockData();
   // 更新设备配方ID(通过 PPID 和设备ID)
   bool updateDeviceRecipeIDByID(const std::string& ppid, int nDeviceID, int nNewRecipeID);
    // 读取配方文件(CSV 或 JSON)
    bool readRecipeFile(const std::string& filename);
   // 更新设备配方ID(通过 PPID 和设备名称)
   bool updateDeviceRecipeIDByName(const std::string& ppid, const std::string& strDeviceName, int nNewRecipeID);
    // 保存配方到文件
    bool saveRecipeFile(const std::string& filename);
   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<std::pair<int, std::string>> 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();
   ~RecipeManager();
    RecipeManager(const RecipeManager&) = delete;
    RecipeManager& operator=(const RecipeManager&) = delete;
   RecipeManager(const RecipeManager&) = delete;
   RecipeManager& operator=(const RecipeManager&) = delete;
private:
    BL::Database* m_pDB;
    static std::recursive_mutex m_mutex;
   BL::Database* m_pDB;
   static std::recursive_mutex m_mutex;
};
#endif // RECIPE_MANAGER_H