From bfb99d288505ae75b85d196e6afe051386f5e6c8 Mon Sep 17 00:00:00 2001 From: FalconFly <12919280+falconfly@user.noreply.gitee.com> Date: Mon, 15 Jul 2024 15:33:16 +0800 Subject: [PATCH] =?UTF-8?q?=E9=87=8D=E5=86=99=E5=A2=9E=E5=8A=A0WorkMain?= =?UTF-8?q?=E7=AA=97=E4=BD=93=EF=BC=8C=E4=BD=BF=E7=94=A8sqlite=E4=BF=9D?= =?UTF-8?q?=E5=AD=98=E6=95=B0=E6=8D=AE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CalendarNotepad/App.config | 1 + CalendarNotepad/CalendarNotepad.csproj | 4 + CalendarNotepad/Extends/AppConfig.cs | 27 +++++ CalendarNotepad/Extends/DbContext.cs | 26 +++++ CalendarNotepad/Extends/ZipExtend.cs | 2 +- CalendarNotepad/Models/CNOptions.cs | 23 ----- CalendarNotepad/Models/WorkContext.cs | 4 +- CalendarNotepad/Program.cs | 12 ++- CalendarNotepad/WorkMain.Designer.cs | 131 +++++++++++++++++++++++++ CalendarNotepad/WorkMain.cs | 104 ++++++++++++++++++++ CalendarNotepad/WorkMain.resx | 120 ++++++++++++++++++++++ 11 files changed, 427 insertions(+), 27 deletions(-) create mode 100644 CalendarNotepad/Extends/AppConfig.cs create mode 100644 CalendarNotepad/Extends/DbContext.cs delete mode 100644 CalendarNotepad/Models/CNOptions.cs create mode 100644 CalendarNotepad/WorkMain.Designer.cs create mode 100644 CalendarNotepad/WorkMain.cs create mode 100644 CalendarNotepad/WorkMain.resx diff --git a/CalendarNotepad/App.config b/CalendarNotepad/App.config index 406ac6c..27135b1 100644 --- a/CalendarNotepad/App.config +++ b/CalendarNotepad/App.config @@ -2,5 +2,6 @@ + \ No newline at end of file diff --git a/CalendarNotepad/CalendarNotepad.csproj b/CalendarNotepad/CalendarNotepad.csproj index b57c89e..6b18da8 100644 --- a/CalendarNotepad/CalendarNotepad.csproj +++ b/CalendarNotepad/CalendarNotepad.csproj @@ -8,4 +8,8 @@ enable + + + + \ No newline at end of file diff --git a/CalendarNotepad/Extends/AppConfig.cs b/CalendarNotepad/Extends/AppConfig.cs new file mode 100644 index 0000000..7e68f3c --- /dev/null +++ b/CalendarNotepad/Extends/AppConfig.cs @@ -0,0 +1,27 @@ +using System.Configuration; + +namespace CalendarNotepad.Extends +{ + /// + /// 应用配置 + /// + public static class AppConfig + { + /// + /// 获取配置值 + /// + /// 配置的键 + /// 配置值,默认string.Empty + public static string GetValue(string key) => ConfigurationManager.AppSettings[key] ?? string.Empty; + + /// + /// 保存的文件名sqliteFile + /// + public static string GetSaveFileName => GetValue("saveFileName") ?? ""; + + /// + /// sqliteFile名 + /// + public static string SqliteFileName => GetValue("sqliteFile") ?? ""; + } +} diff --git a/CalendarNotepad/Extends/DbContext.cs b/CalendarNotepad/Extends/DbContext.cs new file mode 100644 index 0000000..44fb97e --- /dev/null +++ b/CalendarNotepad/Extends/DbContext.cs @@ -0,0 +1,26 @@ +using CalendarNotepad.Models; +using SqlSugar; + +namespace CalendarNotepad.Extends +{ + /// + /// 数据库上下文 + /// + public class DbContext:SqlSugar.SqlSugarClient + { + public DbContext() : base(GetConfig()) { + + } + + public static ConnectionConfig GetConfig() => new ConnectionConfig { + DbType = DbType.Sqlite, + ConnectionString = AppConfig.SqliteFileName, + IsAutoCloseConnection = false, + }; + + public void DbInit() { + using var db = new DbContext(); + db.CodeFirst.InitTables(); + } + } +} diff --git a/CalendarNotepad/Extends/ZipExtend.cs b/CalendarNotepad/Extends/ZipExtend.cs index 13879a6..289db93 100644 --- a/CalendarNotepad/Extends/ZipExtend.cs +++ b/CalendarNotepad/Extends/ZipExtend.cs @@ -12,7 +12,7 @@ namespace CalendarNotepad.Extends /// /// 压缩文件名 /// - public static string ZipFileName { get; set; } = CNOptions.GetSaveFileName; + public static string ZipFileName { get; set; } = AppConfig.GetSaveFileName; /// /// 某个文件保存到压缩文件内 /// diff --git a/CalendarNotepad/Models/CNOptions.cs b/CalendarNotepad/Models/CNOptions.cs deleted file mode 100644 index a5da615..0000000 --- a/CalendarNotepad/Models/CNOptions.cs +++ /dev/null @@ -1,23 +0,0 @@ -using System.Configuration; - -namespace CalendarNotepad.Models -{ - /// - /// 应用配置 - /// - public static class CNOptions - { - /// - /// 获取配置的值 - /// - /// 键 - /// - public static string? GetValue(string key) => ConfigurationManager.AppSettings[key]; - - /// - /// 保存的文件名 - /// - /// - public static string GetSaveFileName => GetValue("saveFileName") ?? ""; - } -} diff --git a/CalendarNotepad/Models/WorkContext.cs b/CalendarNotepad/Models/WorkContext.cs index ac59bd9..4830644 100644 --- a/CalendarNotepad/Models/WorkContext.cs +++ b/CalendarNotepad/Models/WorkContext.cs @@ -58,7 +58,7 @@ namespace CalendarNotepad.Models } public void SaveToFile() { - var file = CNOptions.GetSaveFileName; + var file = AppConfig.GetSaveFileName; if(file.IsNullOrEmpty()) { MessageBox.Show("没有配置数据保存文件!"); return; @@ -94,7 +94,7 @@ namespace CalendarNotepad.Models } public void LoadFromFile() { - var file = CNOptions.GetSaveFileName; + var file = AppConfig.GetSaveFileName; if(file.IsNullOrEmpty()) { MessageBox.Show("没有配置数据保存文件!"); return; diff --git a/CalendarNotepad/Program.cs b/CalendarNotepad/Program.cs index 56a1791..4f2e92a 100644 --- a/CalendarNotepad/Program.cs +++ b/CalendarNotepad/Program.cs @@ -1,3 +1,5 @@ +using CalendarNotepad.Extends; + namespace CalendarNotepad { internal static class Program @@ -9,8 +11,16 @@ namespace CalendarNotepad static void Main() { // To customize application configuration such as set high DPI settings or default font, // see https://aka.ms/applicationconfiguration. + + Console.WriteLine("ʼݿ⣡"); + using(var db = new DbContext()) { + db.DbInit(); + } + Console.WriteLine("ʼݿɣ"); + + Console.WriteLine("!!"); ApplicationConfiguration.Initialize(); - Application.Run(new Main()); + Application.Run(new WorkMain()); } } } \ No newline at end of file diff --git a/CalendarNotepad/WorkMain.Designer.cs b/CalendarNotepad/WorkMain.Designer.cs new file mode 100644 index 0000000..31c0369 --- /dev/null +++ b/CalendarNotepad/WorkMain.Designer.cs @@ -0,0 +1,131 @@ +namespace CalendarNotepad +{ + partial class WorkMain + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) { + if(disposing && (components != null)) { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() { + tabControl1 = new TabControl(); + tpWorkManage = new TabPage(); + splitContainer1 = new SplitContainer(); + mcWorkDay = new MonthCalendar(); + rtbMsg = new RichTextBox(); + tabControl1.SuspendLayout(); + tpWorkManage.SuspendLayout(); + ((System.ComponentModel.ISupportInitialize)splitContainer1).BeginInit(); + splitContainer1.Panel1.SuspendLayout(); + splitContainer1.Panel2.SuspendLayout(); + splitContainer1.SuspendLayout(); + SuspendLayout(); + // + // tabControl1 + // + tabControl1.Controls.Add(tpWorkManage); + tabControl1.Dock = DockStyle.Fill; + tabControl1.Location = new Point(0,0); + tabControl1.Name = "tabControl1"; + tabControl1.SelectedIndex = 0; + tabControl1.Size = new Size(740,435); + tabControl1.TabIndex = 0; + // + // tpWorkManage + // + tpWorkManage.Controls.Add(splitContainer1); + tpWorkManage.Location = new Point(4,26); + tpWorkManage.Name = "tpWorkManage"; + tpWorkManage.Padding = new Padding(3); + tpWorkManage.Size = new Size(732,405); + tpWorkManage.TabIndex = 1; + tpWorkManage.Text = "录入记录"; + tpWorkManage.UseVisualStyleBackColor = true; + // + // splitContainer1 + // + splitContainer1.Dock = DockStyle.Fill; + splitContainer1.FixedPanel = FixedPanel.Panel1; + splitContainer1.Location = new Point(3,3); + splitContainer1.Name = "splitContainer1"; + // + // splitContainer1.Panel1 + // + splitContainer1.Panel1.Controls.Add(mcWorkDay); + // + // splitContainer1.Panel2 + // + splitContainer1.Panel2.Controls.Add(rtbMsg); + splitContainer1.Size = new Size(726,399); + splitContainer1.SplitterDistance = 236; + splitContainer1.TabIndex = 9; + // + // mcWorkDay + // + mcWorkDay.FirstDayOfWeek = Day.Sunday; + mcWorkDay.Location = new Point(6,6); + mcWorkDay.MaxSelectionCount = 1; + mcWorkDay.MinDate = new DateTime(2000,1,1,0,0,0,0); + mcWorkDay.Name = "mcWorkDay"; + mcWorkDay.ShowTodayCircle = false; + mcWorkDay.TabIndex = 6; + mcWorkDay.TitleBackColor = SystemColors.MenuHighlight; + mcWorkDay.TitleForeColor = Color.FromArgb(255,128,128); + mcWorkDay.DateChanged += mcWorkDay_DateChanged; + // + // rtbMsg + // + rtbMsg.Dock = DockStyle.Fill; + rtbMsg.EnableAutoDragDrop = true; + rtbMsg.Location = new Point(0,0); + rtbMsg.Name = "rtbMsg"; + rtbMsg.Size = new Size(486,399); + rtbMsg.TabIndex = 7; + rtbMsg.Text = ""; + rtbMsg.TextChanged += rtbMsg_TextChanged; + // + // WorkMain + // + AutoScaleDimensions = new SizeF(7F,17F); + AutoScaleMode = AutoScaleMode.Font; + ClientSize = new Size(740,435); + Controls.Add(tabControl1); + Name = "WorkMain"; + Text = "工作记录"; + FormClosing += WorkMain_FormClosing; + Load += WorkMain_Load; + tabControl1.ResumeLayout(false); + tpWorkManage.ResumeLayout(false); + splitContainer1.Panel1.ResumeLayout(false); + splitContainer1.Panel2.ResumeLayout(false); + ((System.ComponentModel.ISupportInitialize)splitContainer1).EndInit(); + splitContainer1.ResumeLayout(false); + ResumeLayout(false); + } + + #endregion + + private TabControl tabControl1; + private TabPage tpWorkManage; + private MonthCalendar mcWorkDay; + private RichTextBox rtbMsg; + private SplitContainer splitContainer1; + } +} \ No newline at end of file diff --git a/CalendarNotepad/WorkMain.cs b/CalendarNotepad/WorkMain.cs new file mode 100644 index 0000000..d793a15 --- /dev/null +++ b/CalendarNotepad/WorkMain.cs @@ -0,0 +1,104 @@ +using CalendarNotepad.Extends; +using CalendarNotepad.Models; + +namespace CalendarNotepad +{ + public partial class WorkMain:Form + { + public DbContext Db { get; set; } = new DbContext(); + + public WorkMain() { + InitializeComponent(); + this.rtbMsg.EnableAutoDragDrop = true; + + //if(File.Exists(AppConfig.GetSaveFileName)) { + // var str = ZipExtend.GetFormFile("WorkContext.json"); + // if(str.IsNullOrEmpty()) { + // return; + // } + // var wl = JsonSerializer.Deserialize(str) ?? new WorkUnitList(); + // if(wl == null) { + // return; + // } + // this.Db.Deleteable().ExecuteCommand(); + // foreach(var i in wl) { + // this.Db.Insertable(i).ExecuteCommand(); + // } + //} + + + + } + + private void rtbMsg_TextChanged(object sender,EventArgs e) { + + var wd = this.mcWorkDay.SelectionStart.ToString("yyyyMMdd"); + var dr = "全天"; + var msg = this.rtbMsg.Text; + var qu = this.Db.Queryable().Where(a => a.DayRange == dr && a.WorkDay == wd).ToList(); + if(qu.Any() && msg.IsNullOrEmpty()) { + this.Db.Deleteable() + .Where(a => a.DayRange == dr && a.WorkDay == wd) + .ExecuteCommand(); + return; + } + if(qu.Any() && msg.IsNotNullOrEmpty()) { + this.Db.Updateable() + .Where(a => a.DayRange == dr && a.WorkDay == wd) + .SetColumns(a => a.WorkMessage == msg) + .ExecuteCommand(); + return; + } + if(!qu.Any() && msg.IsNotNullOrEmpty()) { + this.Db.Insertable(new WorkUnit { + DayRange = dr,WorkDay = wd,WorkMessage = msg, + }).ExecuteCommand(); + return; + } + } + + private void WorkMain_FormClosing(object sender,FormClosingEventArgs e) { + if(this.Db != null) { + this.Db.Close(); + this.Db.Dispose(); + } + } + + private void mcWorkDay_DateChanged(object sender,DateRangeEventArgs e) { + var wd = this.mcWorkDay.SelectionStart.ToString("yyyyMMdd"); + var sr = "全天"; + var qu = this.Db.Queryable().Where(a => a.DayRange == sr && a.WorkDay == wd).ToList(); + if(qu.Any()) { + this.rtbMsg.Text = qu.First().WorkMessage; + } + else { + this.rtbMsg.Rtf = ""; + } + + Task.Factory.StartNew(() => { + this.Invoke(() => { + this.mcWorkDay.BoldedDates = GetBoldDays().ToArray(); + }); + }); + } + + private List GetBoldDays() { + var year = this.mcWorkDay.SelectionStart.Year; + var month = this.mcWorkDay.SelectionStart.Month; + var days = new List(); + for(int i = 1;i < 32;i++) { + var wd = $"{year}{month.ToString("D2")}{i.ToString("D2")}"; + var qu = this.Db.Queryable().Where(a => a.WorkDay == wd && a.WorkMessage != "").ToList(); + if(qu.Any()) { + days.Add(new DateTime(year,month,i)); + } + } + + return days; + } + + private void WorkMain_Load(object sender,EventArgs e) { + mcWorkDay_DateChanged(null,null); + } + } +} diff --git a/CalendarNotepad/WorkMain.resx b/CalendarNotepad/WorkMain.resx new file mode 100644 index 0000000..a395bff --- /dev/null +++ b/CalendarNotepad/WorkMain.resx @@ -0,0 +1,120 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file