|
- 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<SaveData> saveDatas { get; set; } = new List<SaveData>();
- public ObservableCollection<RecipeChart> recipeCharts { get; set; } = new ObservableCollection<RecipeChart>();
- static string directoryPath = $"AccessFile\\DB\\{Json<DevicePar>.Data.ProjectTypeName.ToString()}";
- static string path
- {
- get
- {
- string tempPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, $"AccessFile\\DB\\{Json<DevicePar>.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<SaveNameData>().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<SaveNameData>().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<SaveData> FindList(RecipeChart chart)
- {
- return Db.Queryable<SaveData>().SplitTable().Where(o => o.ProductNumberId == chart.Id).ToList();
- }
- public void Init()
- {
- try
- {
- if (!File.Exists(path))
- {
- Db.DbMaintenance.CreateDatabase();
- Db.CodeFirst.SetStringDefaultLength(100).InitTables<SaveData>();
- Db.CodeFirst.InitTables<SaveNameData>();
- //Db.CodeFirst.SetStringDefaultLength(100).InitTables<SaveNameData>();
- }
- }
- 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<SaveData> SelectId(string id)
- {
- try
- {
- return Db.Queryable<SaveData>().SplitTable().Where(o => o.ProductNumberId == id).ToList();
- }
- catch (Exception)
- {
- return null;
- }
- }
- public List<String> SelectAllName()
- {
- return Db.Queryable<SaveData>()
- .Select(x => x.Name)
- .Distinct()
- .ToList();
- }
-
- }
- }
|