using LinqToDB; using log4net; using MasaBlazorApp3.DataAccess.Dao; using MasaBlazorApp3.Pojo; using MasaBlazorApp3.Pojo.Config; using MasaBlazorApp3.Pojo.Vo; using MasaBlazorApp3.Port; using Microsoft.Extensions.Options; using Mysqlx.Crud; using System.Data; namespace MasaBlazorApp3.DataAccess.Impl { public class ChannelListDao : IChannelListDao { private AppDataConnection _connection; private readonly SettingConfig _setting; private GlobalStateService _globalStateService; private readonly PortUtil _portUtil; private readonly ILog logger = LogManager.GetLogger(typeof(ChannelListDao)); public ChannelListDao(AppDataConnection connection, IOptions setting, GlobalStateService globalStateService, PortUtil portUtil) { _globalStateService = globalStateService; _connection = connection; _setting = setting.Value; _portUtil = portUtil; } public async Task> GetAllChannelList(int DrawerType, string drugName, int? take, int? skip) { var query = _connection.ChannelStock.AsQueryable(); if (DrawerType != 0) { query = query.Where(cl => cl.DrawerType == DrawerType); } if (!String.IsNullOrEmpty(drugName)) { query = query.Where(cl => cl.Drug.DrugName.Equals(drugName)); } //query = query.Where(cl => !String.IsNullOrEmpty(cl.DrugId)); query = query.Where(cl => cl.MachineId == _setting.machineId); int pagedData = await query.CountAsync(); List list = await query .LoadWith(cl => cl.Drug) .LoadWith(cl => cl.Drug.Manus) .LoadWith(cl => cl.drugManuNo) .OrderBy((cl) => cl.DrawerNo) .ThenBy((cl) => cl.ColNo) .Skip((int)skip) .Take((int)take) .ToListAsync(); return new PageData() { TotalDesserts = pagedData, Desserts = list }; } public async Task> GetChannelStockByDrugId(string DrugId) { var query = _connection.ChannelStock.AsQueryable(); return await query .LoadWith(cs => cs.Drug) .InnerJoin( _connection.ChannelList.Where(cl => cl.MachineId.Equals(_setting.machineId)).Where(cl => cl.DrawerType == 1), (cs, cl) => cs.ListId == cl.Id, (cs, cl) => cs ) .Where(cs => cs.DrugId == DrugId) .Where(cs => cs.MachineId == _setting.machineId) //.Where(cs => cs.Quantity > 0) .OrderBy((cs) => cs.EffDate) .ThenBy((cs) => cs.DrawerNo) .ThenBy((cs) => cs.ColNo) .ToListAsync(); } public async Task> GetChannelStockByDrawerNo(int DrawerNo, int Quantity = 0) { var query = _connection.ChannelStock.AsQueryable() .LoadWith(cs => cs.Drug) .LoadWith(cs => cs.drugManuNo) .LoadWith(cs => cs.Drug.Manus) .Where(cs => cs.DrawerNo == DrawerNo) .Where(cs => cs.DrugId != String.Empty) .Where(cs => cs.DrawerType == 1) .Where(cs => cs.MachineId == _setting.machineId); if (Quantity > 0) { query = query.Where(cs => cs.Quantity > 0); } return await query .OrderBy((cs) => cs.DrawerNo) .ThenBy((cs) => cs.ColNo) .ToListAsync(); } public async Task> GetChannelListByDrawerNo(int DrawerNo) { var query = _connection.ChannelList.AsQueryable(); return await query .LoadWith(cl => cl.Drug) .LoadWith(cl => cl.ChannelStocks.Where(cs => cs.Quantity > 0)) .Where(cl => cl.DrawerNo == DrawerNo) .Where(cl => cl.DrawerType == 1) .Where(cl => cl.MachineId == _setting.machineId) .OrderBy((cl) => cl.DrawerNo) .ThenBy((cl) => cl.ColNo) .ToListAsync(); } public async Task> GetAllDrugChannelStock() { var query = _connection.ChannelStock.AsQueryable(); return await query .LoadWith(cs => cs.Drug) .Where(cs => !String.IsNullOrEmpty(cs.DrugId)) .Where(cs => cs.DrawerType == 1) .Where(cs => cs.MachineId == _setting.machineId) .OrderBy((cs) => cs.DrugId) .ThenBy((cs) => cs.EffDate) .ThenBy((cs) => cs.DrawerNo) .ThenBy((cs) => cs.ColNo) .ToListAsync(); } public async Task> GetAllDrugChannelList() { var query = _connection.ChannelList.AsQueryable(); return await query .LoadWith(cl => cl.Drug) .LoadWith(cl => cl.ChannelStocks) .Where(cl => !String.IsNullOrEmpty(cl.DrugId)) .Where(cl => cl.DrawerType == 1) .Where(cl => cl.MachineId == _setting.machineId) .OrderBy((cl) => cl.DrugId) .ThenBy((cl) => cl.DrawerNo) .ThenBy((cl) => cl.ColNo) .ToListAsync(); } public async Task DrawerOperationFinish(List Stocks, int type = 1) { try { _connection.BeginTransaction(); string InvoiceId = "DRAWER_" + CurrentTimeMillis(); var flag = true; for (var i = 0; i < Stocks.Count; i++) { var stock = Stocks[i]; //var ManuNo = !stock.drugManuNo.ManuNo.Equals(stock.ManuNo) ? stock.drugManuNo.ManuNo : stock.ManuNo; //var EffDate = !stock.drugManuNo.ManuNo.Equals(stock.ManuNo) ? stock.drugManuNo.EffDate : stock.EffDate; var ManuNo = stock.ManuNo; var EffDate = stock.EffDate; if (!DateTime.TryParse(stock.EffDate, out DateTime dEffDate)) { //效期转换出错 if (stock.ManuNo != null) { string[] idate= stock.EffDate.Split('/'); foreach (string iS in idate) { if (!string.IsNullOrEmpty(iS.Replace(" ", "").Trim())) { switch (iS.Replace(" ", "").Trim().Length) { case 4: EffDate = iS.Replace(" ", "").Trim(); break; case 2: EffDate += "-" + iS.Replace(" ", "").Trim(); break; case 1: EffDate += "-0"+iS.Replace(" ", "").Trim(); break; } } } } } else { EffDate=dEffDate.ToString("yyyy-MM-dd"); } // 出入库记录 int mid = _connection.InsertWithInt32Identity(new MachineRecord() { MachineId = _setting.machineId, DrawerNo = stock.DrawerNo, ColNo = stock.ColNo, DrugId = stock.DrugId, ManuNo = ManuNo, EffDate = !String.IsNullOrEmpty(EffDate) ? DateTime.ParseExact(EffDate, "yyyy-MM-dd", System.Globalization.CultureInfo.CurrentCulture) : null, OperationTime = DateTime.Now, Type = type, Quantity = type == 1 ? stock.AddQuantity : stock.TakeQuantity, Operator = _globalStateService.Operator.Id, Reviewer = _globalStateService.Reviewer?.Id ?? _globalStateService.Operator.Id, InvoiceId = InvoiceId }); // 更新库存 var stockQ = _connection.ChannelStock.Where(cs => cs.Id == stock.Id) .Set(cs => cs.Quantity, type == 1 ? stock.Quantity + stock.AddQuantity : stock.Quantity - stock.TakeQuantity); // 入库时如果库存为0则有可能会修改批次效期进行保存 if (type == 1 && stock.Quantity == 0 && !stock.drugManuNo.ManuNo.Equals(stock.ManuNo)) { stockQ = stockQ.Set(cs => cs.ManuNo, stock.drugManuNo.ManuNo).Set(cs => cs.EffDate, stock.drugManuNo.EffDate.ToString()).Set(cs => cs.Dmnguid, stock.drugManuNo.Id); } int r = stockQ.Update(); // 获取更新完库存之后的药品库存 List list = await _connection.ChannelStock.AsQueryable() .InnerJoin( _connection.ChannelList.Where(cl => cl.MachineId.Equals(_setting.machineId)).Where(cl => cl.DrawerType == 1), (cs, cl) => cs.ListId == cl.Id, (cs, cl) => cs ) .Where(cs => cs.DrugId.Equals(stock.DrugId)) .ToListAsync(); // 保存账册 int acid = _connection.InsertWithInt32Identity(new AccountBook() { MachineId = _setting.machineId, DrugId = stock.DrugId, ManuNo = ManuNo, EffDate = !String.IsNullOrEmpty(EffDate) ? DateTime.ParseExact(EffDate, "yyyy-MM-dd", System.Globalization.CultureInfo.CurrentCulture) : null, OperationTime = DateTime.Now, Type = type, OutQuantity = stock.TakeQuantity, AddQuantity = stock.AddQuantity, Operator = _globalStateService.Operator.Id, Reviewer = _globalStateService.Reviewer?.Id ?? _globalStateService.Operator.Id, ManuStock = list.Where(it => it.ManuNo == stock.ManuNo).Sum(it => it.Quantity), TotalStock = list.Sum(it => it.Quantity), InvoiceId = InvoiceId }); if (mid > 0 && r > 0 && acid > 0) { } else { flag = false; break; } } if (flag) { _connection.CommitTransaction(); } else { _connection.RollbackTransaction(); } return flag; } catch (Exception ex) { logger.Error($"抽屉{(type == 1 ? "加药" : "取药")}操作完成保存数据库失败,错误:" + ex.Message); _connection.RollbackTransaction(); return false; } } public long CurrentTimeMillis() { return (long)(DateTime.UtcNow - new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalMilliseconds; } public async Task UnBind(string id) { var r = await _connection.ChannelStock .Where(cs => cs.Id == id) .Set(cs => cs.DrugId, String.Empty) .Set(cs => cs.Dmnguid, String.Empty) .Set(cs => cs.EffDate, String.Empty) .Set(cs => cs.ManuNo, String.Empty) .UpdateAsync(); return r > 0; } public async Task Bind(ChannelStock Stock) { var q = _connection.ChannelStock .Where(cs => cs.Id == Stock.Id) .Set(cs => cs.Dmnguid, Stock.drugManuNo?.Id ?? String.Empty) .Set(cs => cs.EffDate, Stock.drugManuNo?.EffDate.ToString().Substring(0, 10) ?? String.Empty) .Set(cs => cs.ManuNo, Stock.drugManuNo?.ManuNo ?? String.Empty); if (Stock.Drug != null && !Stock.Drug.DrugId.Equals(Stock.DrugId)) { q = q.Set(cs => cs.DrugId, Stock.Drug?.DrugId); } var r = await q.UpdateAsync(); return r > 0; } public async Task> GetAllChannelListWithDrug(int DrawerType, string drugName, int? take, int? skip) { var query = _connection.ChannelStock.AsQueryable(); if (DrawerType != 0) { query = query.Where(cl => cl.DrawerType == DrawerType); } if (!String.IsNullOrEmpty(drugName)) { query = query.Where(cl => cl.Drug.DrugName.Equals(drugName)); } //query = query.Where(cl => !String.IsNullOrEmpty(cl.DrugId)); query = query.Where(cl => cl.MachineId == _setting.machineId); int pagedData = await query.CountAsync(); List list = await query .LoadWith(cl => cl.Drug) .LoadWith(cl => cl.Drug.Manus) .LoadWith(cl => cl.drugManuNo) .OrderBy((cl) => cl.DrawerNo) .ThenBy((cl) => cl.ColNo) .Skip((int)skip) .Take((int)take) .ToListAsync(); var other = _connection.DrugInfo.AsQueryable(); List drugInfos = await other .LoadWith(di => di.Manus) .OrderBy((di) => di.DrugId) .ToListAsync(); return new PageMultiData() { TotalDesserts = pagedData, Desserts = list, Other = drugInfos }; } //抽屉加药、取药获取数据 public async Task> GetChannelStockByDrawerNoWithDrawers(int DrawerNo, int Quantity = 0) { var query = _connection.ChannelStock.AsQueryable() .LoadWith(cs => cs.Drug) .LoadWith(cs => cs.drugManuNo) .LoadWith(cs => cs.Drug.Manus) .Where(cs => cs.DrawerNo == DrawerNo) .Where(cs => cs.DrugId != String.Empty) .Where(cs => cs.DrawerType == 1) .Where(cs => cs.MachineId == _setting.machineId); if (Quantity > 0) { query = query.Where(cs => cs.Quantity > 0); } int[] ints = _connection.ChannelStock.Where(cs => cs.MachineId == _setting.machineId).GroupBy(cs => cs.DrawerNo).Select(cs => cs.Key).ToArray(); List channelStocks = await query .OrderBy((cs) => cs.DrawerNo) .ThenBy((cs) => cs.ColNo) .ToListAsync(); return new ChannelStockWithDrawerCount() { DrawerArray = ints, ChannelStocks = channelStocks }; } //盘点 public async Task DrawerCheckFinish(List Stocks) { try { _connection.BeginTransaction(); string InvoiceId = "DRAWER_" + CurrentTimeMillis(); var flag = true; for (var i = 0; i < Stocks.Count; i++) { var stock = Stocks[i]; //var ManuNo = !stock.drugManuNo.ManuNo.Equals(stock.ManuNo) ? stock.drugManuNo.ManuNo : stock.ManuNo; //var EffDate = !stock.drugManuNo.ManuNo.Equals(stock.ManuNo) ? stock.drugManuNo.EffDate : stock.EffDate; var ManuNo = stock.ManuNo; var EffDate = stock.EffDate; // 出入库记录 int mid = _connection.InsertWithInt32Identity(new MachineRecord() { MachineId = _setting.machineId, DrawerNo = stock.DrawerNo, ColNo = stock.ColNo, DrugId = stock.DrugId, ManuNo = ManuNo, EffDate = !String.IsNullOrEmpty(EffDate) ? DateTime.ParseExact(EffDate, "yyyy-MM-dd", System.Globalization.CultureInfo.CurrentCulture) : null, OperationTime = DateTime.Now, Type = 4, Quantity = stock.CheckQuantity, Operator = _globalStateService.Operator.Id, Reviewer = _globalStateService.Reviewer?.Id ?? _globalStateService.Operator.Id, InvoiceId = InvoiceId }); // 更新库存 var stockQ = _connection.ChannelStock.Where(cs => cs.Id == stock.Id) .Set(cs => cs.Quantity, stock.CheckQuantity); int r = stockQ.Update(); // 获取更新完库存之后的药品库存 List list = await _connection.ChannelStock.AsQueryable() .InnerJoin( _connection.ChannelList.Where(cl => cl.MachineId.Equals(_setting.machineId)).Where(cl => cl.DrawerType == 1), (cs, cl) => cs.ListId == cl.Id, (cs, cl) => cs ) .Where(cs => cs.DrugId.Equals(stock.DrugId)) .ToListAsync(); // 保存账册 int acid = _connection.InsertWithInt32Identity(new AccountBook() { MachineId = _setting.machineId, DrugId = stock.DrugId, ManuNo = ManuNo, EffDate = !String.IsNullOrEmpty(EffDate) ? DateTime.ParseExact(EffDate, "yyyy-MM-dd", System.Globalization.CultureInfo.CurrentCulture) : null, OperationTime = DateTime.Now, Type = 3, OutQuantity = stock.TakeQuantity, AddQuantity = stock.AddQuantity, Operator = _globalStateService.Operator.Id, Reviewer = _globalStateService.Reviewer?.Id ?? _globalStateService.Operator.Id, ManuStock = list.Where(it => it.ManuNo == stock.ManuNo).Sum(it => it.Quantity), TotalStock = list.Sum(it => it.Quantity), InvoiceId = InvoiceId }); if (mid > 0 && r > 0 && acid > 0) { //根据抽屉类型判断是否需要写标签 if (stock.BoardType.ToString().Contains("5")) { await _portUtil.WriteQuantityMethod(stock.CheckQuantity, stock.DrawerNo, stock.ColNo); } } else { flag = false; break; } } if (flag) { _connection.CommitTransaction(); } else { _connection.RollbackTransaction(); } return flag; } catch (Exception ex) { logger.Error($"抽屉盘点操作完成保存数据库失败,错误:" + ex.Message); _connection.RollbackTransaction(); return false; } } //抽屉获取库存数据--药品绑定 public async Task> GetChannelStockByBiaoDing(int DrawerNo, int Quantity = 0) { var query = _connection.ChannelStock.AsQueryable() .LoadWith(cs => cs.Drug) .LoadWith(cs => cs.drugManuNo) .LoadWith(cs => cs.Drug.Manus) .Where(cs => cs.DrawerType == 1) .Where(cs => cs.MachineId == _setting.machineId); if (DrawerNo > 0) { query = query.Where(cs => cs.DrawerNo == DrawerNo); } int[] ints = _connection.ChannelStock.Where(cs => cs.MachineId == _setting.machineId).GroupBy(cs => cs.DrawerNo).Select(cs => cs.Key).ToArray(); List channelStocks = await query .OrderBy((cs) => cs.DrawerNo) .ThenBy((cs) => cs.ColNo) .ToListAsync(); return new ChannelStockWithDrawerCount() { DrawerArray = ints, ChannelStocks = channelStocks }; } } }