XiangTan_JiaoJie_Bak/DM_Weight/ViewModels/AccountWindowViewModel.cs

162 lines
5.2 KiB
C#

using DM_Weight.Models;
using DM_Weight.msg;
using DM_Weight.Report;
using DM_Weight.util;
using log4net;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DM_Weight.ViewModels
{
public class AccountWindowViewModel : BindableBase, INavigationAware, IRegionMemberLifetime
{
private readonly ILog logger = LogManager.GetLogger(typeof(AccountWindowViewModel));
public static AccountWindowViewModel vm;
private DateTime? _startDate = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day);
private DateTime? nowDate = 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);
}
}
}
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);
}
}
}
IEventAggregator _eventAggregator;
public AccountWindowViewModel(IEventAggregator eventAggregator)
{
_eventAggregator = eventAggregator;
vm = this;
}
/// <summary>
/// 导出账册
/// </summary>
public DelegateCommand DownloadAccountBook
{
get => new DelegateCommand(() =>
{
try
{
if (DrugInfo == null || string.IsNullOrEmpty(DrugInfo.DrugId))
{
AlertMsg alertMsg = new AlertMsg
{
Message = $"请选择药品!",
Type = MsgType.ERROR,
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
return;
}
GridReportUtil.PrintReportAccountBook(StartDate, EndDate, DrugInfo.DrugId);
}
catch (Exception ex)
{
AlertMsg alertMsg = new AlertMsg
{
Message = "导出报表异常!",
Type = MsgType.ERROR,
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
logger.Info($"导出报表异常{ex.Message}");
return;
}
});
}
public bool KeepAlive => false;
public bool IsNavigationTarget(NavigationContext navigationContext)
{
return true;
}
public void OnNavigatedFrom(NavigationContext navigationContext)
{
}
public void OnNavigatedTo(NavigationContext navigationContext)
{
//药品信息
GetAllDrugInfos();
}
/// <summary>
/// 药品
/// </summary>
private List<DrugInfo>? _drugInfos;
public List<DrugInfo>? DrugInfos
{
get => _drugInfos;
set => SetProperty(ref _drugInfos, value);
}
private DrugInfo? _drugInfo;
public DrugInfo? DrugInfo
{
get => _drugInfo;
set
{
SetProperty(ref _drugInfo, value);
}
}
private void GetAllDrugInfos()
{
var list = SqlSugarHelper.Db.Queryable<DrugInfo>().Includes<DrugManuNo>(di => di.DrugManuNos).OrderBy(di => di.DrugId).ToList();
DrugInfos = list;
DrugInfo = list[0];
}
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 DrugName FROM `drug_info` d";
if (string.IsNullOrEmpty(text))
{
DrugInfos = SqlSugarHelper.Db.SqlQueryable<DrugInfo>(str).OrderBy(di => di.DrugName).OrderBy(di => di.DrugId).ToList();
return;
}
if (DrugInfos != null)
{
DrugInfos.Clear();
}
DrugInfos = SqlSugarHelper.Db.SqlQueryable<DrugInfo>(str).Where(di => di.DrugName.Contains(text) || di.PyCode.Contains(text)).OrderBy(di => di.DrugName).OrderBy(di => di.DrugId).ToList();
}
}
}