Skip to content

Commit

Permalink
🔥 导出文档进度条优化
Browse files Browse the repository at this point in the history
  • Loading branch information
TeslaFly01 committed Apr 1, 2023
1 parent dce6fe9 commit 9c47875
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 27 deletions.
5 changes: 5 additions & 0 deletions SmartSQL/SmartSQL.DocUtils/DBDoc/ChmDoc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ private bool BuildDoc(string filePath)

ZlpIOHelper.WriteAllText(Path.Combine(this.WorkTmpDir, "数据库目录.html"), list, Utf8);
int count = 0;
int count_total = Dto.Tables.Count + Dto.Views.Count + Dto.Procs.Count ;
//生成表
foreach (var tab in this.Dto.Tables)
{
Expand All @@ -91,6 +92,7 @@ private bool BuildDoc(string filePath)
base.OnProgress(new ChangeRefreshProgressArgs
{
BuildNum = count,
TotalNum = count_total,
BuildName = tab.TableName
});
}
Expand All @@ -113,6 +115,7 @@ private bool BuildDoc(string filePath)
base.OnProgress(new ChangeRefreshProgressArgs
{
BuildNum = count,
TotalNum = count_total,
BuildName = item.ObjectName
});
}
Expand All @@ -135,6 +138,7 @@ private bool BuildDoc(string filePath)
base.OnProgress(new ChangeRefreshProgressArgs
{
BuildNum = count,
TotalNum = count_total,
BuildName = item.ObjectName
});
}
Expand All @@ -150,6 +154,7 @@ private bool BuildDoc(string filePath)
base.OnProgress(new ChangeRefreshProgressArgs
{
BuildNum = count,
TotalNum = count_total,
IsEnd = true
});
return true;
Expand Down
2 changes: 2 additions & 0 deletions SmartSQL/SmartSQL.DocUtils/DBDoc/ExcelDoc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ public void ExportExcelByEpplus(string fileName, DBDto dto)
base.OnProgress(new ChangeRefreshProgressArgs
{
BuildNum = tables.Count,
TotalNum = tables.Count,
IsEnd = true
});
}
Expand Down Expand Up @@ -281,6 +282,7 @@ private void CreateTableSheet(OfficeOpenXml.ExcelPackage epck, string sheetName,
base.OnProgress(new ChangeRefreshProgressArgs
{
BuildNum = count,
TotalNum = tables.Count,
BuildName = table.TableName
});
}
Expand Down
5 changes: 5 additions & 0 deletions SmartSQL/SmartSQL.DocUtils/DBDoc/MarkDownDoc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ private bool BuildDoc(string filePath)
sb.Append(dirMD);
sb.AppendLine();
int count = 0;
int count_total = Dto.Tables.Count + Dto.Views.Count + Dto.Procs.Count;
if (this.Dto.Tables.Any())
{
sb.Append("## 📒 表结构");
Expand All @@ -56,6 +57,7 @@ private bool BuildDoc(string filePath)
base.OnProgress(new ChangeRefreshProgressArgs
{
BuildNum = count,
TotalNum = count_total,
BuildName = dto.TableName
});
}
Expand All @@ -78,6 +80,7 @@ private bool BuildDoc(string filePath)
base.OnProgress(new ChangeRefreshProgressArgs
{
BuildNum = count,
TotalNum = count_total,
BuildName = item.ObjectName
});
}
Expand All @@ -100,6 +103,7 @@ private bool BuildDoc(string filePath)
base.OnProgress(new ChangeRefreshProgressArgs
{
BuildNum = count,
TotalNum = count_total,
BuildName = item.ObjectName
});
}
Expand All @@ -111,6 +115,7 @@ private bool BuildDoc(string filePath)
base.OnProgress(new ChangeRefreshProgressArgs
{
BuildNum = count,
TotalNum = count_total,
IsEnd = true
});
return true;
Expand Down
5 changes: 5 additions & 0 deletions SmartSQL/SmartSQL.DocUtils/DBDoc/PdfDoc.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@ private void ExportPdfByITextSharp(string fileName, string fontPath, DBDto dto)
pdfDocument.Add(tableChapter);
pdfDocument.Add(new Paragraph("\n", pdfFont)); // 换行
int count = 0;
int count_total = tables.Count + dto.Views.Count + dto.Procs.Count ;
foreach (var table in tables)
{
string docTableName = table.TableName + " " + (!string.IsNullOrWhiteSpace(table.Comment) ? table.Comment : "");
Expand Down Expand Up @@ -181,6 +182,7 @@ private void ExportPdfByITextSharp(string fileName, string fontPath, DBDto dto)
base.OnProgress(new ChangeRefreshProgressArgs
{
BuildNum = count,
TotalNum = count_total,
BuildName = table.TableName
});
}
Expand Down Expand Up @@ -210,6 +212,7 @@ private void ExportPdfByITextSharp(string fileName, string fontPath, DBDto dto)
base.OnProgress(new ChangeRefreshProgressArgs
{
BuildNum = count,
TotalNum = count_total,
BuildName = item.ObjectName
});
}
Expand Down Expand Up @@ -242,6 +245,7 @@ private void ExportPdfByITextSharp(string fileName, string fontPath, DBDto dto)
base.OnProgress(new ChangeRefreshProgressArgs
{
BuildNum = count,
TotalNum = count_total,
BuildName = item.ObjectName
});
}
Expand All @@ -253,6 +257,7 @@ private void ExportPdfByITextSharp(string fileName, string fontPath, DBDto dto)
base.OnProgress(new ChangeRefreshProgressArgs
{
BuildNum =count,
TotalNum = count_total,
IsEnd = true
});
}
Expand Down
7 changes: 4 additions & 3 deletions SmartSQL/SmartSQL/UserControl/Controls/ExportLoading.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:svgc="http://sharpvectors.codeplex.com/svgc/"
IsVisibleChanged="HandleVisibleChanged"
Loaded="UserControl_Loaded"
mc:Ignorable="d">
<UserControl.Background>
<SolidColorBrush Opacity="{Binding BackgroundOpacity}" Color="Black" />
Expand All @@ -32,14 +33,14 @@
</StackPanel>
<TextBlock
x:Name="ProgressTitleText"
Margin="50,30,50,0"
Margin="50,40,50,0"
HorizontalAlignment="Left"
FontSize="10"
Foreground="#383c46"
Text="正在导出中..." />
Text="{Binding ProgressTitle, Mode=TwoWay}" />
<ProgressBar
x:Name="ProgressBar"
Margin="50,5,50,0"
Margin="50,6,50,0"
HorizontalAlignment="Stretch"
Orientation="Horizontal"
ValueChanged="ProgressBar_OnValueChanged"
Expand Down
35 changes: 31 additions & 4 deletions SmartSQL/SmartSQL/UserControl/Controls/ExportLoading.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
using System;
using SmartSQL.Annotations;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
Expand All @@ -19,8 +22,17 @@ namespace SmartSQL.UserControl.Controls
/// <summary>
/// Loading.xaml 的交互逻辑
/// </summary>
public partial class ExportLoading : System.Windows.Controls.UserControl
public partial class ExportLoading : System.Windows.Controls.UserControl, INotifyPropertyChanged
{

public event PropertyChangedEventHandler PropertyChanged;

[NotifyPropertyChangedInvocator]
protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}

