HuNan_DM/DM_Weight/ViewModels/MachineRecordWindowViewMode...

363 lines
11 KiB
C#
Raw Normal View History

2023-11-13 13:25:46 +08:00
using Prism.Commands;
using Prism.Mvvm;
using Prism.Regions;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using DM_Weight.Models;
using DM_Weight.Port;
using DM_Weight.Report;
using DM_Weight.util;
using gregn6Lib;
using Newtonsoft.Json;
using System.IO;
using System.Configuration;
using System.Reflection.PortableExecutable;
using DM_Weight.msg;
using Prism.Events;
using Prism.Services.Dialogs;
using System.Threading.Channels;
2023-11-13 13:25:46 +08:00
namespace DM_Weight.ViewModels
{
public class MachineRecordWindowViewModel : BindableBase, IConfirmNavigationRequest, IRegionMemberLifetime
{
public static MachineRecordWindowViewModel vm;
IEventAggregator _eventAggregator;
private int _pageNum = 1;
public int PageNum
{
get => _pageNum;
set
{
SetProperty(ref _pageNum, value);
RequestData();
}
}
private int _pageCount = 1;
public int PageCount
{
get => _pageCount;
set
{
SetProperty(ref _pageCount, value);
}
}
private int _pageSize = 8;
public int PageSize
{
get => _pageSize;
set
{
SetProperty(ref _pageSize, value);
}
}
private int _totalCount = 0;
public int TotalCount
{
get => _totalCount;
set
{
SetProperty(ref _totalCount, value);
}
}
private DateTime? _startDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
public DateTime? StartDate
{
get => _startDate;
set
{
if (value != null)
{
SetProperty(ref _startDate, new DateTime(value?.Year ?? 0, value?.Month ?? 0, value?.Day ?? 0));
}
else
{
SetProperty(ref _startDate, value);
}
RequestData();
}
}
private DateTime? _endDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59);
public DateTime? EndDate
{
get => _endDate;
set
{
if (value != null)
{
TimeSpan ershisi = new TimeSpan(23, 59, 59);
SetProperty(ref _endDate, new DateTime(value?.Year ?? 0, value?.Month ?? 0, value?.Day ?? 0, 23, 59, 59));
}
else
{
SetProperty(ref _endDate, value);
}
RequestData();
}
}
private int _type;
public int Type
{
get { return _type; }
set
{
SetProperty(ref _type, value);
}
}
private DrugInfo? _drugInfo;
public DrugInfo? DrugInfo
{
get => _drugInfo;
set
{
SetProperty(ref _drugInfo, value);
RequestData();
}
}
private List<DrugInfo>? _drugInfos;
public List<DrugInfo>? DrugInfos
{
get => _drugInfos;
set => SetProperty(ref _drugInfos, value);
}
private List<MachineRecord>? machineRecords;
public List<MachineRecord>? MachineRecords
{
get { return machineRecords; }
set { SetProperty(ref machineRecords, value); }
}
//盘点记录 导出账册 选中的数据
private List<MachineRecord>? selectMachineRecords;
public List<MachineRecord>? SelectMachineRecords
{
get => selectMachineRecords;
set { SetProperty(ref selectMachineRecords, value);}
}
2023-11-27 15:35:22 +08:00
public List<string>? OldInvoiceIdLst = new List<string>();
//private SqlSugarScope SqlSugarHelper.Db;
2023-11-27 15:35:22 +08:00
public MachineRecordWindowViewModel(IEventAggregator eventAggregator, SqlSugarScope sqlSugarScope)
2023-11-13 13:25:46 +08:00
{
vm = this;
_eventAggregator = eventAggregator;
//this.SqlSugarHelper.Db = sqlSugarScope;
2023-11-13 13:25:46 +08:00
}
public DelegateCommand Query
{
get => new DelegateCommand(() =>
{
RequestData();
});
}
public DelegateCommand Download
{
get => new DelegateCommand(() =>
{
//if (Type == 4)
//{
// if (SelectMachineRecords != null && SelectMachineRecords.Count > 0)
// {
// GridReportUtil.PrintReportMechineRecordForSelect(SelectMachineRecords);
// }
//}
//else
{
GridReportUtil.PrintReportMechineRecord(Type, StartDate, EndDate);
}
2023-11-13 13:25:46 +08:00
});
}
//public DelegateCommand DownloadAccountBook
//{
// get => new DelegateCommand(() =>
// {
// GridReportUtil.PrintReportAccountBook(StartDate, EndDate, DrugInfo == null ? "" : DrugInfo.DrugId);
2023-11-13 13:25:46 +08:00
// });
//}
2023-11-13 13:25:46 +08:00
public DelegateCommand SaveCommand
{
get => new DelegateCommand(() =>
{
//保存修改的凭证号
UpdatPZHSave();
});
}
private void UpdatPZHSave()
{
var f = SqlSugarHelper.Db.UseTran(() =>
2023-11-13 13:25:46 +08:00
{
for (int i = 0; i < MachineRecords.Count; i++)
{
string oldInvoiceId = OldInvoiceIdLst[i];
2023-11-27 15:35:22 +08:00
2023-11-13 13:25:46 +08:00
// 更新数据 库存信息
SqlSugarHelper.Db.Updateable(new MachineRecord()
2023-11-13 13:25:46 +08:00
{
Id = MachineRecords[i].Id,
InvoiceId = MachineRecords[i].InvoiceId
2023-11-27 15:35:22 +08:00
}).UpdateColumns(it => new { it.InvoiceId }).Where(it => it.InvoiceId == oldInvoiceId).ExecuteCommand();
2023-11-13 13:25:46 +08:00
}
return true;
});
if (f.Data)
{
AlertMsg alertMsg = new AlertMsg
{
Message = "保存成功",
Type = MsgType.SUCCESS
};
2023-11-27 15:35:22 +08:00
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
RequestData();
2023-11-13 13:25:46 +08:00
}
if (!f.IsSuccess)
{
AlertMsg alertMsg = new AlertMsg
{
Message = "操作失败!",
Type = MsgType.ERROR,
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
}
}
void ReportInitialize()
{
}
//这个方法用于拦截请求,continuationCallback(true)就是不拦截continuationCallback(false)拦截本次操作
public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
{
continuationCallback(true);
}
//接收导航传过来的参数
public void OnNavigatedTo(NavigationContext navigationContext)
{
//取出Type决定页面显示内容
Type = navigationContext.Parameters.GetValue<int>("Type");
//查询表格数据
RequestData();
GetAllDrugInfos();
}
void RequestData()
{
int totalCount = 0;
string machineId = ConfigurationManager.AppSettings["machineId"] ?? "DM1";
MachineRecords = SqlSugarHelper.Db.Queryable<MachineRecord>()
2023-11-13 13:25:46 +08:00
.Includes<DrugInfo>(mr => mr.DrugInfo)
.Includes<UserList>(mr => mr.User)
.Where(mr => mr.MachineId == machineId)
.WhereIF(Type == 3, (mr) => new int[] { 31, 32 }.Contains(mr.Type))
.WhereIF(Type != 3, (mr) => mr.Type == Type)
.WhereIF(StartDate != null, (mr) => mr.OperationTime > StartDate)
.WhereIF(EndDate != null, (mr) => mr.OperationTime < EndDate)
.WhereIF(DrugInfo != null, (mr) => mr.DrugId == DrugInfo.DrugId)
//.Select(mr => mr)
.ToPageList(PageNum, PageSize, ref totalCount);
//.ToList();
TotalCount = totalCount;
2023-11-27 15:35:22 +08:00
OldInvoiceIdLst = MachineRecords.Select(mr => mr.InvoiceId).ToList();
2023-11-13 13:25:46 +08:00
}
//每次导航的时候该实列用不用重新创建true是不重新创建,false是重新创建
public bool IsNavigationTarget(NavigationContext navigationContext)
{
return true;
}
//这个方法用于拦截请求
public void OnNavigatedFrom(NavigationContext navigationContext)
{
}
private void GetAllDrugInfos()
{
string str = "SELECT d.drug_id,d.py_code,d.drug_barcode,d.drug_name,d.drug_brand_name,d.drug_spec,d.dosage,d.pack_unit,d.manufactory,d.max_stock,CONCAT(drug_name,' ',drug_spec)as drug_name_spec FROM `drug_info` d";
DrugInfos = SqlSugarHelper.Db.SqlQueryable<DrugInfo>(str).OrderBy(di => di.DrugName).OrderBy(di => di.DrugId).ToList();
2023-11-13 13:25:46 +08:00
}
public void UpdateComboBoxItems(string text)
{
string str = @"SELECT d.drug_id,d.py_code,d.drug_barcode,d.drug_name,d.drug_brand_name,d.drug_spec,d.dosage,d.pack_unit,
d.manufactory,d.max_stock,CONCAT(drug_name,' ',drug_spec)as drug_name_spec FROM `drug_info` d";
if (string.IsNullOrEmpty(text))
{
DrugInfos = SqlSugarHelper.Db.SqlQueryable<DrugInfo>(str).OrderBy(di => di.DrugName).OrderBy(di => di.DrugId).ToList();
2023-11-13 13:25:46 +08:00
return;
}
if (DrugInfos != null)
{
DrugInfos.Clear();
}
DrugInfos = SqlSugarHelper.Db.SqlQueryable<DrugInfo>(str).Where(di => di.DrugName.Contains(text) || di.PyCode.Contains(text) || di.DrugId.Contains(text)).OrderBy(di => di.DrugName).OrderBy(di => di.DrugId).ToList();
2023-11-13 13:25:46 +08:00
}
public DelegateCommand RowSelected
{
get => new DelegateCommand(() =>
{
if (MachineRecords != null)
{
SelectMachineRecords = MachineRecords.Select(x =>
{
x.IsSelected = !x.IsSelected;
return x;
}).ToList();
}
});
}
public bool KeepAlive => true;
2024-09-12 18:01:56 +08:00
//导出处方登记专册
public DelegateCommand AccountCommand
{
get => new DelegateCommand(() => {
GridReportUtil.PrintReportOrderAccount(DrugInfo==null?"":DrugInfo.DrugId, StartDate, EndDate);
});
}
//导出回收登记记录
public DelegateCommand ReturnRecordCommand
{
get => new DelegateCommand(() =>
{
GridReportUtil.PrintReportReturnEmpty(StartDate, EndDate);
});
}
2023-11-13 13:25:46 +08:00
}
}