From c12fd60526fa8fa362adea4175237ab16002ec11 Mon Sep 17 00:00:00 2001 From: xxe Date: Fri, 9 Sep 2022 10:16:24 +0800 Subject: [PATCH] =?UTF-8?q?=E5=88=B7=E5=8D=A1=E7=AB=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- HKCardOUT/Bootstrapper.cs | 5 +++ HKCardOUT/HKCardOUT.csproj | 3 -- HKCardOUT/Helper/DataBus.cs | 2 +- HKCardOUT/Logic/BaseEntity.cs | 22 ++++++++++++ HKCardOUT/Logic/DbContext.cs | 8 +++++ HKCardOUT/Logic/Model/SaleLog.cs | 29 ++++++++++++++++ HKCardOUT/Logic/Service/HKCore.cs | 48 +++++++++++++++++++++++++++ HKCardOUT/ViewModels/RootViewModel.cs | 30 +++++++++++++++-- HKCardOUT/Views/RootView.xaml | 22 +++++++++++- 9 files changed, 161 insertions(+), 8 deletions(-) create mode 100644 HKCardOUT/Logic/BaseEntity.cs create mode 100644 HKCardOUT/Logic/Model/SaleLog.cs create mode 100644 HKCardOUT/Logic/Service/HKCore.cs diff --git a/HKCardOUT/Bootstrapper.cs b/HKCardOUT/Bootstrapper.cs index 074a015..3b32dae 100644 --- a/HKCardOUT/Bootstrapper.cs +++ b/HKCardOUT/Bootstrapper.cs @@ -8,6 +8,9 @@ using Microsoft.Extensions.Configuration; using HKCardOUT.Helper; using HKCardOUT.QuartzUtil.Job; using HKCardOUT.QuartzUtil; +using HKCardOUT.Logic; +using HKCardOUT.Logic.Service; +using ImTools; namespace HKCardOUT { @@ -23,6 +26,7 @@ namespace HKCardOUT protected override void ConfigureIoC(IStyletIoCBuilder builder) { + builder.Bind().ToSelf(); } /// @@ -34,6 +38,7 @@ namespace HKCardOUT DataBus.ConnectionString = configer.GetConnectionString("Sqlite"); DataBus.Cron = configer["Cron"]; DataBus.SaasRoute = configer["SaasRoute"]; + DbContext.InitTable(); base.Configure(); } diff --git a/HKCardOUT/HKCardOUT.csproj b/HKCardOUT/HKCardOUT.csproj index 77fb2e6..2b95ada 100644 --- a/HKCardOUT/HKCardOUT.csproj +++ b/HKCardOUT/HKCardOUT.csproj @@ -39,9 +39,6 @@ Always - - - diff --git a/HKCardOUT/Helper/DataBus.cs b/HKCardOUT/Helper/DataBus.cs index be24fe1..2f01623 100644 --- a/HKCardOUT/Helper/DataBus.cs +++ b/HKCardOUT/Helper/DataBus.cs @@ -17,7 +17,7 @@ namespace HKCardOUT.Helper set { var Route = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "DATA"); - _ConnectionString = SyncStatic.CreateFile(Path.Combine(SyncStatic.CreateDir(Route), value)); + _ConnectionString = $"DataSource={SyncStatic.CreateFile(Path.Combine(SyncStatic.CreateDir(Route), value))}"; } } public static bool NetWordState { get; set; } = false; diff --git a/HKCardOUT/Logic/BaseEntity.cs b/HKCardOUT/Logic/BaseEntity.cs new file mode 100644 index 0000000..e9c193e --- /dev/null +++ b/HKCardOUT/Logic/BaseEntity.cs @@ -0,0 +1,22 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HKCardOUT.Logic +{ + public class BaseEntity + { + [SugarColumn(IsPrimaryKey = true)] + public Guid Id { get; set; } + [SugarColumn(ColumnDataType = "DATETIME")] + public DateTime CreateTime { get; set; } + public void Create() + { + this.Id = Guid.NewGuid(); + this.CreateTime = DateTime.Now; + } + } +} diff --git a/HKCardOUT/Logic/DbContext.cs b/HKCardOUT/Logic/DbContext.cs index 3be36cf..84fa37e 100644 --- a/HKCardOUT/Logic/DbContext.cs +++ b/HKCardOUT/Logic/DbContext.cs @@ -3,6 +3,7 @@ using SqlSugar; using System; using System.Collections.Generic; using System.Linq; +using System.Reflection; using System.Text; using System.Threading.Tasks; @@ -22,5 +23,12 @@ namespace HKCardOUT.Logic }; }); + + public static void InitTable() + { + var Tables = Assembly.GetAssembly(typeof(DbContext)) + .GetTypes().Where(t => t.BaseType == typeof(BaseEntity)).ToArray(); + Context.CodeFirst.InitTables(Tables); + } } } diff --git a/HKCardOUT/Logic/Model/SaleLog.cs b/HKCardOUT/Logic/Model/SaleLog.cs new file mode 100644 index 0000000..65893c4 --- /dev/null +++ b/HKCardOUT/Logic/Model/SaleLog.cs @@ -0,0 +1,29 @@ +using SqlSugar; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HKCardOUT.Logic.Model +{ + [SugarTable("SaleLog")] + public class SaleLog : BaseEntity + { + /// + /// 卡号 + /// + [SugarColumn(IsNullable = false)] + public string CardNo { get; set; } + /// + /// 消费金额 + /// + [SugarColumn(IsNullable = false, ColumnDataType = "decimal(10,2)")] + public decimal Money { get; set; } + /// + /// 消费位置 + /// + [SugarColumn(IsNullable = false)] + public string Location { get; set; } + } +} diff --git a/HKCardOUT/Logic/Service/HKCore.cs b/HKCardOUT/Logic/Service/HKCore.cs new file mode 100644 index 0000000..82fc257 --- /dev/null +++ b/HKCardOUT/Logic/Service/HKCore.cs @@ -0,0 +1,48 @@ +using HKCardOUT.Logic.Model; +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace HKCardOUT.Logic.Service +{ + public class HKCore + { + /// + /// 刷卡消费 + /// + /// + /// + public bool DeviceSale(SaleLog input) + { + var Old = DbContext.Context.Queryable() + .Where(t => t.CardNo == input.CardNo).OrderByDescending(t => t.CreateTime).First(); + if (Old != null) + { + //10秒防止重复刷卡 + if (Old.CreateTime.Subtract(DateTime.Now).TotalSeconds > 10d) + { + return DbContext.Context.Insertable(input).CallEntityMethod(t => t.Create()).ExecuteCommand() > 0; + } + return false; + } + else + return DbContext.Context.Insertable(input).CallEntityMethod(t => t.Create()).ExecuteCommand() > 0; + + } + /// + /// 获取当天消费前100条展示 + /// + /// + public List PullDaySaleLog() + { + var Begin = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 00:00:00")); + var End = DateTime.Parse(DateTime.Now.ToString("yyyy-MM-dd 23:59:59")); + return DbContext.Context.Queryable() + .Where(t => t.CreateTime >= Begin) + .Where(t => t.CreateTime < End) + .OrderByDescending(t => t.CreateTime).ToPageList(0, 10); + } + } +} diff --git a/HKCardOUT/ViewModels/RootViewModel.cs b/HKCardOUT/ViewModels/RootViewModel.cs index d40857b..061698b 100644 --- a/HKCardOUT/ViewModels/RootViewModel.cs +++ b/HKCardOUT/ViewModels/RootViewModel.cs @@ -1,19 +1,43 @@ using HKCardOut.Helper; using HKCardOUT.Helper; +using HKCardOUT.Logic.Model; +using HKCardOUT.Logic.Service; using Stylet; +using StyletIoC; using System; +using System.Collections.ObjectModel; using System.Threading; namespace HKCardOUT.ViewModels { - public class RootViewModel : PropertyChangedBase + public class RootViewModel : Conductor { - - public RootViewModel() + IContainer Container; + public RootViewModel(IContainer Container) { + this.Container = Container; MainThread(); } + protected override void OnViewLoaded() + { + Result = new ObservableCollection(this.Container.Get().PullDaySaleLog()); + //ThreadManage.GetInstance().Start(() => + //{ + // Result = new ObservableCollection(this.Container.Get().PullDaySaleLog()); + // Thread.Sleep(3000); + //}, "消费记录查询"); + } + + #region 属性 + ObservableCollection _Result; + public ObservableCollection Result + { + get => _Result; + set => SetAndNotify(ref _Result, value); + } + #endregion + #region 方法 private void MainThread() { diff --git a/HKCardOUT/Views/RootView.xaml b/HKCardOUT/Views/RootView.xaml index 2f61bb6..43a5997 100644 --- a/HKCardOUT/Views/RootView.xaml +++ b/HKCardOUT/Views/RootView.xaml @@ -18,5 +18,25 @@ - + + + + + +