HuNan_DM/DM_Weight/ViewModels/SpecialAccountWindowViewMod...

301 lines
13 KiB
C#

using DM_Weight.Models;
using DM_Weight.msg;
using DM_Weight.Report;
using DM_Weight.Services;
using DM_Weight.util;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DM_Weight.ViewModels
{
internal class SpecialAccountWindowViewModel : BindableBase, INavigationAware, IRegionMemberLifetime
{
public static SpecialAccountWindowViewModel 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);
}
}
}
private static readonly DateTime Jan1st1970 = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
/// <summary>
/// 账册
/// </summary>
private List<AccountModel> _accountList = new();
public List<AccountModel> AccountList
{
get => _accountList;
set => SetProperty(ref _accountList, value);
}
/// <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);
}
}
MachineRecordService _machineRecordService;
ChannelStockService _channelStockService;
IEventAggregator _eventAggregator;
public SpecialAccountWindowViewModel(MachineRecordService machineRecord, ChannelStockService channelStockService, IEventAggregator eventAggregator)
{
_machineRecordService = machineRecord;
_channelStockService = channelStockService;
_eventAggregator = eventAggregator;
vm = this;
}
private void GetAllDrugInfos()
{
var list = SqlSugarHelper.Db.Queryable<DrugInfo>().Includes<DrugManuNo>(di => di.DrugManuNos).OrderBy(di => di.DrugId).ToList();
DrugInfos = list;
DrugInfo = list[0];
}
/// <summary>
/// 生成日结存
/// </summary>
public DelegateCommand DayAccountBook
{
get => new DelegateCommand(() =>
{
DayAccountAction();
});
}
//生成一条当日没有记录的账册记录
private void DayAccountAction()
{
try
{
string machineId = ConfigurationManager.AppSettings["machineId"] ?? "DM1";
//根据已生成的日结存记录
int deleteNum = SqlSugarHelper.Db.Deleteable<MachineRecord>()
.Where(cs => cs.Type.Equals(5) && cs.OperationTime.ToString("yyyy-MM-dd") == DateTime.Now.ToString("yyyy-MM-dd") && cs.MachineId.Equals(machineId)).ExecuteCommand();
int inQuantity = 0; //当日入库量
int outQuantity = 0; //当日出库量
//查询账册中所有记录,与库存中的记录做比校,如果账册中没有,则添加一条,有则添加一条总日结存
List<AccountModel> accountList = _machineRecordService.ReportAccountBook(nowDate, null, 0);
//库存中的记录
List<ChannelStockCount> channelStockList = _channelStockService.GetAll();
//List<ChannelStock> channelStockInsert = new List<ChannelStock>();
if (accountList != null && accountList.Count() > 0)
{
//账册中有记录则与库存记录对比
for (int i = 0; i < channelStockList.Count; i++)
{
string drugId = channelStockList[i].DrugId;
string manuNo = channelStockList[i].ManuNo;
int quantity = channelStockList[i].Quantity;
int Count = accountList.Where(cs => cs.DrugId == drugId && cs.ManuNo == manuNo).Count();
if (Count <= 0)
{
inQuantity = 0;
outQuantity = 0;
}
else
{
inQuantity = accountList.Where(cs => cs.DrugId == drugId && cs.ManuNo == manuNo).Sum(cs => cs.InQuantity);
outQuantity = accountList.Where(cs => cs.DrugId == drugId && cs.ManuNo == manuNo).Sum(cs => cs.OutQuantity);
}
// 获取药品总库存
int channelStockQuantity = SqlSugarHelper.Db.Queryable<ChannelStock>()
.Where(cs => cs.DrugId.Equals(drugId)).Sum(it => it.Quantity);
//没有直接插入
// 保存数据 出/入库记录
string InvoiceId = "Account_" + CurrentTimeMillis();
SqlSugarHelper.Db.Insertable(new MachineRecord()
{
MachineId = channelStockList[i].MachineId,
DrawerNo = 0,//channelStockList[i].DrawerNo,
ColNo = 0,// channelStockList[i].ColNo,FV
DrugId = drugId,
ManuNo = manuNo,
EffDate = null,// !String.IsNullOrEmpty(channelStockList[i].EffDate) ? DateTime.ParseExact(channelStockList[i].EffDate, "yyyy-MM-dd", System.Globalization.CultureInfo.CurrentCulture) : null,
Operator = HomeWindowViewModel.Operator?.Id,
Reviewer = HomeWindowViewModel.Reviewer?.Id,
OperationTime = DateTime.Now,
Quantity = 0,
Type = 5,
InvoiceId = "日结存",//InvoiceId,
ReturnQuantity1 = inQuantity, //当日入库量总数
ReturnQuantity2 = outQuantity, //当日出库量总数
StockQuantity = channelStockQuantity,
ManunoQuantity = quantity,
SupplierDept = string.Empty,// ConfigurationManager.AppSettings["supplierDept"].ToString(),
ReceiveDept = string.Empty,//ConfigurationManager.AppSettings["receiveDept"].ToString()
}).ExecuteCommand();
}
}
else
{
for (int i = 0; i < channelStockList.Count; i++)
{
//账册中没有记录则把库存中的所有数据都要生成一条账册记录
// 保存数据 出/入库记录
// 获取药品总库存
string drugId = channelStockList[i].DrugId;
string manuNo = channelStockList[i].ManuNo;
int quantity = channelStockList[i].Quantity;
int channelStockQuantity = SqlSugarHelper.Db.Queryable<ChannelStock>()
.Where(cs => cs.DrugId.Equals(drugId)).Sum(it => it.Quantity);
string InvoiceId = "Account_" + CurrentTimeMillis();
SqlSugarHelper.Db.Insertable(new MachineRecord()
{
MachineId = channelStockList[i].MachineId,
DrawerNo = 0,//channelStockList[i].DrawerNo,
ColNo = 0,//channelStockList[i].ColNo,
DrugId = drugId,
ManuNo = manuNo,// channelStockList[i].ManuNo,
EffDate = null,// !String.IsNullOrEmpty(channelStockList[i].EffDate) ? DateTime.ParseExact(channelStockList[i].EffDate, "yyyy-MM-dd", System.Globalization.CultureInfo.CurrentCulture) : null,
Operator = HomeWindowViewModel.Operator?.Id,
Reviewer = HomeWindowViewModel.Reviewer?.Id,
OperationTime = DateTime.Now,
Quantity = 0,
Type = 5,
InvoiceId = "日结存",// string.Empty,//InvoiceId,
ReturnQuantity1 = inQuantity, //当日入库量总数
ReturnQuantity2 = outQuantity, //当日出库量总数
StockQuantity = channelStockQuantity,
ManunoQuantity = quantity,
SupplierDept = string.Empty,//ConfigurationManager.AppSettings["supplierDept"].ToString(),
ReceiveDept = string.Empty,//ConfigurationManager.AppSettings["receiveDept"].ToString()
}).ExecuteCommand();
}
}
AlertMsg alertMsg = new AlertMsg
{
Message = "日结存已生成完成",
Type = MsgType.SUCCESS,
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
}
catch (Exception ex)
{
AlertMsg alertMsg = new AlertMsg
{
Message = "生成日结存失败",
Type = MsgType.ERROR,
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
}
}
public long CurrentTimeMillis()
{
return (long)(DateTime.UtcNow - Jan1st1970).TotalMilliseconds;
}
/// <summary>
/// 导出账册
/// </summary>
public DelegateCommand DownloadAccountBook
{
get => new DelegateCommand(() =>
{
GridReportUtil.PrintReportSpecialAccount(EndDate ?? DateTime.Now, DrugInfo == null ? "" : DrugInfo.DrugId);
});
}
/// <summary>
/// 导出账册2
/// </summary>
public DelegateCommand DownloadAccountBook2
{
get => new DelegateCommand(() =>
{
GridReportUtil.PrintReportSpecialAccount2New(EndDate ?? DateTime.Now, DrugInfo == null ? "" : DrugInfo.DrugId);
});
}
public bool KeepAlive => true;
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 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) || di.DrugId.Contains(text)).OrderBy(di => di.DrugName).OrderBy(di => di.DrugId).ToList();
}
public void OnNavigatedTo(NavigationContext navigationContext)
{
//药品信息
GetAllDrugInfos();
}
public bool IsNavigationTarget(NavigationContext navigationContext)
{
return true;
}
public void OnNavigatedFrom(NavigationContext navigationContext)
{
}
}
}