重写增加WorkMain窗体,使用sqlite保存数据

This commit is contained in:
FalconFly 2024-07-15 15:33:16 +08:00
parent 10da0285c1
commit bfb99d2885
11 changed files with 427 additions and 27 deletions

View File

@ -2,5 +2,6 @@
<configuration>
<appSettings>
<add key="saveFileName" value="data.db"/>
<add key ="sqliteFile" value="DataSource=work.db"/>
</appSettings>
</configuration>

View File

@ -8,4 +8,8 @@
<ImplicitUsings>enable</ImplicitUsings>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="SqlSugarCore" Version="5.1.4.162" />
</ItemGroup>
</Project>

View File

@ -0,0 +1,27 @@
using System.Configuration;
namespace CalendarNotepad.Extends
{
/// <summary>
/// 应用配置
/// </summary>
public static class AppConfig
{
/// <summary>
/// 获取配置值
/// </summary>
/// <param name="key">配置的键</param>
/// <returns>配置值默认string.Empty</returns>
public static string GetValue(string key) => ConfigurationManager.AppSettings[key] ?? string.Empty;
/// <summary>
/// 保存的文件名sqliteFile
/// </summary>
public static string GetSaveFileName => GetValue("saveFileName") ?? "";
/// <summary>
/// sqliteFile名
/// </summary>
public static string SqliteFileName => GetValue("sqliteFile") ?? "";
}
}

View File

@ -0,0 +1,26 @@
using CalendarNotepad.Models;
using SqlSugar;
namespace CalendarNotepad.Extends
{
/// <summary>
/// 数据库上下文
/// </summary>
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<WorkUnit>();
}
}
}

View File

@ -12,7 +12,7 @@ namespace CalendarNotepad.Extends
/// <summary>
/// 压缩文件名
/// </summary>
public static string ZipFileName { get; set; } = CNOptions.GetSaveFileName;
public static string ZipFileName { get; set; } = AppConfig.GetSaveFileName;
/// <summary>
/// 某个文件保存到压缩文件内
/// </summary>

View File

@ -1,23 +0,0 @@
using System.Configuration;
namespace CalendarNotepad.Models
{
/// <summary>
/// 应用配置
/// </summary>
public static class CNOptions
{
/// <summary>
/// 获取配置的值
/// </summary>
/// <param name="key">键</param>
/// <returns>值</returns>
public static string? GetValue(string key) => ConfigurationManager.AppSettings[key];
/// <summary>
/// 保存的文件名
/// </summary>
/// <returns></returns>
public static string GetSaveFileName => GetValue("saveFileName") ?? "";
}
}

View File

@ -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;

View File

@ -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());
}
}
}

131
CalendarNotepad/WorkMain.Designer.cs generated Normal file
View File

@ -0,0 +1,131 @@
namespace CalendarNotepad
{
partial class WorkMain
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing) {
if(disposing && (components != null)) {
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
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;
}
}

104
CalendarNotepad/WorkMain.cs Normal file
View File

@ -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<WorkUnitList>(str) ?? new WorkUnitList();
// if(wl == null) {
// return;
// }
// this.Db.Deleteable<WorkUnit>().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<WorkUnit>().Where(a => a.DayRange == dr && a.WorkDay == wd).ToList();
if(qu.Any() && msg.IsNullOrEmpty()) {
this.Db.Deleteable<WorkUnit>()
.Where(a => a.DayRange == dr && a.WorkDay == wd)
.ExecuteCommand();
return;
}
if(qu.Any() && msg.IsNotNullOrEmpty()) {
this.Db.Updateable<WorkUnit>()
.Where(a => a.DayRange == dr && a.WorkDay == wd)
.SetColumns(a => a.WorkMessage == msg)
.ExecuteCommand();
return;
}
if(!qu.Any() && msg.IsNotNullOrEmpty()) {
this.Db.Insertable<WorkUnit>(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<WorkUnit>().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<DateTime> GetBoldDays() {
var year = this.mcWorkDay.SelectionStart.Year;
var month = this.mcWorkDay.SelectionStart.Month;
var days = new List<DateTime>();
for(int i = 1;i < 32;i++) {
var wd = $"{year}{month.ToString("D2")}{i.ToString("D2")}";
var qu = this.Db.Queryable<WorkUnit>().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);
}
}
}

View File

@ -0,0 +1,120 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema
Version 2.0
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.
Example:
... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
<resheader name="reader">System.Resources.ResXResourceReader, System.Windows.Forms, ...</resheader>
<resheader name="writer">System.Resources.ResXResourceWriter, System.Windows.Forms, ...</resheader>
<data name="Name1"><value>this is my long string</value><comment>this is a comment</comment></data>
<data name="Color1" type="System.Drawing.Color, System.Drawing"">Blue</data>
<data name="Bitmap1" mimetype="application/x-microsoft.net.object.binary.base64">
<value>[base64 mime encoded serialized .NET Framework object]</value>
</data>
<data name="Icon1" type="System.Drawing.Icon, System.Drawing" mimetype="application/x-microsoft.net.object.bytearray.base64">
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>
There are any number of "resheader" rows that contain simple
name/value pairs.
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.
mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.
mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>