using HandyControl.Controls; using MiniExcelLibs; using Ping9719.WpfEx.Mvvm; using System; using System.Collections.Generic; using System.Linq; using System.Runtime.CompilerServices; using System.Text; using System.Threading; using System.Threading.Tasks; using System.Windows.Input; using System.Windows.Media; using SqlSugar; using HandyControl.Data; using System.Windows; using Newtonsoft.Json.Linq; using 货架标准上位机.Views.Controls; using WCS.Model.ApiModel.User; using 货架标准上位机.Api; using WCS.Model; using WCS.Model.ApiModel; using System.Windows.Controls; using WCS.Model.ApiModel.InterfaceRecord; using HandyControl.Collections; using WCS.Model.ApiModel.MatBaseInfo; using WCS.Model.ApiModel.MatInventoryDetail; using HandyControl.Tools.Extension; using WCS.Model.ApiModel.Stocktaking; using WCS.Model.ApiModel.InOutRecord; namespace 货架标准上位机.ViewModel { public class InOutRecordViewModel : BindableBase { public InOutRecordViewModel() { } public void InitMatCode() { //调用接口更新! Task.Run(() => { var body = new GetMatCodeListRequest() { IsFromBaseData = false, UserName = LocalStatic.CurrentUser, DeviceType = LocalFile.Config.DeviceType, }; var Result = ApiHelp.GetDataFromHttp>>(LocalFile.Config.ApiIpHost + "matBaseInfo/getMatCodeList", body, "POST"); if (Result != null && Result.Data != null && Result.Data.Count() > 0) { matCodes = Result.Data.Select(t => new DataModel() { MatCode = t }).ToList(); } }); } #region Property private List dataGridItemSource; public List DataGridItemSource { get { return dataGridItemSource; } set { SetProperty(ref dataGridItemSource, value); } } /// /// 物料编码 /// private string matCode; public string MatCode { get { return matCode; } set { SetProperty(ref matCode, value); FilterItems(value); } } public ManualObservableCollection Items { get; set; } = new(); private List matCodes = new List(); private void FilterItems(string key) { //至少输入三个字符 避免删除或输入时界面变卡 if (string.IsNullOrEmpty(key) || key.Length < 3) { Items.Clear(); return; } key = key.ToUpper(); Items.CanNotify = false; Items.Clear(); foreach (var matCode in matCodes) { if (matCode.MatCode.ToUpper().Contains(key)) { Items.Add(matCode); } } Items.CanNotify = true; } public class DataModel() { public string MatCode { get; set; } } /// /// 物料名称 /// private string matName; public string MatName { get { return matName; } set { SetProperty(ref matName, value); } } /// /// 物料批次 /// private string matBatch; public string MatBatch { get { return matBatch; } set { SetProperty(ref matBatch, value); } } /// /// 库位 /// private string storeCode; public string StoreCode { get { return storeCode; } set { SetProperty(ref storeCode, value); } } /// /// 物料条码 物料SN /// private string matSN; public string MatSN { get { return matSN; } set { SetProperty(ref matSN, value); } } #endregion #region Command public ICommand BtnResetCommand { get => new DelegateCommand(BtnReset); } public void BtnReset() { MatCode = string.Empty; MatName = string.Empty; MatSN = string.Empty; StoreCode = string.Empty; } public ICommand BtnSearchCommand { get => new DelegateCommand(BtnSearchReset); } public void BtnSearchReset() { BtnSearch(true); } public void BtnSearch(bool IsPageReset = true) { if (CurrentPage == 0 || IsPageReset) { CurrentPage = 1; return; } #region 调用接口获取数据 var dia = Dialog.Show(new TextDialog()); try { var body = new GetInOutRecordRequest() { MatName = MatName, MatSN = MatSN, MatBatch = MatBatch, MatCode = MatCode, StoreCode = StoreCode, UserName = LocalStatic.CurrentUser, DeviceType = LocalFile.Config.DeviceType, PageNumber = CurrentPage, PageSize = 10, }; var Result = ApiHelp.GetDataFromHttp>(LocalFile.Config.ApiIpHost + "inOutRecord/getInOutRecord", body, "POST"); if (Result != null && Result.Data != null && Result.Data.Lists != null) { DataGridItemSource = Result.Data.Lists; MaxPage = Result.Data.MaxPage; TotalCount = Result.Data.TotalCount; } } catch (Exception ex) { Growl.Error("加载数据失败:" + ex.Message); } finally { dia.Close(); } #endregion } public ICommand BtnExportCommand { get => new DelegateCommand(BtnExport); } public async void BtnExport() { try { #region 选择文件保存路径 Microsoft.Win32.SaveFileDialog sfd = new Microsoft.Win32.SaveFileDialog(); sfd.Filter = ".xlsx文件(*.xlsx)|*.xlsx"; sfd.FileName = "出入库记录" + DateTime.Now.ToString("yyyyMMddhhmmss"); sfd.OverwritePrompt = true; if (sfd.ShowDialog() != true) { return; } string path = sfd.FileName; #endregion #region 调用接口导出数据 var body = new GetInOutRecordRequest() { MatName = MatName, MatSN = MatSN, MatBatch = MatBatch, MatCode = MatCode, StoreCode = StoreCode, UserName = LocalStatic.CurrentUser, DeviceType = LocalFile.Config.DeviceType, PageNumber = CurrentPage, PageSize = 10, }; await ApiHelp.PostDownloadFileAsync(path, System.Net.Http.HttpMethod.Post, LocalFile.Config.ApiIpHost + "inOutRecord/exportInOutRecord", body); Growl.Success("导出成功!"); #endregion } catch (Exception ex) { Growl.Error("导出失败:" + ex.Message); } } #endregion #region PageOperation 分页操作 private int currentPage; public int CurrentPage { get { return currentPage; } set { SetProperty(ref currentPage, value); BtnSearch(false); } } private int maxPage; public int MaxPage { get { return maxPage; } set { SetProperty(ref maxPage, value); } } //总数量 private int totalCount; public int TotalCount { get { return totalCount; } set { SetProperty(ref totalCount, value); } } public ICommand BtnFirstPageCommand { get => new DelegateCommand(BtnFirstPage); } public void BtnFirstPage() { CurrentPage = 1; } public ICommand BtnPrePageCommand { get => new DelegateCommand(BtnPrePage); } public void BtnPrePage() { if (CurrentPage > 1) { CurrentPage--; } } public ICommand BtnNextPageCommand { get => new DelegateCommand(BtnNextPage); } public void BtnNextPage() { if (CurrentPage < MaxPage) { CurrentPage++; } } public ICommand BtnLastPageCommand { get => new DelegateCommand(BtnLastPage); } public void BtnLastPage() { if (CurrentPage != MaxPage) { CurrentPage = MaxPage; } } #endregion } }