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,//设置自动关闭连接
});
///
/// 初始化
///
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 主料数据操作
///
/// 添加主料
///
///
///
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().Any(p => p.Name == mb.Name)) return false;
return Db.Insertable(tempMB).ExecuteCommand() > 0;
}
///
/// 编辑主料
///
///
///
public bool EditIngredients(IngredientsTB req)
{
IngredientsTB tempMB = Db.Queryable().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;
}
///
/// 删除主料
///
///
///
public bool DelIngredients(string id)
{
return Db.Ado.ExecuteCommand($"DELETE From [IngredientsTB] WHERE Id='{id}'") > 0;
}
///
/// 获取所有主料
///
///
public List GetIngredients()
{
return Db.Queryable().ToList();
}
///
/// 获取指定主料信息
///
///
///
public IngredientsTB GetIngredientsInfo(string id)
{
return Db.Queryable().First(p => p.Id == id);
}
///
/// 查询指定位置是否占用。
///
///
///
public bool GetIngredientsInfoByLoc(int loc)
{
return Db.Queryable().Any(p => p.Loc == loc);
}
#endregion
#region 辅料数据操作
///
/// 添加辅料
///
///
///
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().Any(p => p.Name == mb.Name)) return false;
return Db.Insertable(tempMB).ExecuteCommand() > 0;
}
///
/// 编辑辅料
///
///
///
public bool EditAccessories(AccessoriesTB req)
{
AccessoriesTB tempMB = Db.Queryable().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;
}
///
/// 删除辅料
///
///
///
public bool DelAccessories(string id)
{
return Db.Ado.ExecuteCommand($"DELETE From [AccessoriesTB] WHERE Id='{id}'") > 0;
}
///
/// 获取所有辅料
///
///
public List GetAccessories()
{
return Db.Queryable().ToList();
}
///
/// 获取指定的辅料
///
///
///
public AccessoriesTB GetAccessoriesInfo(string id)
{
return Db.Queryable().First(p => p.Id == id);
}
///
/// 查询指定位置是否占用。
///
///
///
public bool GetAccessoriesInfoByLoc(int loc)
{
return Db.Queryable().Any(p => p.Loc == loc);
}
#endregion
#region 调料数据操作
///
/// 添加调料
///
///
///
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().Any(p => p.Name == mb.Name)) return false;
return Db.Insertable(tempMB).ExecuteCommand() > 0;
}
///
/// 编辑调料
///
///
///
public bool EditSeasoning(SeasoningTB req)
{
SeasoningTB tempMB = Db.Queryable().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;
}
///
/// 删除调料
///
///
///
public bool DelSeasoning(string id)
{
return Db.Ado.ExecuteCommand($"DELETE From [SeasoningTB] WHERE Id='{id}'") > 0;
}
///
/// 获取所有调料
///
///
public List GetSeasoning()
{
return Db.Queryable().ToList();
}
///
/// 获取指定的调料信息
///
///
///
public SeasoningTB GetSeasoningInfo(string id)
{
return Db.Queryable().First(p => p.Id == id);
}
///
/// 查询指定位置是否占用。
///
///
///
public bool GetSeasoningInfoByLoc(int loc)
{
return Db.Queryable().Any(p => p.Loc == loc);
}
#endregion
}
}