using SqlSugar; using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Threading.Tasks; namespace BPASmartClient.Academy.Model { public class Sqlite { private static Sqlite instance; public static Sqlite GetInstance { get; set; } = instance ??= new Sqlite(); private Sqlite() { } public List saveDatas { get; set; } = new List(); public ObservableCollection recipeCharts { get; set; } = new ObservableCollection(); static string directoryPath = $"AccessFile\\DB\\{Json.Data.ProjectTypeName.ToString()}"; static string path { get { string tempPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"AccessFile\\DB\\{Json.Data.ProjectTypeName.ToString()}"); Directory.CreateDirectory(tempPath); return Path.Combine(tempPath, $"data.db"); } } public SqlSugarScope Db = new SqlSugarScope(new ConnectionConfig() { ConnectionString = $"Data Source = {path}", DbType = DbType.Sqlite, IsAutoCloseConnection = true, }); public void FindDateList(DateTime date) { int i = 1; recipeCharts.Clear(); Db.Queryable().Where(o => o.Createtime.Date == date.Date).ToList().ForEach(t => { recipeCharts.Add(new RecipeChart() { Num = i++, CreateTime = t.Createtime, Name = t.ProductNumber, Id = t.Id }); }); if (recipeCharts.Count<=0) { MessageNotify.GetInstance.OpenMsg("未查询到有效记录"); } } public void FindName(string Name) { int i = 1; recipeCharts.Clear(); Db.Queryable().Where(o => o.ProductNumber.Contains(Name)).ToList().ForEach(t => { recipeCharts.Add(new RecipeChart() { Num = i++, CreateTime = t.Createtime, Name = t.ProductNumber, Id = t.Id }); }); } public List FindList(RecipeChart chart) { return Db.Queryable().SplitTable().Where(o => o.ProductNumberId == chart.Id).ToList(); } public void Init() { try { if (!File.Exists(path)) { Db.DbMaintenance.CreateDatabase(); Db.CodeFirst.SetStringDefaultLength(100).InitTables(); Db.CodeFirst.InitTables(); //Db.CodeFirst.SetStringDefaultLength(100).InitTables(); } } catch (Exception ex) { MessageNotify.GetInstance.OpenMsg("创建数据库失败"); MessageNotify.GetInstance.ShowRunLog(ex.Message); } } public bool AddData(SaveData dfb) { try { return Db.Insertable(dfb).SplitTable().ExecuteCommand() > 0; } catch (Exception) { return false; } } public bool AddData(SaveNameData snd) { try { return Db.Insertable(snd).ExecuteCommand() > 0; } catch (Exception) { return false; } } public List SelectId(string id) { try { return Db.Queryable().SplitTable().Where(o => o.ProductNumberId == id).ToList(); } catch (Exception) { return null; } } public List SelectAllName() { return Db.Queryable() .Select(x => x.Name) .Distinct() .ToList(); } } }