public static readonly DependencyProperty BackgroundOpacityProperty = DependencyProperty.Register(
"BackgroundOpacity", typeof(double), typeof(ExportLoading), new PropertyMetadata(default(double)));

Expand Down Expand Up @@ -54,7 +66,11 @@ public string ProgressTitle
public double ProgressNum
{
get => (double)GetValue(ProgressNumProperty);
set => SetValue(ProgressNumProperty, value);
set
{
SetValue(ProgressNumProperty, value);
OnPropertyChanged(nameof(ProgressNum));
}
}

public ExportLoading()
Expand All @@ -65,6 +81,11 @@ public ExportLoading()
animationTimer.Interval = new TimeSpan(0, 0, 0, 0, 90);
}

private void UserControl_Loaded(object sender, RoutedEventArgs e)
{
ProgressTitle = "正在为您准备导出文档,请耐心等候";
}

#region Data
private readonly DispatcherTimer animationTimer;
#endregion
Expand Down Expand Up @@ -124,15 +145,21 @@ private void HandleVisibleChanged(object sender,
bool isVisible = (bool)e.NewValue;

if (isVisible)
{
Start();
}
else
{
Stop();
ProgressTitle="正在为您准备导出文档,请耐心等候";
ProgressNum=0;
}
}
#endregion

