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 dataGridItemSource; public ObservableCollection 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>>(path, System.Net.Http.HttpMethod.Post, LocalFile.Config.ApiIpHost + "outstore/importMat", LocalStatic.CurrentUser, LocalFile.Config.DeviceType); if (result.Code == 200) { DataGridItemSource = new ObservableCollection(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(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(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 } }