XiangTan_JiaoJie_Bak/DM_Weight/ViewModels/CheckStockWindowViewModel.cs

437 lines
25 KiB
C#

using Common.Logging;
using DM_Weight.Models;
using DM_Weight.msg;
using DM_Weight.Port;
using DM_Weight.util;
using log4net;
using MaterialDesignThemes.Wpf;
using Prism.Commands;
using Prism.Events;
using Prism.Mvvm;
using Prism.Regions;
using Prism.Services.Dialogs;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Reflection.Metadata.Ecma335;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Media;
namespace DM_Weight.ViewModels
{
public class CheckStockWindowViewModel : BindableBase, INavigationAware, IRegionMemberLifetime
{
private readonly log4net.ILog logger = log4net.LogManager.GetLogger(typeof(CheckStockWindowViewModel));
private int _drawerNo = 0;
public int DrawerNo
{
get => _drawerNo;
set => SetProperty(ref _drawerNo, value);
}
private SolidColorBrush _colorBrush;
public SolidColorBrush SnackbarBackground
{
get => _colorBrush;
set => SetProperty(ref _colorBrush, value);
}
private ISnackbarMessageQueue _snackbarMessageQueue = new SnackbarMessageQueue(TimeSpan.FromSeconds(3));
public ISnackbarMessageQueue SnackbarMessageQueue
{
get => _snackbarMessageQueue;
set => SetProperty(ref _snackbarMessageQueue, value);
}
private List<ChannelStock> _channelStocks = new();
public List<ChannelStock> ChannelStocks
{
get => _channelStocks;
set => SetProperty(ref _channelStocks, value);
}
private ChannelStock _channelStock;
public ChannelStock _ChannelStock
{
get => _channelStock;
set => SetProperty(ref _channelStock, value);
}
private bool _isEnable = true;
public bool IsEnable { get => _isEnable; set => SetProperty(ref _isEnable, value); }
private int _status = 0;
public int Status { get => _status; set { SetProperty(ref _status, value); } }
private readonly IDialogService _dialogService;
public bool KeepAlive => false;
IEventAggregator _eventAggregator;
public CheckStockWindowViewModel(IDialogService dialogService, IEventAggregator eventAggregator)
{
_dialogService = dialogService;
_eventAggregator = eventAggregator;
}
private void RequestData()
{
List<ChannelStock> queryData = SqlSugarHelper.Db.Queryable<ChannelStock>()
.Includes(cs => cs.DrugInfo)
.Where(cs => cs.DrawerNo == DrawerNo + 1)
.Where(cs => cs.DrugId != null)
.Where(cs => cs.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM5"))
.OrderBy(cs => cs.DrugId)
.ToList();
ChannelStocks = queryData.Select(it => { it.CheckQuantity = it.Quantity; return it; }).ToList();
}
public void OnNavigatedTo(NavigationContext navigationContext)
{
logger.Info("进入OnNavigatedTo");
//删除药品有批次但是库存为0的药品
List<ChannelStock> csList = SqlSugarHelper.Db.Queryable<ChannelStock>().Where(cs => cs.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM5") && cs.Quantity <= 0 && !string.IsNullOrEmpty(cs.ManuNo)).ToList();
if (csList != null && csList.Count > 0)
{
SqlSugarHelper.Db.Deleteable<ChannelStock>().Where(cs => cs.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM5") && cs.Quantity <= 0 && !string.IsNullOrEmpty(cs.ManuNo)).ExecuteCommand();
for (int i = 0; i < csList.Count; i++)
{
//如果channel_stock下无数据则channel_list同步删除
int iListCount= SqlSugarHelper.Db.Queryable<ChannelStock>().Where(cs => cs.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM5") && cs.DrawerNo==csList[i].DrawerNo&& cs.DrugId == csList[i].DrugId).Count();
if (iListCount <=0)
SqlSugarHelper.Db.Deleteable<ChannelList>().Where(cl => cl.MachineId.Equals(ConfigurationManager.AppSettings["machineId"] ?? "DM5") && cl.Id == csList[i].Chnguid).ExecuteCommand();
}
}
RequestData();
logger.Info("结束RequestData");
}
public bool IsNavigationTarget(NavigationContext navigationContext)
{
return true;
}
public void OnNavigatedFrom(NavigationContext navigationContext)
{
// 取消消息订阅
//_eventAggregator.GetEvent<PortUtilEvent>().Unsubscribe(DoMyPrismEvent);
}
public DelegateCommand<string> UpdateDrawerNo
{
get => new DelegateCommand<string>((DrawerNo) =>
{
this.DrawerNo = Convert.ToInt32(DrawerNo);
RequestData();
}
);
}
private static readonly DateTime Jan1st1970 = new DateTime
(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
public long CurrentTimeMillis()
{
return (long)(DateTime.UtcNow - Jan1st1970).TotalMilliseconds;
}
//保存修改的库存数
public DelegateCommand SaveCommand
{
get => new DelegateCommand(() =>
{
List<ChannelStock> record = ChannelStocks.FindAll(it => it.Quantity != it.CheckQuantity).ToList();
if (record.Count > 0)
{
bool iFlag = true;
string InvoiceId = "CHECK_" + CurrentTimeMillis();
var f = SqlSugarHelper.Db.UseTran(() =>
{
for (int i = 0; i < record.Count; i++)
{
iFlag = true;
ChannelStock it = record[i];
if (it.AddToJJNum > 0 || it.NeedNum > 0)
{
//有待取药或待入库药品,弹出提示,确认后保存
DialogParameters dialogParameters = new DialogParameters();
dialogParameters.Add("ConfirmInfo", $"所选药品{it.DrugInfo.DrugName}存在待取药或待入库数据是否确认保存?");
DialogServiceExtensions.ShowDialogHost(_dialogService, "ConfirmDialog", dialogParameters, (IDialogResult dialogResult) =>
{
if (dialogResult.Result == ButtonResult.OK)
{
iFlag = true;
}
else
{
iFlag = false;
}
}, "RootDialog");
}
if (iFlag)
{
// 更新数据 库存信息
SqlSugarHelper.Db.Updateable(new ChannelStock()
{
Quantity = it.CheckQuantity,
ManuNo = it.ManuNo,
EffDate = it.EffDate,
Id = it.Id,
NeedNum = 0,
AddToJJNum = 0,
State = 0,
}).UpdateColumns(it => new { it.Quantity, it.ManuNo, it.EffDate, it.NeedNum, it.AddToJJNum }).ExecuteCommand();
//SqlSugarHelper.Db.Updateable(new ChannelList()
//{
// State = 0,
// Id = it.Chnguid
//}).UpdateColumns(cl => new { cl.State }).ExecuteCommand();
//盘点改库存记录账册,多了则记录入库、少了记录出库
if(it.CheckQuantity!=it.Quantity)
{
//查询上一条账册中的空瓶数
AccountBookG2 accountBookEmpty = SqlSugarHelper.Db.Queryable<AccountBookG2>()
.Where(ab => ab.MachineId.Equals(ConfigurationManager.AppSettings["dm_machineId"].ToString()))
.Where(ab => ab.Type == 1 || ab.Type == 2)
.Where(ab => ab.DrugId == it.DrugId)
.Where(ab => ab.ManuNo == it.ManuNo).OrderByDescending(ab => ab.Id).First();
//修改凌晨生成的日结存与总结存数据
AccountBookG2 accountBookG2Day = SqlSugarHelper.Db.Queryable<AccountBookG2>()
.Where(ab => ab.MachineId.Equals(ConfigurationManager.AppSettings["dm_machineId"].ToString()))
.Where(ab => ab.Type == 3)
.Where(ab => ab.DrugId == it.DrugId)
.Where(ab => ab.ManuNo == it.ManuNo)
.Where(ab => ab.CreateDate == DateTime.Now.ToString("yyyy-MM-dd")).First();
// 获取更新完库存后的药品库存
List<ChannelStock> nowChannels = SqlSugarHelper.Db.Queryable<ChannelStock>()
.Where(cs => (cs.MachineId.Equals(ConfigurationManager.AppSettings["machineId"]) && cs.DrawerType == 1) || cs.MachineId.Equals(ConfigurationManager.AppSettings["jj_machineId"]))
.Where(cs => cs.DrugId.Equals(it.DrugId))
.Where(cs => cs.ManuNo.Equals(it.ManuNo))
//.Where(cs => cs.DrawerType == 1)
.ToList();
int manuStock = 0;
if (it.CheckQuantity-it.Quantity>0)
{
//盘多了,记录入库
if (accountBookG2Day != null)
{
accountBookG2Day.ManuStock = accountBookG2Day.ManuStock + (it.CheckQuantity - it.Quantity);
accountBookG2Day.AddQuantity = accountBookG2Day.AddQuantity + (it.CheckQuantity - it.Quantity);
//负数直接记0
accountBookG2Day.TotalStock = (accountBookEmpty != null ? (accountBookEmpty.TotalStock > 0 && accountBookEmpty.TotalStock > (it.CheckQuantity - it.Quantity) ? accountBookEmpty.TotalStock - (it.CheckQuantity - it.Quantity) : 0) : 0);
accountBookG2Day.CreateTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59);
SqlSugarHelper.Db.Updateable(accountBookG2Day).ExecuteCommand();
manuStock = accountBookG2Day.ManuStock;
}
else
{
//生成日结存时可能没有该库位的绑定信息,需要写入日结存
int iDayResult = SqlSugarHelper.Db.Insertable(new AccountBookG2()
{
DrugId = it.DrugId,
Type = 3,
ManuNo = it.ManuNo,
EffDate = it.EffDate,
YQuantity = 0,
ManuStock = (it.CheckQuantity - it.Quantity),
TotalStock = (it.CheckQuantity - it.Quantity),
UserId1 = HomeWindowViewModel.Operator?.Id,
UserId2 = HomeWindowViewModel.Reviewer?.Id,
MachineId = ConfigurationManager.AppSettings["machineId"].ToString(),
CreateDate = DateTime.Now.ToString("yyyy-MM-dd"),
CreateTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59),
InvoiceNo = "日结存",
AddQuantity = (it.CheckQuantity - it.Quantity)
}).ExecuteCommand();
if (iDayResult <= 0)
{
logger.Info($"未写入日结存数据{it.DrugId}-{it.ManuNo}-{it.EffDate}-{it.CheckQuantity - it.Quantity}");
}
}
//保存账册
SqlSugarHelper.Db.Insertable(new AccountBookG2()
{
DrugId = it.DrugId,
Type = 1,
Department = ConfigurationManager.AppSettings["department"].ToString(),
InvoiceNo = InvoiceId,
ManuNo = it.ManuNo,
EffDate = it.EffDate,
AddQuantity = it.CheckQuantity - it.Quantity,
UserId1 = HomeWindowViewModel.Operator?.Id,
UserId2 = HomeWindowViewModel.Reviewer?.Id,
MachineId = ConfigurationManager.AppSettings["machineId"].ToString(),
CreateDate = DateTime.Now.ToString("yyyy-MM-dd"),
CreateTime = DateTime.Now,
ManuStock = manuStock > 0 ? manuStock : nowChannels.Sum(it => it.Quantity),
TotalStock = (accountBookEmpty != null ? (accountBookEmpty.TotalStock > 0 && accountBookEmpty.TotalStock > (it.CheckQuantity - it.Quantity) ? accountBookEmpty.TotalStock - (it.CheckQuantity - it.Quantity) : 0) : 0) //负数直接记0
}).ExecuteCommand();
}
else
{
//盘少了,记录出库
if (accountBookG2Day != null)
{
accountBookG2Day.ManuStock = accountBookG2Day.ManuStock - (it.Quantity-it.CheckQuantity);
accountBookG2Day.OutQuantity = accountBookG2Day.OutQuantity + (it.Quantity - it.CheckQuantity);
SqlSugarHelper.Db.Updateable(accountBookG2Day).ExecuteCommand();
//accountBookG2Day.UseDose = (Convert.ToInt32(accountBookG2Day.UseDose) + Convert.ToInt32(oi._OrderDetail.surgicalResidual.UseDose)).ToString();//盘点减的库存没有使用剂量
//accountBookG2Day.ResidualDose = (Convert.ToInt32(accountBookG2Day.ResidualDose) + Convert.ToInt32(oi._OrderDetail.surgicalResidual.ResidualDose)).ToString();//盘点减的库存没有剩余剂量
accountBookG2Day.TotalStock = (accountBookEmpty != null ? (accountBookEmpty.TotalStock > 0 ? accountBookEmpty.TotalStock : 0) : 0) + (it.Quantity - it.CheckQuantity);
manuStock = accountBookG2Day.ManuStock;
}
else
{
//生成日结存时可能没有该库位的绑定信息,需要写入日结存
int iDayResult = SqlSugarHelper.Db.Insertable(new AccountBookG2()
{
DrugId =it.DrugId,
Type = 3,
ManuNo = it.ManuNo,
EffDate = it.EffDate,
YQuantity = 0,
ManuStock = nowChannels.Sum(it => it.Quantity), //oi._OrderDetail.Quantity,
TotalStock = it.Quantity - it.CheckQuantity,
UserId1 = HomeWindowViewModel.Operator?.Id,
UserId2 = HomeWindowViewModel.Reviewer?.Id,
MachineId = ConfigurationManager.AppSettings["dm_machineId"].ToString(),
CreateDate = DateTime.Now.ToString("yyyy-MM-dd"),
CreateTime = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 59),
InvoiceNo = "日结存",
OutQuantity = (it.Quantity - it.CheckQuantity),
//UseDose = oi._OrderDetail.surgicalResidual.UseDose,
//ResidualDose = oi._OrderDetail.surgicalResidual.ResidualDose
}).ExecuteCommand();
if (iDayResult <= 0)
{
logger.Info($"未写入日结存数据{it.DrugId}-{it.ManuNo}-{it.EffDate}-{it.Quantity-it.CheckQuantity}");
}
}
//查询上一条账册中的空瓶数
//AccountBookG2 accountBookEmpty = SqlSugarHelper.Db.Queryable<AccountBookG2>()
//.Where(ab => ab.MachineId.Equals(ConfigurationManager.AppSettings["dm_machineId"].ToString()))
//.Where(ab => ab.Type == 1 || ab.Type == 2)
//.Where(ab => ab.DrugId == oi._OrderDetail.DrugId)
//.Where(ab => ab.ManuNo == cs.ManuNo).OrderByDescending(ab => ab.Id).First();
//保存账册
int iInsertResult = SqlSugarHelper.Db.Insertable(new AccountBookG2()
{
DrugId = it.DrugId,
Type = 2,
//Department = oi.DeptName,
OrderNo = $"checkStock_{it.DrawerNo}_{it.ColNo}_{it.Quantity}_{it.CheckQuantity}_{DateTime.Now}",
ManuNo = it.ManuNo,
EffDate = it.EffDate,
OutQuantity = it.Quantity-it.CheckQuantity,
UserId1 = HomeWindowViewModel.Operator?.Id,
UserId2 = HomeWindowViewModel.Reviewer?.Id,
MachineId = ConfigurationManager.AppSettings["dm_machineId"].ToString(),
CreateDate = DateTime.Now.ToString("yyyy-MM-dd"),
CreateTime = DateTime.Now,
InvoiceNo = $"checkStock_{it.DrawerNo}_{it.ColNo}_{it.Quantity}_{it.CheckQuantity}_{DateTime.Now}",
ManuStock = manuStock > 0 ? manuStock : nowChannels.Sum(it => it.Quantity),
TotalStock = (accountBookEmpty != null ? (accountBookEmpty.TotalStock > 0 ? accountBookEmpty.TotalStock : 0) : 0) + (it.Quantity - it.CheckQuantity),
//ShoushuJian = drawerNo.ToString()
}).ExecuteCommand();
//修改凌晨生成的日结存与总结存数据
}
}
// 保存数据 盘点记录
SqlSugarHelper.Db.Insertable(new MachineRecord()
{
MachineId = it.MachineId,
DrawerNo = it.DrawerNo,
ColNo = it.ColNo,
DrugId = it.DrugId,
ManuNo = it.ManuNo,
EffDate = !String.IsNullOrEmpty(it.EffDate) ? DateTime.ParseExact(it.EffDate, "yyyy-MM-dd", System.Globalization.CultureInfo.CurrentCulture) : null,
Operator = HomeWindowViewModel.Operator?.Id,
Reviewer = HomeWindowViewModel.Reviewer?.Id,
OperationTime = DateTime.Now,
Quantity = it.CheckQuantity - it.Quantity,
Type = 4,
InvoiceId = $"{it.DrawerNo}{it.DrugInfo.DrugName}盘前库存{it.Quantity},后{it.CheckQuantity}" // InvoiceId
//,StockQuantity = nowChannels.Sum(it => it.Quantity),
//CheckQuantity = it.CheckQuantity
}).ExecuteCommand();
logger.Info($"库存盘点->药箱号【{it.DrawerNo}】药品【{it.DrugInfo.DrugName}】盘点前库存【{it.Quantity}】,更改后【{it.CheckQuantity}】");
}
else
{
return false;
break;
}
}
return true;
});
if (!iFlag)
{
return;
}
if (f.Data)
{
RequestData();
AlertMsg alertMsg = new AlertMsg
{
Message = "操作完成",
Type = MsgType.SUCCESS,
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
}
if (!f.IsSuccess)
{
AlertMsg alertMsg = new AlertMsg
{
Message = "操作失败!",
Type = MsgType.ERROR,
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
}
Status = 0;
}
else
{
AlertMsg alertMsg = new AlertMsg
{
Message = "盘点完成,库存无改变",
Type = MsgType.ERROR
};
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
Status = 0;
}
});
}
//刷新
public DelegateCommand Query
{
get => new DelegateCommand(RequestData);
}
public DelegateCommand RowSelected
{
get => new DelegateCommand(() =>
{
if (_channelStock != null)
{
}
});
}
}
}