private void ProgressBar_OnValueChanged(object sender, RoutedPropertyChangedEventArgs<double> e)
{
ProgressTitleText.Text = $"正在导出 {ProgressTitle}";
//ProgressTitleText.Text = $"正在导出 {ProgressTitle}";
}
}
}
54 changes: 34 additions & 20 deletions SmartSQL/SmartSQL/Views/ExportDoc.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
Expand Down Expand Up @@ -984,24 +985,25 @@ private void BtnExport_OnClick(object sender, RoutedEventArgs e)
var dbDto = new DBDto(selectedDatabase.DbName);
Task.Factory.StartNew(() =>
{
//文档标题
dbDto.DocTitle = docTitle;
//数据库类型
dbDto.DBType = selectedConnection.DbType.ToString();
//对象列表
dbDto.Tables = Trans2Table(exportData, selectedConnection, selectedDatabase);
dbDto.Procs = Trans2Dictionary(exportData, selectedConnection, selectedDatabase, "Proc");
dbDto.Views = Trans2Dictionary(exportData, selectedConnection, selectedDatabase, "View");

//判断文档路径是否存在
if (!Directory.Exists(floderPath))
{
Directory.CreateDirectory(floderPath);
}
var filePath = Path.Combine(floderPath, fileName);
var doc = DocFactory.CreateInstance((DocType)(Enum.Parse(typeof(DocType), doctype)), dbDto);
try
{
//文档标题
dbDto.DocTitle = docTitle;
//数据库类型
dbDto.DBType = selectedConnection.DbType.ToString();
//对象列表
dbDto.Tables = Trans2Table(exportData, selectedConnection, selectedDatabase);
dbDto.Procs = Trans2Dictionary(exportData, selectedConnection, selectedDatabase, "Proc");
dbDto.Views = Trans2Dictionary(exportData, selectedConnection, selectedDatabase, "View");

//判断文档路径是否存在
if (!Directory.Exists(floderPath))
{
Directory.CreateDirectory(floderPath);
}
var filePath = Path.Combine(floderPath, fileName);
var doc = DocFactory.CreateInstance((DocType)(Enum.Parse(typeof(DocType), doctype)), dbDto);

doc.ChangeRefreshProgressEvent += Doc_ChangeRefreshProgressEvent;
var bulResult = doc.Build(filePath);
Dispatcher.Invoke(() =>
Expand Down Expand Up @@ -1036,11 +1038,21 @@ private void BtnExport_OnClick(object sender, RoutedEventArgs e)
/// <param name="e"></param>
private void Doc_ChangeRefreshProgressEvent(object sender, DocUtils.DBDoc.Doc.ChangeRefreshProgressArgs e)
{
Dispatcher.Invoke(() =>
Dispatcher.BeginInvoke(new Action(() =>
{
LoadingG.ProgressTitle = e.BuildName;
LoadingG.ProgressNum = double.Parse((e.TotalNum / e.BuildNum).ToString());
});
var d = (e.BuildNum * 1.0f / e.TotalNum) * 100;
var pNum = double.Parse(d.ToString());
LoadingG.ProgressNum = pNum;
if (!e.IsEnd)
{
LoadingG.ProgressTitle = e.BuildName;
}
else
{
LoadingG.ProgressTitle = "文档生成完毕";
}
}));
Thread.Sleep(50);
}

private List<ViewProDto> Trans2Dictionary(List<TreeNodeItem> treeViewData, ConnectConfigs selectedConnection, DataBase selectedDatabase, string type)
Expand Down Expand Up @@ -1200,6 +1212,8 @@ private List<TableDto> Trans2Table(List<TreeNodeItem> treeViewData, ConnectConfi
#endregion
}



/// <summary>
/// 取消
/// </summary>
Expand Down

0 comments on commit 9c47875

Please sign in to comment.