Files
wcs/货架标准上位机/ViewModels/OutInventoryImportDucumentViewModel.cs
2024-10-17 12:59:41 +08:00

213 lines
7.5 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

using HandyControl.Controls;
using Ping9719.WpfEx.Mvvm;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.IO;
using System.Linq;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Input;
using WCS.BLL.DbModels;
using WCS.Model;
using WCS.Model.ApiModel.MatBaseInfo;
using WCS.Model.ApiModel.MatInventoryDetail;
using WCS管理系统.Api;
namespace WCS管理系统.ViewModels
{
public class OutInventoryImportDucumentViewModel : BindableBase
{
#region Property
private ObservableCollection<MatInventorySummaryModel> dataGridItemSource;
public ObservableCollection<MatInventorySummaryModel> DataGridItemSource
{
get { return dataGridItemSource; }
set
{
SetProperty(ref dataGridItemSource, value);
}
}
private MatInventorySummaryModel selectedItemSource;
public MatInventorySummaryModel SelectedItemSource
{
get { return selectedItemSource; }
set
{
SetProperty(ref selectedItemSource, value);
}
}
#endregion
#region Command
public ICommand BtnDownloadExcelCommand { get => new DelegateCommand(BtnDownloadExcel); }
public async void BtnDownloadExcel()
{
try
{
#region
Microsoft.Win32.SaveFileDialog sfd = new Microsoft.Win32.SaveFileDialog();
sfd.Filter = ".xlsx文件(*.xlsx)|*.xlsx";
sfd.FileName = "出库导入模板";
sfd.Title = "请选择文件保存地址";
sfd.OverwritePrompt = true;
if (sfd.ShowDialog() != true)
{
return;
}
string path = sfd.FileName;
#endregion
#region
string sourceFile = System.Environment.CurrentDirectory + "\\Excel\\出库单据导入模板.xlsx"; // 源文件路径
string destinationFile = path; // 目标文件路径
try
{
File.Copy(sourceFile, destinationFile, true); // true表示如果目标文件存在则覆盖它
Growl.Success("文件下载成功!");
}
catch (IOException ioEx)
{
Growl.Success("文件下载失败: " + ioEx.Message);
}
catch (Exception ex)
{
Growl.Success("文件下载失败: " + ex.Message);
}
#endregion
}
catch (Exception ex)
{
Growl.Error("导出失败:" + ex.Message);
}
}
public ICommand BtnImportCommand { get => new DelegateCommand(BtnImport); }
public async void BtnImport()
{
try
{
#region
Microsoft.Win32.OpenFileDialog ofd = new Microsoft.Win32.OpenFileDialog();
ofd.Title = "选择模板";
ofd.Filter = ".xlsx文件(*.xlsx)|*.xlsx";
ofd.Multiselect = false;
if (ofd.ShowDialog() != true)
{
return;
}
#endregion
//已经选择文件 调用接口进行导入数据
string path = ofd.FileName;
#region
var result = await ApiHelp.PostImportFileAsync<ResponseCommon<List<MatInventorySummaryModel>>>(path, System.Net.Http.HttpMethod.Post,
LocalFile.Config.ApiIpHost + "outstore/importMat", LocalStatic.CurrentUser, LocalFile.Config.DeviceType);
if (result.Code == 200)
{
DataGridItemSource = new ObservableCollection<MatInventorySummaryModel>(result.Data);
}
else
{
if (result != null && !string.IsNullOrEmpty(result.Message))
HandyControl.Controls.MessageBox.Show(result.Message);
else
{
HandyControl.Controls.MessageBox.Show("导入失败,请重试!");
}
}
#endregion
}
catch (Exception ex)
{
Growl.Warning("导入失败:" + ex.Message);
}
}
public ICommand DelCommand { get => new DelegateCommand<MatInventorySummaryModel>(Del); }
public void Del(MatInventorySummaryModel obj)
{
try
{
DataGridItemSource.Remove(obj);
;
}
catch (Exception ex)
{
}
}
// 定义一个事件,当需要关闭窗口时触发
public event Action TrueClose;
public event Action FalseClose;
// 一个方法,当满足某些条件时调用,以触发关闭窗口
protected virtual void OnRequestClose(bool IsTrue)
{
if (IsTrue)
TrueClose?.Invoke();
else
FalseClose?.Invoke();
}
public ICommand GenerateOutOrderCommand { get => new DelegateCommand(GenerateOutOrder); }
public void GenerateOutOrder()
{
//数据校验
if (DataGridItemSource == null || DataGridItemSource.Count == 0)
{
HandyControl.Controls.MessageBox.Show("选择的物料为空无法生成出库单!");
return;
}
foreach (var itemSource in DataGridItemSource)
{
if (itemSource.NeedQty == 0)
{
Growl.Warning("需求数量未填!");
SelectedItemSource = itemSource;
return;
}
}
#region
try
{
var body = new SysOutOrderByMatCodeRequest()
{
OrderType = "出库",
OrderSource = "WCS前端",
ItemList = DataGridItemSource.Select(t => new MatCodeItemList()
{
MatCode = t.MatCode,
MatName = t.MatName,
MatBatch = t.MatBatch,
ReqQty = t.NeedQty,
}).ToList(),
DeviceType = LocalFile.Config.DeviceType,
UserName = LocalStatic.CurrentUser
};
var Result = ApiHelp.GetDataFromHttp<ResponseCommon>(LocalFile.Config.ApiIpHost + "outstore/sysOutOrderByMatCode", body, "POST");
if (Result != null && Result.Code == 200)
{
Growl.Success(Result.Message);
OnRequestClose(true);
}
else if (Result != null)
{
Growl.Warning(Result.Message);
return;
}
}
catch (Exception ex)
{
Growl.Error("加载数据失败:" + ex.Message);
}
finally
{
}
#endregion
}
#endregion
}
}