This commit is contained in:
2025-11-20 15:13:29 +08:00
parent b48e0b79dd
commit 979afae645
28 changed files with 1299 additions and 345 deletions

View File

@@ -28,12 +28,13 @@ namespace FATrace.WPLApp.ViewModels
{
private readonly IFreeSql _fsql;
private readonly ILogService _log;
private readonly CsvServices csvServices;
public RawProUseViewModel(IFreeSql fsql, ILogService log)
public RawProUseViewModel(IFreeSql fsql, ILogService log, CsvServices csvServices)
{
_fsql = fsql;
_log = log;
this.csvServices = csvServices;
Items = new ObservableCollection<RawProUseDto>();
// 集合变化时动态刷新导出命令可用性
Items.CollectionChanged += (s, e) =>
@@ -51,6 +52,8 @@ namespace FATrace.WPLApp.ViewModels
.ObservesProperty(() => IsBusy)
// Items 是集合引用,通常不会替换引用,这里通过 CollectionChanged 手动触发
;
ExportCSVCommand = new DelegateCommand(ExportToCsv, () => !IsBusy && Items.Count > 0)
.ObservesProperty(() => IsBusy);
ClearCommand = new DelegateCommand(ClearFilters, () => !IsBusy)
.ObservesProperty(() => IsBusy);
@@ -212,6 +215,7 @@ namespace FATrace.WPLApp.ViewModels
#region
public DelegateCommand SearchCommand { get; }
public DelegateCommand ExportCommand { get; }
public DelegateCommand ExportCSVCommand { get; }
public DelegateCommand ClearCommand { get; }
public DelegateCommand FirstPageCommand { get; }
public DelegateCommand PrevPageCommand { get; }
@@ -356,6 +360,63 @@ namespace FATrace.WPLApp.ViewModels
}
}
/// <summary>
/// 使用 CsvServices 将当前界面数据导出为 CSV 文件(每条记录一个文件,命名为 InBagCode.csv
/// </summary>
private void ExportToCsv()
{
if (IsBusy) return;
if (Items.Count == 0)
{
System.Windows.MessageBox.Show("无可导出的数据", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
return;
}
try
{
IsBusy = true;
var dr = "D:\\迅雷下载";
// 转换为 RawProUserCsvDto
var all = Items.Select(it => new RawProUserCsvDto
{
RawCode = it.RawCode,
RawName = it.RawName,
InBagCode = it.InBagCode,
BoxCode = it.BoxCode,
Batch = it.Batch,
ShelfLife = it.ShelfLife,
Weight = it.Weight,
DeliveryDate = it.WeightTime.ToString("yyyyMMdd"),
RemainWeight = it.RemainWeight,
StockWeight = it.StockWeight,
WeightTime = it.WeightTime,
OpUser = it.OpUser,
CheckUser = it.CheckUser,
OutTime = it.OutTime
}).ToList();
// 过滤 InBagCode 为空的记录,避免导出失败
var valid = all.Where(x => !string.IsNullOrWhiteSpace(x.InBagCode)).ToList();
int skipped = all.Count - valid.Count;
var svc = csvServices;
var paths = svc.ExportMany(valid, dr, overwrite: true);
_log.Info($"RawProUse CSV 导出完成: 目录={dr}, 成功={valid.Count}, 跳过={skipped}");
System.Windows.MessageBox.Show($"导出完成:成功 {valid.Count} 条,跳过 {skipped} 条。\n目录{dr}", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
}
catch (Exception ex)
{
_log.Error($"RawProUse CSV 导出失败: {ex}");
System.Windows.MessageBox.Show($"导出失败: {ex.Message}", "错误", MessageBoxButton.OK, MessageBoxImage.Error);
}
finally
{
IsBusy = false;
}
}
/// <summary>
/// 导出当前查询结果为 Excel.xlsx
/// </summary>