CalendarNotepad/CalendarNotepad/WorkMain.cs
2024-10-08 11:10:41 +08:00

482 lines
18 KiB
C#

using CalendarNotepad.Extends;
using CalendarNotepad.Models;
using Microsoft.VisualBasic;
using SqlSugar;
using System.Diagnostics;
using System.Reflection;
namespace CalendarNotepad
{
public partial class WorkMain:Form
{
public DbContext Db { get; set; } = new DbContext();
public WorkMain() {
InitializeComponent();
string version = Assembly.GetExecutingAssembly().GetName().Version?.ToString() ?? "";
version = version == "" ? "" : $"({version})";
this.Text = $"工作日志记录{version}";
this.rtbMsg.EnableAutoDragDrop = true;
this.OnDataReset();
this.rtbMsg.TextChanged += RtbMsg_TextChanged;
foreach(var c in this.C_WorkTypes.Controls) {
var rab = c as RadioButton;
if(rab != null) {
rab.CheckedChanged += Rab_CheckedChanged;
}
}
foreach(var c in this.C_WorkOut.Controls) {
var rab = c as RadioButton;
if(rab != null) {
rab.CheckedChanged += Rab_CheckedChanged;
}
}
var fieViewWidth = this.FileListView.Width;
this.FileListView.Columns.Add(new ColumnHeader {
Text = "附件",Width = fieViewWidth - 200,TextAlign = HorizontalAlignment.Left,
});
this.FileListView.Columns.Add(new ColumnHeader {
Text = "类型",Width = 100,TextAlign = HorizontalAlignment.Left,
});
this.FileListView.Columns.Add(new ColumnHeader {
Text = "文件指纹",Width = 100,TextAlign = HorizontalAlignment.Left,
});
this.FileListView.AllowDrop = true;
}
private void Rab_CheckedChanged(object? sender,EventArgs e) {
this.DataChanged();
}
private void RtbMsg_TextChanged(object? sender,EventArgs e) {
this.DataChanged();
}
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");
this.FileListView.Items.Clear();
var qu = this.Db.Queryable<WorkUnit>().Where(a => a.WorkDay == wd).ToList();
if(!qu.Any()) {
this.rtbMsg.Text = "";
this.SetWorkType("休假");
this.SetWorkOut("无");
}
if(qu.Any()) {
var fir = qu.First();
this.rtbMsg.Text = fir.WorkMessage;
this.SetWorkType(fir.WorkType);
this.SetWorkOut(fir.Workout ?? "无");
var plsq = this.Db.Queryable<WorkUnitPlusFile>().Where(a => a.WorkDay == wd).ToList();
if(plsq.Any()) {
foreach(var i in plsq) {
var lit1 = new ListViewItem(i.FileName);
lit1.SubItems.Add(new ListViewItem.ListViewSubItem { Text = "已导入" });
lit1.SubItems.Add(new ListViewItem.ListViewSubItem { Text = i.FileKey });
this.FileListView.Items.Add(lit1);
}
}
}
this.OnDataReset();
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(sender,new DateRangeEventArgs(this.mcWorkDay.SelectionStart,this.mcWorkDay.SelectionEnd));
Task.Factory.StartNew(() => {
try {
while(true) {
var size = new FileInfo(this.Db.SqliteDbfile).Length;
this.Invoke(() => {
this.lab_m_dbsize.Text = $"数据库大小({size})";
});
Task.Delay(TimeSpan.FromSeconds(1));
}
}
catch(Exception) { }
});
}
/// <summary>
/// 获取工作类型
/// </summary>
/// <returns></returns>
private string GetWorkType() {
foreach(Control c in this.C_WorkTypes.Controls) {
var rb = c as RadioButton;
if(rb != null && rb.Checked) {
return rb.Text;
}
}
return "";
}
/// <summary>
/// 设置工作类型
/// </summary>
/// <param name="t">工作类型</param>
private void SetWorkType(string? t) {
if(t.IsNullOrEmpty()) {
return;
}
foreach(Control c in this.C_WorkTypes.Controls) {
var rb = c as RadioButton;
if(rb != null && rb.Text == t) {
rb.Checked = true;
return;
}
}
}
private void btSave_Click(object sender,EventArgs e) {
var wd = this.mcWorkDay.SelectionStart.ToString("yyyyMMdd");
var msg = this.rtbMsg.Text;
var wt = GetWorkType();
var wo = this.GetWorkOut();
var exp = Expressionable.Create<WorkUnit>().And(a => a.WorkDay == wd).ToExpression();
var qu = this.Db.Queryable<WorkUnit>().Where(exp).ToList();
if(qu.Any() && msg.IsNullOrEmpty()) {
this.Db.Deleteable<WorkUnit>().Where(exp).ExecuteCommand();
this.OnDataReset();
return;
}
if(qu.Any() && msg.IsNotNullOrEmpty()) {
this.Db.Updateable<WorkUnit>().Where(exp)
.SetColumns(a => a.WorkMessage == msg)
.SetColumns(a => a.WorkType == wt)
.SetColumns(a => a.Workout == wo)
.ExecuteCommand();
this.OnDataReset();
return;
}
if(!qu.Any() && msg.IsNotNullOrEmpty()) {
this.Db.Insertable<WorkUnit>(new WorkUnit {
WorkDay = wd,
WorkMessage = msg,WorkType = wt,Workout = wo,
}).ExecuteCommand();
this.OnDataReset();
return;
}
}
private void btReset_Click(object sender,EventArgs e) {
mcWorkDay_DateChanged(sender,new DateRangeEventArgs(this.mcWorkDay.SelectionStart,this.mcWorkDay.SelectionEnd));
this.OnDataReset();
}
private void DataChanged() {
this.btSave.Enabled = true;
this.btReset.Enabled = true;
}
private void OnDataReset() {
this.btSave.Enabled = false;
this.btReset.Enabled = false;
}
/// <summary>
/// 设置加班信息
/// </summary>
/// <param name="wo"></param>
private void SetWorkOut(string wo) {
if(wo.IsNullOrEmpty()) {
return;
}
foreach(var c in this.C_WorkOut.Controls) {
var ra = c as RadioButton;
if(ra != null && ra.Text == wo) {
ra.Checked = true;
return;
}
}
}
/// <summary>
/// 获取加班信息
/// </summary>
/// <returns></returns>
private string GetWorkOut() {
foreach(var c in this.C_WorkOut.Controls) {
var ra = c as RadioButton;
if(ra != null && ra.Checked) {
return ra.Text;
}
}
return "";
}
private void rtbMsg_LinkClicked(object sender,LinkClickedEventArgs e) {
if(e.LinkText.IsNullOrEmpty()) {
return;
}
try {
Process.Start(new ProcessStartInfo {
FileName = e.LinkText,
UseShellExecute = true,
});
}
catch(Exception ex) {
MessageBox.Show(ex.ToString());
}
}
private void tsp_Copy_Click(object sender,EventArgs e) {
if(this.rtbMsg.SelectedText.IsNullOrEmpty()) {
this.rtbMsg.SelectAll();
}
Clipboard.SetText(this.rtbMsg.SelectedText);
}
private void tsb_palse_Click(object sender,EventArgs e) {
this.rtbMsg.AppendText(Clipboard.GetText());
}
private void rtbMsg_TextChanged_1(object sender,EventArgs e) {
}
private void ToolStripMenuItem_Click(object sender,EventArgs e) {
var wd = this.mcWorkDay.SelectionStart.ToString("yyyyMMdd");
var items = this.FileListView.SelectedItems;
if(items.Count == 0) {
MessageBox.Show("没有选择列表项!");
return;
}
if(MessageBox.Show($"共选择了{items.Count}项,是否全部删除?","警告",MessageBoxButtons.OKCancel) == DialogResult.Cancel) {
return;
}
foreach(ListViewItem i in items) {
this.FileListView.Items.Remove(i);
this.Db.Deleteable<WorkUnitPlusFile>()
.Where(a => a.WorkDay == wd && a.FileKey == i.SubItems[2].Text)
.ExecuteCommand();
}
}
private void ToolStripMenuItem_Click(object sender,EventArgs e) {
var dlgFile = OpenFileDlg.ShowDialog();
if(dlgFile == DialogResult.Cancel) {
return;
}
if(OpenFileDlg.SafeFileNames.Length == 0) {
return;
}
var wd = this.mcWorkDay.SelectionStart.ToString("yyyyMMdd");
foreach(var f in OpenFileDlg.FileNames) {
var fk = PlusFile.SaveFileToDb(this.Db,f);
this.Db.Insertable(new WorkUnitPlusFile {
FileKey = fk,
WorkDay = wd,
FileName = Path.GetFileName(f),
}).ExecuteCommand();
this.FileListView.Items.Add(new ListViewFileModel(f,fk,"已导入").ToListViewItem());
}
}
private void ToolStripMenuItem_Click(object sender,EventArgs e) {
var items = this.FileListView.SelectedItems;
if(items.Count == 0) {
MessageBox.Show("没有选择文件");
return;
}
foreach(ListViewItem i in items) {
var sdlg = new SaveFileDialog {
Title = "保存附件",
FileName = Path.GetFileName(i.Text),
};
var dresult = sdlg.ShowDialog();
if(dresult != DialogResult.OK) {
return;
}
var filePath = sdlg.FileName;
var fileKey = i.SubItems[2].Text;
var qu = this.Db.Queryable<PlusFileUnit>().Where(a => a.FileId == fileKey).ToList();
if(qu.Any()) {
var fir = qu.First();
if(fir.FileContent != null) {
PlusFile.SaveFile(filePath,fir.FileContent);
}
}
}
}
private void bt_m_ys_Click(object sender,EventArgs e) {
var r = this.Db.DbVacuum();
MessageBox.Show(r);
}
private void bt_m_Clean_Click(object sender,EventArgs e) {
var r = this.Db.DbClean();
MessageBox.Show(r);
}
private void bt_m_Backup_Click(object sender,EventArgs e) {
var sdlg = new SaveFileDialog {
Title = "备份数据库",
FileName = Path.GetFileName($"workdb.{DateTime.Now.ToString("yyyyMMdd")}.bak"),
};
var dresult = sdlg.ShowDialog();
if(dresult != DialogResult.OK) {
return;
}
var filePath = sdlg.FileName;
//File.Copy(this.Db.SqliteDbfile,filePath);
this.Db.DbMaintenance.BackupDataBase(null,filePath);
MessageBox.Show("备份完成");
}
private void tsb_loadin_Click(object sender,EventArgs e) {
ToolStripMenuItem_Click(sender,EventArgs.Empty);
}
private void bt_m_dbshc_Click(object sender,EventArgs e) {
var dlg = new dlgDatabaseSchemas();
dlg.Show();
}
private void ToolStripMenuItem_Click(object sender,EventArgs e) {
var items = this.FileListView.SelectedItems;
if(items.Count == 0) {
MessageBox.Show("必须要选择更名的文件");
return;
}
if(items.Count > 1) {
MessageBox.Show("不能同时对多个文件更名,只能选择一个");
return;
}
var item = items[0];
var fileName = item.Text;
var fileKey = item.SubItems[2].Text;
var dlgr = Interaction.InputBox($"原文件名:{fileName}",Title: "修改文件名",DefaultResponse: fileName);
if(dlgr == "") {
return;
}
if(fileName == dlgr) {
return;
}
var wd = this.WorkDayKey;
this.Db.Updateable<WorkUnitPlusFile>().Where(a => a.WorkDay == wd && a.FileKey == fileKey).SetColumns(a => a.FileName == dlgr).ExecuteCommand();
item.Text = dlgr;
MessageBox.Show("文件名修改完成");
}
private void ToolStripMenuItem_Click(object sender,EventArgs e) {
var items = this.FileListView.SelectedItems;
if(items.Count == 0) {
MessageBox.Show("必须要选择打开的文件");
return;
}
if(items.Count > 1) {
MessageBox.Show("不能同时打开对多个文件,只能选择一个");
return;
}
var item = items[0];
var fk = item.SubItems[2].Text;
var fileName = item.Text;
var qu = this.Db.Queryable<PlusFileUnit>()
.Where(a => a.FileId == fk)
.Select(a => a.FileContent)
.ToList();
if(!qu.Any() || qu.First() == null) {
MessageBox.Show("打开文件错误,没有找到文件,需要重新导入文件!");
return;
}
var fc = qu.First();
var tempPath = Path.GetTempPath();
//var ran = new Random();
//var fn = ran.NextInt64(10000,99999).ToString();
var path = Path.Combine(tempPath,"CalendarNotepadFile");
if(!Directory.Exists(path)) {
Directory.CreateDirectory(path);
}
var fullPath = Path.Combine(path,fileName);
if(File.Exists(fullPath)) {
File.Delete(fullPath);
}
using(var fs = File.Create(fullPath,fc.Length)) {
fs.Write(fc,0,fc.Length);
fs.Close();
}
Process process = new Process();
process.StartInfo.FileName = fullPath;
process.StartInfo.UseShellExecute = true;
process.EnableRaisingEvents = true;
process.Exited += new EventHandler((s,e) => {
File.Delete(fullPath);
});
// 启动进程
process.Start();
}
private void FileListView_DoubleClick(object sender,EventArgs e) {
ToolStripMenuItem_Click(sender,e);
}
private void FileListView_DragEnter(object sender,DragEventArgs e) {
if(e.Data == null) {
e.Effect = DragDropEffects.None;
return;
}
var isfd = e.Data.GetDataPresent(DataFormats.FileDrop);
if(isfd) {
e.Effect = DragDropEffects.Copy;
return;
}
e.Effect = DragDropEffects.None;
}
private void FileListView_DragDrop(object sender,DragEventArgs e) {
if(e.Data == null) {
return;
}
string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
var wd = this.mcWorkDay.SelectionStart.ToString("yyyyMMdd");
foreach(string file in files) {
MessageBox.Show(file);
var fk = PlusFile.SaveFileToDb(this.Db,file);
this.Db.Insertable(new WorkUnitPlusFile {
FileKey = fk,
WorkDay = wd,
FileName = Path.GetFileName(file),
}).ExecuteCommand();
this.FileListView.Items.Add(new ListViewFileModel(file,fk,"已导入").ToListViewItem());
}
}
/// <summary>
/// 当前记录日志的键
/// </summary>
public string WorkDayKey {
get => this.mcWorkDay.SelectionStart.ToString("yyyyMMdd");
}
}
}