|
- using BPASmartClient.MorkCL.Model.DB;
- using SqlSugar;
- using System;
- using System.Collections.Generic;
- using System.IO;
- using System.Linq;
- using System.Reflection;
- using System.Text;
- using System.Threading.Tasks;
-
- namespace BPASmartClient.MorkCL.Server
- {
- internal class SqliteHelper : ISqlite
- {
-
- private volatile static SqliteHelper _Instance;
- public static SqliteHelper GetInstance => _Instance ?? (_Instance = new SqliteHelper());
- private SqliteHelper() { }
-
- static string path
- {
- get
- {
- Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory);
- return Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "AccessFile\\DB\\Material.db");
- }
- }
-
- public SqlSugarScope Db = new SqlSugarScope(new ConnectionConfig()
- {
- ConnectionString = $"Data Source={path}",
- DbType = DbType.Sqlite,
- IsAutoCloseConnection = true,//设置自动关闭连接
- });
-
- /// <summary>
- /// 初始化
- /// </summary>
- public void Init()
- {
- try
- {
- if (!File.Exists(path))
- {
- //创建数据库
- Db.DbMaintenance.CreateDatabase();
-
- //创建数据表
- string spnaName = "BPASmartClient.MorkCL.Model.DB";//实体类的命名空间
- Type[] ass = Assembly.LoadFrom(AppContext.BaseDirectory + "BPASmartClient.MorkCL.dll").GetTypes().Where(p => p.Namespace == spnaName).ToArray();
- Db.CodeFirst.SetStringDefaultLength(200).InitTables(ass);
- }
- }
- catch (Exception ex)
- {
- MessageLog.GetInstance.ShowEx(ex.ToString());
- }
- }
-
- #region 主料数据操作
- /// <summary>
- /// 添加主料
- /// </summary>
- /// <param name="mb"></param>
- /// <returns></returns>
- public bool AddIngredients(IngredientsTB mb)
- {
- IngredientsTB tempMB = new IngredientsTB()
- {
- Id = Guid.NewGuid().ToString(),
- Name = mb.Name,
- Description = mb.Description,
- Loc = mb.Loc,
- };
- if (Db.Queryable<IngredientsTB>().Any(p => p.Name == mb.Name)) return false;
- return Db.Insertable(tempMB).ExecuteCommand() > 0;
- }
-
- /// <summary>
- /// 编辑主料
- /// </summary>
- /// <param name="req"></param>
- /// <returns></returns>
- public bool EditIngredients(IngredientsTB req)
- {
- IngredientsTB tempMB = Db.Queryable<IngredientsTB>().First(p => p.Id == req.Id);
- if (tempMB == null) return false;
- tempMB.Name = req.Name;
- tempMB.Loc = req.Loc;
- tempMB.Description = req.Description;
- return Db.Updateable(tempMB).ExecuteCommand() > 0;
- }
-
- /// <summary>
- /// 删除主料
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public bool DelIngredients(string id)
- {
- return Db.Ado.ExecuteCommand($"DELETE From [IngredientsTB] WHERE Id='{id}'") > 0;
- }
-
- /// <summary>
- /// 获取所有主料
- /// </summary>
- /// <returns></returns>
- public List<IngredientsTB> GetIngredients()
- {
- return Db.Queryable<IngredientsTB>().ToList();
- }
-
- /// <summary>
- /// 获取指定主料信息
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public IngredientsTB GetIngredientsInfo(string id)
- {
- return Db.Queryable<IngredientsTB>().First(p => p.Id == id);
- }
-
- /// <summary>
- /// 查询指定位置是否占用。
- /// </summary>
- /// <param name="loc"></param>
- /// <returns></returns>
- public bool GetIngredientsInfoByLoc(int loc)
- {
- return Db.Queryable<IngredientsTB>().Any(p => p.Loc == loc);
- }
-
- #endregion
-
-
- #region 辅料数据操作
- /// <summary>
- /// 添加辅料
- /// </summary>
- /// <param name="mb"></param>
- /// <returns></returns>
- public bool AddAccessories(AccessoriesTB mb)
- {
- AccessoriesTB tempMB = new AccessoriesTB()
- {
- Id = Guid.NewGuid().ToString(),
- Name = mb.Name,
- Description = mb.Description,
- Loc = mb.Loc,
- };
- if (Db.Queryable<AccessoriesTB>().Any(p => p.Name == mb.Name)) return false;
- return Db.Insertable(tempMB).ExecuteCommand() > 0;
- }
-
- /// <summary>
- /// 编辑辅料
- /// </summary>
- /// <param name="req"></param>
- /// <returns></returns>
- public bool EditAccessories(AccessoriesTB req)
- {
- AccessoriesTB tempMB = Db.Queryable<AccessoriesTB>().First(p => p.Id == req.Id);
- if (tempMB == null) return false;
- tempMB.Name = req.Name;
- tempMB.Loc = req.Loc;
- tempMB.Description = req.Description;
- return Db.Updateable(tempMB).ExecuteCommand() > 0;
- }
-
- /// <summary>
- /// 删除辅料
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public bool DelAccessories(string id)
- {
- return Db.Ado.ExecuteCommand($"DELETE From [AccessoriesTB] WHERE Id='{id}'") > 0;
- }
-
- /// <summary>
- /// 获取所有辅料
- /// </summary>
- /// <returns></returns>
- public List<AccessoriesTB> GetAccessories()
- {
- return Db.Queryable<AccessoriesTB>().ToList();
- }
-
- /// <summary>
- /// 获取指定的辅料
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public AccessoriesTB GetAccessoriesInfo(string id)
- {
- return Db.Queryable<AccessoriesTB>().First(p => p.Id == id);
- }
-
- /// <summary>
- /// 查询指定位置是否占用。
- /// </summary>
- /// <param name="loc"></param>
- /// <returns></returns>
- public bool GetAccessoriesInfoByLoc(int loc)
- {
- return Db.Queryable<AccessoriesTB>().Any(p => p.Loc == loc);
- }
-
- #endregion
-
- #region 调料数据操作
- /// <summary>
- /// 添加调料
- /// </summary>
- /// <param name="mb"></param>
- /// <returns></returns>
- public bool AddSeasoning(SeasoningTB mb)
- {
- SeasoningTB tempMB = new SeasoningTB()
- {
- Id = Guid.NewGuid().ToString(),
- Name = mb.Name,
- Description = mb.Description,
- Loc = mb.Loc,
- };
- if (Db.Queryable<SeasoningTB>().Any(p => p.Name == mb.Name)) return false;
- return Db.Insertable(tempMB).ExecuteCommand() > 0;
- }
-
- /// <summary>
- /// 编辑调料
- /// </summary>
- /// <param name="req"></param>
- /// <returns></returns>
- public bool EditSeasoning(SeasoningTB req)
- {
- SeasoningTB tempMB = Db.Queryable<SeasoningTB>().First(p => p.Id == req.Id);
- if (tempMB == null) return false;
- tempMB.Name = req.Name;
- tempMB.Loc = req.Loc;
- tempMB.Description = req.Description;
- return Db.Updateable(tempMB).ExecuteCommand() > 0;
- }
-
- /// <summary>
- /// 删除调料
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public bool DelSeasoning(string id)
- {
- return Db.Ado.ExecuteCommand($"DELETE From [SeasoningTB] WHERE Id='{id}'") > 0;
- }
-
- /// <summary>
- /// 获取所有调料
- /// </summary>
- /// <returns></returns>
- public List<SeasoningTB> GetSeasoning()
- {
- return Db.Queryable<SeasoningTB>().ToList();
- }
-
- /// <summary>
- /// 获取指定的调料信息
- /// </summary>
- /// <param name="id"></param>
- /// <returns></returns>
- public SeasoningTB GetSeasoningInfo(string id)
- {
- return Db.Queryable<SeasoningTB>().First(p => p.Id == id);
- }
-
- /// <summary>
- /// 查询指定位置是否占用。
- /// </summary>
- /// <param name="loc"></param>
- /// <returns></returns>
- public bool GetSeasoningInfoByLoc(int loc)
- {
- return Db.Queryable<SeasoningTB>().Any(p => p.Loc == loc);
- }
-
- #endregion
-
-
-
-
- }
- }
|