Files
wcs/货架标准上位机/ViewModels/MatBaseInfoViewModel.cs
2025-01-11 11:25:24 +08:00

484 lines
16 KiB
C#

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 WCS.BLL.DbModels;
using WCS.Model.ApiModel.MatBaseInfo;
using System.Collections.ObjectModel;
using HandyControl.Tools.Extension;
using .Tool;
namespace .ViewModel
{
public class MatBaseInfoViewModel : BindableBase
{
#region Property
private ObservableCollection<MatBaseInfoModel> dataGridItemSource;
public ObservableCollection<MatBaseInfoModel> DataGridItemSource
{
get { return dataGridItemSource; }
set
{
SetProperty(ref dataGridItemSource, value);
}
}
public MatBaseInfoModel selectedataGridItem;
public MatBaseInfoModel SelectedataGridItem
{
get { return selectedataGridItem; }
set
{
SetProperty(ref selectedataGridItem, value);
}
}
/// <summary>
/// 物料编码
/// </summary>
private string matCode;
public string MatCode
{
get { return matCode; }
set
{
SetProperty(ref matCode, value);
}
}
/// <summary>
/// 物料名称
/// </summary>
private string matName;
public string MatName
{
get { return matName; }
set
{
SetProperty(ref matName, value);
}
}
/// <summary>
/// 物料规格
/// </summary>
private string matSpec;
public string MatSpec
{
get { return matSpec; }
set
{
SetProperty(ref matSpec, value);
}
}
/// <summary>
/// 请求时间的类型
/// </summary>
private bool? isEnable;
public bool? IsEnable
{
get { return isEnable; }
set
{
SetProperty(ref isEnable, value);
}
}
#endregion
#region Command
public ICommand BtnResetCommand { get => new DelegateCommand(BtnReset); }
public void BtnReset()
{
MatCode = string.Empty;
MatName = string.Empty;
MatSpec = string.Empty;
IsEnable = null;
}
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 GetMatBaseInfoRequest()
{
MatCode = MatCode,
MatName = MatName,
MatSpec = MatSpec,
IsEnable = IsEnable,
UserName = LocalStatic.CurrentUser,
DeviceType = LocalFile.Config.DeviceType,
PageNumber = CurrentPage,
PageSize = PageSize,
};
var Result = ApiHelp.GetDataFromHttp<PageQueryResponse<MatBaseInfoModel>>(LocalFile.Config.ApiIpHost + "matBaseInfo/getMatBaseInfo", body, "POST");
if (Result != null && Result.Data != null && Result.Data.Lists != null)
{
DataGridItemSource = new ObservableCollection<MatBaseInfoModel>(Result.Data.Lists);
MaxPage = Result.Data.MaxPage;
TotalCount = Result.Data.TotalCount;
}
}
catch (Exception ex)
{
Growl.Error("加载数据失败:" + ex.Message);
}
finally
{
dia.Close();
}
#endregion
}
/// <summary>
/// 导出数据为Excel文件
/// </summary>
public ICommand BtnExportCommand { get => new DelegateCommand(BtnExport); }
public async void BtnExport()
{
try
{
#region
Microsoft.Win32.SaveFileDialog sfd = new Microsoft.Win32.SaveFileDialog();
sfd.Title = "选择文件保存路径";
sfd.Filter = ".xlsx文件(*.xlsx)|*.xlsx";
sfd.FileName = "物料管理" + DateTime.Now.ToString("yyyyMMddhhmmss");
sfd.OverwritePrompt = true;
if (sfd.ShowDialog() != true)
{
return;
}
string path = sfd.FileName;
#endregion
var body = new GetMatBaseInfoRequest()
{
MatCode = MatCode,
MatName = MatName,
MatSpec = MatSpec,
IsEnable = IsEnable,
UserName = LocalStatic.CurrentUser,
DeviceType = LocalFile.Config.DeviceType,
PageNumber = CurrentPage,
PageSize = 65535,
};
await ApiHelp.PostDownloadFileAsync(path, System.Net.Http.HttpMethod.Post, LocalFile.Config.ApiIpHost + "matBaseInfo/exportMatBaseInfo", body);
Growl.Success("导出成功!");
}
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.FileName = "物料管理" + DateTime.Now.ToString("yyyyMMddhhmmss");
ofd.Multiselect = false;
if (ofd.ShowDialog() != true)
{
return;
}
#endregion
//已经选择文件 调用接口进行导入数据
string path = ofd.FileName;
var result = await ApiHelp.PostImportFileAsync<ResponseCommon<List<string>>>(path, System.Net.Http.HttpMethod.Post,
LocalFile.Config.ApiIpHost + "matBaseInfo/importMatBaseInfo", LocalStatic.CurrentUser, LocalFile.Config.DeviceType);
if (result.Code == 200)
{
Growl.Success("成功导入!");
CurrentPage = 0;
}
else
{
if (result.Data != null && result.Data.Count > 0)
HandyControl.Controls.MessageBox.Show(result.Message + "\t\n" + String.Join("\t\n", result.Data));
else
HandyControl.Controls.MessageBox.Show(result.Message);
}
}
catch (Exception ex)
{
Growl.Error("导入失败:" + ex.Message);
}
}
/// <summary>
/// 物料新增操作
/// </summary>
public ICommand BtnAddCommand { get => new DelegateCommand(BtnAdd); }
public async void BtnAdd()
{
var addView = new MatBaseInfoAddOrUpdateView("新增物料数据");
addView.ShowDialog();
if (addView.DialogResult == true)
{
var matBaseInfo = addView.matBaseInfo;
matBaseInfo.ModifyTime = DateTime.Now;
matBaseInfo.ModifyUser = LocalStatic.CurrentUser;
var body = new AddMatBaseInfoRequest<MatBaseInfoModel>()
{
UserName = LocalStatic.CurrentUser,
DeviceType = LocalFile.Config.DeviceType,
MatBaseInfo = matBaseInfo,
AddOrUpdate = AddOrUpdate.Add
};
var Result = ApiHelp.GetDataFromHttp<ResponseBase<UserModel>>(LocalFile.Config.ApiIpHost + "matBaseInfo/addOrUpdateMatBaseInfo", body, "POST");
if (Result != null && Result.Code == 200)
{
CurrentPage = 1;
Growl.Success("添加成功!");
}
else
{
Growl.Error($"{Result?.Message?.ToString()}");
}
}
}
/// <summary>
/// 物料修改操作
/// </summary>
public ICommand BtnEditCommand { get => new DelegateCommand(BtnEdit); }
public async void BtnEdit()
{
//查询勾选的第一个数据
var matBaseInfo = DataGridItemSource?.Where(t => t.IsSelected == true).FirstOrDefault();
if (matBaseInfo == null)
{
Growl.Warning("请选择需要修改的数据!");
return;
}
else
{
var addView = new MatBaseInfoAddOrUpdateView("修改物料数据", matBaseInfo);
addView.ShowDialog();
if (addView.DialogResult == true)
{
matBaseInfo = addView.matBaseInfo;
matBaseInfo.ModifyTime = DateTime.Now;
matBaseInfo.ModifyUser = LocalStatic.CurrentUser;
var body = new AddMatBaseInfoRequest<MatBaseInfoModel>()
{
UserName = LocalStatic.CurrentUser,
DeviceType = LocalFile.Config.DeviceType,
MatBaseInfo = matBaseInfo,
AddOrUpdate = AddOrUpdate.Update
};
var Result = ApiHelp.GetDataFromHttp<ResponseBase<UserModel>>(LocalFile.Config.ApiIpHost + "matBaseInfo/addOrUpdateMatBaseInfo", body, "POST");
if (Result != null && Result.Code == 200)
{
CurrentPage = 1;
Growl.Success("修改成功!");
}
else
{
Growl.Error($"{Result?.Message?.ToString()}");
}
}
}
}
/// <summary>
/// 物料删除操作
/// </summary>
public ICommand BtnDeleteCommand { get => new DelegateCommand(BtnDelete); }
public async void BtnDelete()
{
Growl.Ask($"是否删除所有勾选得数据]!", isConfirmed =>
{
if (isConfirmed)
{
//查询勾选的第一个数据
var matBaseInfoIds = DataGridItemSource?.Where(t => t.IsSelected == true)
.Select(t => t.Id)
.ToList();
if (matBaseInfoIds == null)
{
Growl.Warning("请选择需要修改的数据!");
}
else
{
var body = new DeleteMatBaseInfosRequest()
{
UserName = LocalStatic.CurrentUser,
DeviceType = LocalFile.Config.DeviceType,
MatBaseInfoIds = matBaseInfoIds,
};
var Result = ApiHelp.GetDataFromHttp<ResponseBase<UserModel>>(LocalFile.Config.ApiIpHost + "matBaseInfo/deleteMatBaseInfo", body, "POST");
if (Result != null && Result.Code == 200)
{
CurrentPage = 1;
Growl.Success("删除成功!" + Result?.Message);
}
else
{
Growl.Error($"{Result?.Message?.ToString()}");
}
}
}
return true;
});
}
public ICommand BtnPrintCommand { get => new DelegateCommand(BtnPrint); }
public async void BtnPrint()
{
var matBaseInfo = DataGridItemSource?.Where(t => t.IsSelected == true).FirstOrDefault();
if (matBaseInfo == null)
{
Growl.Warning("请勾选数据!");
return;
}
var generateWindow = new MatBaseInoGenarateMatInfoView(matBaseInfo);
var result = generateWindow.ShowDialog();
}
public ICommand BtnScanPrintCommand { get => new DelegateCommand(BtnScanPrint); }
public async void BtnScanPrint()
{
try
{
var generateWindow = new MatBaseInoScanGenarateMatInfoView();
LocalStatic.IsScanGenarateMatInfoWindowOpen = true;
LocalStatic.ScanGenarateMatInfoWindow = generateWindow;
var result = generateWindow.ShowDialog();
}
catch (Exception ex)
{
}
finally
{
LocalStatic.IsScanGenarateMatInfoWindowOpen = false;
}
}
#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); }
}
private int pageSize = 10;
public int PageSize
{
get => pageSize;
set
{
SetProperty(ref pageSize, value);
BtnSearch(true);
}
}
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
}
}