1642 lines
		
	
	
		
			72 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			1642 lines
		
	
	
		
			72 KiB
		
	
	
	
		
			C#
		
	
	
	
 | 
						||
using LinqToDB;
 | 
						||
using log4net;
 | 
						||
using MasaBlazorApp3.DataAccess.Dao;
 | 
						||
using MasaBlazorApp3.Pages;
 | 
						||
using MasaBlazorApp3.Pojo;
 | 
						||
using MasaBlazorApp3.Pojo.Config;
 | 
						||
using MasaBlazorApp3.Pojo.Vo;
 | 
						||
using MasaBlazorApp3.Port;
 | 
						||
using Microsoft.Extensions.Options;
 | 
						||
using Mysqlx.Crud;
 | 
						||
using System.Collections.Generic;
 | 
						||
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<SettingConfig> setting, GlobalStateService globalStateService, PortUtil portUtil)
 | 
						||
        {
 | 
						||
            _globalStateService = globalStateService;
 | 
						||
            _connection = connection;
 | 
						||
            _setting = setting.Value;
 | 
						||
            _portUtil = portUtil;
 | 
						||
        }
 | 
						||
 | 
						||
        public async Task<PageData<ChannelStock>> 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<ChannelStock> 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<ChannelStock>()
 | 
						||
            {
 | 
						||
 | 
						||
                TotalDesserts = pagedData,
 | 
						||
                Desserts = list
 | 
						||
            };
 | 
						||
        }
 | 
						||
 | 
						||
        public async Task<List<ChannelStock>> 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<List<ChannelStock>> 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<List<ChannelList>> 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<List<ChannelStock>> 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<List<ChannelList>> 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<bool> DrawerOperationFinish(List<ChannelStock> 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<ChannelStock> 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<bool> 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<bool> 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.Value.ToString("yyyy-MM-dd") ?? 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<PageMultiData<ChannelStock, DrugInfo>> 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<ChannelStock> 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<DrugInfo> drugInfos = await other
 | 
						||
                .LoadWith(di => di.Manus)
 | 
						||
                .OrderBy((di) => di.DrugId)
 | 
						||
                .ToListAsync();
 | 
						||
 | 
						||
            return new PageMultiData<ChannelStock, DrugInfo>()
 | 
						||
            {
 | 
						||
 | 
						||
                TotalDesserts = pagedData,
 | 
						||
                Desserts = list,
 | 
						||
                Other = drugInfos
 | 
						||
            };
 | 
						||
        }
 | 
						||
        //抽屉加药、取药获取数据
 | 
						||
        public async Task<ChannelStockWithDrawerCount<ChannelStock>> 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<ChannelStock> channelStocks = await query
 | 
						||
                .OrderBy((cs) => cs.DrawerNo)
 | 
						||
                .ThenBy((cs) => cs.ColNo)
 | 
						||
                .ToListAsync();
 | 
						||
            return new ChannelStockWithDrawerCount<ChannelStock>() { DrawerArray = ints, ChannelStocks = channelStocks };
 | 
						||
        }
 | 
						||
        //盘点
 | 
						||
        public async Task<bool> DrawerCheckFinish(List<ChannelStock> Stocks)
 | 
						||
        {
 | 
						||
            try
 | 
						||
            {
 | 
						||
                _connection.BeginTransaction();
 | 
						||
 | 
						||
                string InvoiceId = "CHECK_" + 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 = 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)
 | 
						||
                        .Set(cs => cs.ManuNo, ManuNo)
 | 
						||
                        .Set(cs => cs.EffDate, EffDate)
 | 
						||
                        .Set(cs => cs.Dmnguid, stock.drugManuNo?.Id);
 | 
						||
 | 
						||
                    int r = stockQ.Update();
 | 
						||
                    // 获取更新完库存之后的药品库存
 | 
						||
                    List<ChannelStock> 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<ChannelStockWithDrawerCount<ChannelStock>> 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<ChannelStock> channelStocks = await query
 | 
						||
                .OrderBy((cs) => cs.DrawerNo)
 | 
						||
                .ThenBy((cs) => cs.ColNo)
 | 
						||
                .ToListAsync();
 | 
						||
            return new ChannelStockWithDrawerCount<ChannelStock>() { DrawerArray = ints, ChannelStocks = channelStocks };
 | 
						||
        }
 | 
						||
        //手术室药箱获取绑定数据
 | 
						||
        public async Task<PageMultiData<ChannelList, Plan>> GetAllChannelListWithPlan(int? take, int? skip)
 | 
						||
        {
 | 
						||
 | 
						||
            var query = _connection.ChannelList.AsQueryable()
 | 
						||
                .Where(cl => cl.MachineId == _setting.boxMachineId);
 | 
						||
            //.LoadWith(cl=>cl.PlanInfo);
 | 
						||
 | 
						||
 | 
						||
            int pagedData = await query.CountAsync();
 | 
						||
 | 
						||
            List<ChannelList> list = await query
 | 
						||
                .OrderBy((cl) => cl.DrawerNo)
 | 
						||
                .Skip((int)skip)
 | 
						||
                .Take((int)take)
 | 
						||
                .ToListAsync();
 | 
						||
            if (list != null && list.Count > 0)
 | 
						||
            {
 | 
						||
                for (int i = 0; i < list.Count; i++)
 | 
						||
                {
 | 
						||
                    if (!string.IsNullOrEmpty(list[i].DrugId))
 | 
						||
                    {
 | 
						||
                        list[i].PlanInfo = _connection.Plan.AsQueryable().Where(p => p.Id == Convert.ToInt32(list[i].DrugId)).FirstOrDefault();
 | 
						||
                    }
 | 
						||
                }
 | 
						||
            }
 | 
						||
 | 
						||
            var other = _connection.Plan.AsQueryable().Where(p => p.UseState == 1 && p._PlanDetails.Count > 0);
 | 
						||
            List<Plan> planInfos = await other
 | 
						||
                .LoadWith(p => p._PlanDetails.Where(pd => pd.UseState == 1))
 | 
						||
                .OrderBy((p) => p.Id)
 | 
						||
                .ToListAsync();
 | 
						||
            if (planInfos != null && planInfos.Count > 0)
 | 
						||
            {
 | 
						||
                for (int i = 0; i < planInfos.Count(); i++)
 | 
						||
                {
 | 
						||
                    for (int j = 0; j < planInfos[i]._PlanDetails.Count(); j++)
 | 
						||
                    {
 | 
						||
                        planInfos[i]._PlanDetails[j]._DrugInfo =
 | 
						||
                             _connection.DrugInfo.AsQueryable().Where(di => di.DrugId == planInfos[i]._PlanDetails[j].DrugId).First();
 | 
						||
 | 
						||
                    }
 | 
						||
                }
 | 
						||
            }
 | 
						||
 | 
						||
            return new PageMultiData<ChannelList, Plan>()
 | 
						||
            {
 | 
						||
 | 
						||
                TotalDesserts = pagedData,
 | 
						||
                Desserts = list,
 | 
						||
                Other = planInfos
 | 
						||
            };
 | 
						||
        }
 | 
						||
        /// <summary>
 | 
						||
        /// 手术室药箱绑定套餐
 | 
						||
        /// </summary>
 | 
						||
        /// <returns></returns>
 | 
						||
        public async Task<bool> BindBox(ChannelList list)
 | 
						||
        {
 | 
						||
            try
 | 
						||
            {
 | 
						||
                _connection.BeginTransaction();
 | 
						||
                bool bFlag = true;
 | 
						||
 | 
						||
                var q = _connection.ChannelList
 | 
						||
                    .Where(cs => cs.Id == list.Id).Set(cs => cs.DrugId, list.PlanInfo.Id.ToString());
 | 
						||
                //将套餐中的药品信息写入channelStock
 | 
						||
                //查询套餐中药品信息
 | 
						||
                var query = _connection.PlanDetails.AsQueryable().Where(p => p.PlanId == list.PlanInfo.Id&&p.UseState==1);
 | 
						||
                List<PlanDetails> planInfos = await query.ToListAsync();
 | 
						||
                if (planInfos != null && planInfos.Count > 0)
 | 
						||
                {
 | 
						||
                    for (int i = 0; i < planInfos.Count; i++)
 | 
						||
                    {
 | 
						||
                        int mid = await _connection.InsertAsync(new ChannelStock()
 | 
						||
                        {
 | 
						||
                            Id = Guid.NewGuid().ToString(),
 | 
						||
                            ListId = list.Id,
 | 
						||
                            MachineId = list.MachineId,
 | 
						||
                            DrawerNo = list.DrawerNo,
 | 
						||
                            DrugId = planInfos[i].DrugId.ToString(),
 | 
						||
                            BaseQuantity = planInfos[i].BaseQuantity,
 | 
						||
                            BoxState = 1
 | 
						||
                        });
 | 
						||
                        if (mid > 0)
 | 
						||
                        {
 | 
						||
                            bFlag = true;
 | 
						||
                        }
 | 
						||
                        else
 | 
						||
                        {
 | 
						||
                            bFlag = false;
 | 
						||
                            break;
 | 
						||
                        }
 | 
						||
                    }
 | 
						||
                }
 | 
						||
                var r = await q.UpdateAsync();
 | 
						||
                if (bFlag && r > 0)
 | 
						||
                {
 | 
						||
                    _connection.CommitTransaction();
 | 
						||
                    return true;
 | 
						||
                }
 | 
						||
                else
 | 
						||
                {
 | 
						||
                    _connection.RollbackTransaction();
 | 
						||
                    return false;
 | 
						||
                }
 | 
						||
            }
 | 
						||
            catch (Exception ex)
 | 
						||
            {
 | 
						||
                _connection.RollbackTransaction();
 | 
						||
                logger.Info($"绑定套餐异常{ex.Message}");
 | 
						||
                return false;
 | 
						||
            }
 | 
						||
        }
 | 
						||
        /// <summary>
 | 
						||
        /// 手术室药箱解绑套餐
 | 
						||
        /// </summary>
 | 
						||
        /// <param name="list"></param>
 | 
						||
        /// <returns></returns>
 | 
						||
        public async Task<bool> UnBindBox(ChannelList list)
 | 
						||
        {
 | 
						||
            try
 | 
						||
            {
 | 
						||
                _connection.BeginTransaction();
 | 
						||
 | 
						||
                var r = await _connection.ChannelList
 | 
						||
                     .Where(cs => cs.Id == list.Id)
 | 
						||
                     .Set(cs => cs.DrugId, String.Empty)
 | 
						||
                     .UpdateAsync();
 | 
						||
 | 
						||
                var cs = await _connection.ChannelStock.Where(cs => cs.ListId == list.Id).DeleteAsync();
 | 
						||
                if (r > 0 && cs > 0)
 | 
						||
                {
 | 
						||
                    _connection.CommitTransaction();
 | 
						||
                    return true;
 | 
						||
                }
 | 
						||
                else
 | 
						||
                {
 | 
						||
                    _connection.RollbackTransaction();
 | 
						||
                    return false;
 | 
						||
                }
 | 
						||
            }
 | 
						||
            catch (Exception ex)
 | 
						||
            {
 | 
						||
                _connection.RollbackTransaction();
 | 
						||
                logger.Info($"手术室药箱解绑异常:{ex.Message}");
 | 
						||
                return false;
 | 
						||
            }
 | 
						||
        }
 | 
						||
        //获取手术室药箱中所有要补药的数据
 | 
						||
        public async Task<PageData<ChannelList>> GetAllBoxAddDrug(int? take, int? skip)
 | 
						||
        {
 | 
						||
            try
 | 
						||
            {
 | 
						||
                List<ChannelList> channelLists = new List<ChannelList>();
 | 
						||
                var query = _connection.ChannelStock//.AsQueryable()
 | 
						||
                .Where(cs => cs.MachineId == _setting.boxMachineId && cs.BoxState == 1)
 | 
						||
                .GroupBy(cs => new { cs.DrawerNo, cs.DrugId })
 | 
						||
                .Select(g => new
 | 
						||
                {
 | 
						||
                    DrawerNo = g.Key.DrawerNo,
 | 
						||
                    DrugId = g.Key.DrugId,
 | 
						||
                    sumQuantity = g.Sum(cs => cs.Quantity)
 | 
						||
                })
 | 
						||
                .ToList();
 | 
						||
 | 
						||
                var queryChannelStock = _connection.ChannelStock.AsQueryable()
 | 
						||
               .Where(cs => cs.MachineId == _setting.boxMachineId && cs.BoxState == 1)
 | 
						||
               .LoadWith(cs => cs.Drug).ToList();
 | 
						||
 | 
						||
 | 
						||
                var queryList = await _connection.ChannelList.AsQueryable().Where(cl => cl.MachineId == _setting.boxMachineId).ToListAsync();
 | 
						||
                for (int i = 0; i < queryList.Count; i++)
 | 
						||
                {
 | 
						||
                    foreach (var item in query)
 | 
						||
                    {
 | 
						||
                        if (queryList[i].DrawerNo == item.DrawerNo)
 | 
						||
                        {
 | 
						||
                            ChannelStock stock = queryChannelStock.Where(cs => cs.DrawerNo == item.DrawerNo && cs.DrugId == item.DrugId && cs.BaseQuantity > item.sumQuantity).FirstOrDefault();
 | 
						||
                            if (stock != null)
 | 
						||
                            {
 | 
						||
                                stock.NeedQuantity = stock.BaseQuantity - item.sumQuantity;
 | 
						||
                                queryList[i].ChannelStocks.Add(stock);
 | 
						||
                            }
 | 
						||
                        }
 | 
						||
                    }
 | 
						||
                    if (queryList[i].ChannelStocks != null && queryList[i].ChannelStocks.Count > 0)
 | 
						||
                    {
 | 
						||
                        channelLists.Add(queryList[i]);
 | 
						||
                    }
 | 
						||
                }
 | 
						||
                int pagedData = channelLists.Count;
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
                return new PageData<ChannelList>()
 | 
						||
                {
 | 
						||
 | 
						||
                    TotalDesserts = pagedData,
 | 
						||
                    Desserts = channelLists
 | 
						||
                };
 | 
						||
            }
 | 
						||
            catch (Exception ex)
 | 
						||
            {
 | 
						||
                logger.Info($"获取药箱中补药数据失败{ex.Message}");
 | 
						||
                return null;
 | 
						||
            }
 | 
						||
        }
 | 
						||
 | 
						||
        //手术室药箱补药获取毒麻柜中的药品信息
 | 
						||
        public async Task<List<BoxTakeVo>> getTakeInfoByBox(ChannelList cl)
 | 
						||
        {
 | 
						||
            try
 | 
						||
            {
 | 
						||
                List<BoxTakeVo> tempData = new();
 | 
						||
                List<BoxTakeVo> tempData2 = new();
 | 
						||
                var flag = true;
 | 
						||
                for (int i = 0; i < cl.ChannelStocks.Count; i++)
 | 
						||
                {
 | 
						||
                    ChannelStock boxCs = cl.ChannelStocks[i];
 | 
						||
                    // 当前药品取药数量
 | 
						||
                    var Quantity = boxCs.NeedQuantity;
 | 
						||
                    List<ChannelStock> stockList = await _connection.ChannelStock
 | 
						||
                        .Where(cs => cs.MachineId == _setting.machineId && cs.DrawerType == 1 && cs.DrugId == boxCs.DrugId && cs.Quantity > 0 && cs.ManuNo != null)
 | 
						||
                        .AsQueryable()
 | 
						||
                        .OrderBy(cs => cs.EffDate).ToListAsync();
 | 
						||
 | 
						||
                    // 当前药品的库存总量
 | 
						||
                    var total = stockList.Sum(current => current.Quantity);
 | 
						||
 | 
						||
                    tempData2.Add(new BoxTakeVo()
 | 
						||
                    {
 | 
						||
                        Drug = boxCs.Drug,
 | 
						||
                        StockQuantity = total,
 | 
						||
                        Quantity = Quantity
 | 
						||
                    });
 | 
						||
 | 
						||
                    if (flag)
 | 
						||
                    {
 | 
						||
                        // 盘点库存是否足够
 | 
						||
                        if (total >= Quantity)
 | 
						||
                        {
 | 
						||
 | 
						||
                            for (var j = 0; Quantity > 0; j++)
 | 
						||
                            {
 | 
						||
                                ChannelStock stock = stockList[j];
 | 
						||
                                if (Quantity > stock.Quantity)
 | 
						||
                                {
 | 
						||
                                    // 取药数量大于库存
 | 
						||
                                    tempData.Add(new BoxTakeVo()
 | 
						||
                                    {
 | 
						||
                                        Drug = boxCs.Drug,
 | 
						||
                                        BoxDetail = boxCs,
 | 
						||
                                        ChannelStock = stock,
 | 
						||
                                        StockQuantity = total,
 | 
						||
                                        Quantity = stock.Quantity,
 | 
						||
                                    });
 | 
						||
                                    Quantity -= stock.Quantity;
 | 
						||
                                }
 | 
						||
                                else
 | 
						||
                                {
 | 
						||
                                    //取药数量小于库存
 | 
						||
                                    tempData.Add(new BoxTakeVo()
 | 
						||
                                    {
 | 
						||
                                        Drug = boxCs.Drug,
 | 
						||
                                        BoxDetail = boxCs,
 | 
						||
                                        ChannelStock = stock,
 | 
						||
                                        StockQuantity = total,
 | 
						||
                                        Quantity = Quantity,
 | 
						||
                                    });
 | 
						||
                                    Quantity = 0;
 | 
						||
                                }
 | 
						||
                            }
 | 
						||
                        }
 | 
						||
                        else
 | 
						||
                        {
 | 
						||
                            // 库存不足
 | 
						||
                            flag = false;
 | 
						||
                        }
 | 
						||
                    }
 | 
						||
                }
 | 
						||
                if (flag)
 | 
						||
                {
 | 
						||
                    return tempData;
 | 
						||
                }
 | 
						||
                else
 | 
						||
                {
 | 
						||
                    return tempData2;
 | 
						||
                }
 | 
						||
                return tempData;
 | 
						||
 | 
						||
            }
 | 
						||
            catch (Exception ex)
 | 
						||
            {
 | 
						||
                logger.Info($"获取数据异常{ex.Message}");
 | 
						||
                return null;
 | 
						||
            }
 | 
						||
        }
 | 
						||
        //手术室药箱补药完成
 | 
						||
        public async Task<bool> BoxTakeFinish(List<BoxTakeVo> datas, ChannelList boxChannelList)
 | 
						||
        {
 | 
						||
 | 
						||
            try
 | 
						||
            {
 | 
						||
                _connection.BeginTransaction();
 | 
						||
                var flag = true;
 | 
						||
                // 更新处方状态
 | 
						||
                //int r1 = _connection.ChannelStock.Where(oi => oi.OrderNo == datas.First().OrderDetail.OrderNo)
 | 
						||
                //        .Set(oi => oi.Status, 1)
 | 
						||
                //        .Update();
 | 
						||
                //if (!(r1 > 0))
 | 
						||
                //{
 | 
						||
                //    flag = false;
 | 
						||
                //    logger.Error("处方取药完成更新处方状态失败");
 | 
						||
                //    _connection.RollbackTransaction();
 | 
						||
                //    return flag;
 | 
						||
                //}
 | 
						||
                for (var i = 0; i < datas.Count; i++)
 | 
						||
                {
 | 
						||
                    var boxTakeVo = datas[i];
 | 
						||
                    var EffDate = boxTakeVo.ChannelStock.EffDate;
 | 
						||
                    if (!DateTime.TryParse(boxTakeVo.ChannelStock.EffDate, out DateTime dEffDate))
 | 
						||
                    {
 | 
						||
                        //效期转换出错
 | 
						||
                        if (boxTakeVo.ChannelStock.EffDate != null)
 | 
						||
                        {
 | 
						||
                            string[] idate = boxTakeVo.ChannelStock.EffDate.Split('/');
 | 
						||
                            foreach (string iS in idate)
 | 
						||
                            {
 | 
						||
                                if (!string.IsNullOrEmpty(iS.Trim()))
 | 
						||
                                {
 | 
						||
                                    switch (iS.Trim().Length)
 | 
						||
                                    {
 | 
						||
                                        case 4:
 | 
						||
                                            EffDate = iS.Trim();
 | 
						||
                                            break;
 | 
						||
                                        case 2:
 | 
						||
                                            EffDate += "-" + iS.Trim();
 | 
						||
                                            break;
 | 
						||
                                        case 1:
 | 
						||
                                            EffDate += "-0" + iS.Trim();
 | 
						||
                                            break;
 | 
						||
                                    }
 | 
						||
                                }
 | 
						||
                            }
 | 
						||
                        }
 | 
						||
                    }
 | 
						||
                    else
 | 
						||
                    {
 | 
						||
                        EffDate = dEffDate.ToString("yyyy-MM-dd");
 | 
						||
                    }
 | 
						||
                    // 出库记录
 | 
						||
                    int mid = _connection.InsertWithInt32Identity(new MachineRecord()
 | 
						||
                    {
 | 
						||
                        MachineId = _setting.machineId,
 | 
						||
                        DrawerNo = boxTakeVo.ChannelStock.DrawerNo,
 | 
						||
                        ColNo = boxTakeVo.ChannelStock.ColNo,
 | 
						||
                        DrugId = boxTakeVo.ChannelStock.DrugId,
 | 
						||
                        ManuNo = boxTakeVo.ChannelStock.ManuNo,
 | 
						||
                        EffDate = !String.IsNullOrEmpty(EffDate) ? DateTime.ParseExact(EffDate, "yyyy-MM-dd", System.Globalization.CultureInfo.CurrentCulture) : null,
 | 
						||
                        OperationTime = DateTime.Now,
 | 
						||
                        Type = 2,
 | 
						||
                        Quantity = boxTakeVo.GetQuantity,
 | 
						||
                        Operator = _globalStateService.Operator.Id,
 | 
						||
                        Reviewer = _globalStateService.Reviewer?.Id ?? _globalStateService.Operator.Id,
 | 
						||
                        InvoiceId = boxTakeVo.BoxDetail.Id.ToString(),
 | 
						||
                    });
 | 
						||
                    // 更新库存
 | 
						||
                    int r = _connection.ChannelStock.Where(cs => cs.Id == boxTakeVo.ChannelStock.Id)
 | 
						||
                        .Set(cs => cs.Quantity, boxTakeVo.ChannelStock.Quantity - boxTakeVo.GetQuantity)
 | 
						||
                        .Update();
 | 
						||
                    #region 保存账册,给药箱补药不记录出库账册
 | 
						||
                    // 获取更新完库存之后的药品库存
 | 
						||
                    //List<ChannelStock> 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(boxTakeVo.ChannelStock.DrugId))
 | 
						||
                    //     .ToListAsync();
 | 
						||
                    // 保存账册,给药箱补药不记录出库账册
 | 
						||
                    //int acid = _connection.InsertWithInt32Identity(new AccountBook()
 | 
						||
                    //{
 | 
						||
                    //    MachineId = _setting.machineId,
 | 
						||
                    //    DrugId = boxTakeVo.ChannelStock.DrugId,
 | 
						||
                    //    ManuNo = boxTakeVo.ChannelStock.ManuNo,
 | 
						||
                    //    EffDate = !String.IsNullOrEmpty(EffDate) ? DateTime.ParseExact(EffDate, "yyyy-MM-dd", System.Globalization.CultureInfo.CurrentCulture) : null,
 | 
						||
                    //    OperationTime = DateTime.Now,
 | 
						||
                    //    Type = 2,
 | 
						||
                    //    OutQuantity = boxTakeVo.GetQuantity,
 | 
						||
                    //    AddQuantity = 0,
 | 
						||
                    //    Operator = _globalStateService.Operator.Id,
 | 
						||
                    //    Reviewer = _globalStateService.Reviewer?.Id ?? _globalStateService.Operator.Id,
 | 
						||
                    //    ManuStock = list.Where(it => it.ManuNo == boxTakeVo.ChannelStock.ManuNo).Sum(it => it.Quantity),
 | 
						||
                    //    TotalStock = list.Sum(it => it.Quantity),
 | 
						||
                    //    //InvoiceId = orderTakeVo.OrderDetail.Id.ToString()
 | 
						||
                    //});
 | 
						||
                    #endregion
 | 
						||
 | 
						||
                    //更新药箱中的药品数量
 | 
						||
                    int boxID = _connection.Insert(new ChannelStock()
 | 
						||
                    {
 | 
						||
                        Id = Guid.NewGuid().ToString(),
 | 
						||
                        ListId = boxTakeVo.BoxDetail.Id,
 | 
						||
                        MachineId = _setting.boxMachineId,
 | 
						||
                        DrawerNo = boxTakeVo.BoxDetail.DrawerNo,
 | 
						||
                        AddToQuantity = boxTakeVo.GetQuantity,
 | 
						||
                        NeedQuantity = 0,
 | 
						||
                        DrugId = boxTakeVo.ChannelStock.DrugId,
 | 
						||
                        ManuNo = boxTakeVo.ChannelStock.ManuNo,
 | 
						||
                        EffDate = EffDate,
 | 
						||
                        BaseQuantity = boxTakeVo.BoxDetail.BaseQuantity,
 | 
						||
                        BoardType = boxTakeVo.BoxDetail.BoardType,
 | 
						||
                        BoxState = 2 //补药完成
 | 
						||
                    });
 | 
						||
                    // 手术室药箱入库记录
 | 
						||
                    int boxMId = _connection.InsertWithInt32Identity(new MachineRecord()
 | 
						||
                    {
 | 
						||
                        MachineId = _setting.machineId,
 | 
						||
                        DrawerNo = boxTakeVo.BoxDetail.DrawerNo,
 | 
						||
                        DrugId = boxTakeVo.ChannelStock.DrugId,
 | 
						||
                        ManuNo = boxTakeVo.ChannelStock.ManuNo,
 | 
						||
                        EffDate = !String.IsNullOrEmpty(EffDate) ? DateTime.ParseExact(EffDate, "yyyy-MM-dd", System.Globalization.CultureInfo.CurrentCulture) : null,
 | 
						||
                        OperationTime = DateTime.Now,
 | 
						||
                        Type = 1,
 | 
						||
                        Quantity = boxTakeVo.GetQuantity,
 | 
						||
                        Operator = _globalStateService.Operator.Id,
 | 
						||
                        Reviewer = _globalStateService.Reviewer?.Id ?? _globalStateService.Operator.Id,
 | 
						||
                        InvoiceId = boxTakeVo.ChannelStock.Id.ToString(),
 | 
						||
                    });
 | 
						||
                    //int totalQuantity = _connection.PlanDetails.Where(p => p.PlanId == Convert.ToInt32(boxChannelList.DrugId)&&p.DrugId== boxTakeVo.BoxDetail.DrugId).Sum(p => p.BaseQuantity);
 | 
						||
                    //int list = _connection.ChannelList.Where(cl => cl.Id == boxChannelList.Id)
 | 
						||
                    //    .Set(cl => cl.TotalQuantity, totalQuantity)
 | 
						||
                    //    .Update();
 | 
						||
                    int delResutl = await _connection.ChannelStock.Where(cs => cs.Id == boxTakeVo.BoxDetail.Id).DeleteAsync();
 | 
						||
                    if (mid > 0 && r > 0 && boxID > 0 && boxMId > 0)
 | 
						||
                    {
 | 
						||
                        if (boxTakeVo.ChannelStock.BoardType.ToString().Contains("5"))
 | 
						||
                        {
 | 
						||
                            await _portUtil.WriteQuantityMethod(boxTakeVo.ChannelStock.Quantity - boxTakeVo.GetQuantity, boxTakeVo.ChannelStock.DrawerNo, boxTakeVo.ChannelStock.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<PageData<ChannelList>> GetBoxWaitInfo(int? take, int? skip)
 | 
						||
        {
 | 
						||
            try
 | 
						||
            {
 | 
						||
                List<ChannelList> channelLists = new List<ChannelList>();
 | 
						||
                var query = _connection.ChannelStock//.AsQueryable()
 | 
						||
                .Where(cs => cs.MachineId == _setting.boxMachineId && cs.BoxState == 2)
 | 
						||
                .GroupBy(cs => new { cs.DrawerNo, cs.DrugId })
 | 
						||
                .Select(g => new
 | 
						||
                {
 | 
						||
                    DrawerNo = g.Key.DrawerNo,
 | 
						||
                    DrugId = g.Key.DrugId,
 | 
						||
                    sumQuantity = g.Sum(cs => cs.Quantity)
 | 
						||
                })
 | 
						||
                .ToList();
 | 
						||
 | 
						||
                var queryChannelStock = _connection.ChannelStock.AsQueryable()
 | 
						||
               .Where(cs => cs.MachineId == _setting.boxMachineId && cs.BoxState == 2)
 | 
						||
               .LoadWith(cs => cs.Drug).ToList();
 | 
						||
 | 
						||
 | 
						||
                var queryList = await _connection.ChannelList.AsQueryable().Where(cl => cl.MachineId == _setting.boxMachineId).ToListAsync();
 | 
						||
                for (int i = 0; i < queryList.Count; i++)
 | 
						||
                {
 | 
						||
                    foreach (var item in query)
 | 
						||
                    {
 | 
						||
                        if (queryList[i].DrawerNo == item.DrawerNo)
 | 
						||
                        {
 | 
						||
                            ChannelStock stock = queryChannelStock.Where(cs => cs.DrawerNo == item.DrawerNo && cs.DrugId == item.DrugId && cs.BaseQuantity > item.sumQuantity).FirstOrDefault();
 | 
						||
                            if (stock != null)
 | 
						||
                            {
 | 
						||
                                stock.NeedQuantity = stock.BaseQuantity - item.sumQuantity;
 | 
						||
                                queryList[i].ChannelStocks.Add(stock);
 | 
						||
                            }
 | 
						||
                        }
 | 
						||
                    }
 | 
						||
                    if (queryList[i].ChannelStocks != null && queryList[i].ChannelStocks.Count > 0)
 | 
						||
                    {
 | 
						||
                        channelLists.Add(queryList[i]);
 | 
						||
                    }
 | 
						||
                }
 | 
						||
                int pagedData = channelLists.Count;
 | 
						||
 | 
						||
 | 
						||
 | 
						||
 | 
						||
                return new PageData<ChannelList>()
 | 
						||
                {
 | 
						||
 | 
						||
                    TotalDesserts = pagedData,
 | 
						||
                    Desserts = channelLists
 | 
						||
                };
 | 
						||
            }
 | 
						||
            catch (Exception ex)
 | 
						||
            {
 | 
						||
                logger.Info($"获取药箱中补药数据失败{ex.Message}");
 | 
						||
                return null;
 | 
						||
            }
 | 
						||
        }
 | 
						||
        //手术室药箱入库获取待入库明细
 | 
						||
        public async Task<List<BoxTakeVo>> getBoxWaitByBox(ChannelList cl, int? take, int? skip)
 | 
						||
        {
 | 
						||
            try
 | 
						||
            {
 | 
						||
                List<BoxTakeVo> tempData = new();
 | 
						||
                List<BoxTakeVo> tempData2 = new();
 | 
						||
                var flag = true;
 | 
						||
                for (int i = 0; i < cl.ChannelStocks.Count; i++)
 | 
						||
                {
 | 
						||
                    ChannelStock boxCs = cl.ChannelStocks[i];
 | 
						||
                    // 当前药品取药数量
 | 
						||
                    var Quantity = boxCs.NeedQuantity;
 | 
						||
                    List<ChannelStock> stockList = await _connection.ChannelStock
 | 
						||
                        .Where(cs => cs.MachineId == _setting.boxMachineId && cs.DrugId == boxCs.DrugId && cs.Quantity > 0 && cs.ManuNo != null)
 | 
						||
                        .AsQueryable()
 | 
						||
                        .Skip((int)skip)
 | 
						||
                        .Take((int)take)
 | 
						||
                        .OrderBy(cs => cs.EffDate).ToListAsync();
 | 
						||
 | 
						||
                    // 当前药品的库存总量
 | 
						||
                    var total = stockList.Sum(current => current.Quantity);
 | 
						||
 | 
						||
                    tempData2.Add(new BoxTakeVo()
 | 
						||
                    {
 | 
						||
                        Drug = boxCs.Drug,
 | 
						||
                        StockQuantity = total,
 | 
						||
                        Quantity = Quantity
 | 
						||
                    });
 | 
						||
                }
 | 
						||
                return tempData;
 | 
						||
 | 
						||
            }
 | 
						||
            catch (Exception ex)
 | 
						||
            {
 | 
						||
                logger.Info($"获取数据异常{ex.Message}");
 | 
						||
                return null;
 | 
						||
            }
 | 
						||
        }
 | 
						||
        // 手术室药箱待入库明细入库完成
 | 
						||
        public async Task<bool> BoxAddBoxFinish(ChannelList boxChannelList)
 | 
						||
        {
 | 
						||
            try
 | 
						||
            {
 | 
						||
                _connection.BeginTransaction();
 | 
						||
                bool flag = true;
 | 
						||
                int totalQuantity =0;
 | 
						||
                for (var i = 0; i < boxChannelList.ChannelStocks.Count; i++)
 | 
						||
                {
 | 
						||
 | 
						||
                    var EffDate = boxChannelList.ChannelStocks[i].EffDate;
 | 
						||
                    //效期转换出错
 | 
						||
                    if (boxChannelList.ChannelStocks[i].EffDate != null)
 | 
						||
                    {
 | 
						||
                        string[] idate = boxChannelList.ChannelStocks[i].EffDate.Split('/');
 | 
						||
                        foreach (string iS in idate)
 | 
						||
                        {
 | 
						||
                            if (!string.IsNullOrEmpty(iS.Trim()))
 | 
						||
                            {
 | 
						||
                                switch (iS.Trim().Length)
 | 
						||
                                {
 | 
						||
                                    case 4:
 | 
						||
                                        EffDate = iS.Trim();
 | 
						||
                                        break;
 | 
						||
                                    case 2:
 | 
						||
                                        EffDate += "-" + iS.Trim();
 | 
						||
                                        break;
 | 
						||
                                    case 1:
 | 
						||
                                        EffDate += "-0" + iS.Trim();
 | 
						||
                                        break;
 | 
						||
                                }
 | 
						||
                            }
 | 
						||
                        }
 | 
						||
                    }
 | 
						||
                    // 手术室药箱入库记录
 | 
						||
                    int mid = _connection.InsertWithInt32Identity(new MachineRecord()
 | 
						||
                    {
 | 
						||
                        MachineId = _setting.boxMachineId,
 | 
						||
                        DrawerNo = boxChannelList.ChannelStocks[i].DrawerNo, 
 | 
						||
                        DrugId = boxChannelList.ChannelStocks[i].DrugId,
 | 
						||
                        ManuNo = boxChannelList.ChannelStocks[i].ManuNo,
 | 
						||
                        EffDate = !String.IsNullOrEmpty(EffDate) ? DateTime.ParseExact(EffDate, "yyyy-MM-dd", System.Globalization.CultureInfo.CurrentCulture) : null,
 | 
						||
                        OperationTime = DateTime.Now,
 | 
						||
                        Type = 1,
 | 
						||
                        Quantity = boxChannelList.ChannelStocks[i].AddToQuantity,
 | 
						||
                        Operator = _globalStateService.Operator.Id,
 | 
						||
                        Reviewer = _globalStateService.Reviewer?.Id ?? _globalStateService.Operator.Id,
 | 
						||
                        InvoiceId = boxChannelList.ChannelStocks[i].Id.ToString(),
 | 
						||
                    });
 | 
						||
                    // 更新库存
 | 
						||
                    boxChannelList.ChannelStocks[i].Quantity= boxChannelList.ChannelStocks[i].Quantity+ boxChannelList.ChannelStocks[i].AddToQuantity;
 | 
						||
                    boxChannelList.ChannelStocks[i].AddToQuantity = 0;
 | 
						||
                    boxChannelList.ChannelStocks[i].NeedQuantity = 0;
 | 
						||
                    boxChannelList.ChannelStocks[i].BoxState=0;
 | 
						||
                    boxChannelList.ChannelStocks[i].EffDate = EffDate;
 | 
						||
                    int r = _connection.Update(boxChannelList.ChannelStocks[i]);
 | 
						||
 | 
						||
                    totalQuantity += _connection.PlanDetails.Where(p => p.PlanId == Convert.ToInt32(boxChannelList.DrugId) && p.DrugId == boxChannelList.ChannelStocks[i].DrugId).Sum(p => p.BaseQuantity);
 | 
						||
                  
 | 
						||
                    if (!(mid > 0 && r > 0 ))                   
 | 
						||
                    {
 | 
						||
                        flag = false;
 | 
						||
                        break;
 | 
						||
                    }
 | 
						||
                }
 | 
						||
                if (flag)
 | 
						||
                {
 | 
						||
                    boxChannelList.TotalQuantity+= totalQuantity;
 | 
						||
                    int updateResult = _connection
 | 
						||
                        .Update(boxChannelList);
 | 
						||
                    if (updateResult > 0)
 | 
						||
                    {
 | 
						||
                        _connection.CommitTransaction();
 | 
						||
                    }
 | 
						||
                    else
 | 
						||
                    {                         flag = false;
 | 
						||
                        _connection.RollbackTransaction();
 | 
						||
                    }
 | 
						||
                }
 | 
						||
                else
 | 
						||
                {
 | 
						||
                    _connection.RollbackTransaction();
 | 
						||
                }
 | 
						||
                return flag;
 | 
						||
            }
 | 
						||
            catch (Exception ex)
 | 
						||
            {
 | 
						||
                logger.Info($"手术室药箱待入库明细入库完成操作异常:{ex.Message}");
 | 
						||
                return false;
 | 
						||
            }
 | 
						||
        }
 | 
						||
 | 
						||
        //手术室药箱获取药箱药品及库存信息
 | 
						||
        public async Task<PageData<PlanDetails>> GetBoxDrugInfo(int DrawerNo, int? take, int? skip)
 | 
						||
        {
 | 
						||
            var query = _connection.PlanDetails.AsQueryable()
 | 
						||
                .InnerJoin(
 | 
						||
                _connection.ChannelList.Where(cl => cl.MachineId == _setting.boxMachineId && cl.DrawerNo.Equals(DrawerNo)),
 | 
						||
                (pd, cl) => pd.PlanId.ToString() == cl.DrugId,
 | 
						||
                (pd, cl) => pd).Where(pd=>pd.UseState==1);
 | 
						||
 | 
						||
 | 
						||
 | 
						||
            int pagedData = await query.CountAsync();
 | 
						||
 | 
						||
            List<PlanDetails> list = await query
 | 
						||
                .LoadWith(pd => pd._DrugInfo)
 | 
						||
                //.LoadWith(pd => cl._PlanDetails._DrugInfo)
 | 
						||
                .OrderBy((pd) => pd.DrugId)
 | 
						||
                //.ThenBy((cl) => cl.ColNo)
 | 
						||
                .Skip((int)skip)
 | 
						||
                .Take((int)take)
 | 
						||
                .ToListAsync();
 | 
						||
            if (list != null && list.Count > 0)
 | 
						||
            {
 | 
						||
                for (int i = 0; i < list.Count; i++)
 | 
						||
                {
 | 
						||
                    list[i].channelStocks = _connection.ChannelStock.AsQueryable()
 | 
						||
                        .Where(cs => cs.MachineId == _setting.boxMachineId && cs.DrawerNo == DrawerNo && cs.DrugId == list[i].DrugId)
 | 
						||
                        .LoadWith(cs => cs.Drug)
 | 
						||
                        .LoadWith(cs => cs.Drug.Manus)
 | 
						||
                        .ToList();
 | 
						||
                }
 | 
						||
            }
 | 
						||
 | 
						||
            //List<ChannelStock> 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<PlanDetails>()
 | 
						||
            {
 | 
						||
 | 
						||
                TotalDesserts = pagedData,
 | 
						||
                Desserts = list
 | 
						||
            };
 | 
						||
        }
 | 
						||
 | 
						||
        //药箱移库时获取选中药箱号的药品信息
 | 
						||
        public async Task<PageData<ChannelStock>> GetChannelStockByDrug(ChannelStock cs, int drawerNo, int? take, int? skip)
 | 
						||
        {
 | 
						||
            try
 | 
						||
            {
 | 
						||
                var query = _connection.ChannelStock.AsQueryable().Where(c => c.MachineId == _setting.boxMachineId && c.DrawerNo.Equals(drawerNo) && c.DrugId.Equals(cs.DrugId) && c.ManuNo != cs.ManuNo && c.EffDate != cs.EffDate);
 | 
						||
 | 
						||
 | 
						||
 | 
						||
                int pagedData = await query.CountAsync();
 | 
						||
 | 
						||
                List<ChannelStock> list = await query
 | 
						||
                    .LoadWith(cs => cs.Drug)
 | 
						||
                    .OrderBy((cs) => cs.DrugId)
 | 
						||
                    .Skip((int)skip)
 | 
						||
                    .Take((int)take)
 | 
						||
                    .ToListAsync();
 | 
						||
 | 
						||
                return new PageData<ChannelStock>()
 | 
						||
                {
 | 
						||
 | 
						||
                    TotalDesserts = pagedData,
 | 
						||
                    Desserts = list
 | 
						||
                };
 | 
						||
            }
 | 
						||
            catch (Exception ex)
 | 
						||
            {
 | 
						||
                logger.Info($"药箱移库时获取选中药箱号的药品信息异常:{ex.Message}");
 | 
						||
                return null;
 | 
						||
            }
 | 
						||
        }
 | 
						||
 | 
						||
 | 
						||
        /// <summary>
 | 
						||
        /// 药箱交换药品获取所有除本药箱外的所有药箱号
 | 
						||
        /// </summary>
 | 
						||
        /// <param name="machineId"></param>
 | 
						||
        /// <returns></returns>
 | 
						||
        public async Task<int[]> GetDrawerNum(ChannelStock channelStock)
 | 
						||
        {
 | 
						||
            int[] ints = _connection.ChannelStock.Where(cs => cs.MachineId == channelStock.MachineId && cs.DrugId == channelStock.DrugId && cs.DrawerNo != channelStock.DrawerNo && !string.IsNullOrEmpty(cs.ManuNo) && cs.ManuNo != channelStock.ManuNo)
 | 
						||
                .GroupBy(cs => cs.DrawerNo).Select(cs => cs.Key).ToArray();
 | 
						||
            return ints;
 | 
						||
        }
 | 
						||
        /// <summary>
 | 
						||
        /// 药箱移除药品,获取所有除本药箱外的所有包含该药品的药箱号
 | 
						||
        /// </summary>
 | 
						||
        /// <param name="machineId"></param>
 | 
						||
        /// <returns></returns>
 | 
						||
        public async Task<int[]> GetDrawerNumForRemove(ChannelStock channelStock)
 | 
						||
        {
 | 
						||
            int[] ints = _connection.ChannelStock.Where(cs => cs.MachineId == channelStock.MachineId && cs.DrugId == channelStock.DrugId && cs.DrawerNo != channelStock.DrawerNo).GroupBy(cs => cs.DrawerNo).Select(cs => cs.Key).ToArray();
 | 
						||
            return ints;
 | 
						||
        }
 | 
						||
        //手术室药箱交换药品完成
 | 
						||
        public async Task<bool> BoxReplaceFinish(ChannelStock stock, List<ChannelStock> stockList)
 | 
						||
        {
 | 
						||
            try
 | 
						||
            {
 | 
						||
                bool flag = true;
 | 
						||
                _connection.BeginTransaction();
 | 
						||
                for (int i = 0; i < stockList.Count; i++)
 | 
						||
                {
 | 
						||
                    //查询出药的药箱是否有该批次的药品
 | 
						||
                    ChannelStock replace1ChannelStock = _connection.ChannelStock.AsQueryable()
 | 
						||
                        .Where(cs => cs.MachineId == stockList[i].MachineId && cs.DrawerNo == stock.DrawerNo && cs.DrugId == stockList[i].DrugId && cs.ManuNo == stockList[i].ManuNo && cs.EffDate == stockList[i].EffDate)
 | 
						||
                        .FirstOrDefault();
 | 
						||
                    if (replace1ChannelStock != null)
 | 
						||
                    {
 | 
						||
                        //如果有该批次的药品,则更新数量
 | 
						||
                        int r = _connection.ChannelStock.Where(cs => cs.Id == replace1ChannelStock.Id)
 | 
						||
                            .Set(cs => cs.Quantity, replace1ChannelStock.Quantity + stockList[i].AddQuantity)
 | 
						||
                            .Update();
 | 
						||
                        if (r <= 0)
 | 
						||
                        {
 | 
						||
                            logger.Info($"更新药箱药品数量失败,药箱号:{stockList[i].DrawerNo},药品ID:{stockList[i].DrugId}");
 | 
						||
                        }
 | 
						||
                    }
 | 
						||
                    else
 | 
						||
                    {
 | 
						||
                        //如果没有该批次的药品,则新增一条记录
 | 
						||
                        int mid = _connection.Insert(new ChannelStock()
 | 
						||
                        {
 | 
						||
                            Id = Guid.NewGuid().ToString(),
 | 
						||
                            MachineId = stockList[i].MachineId,
 | 
						||
                            DrawerNo = stock.DrawerNo,
 | 
						||
                            DrugId = stockList[i].DrugId,
 | 
						||
                            ManuNo = stockList[i].ManuNo,
 | 
						||
                            EffDate = stockList[i].EffDate,
 | 
						||
                            Quantity = stockList[i].AddToQuantity,
 | 
						||
                            BaseQuantity = stockList[i].BaseQuantity,
 | 
						||
                            BoxState = 1
 | 
						||
                        });
 | 
						||
                        if (mid <= 0)
 | 
						||
                        {
 | 
						||
                            logger.Info($"新增药箱药品记录失败,药箱号:{stockList[i].DrawerNo},药品ID:{stockList[i].DrugId}");
 | 
						||
                            flag = false;
 | 
						||
                            break;
 | 
						||
                        }
 | 
						||
                    }
 | 
						||
 | 
						||
                    //记录操作记录
 | 
						||
                    int recordId = _connection.InsertWithInt32Identity(new MachineRecord()
 | 
						||
                    {
 | 
						||
                        MachineId = stock.MachineId,
 | 
						||
                        DrawerNo = stock.DrawerNo,
 | 
						||
                        DrugId = stock.DrugId,
 | 
						||
                        ManuNo = stock.ManuNo,
 | 
						||
                        EffDate = !String.IsNullOrEmpty(stock.EffDate) ? DateTime.ParseExact(stock.EffDate, "yyyy-MM-dd", System.Globalization.CultureInfo.CurrentCulture) : null,
 | 
						||
                        OperationTime = DateTime.Now,
 | 
						||
                        Type = 69, //交换
 | 
						||
                        Quantity = stockList[i].AddQuantity,
 | 
						||
                        Operator = _globalStateService.Operator.Id,
 | 
						||
                        Reviewer = _globalStateService.Reviewer?.Id ?? _globalStateService.Operator.Id,
 | 
						||
                        InvoiceId = stockList[i].Id.ToString(),
 | 
						||
                    });
 | 
						||
                    if (recordId <= 0)
 | 
						||
                    {
 | 
						||
                        logger.Info($"药箱{stock.DrawerNo}交换药品新增操作记录失败,药箱号:{stockList[i].DrawerNo},药品ID:{stockList[i].DrugId}");
 | 
						||
                        flag = false;
 | 
						||
                        break;
 | 
						||
                    }
 | 
						||
 | 
						||
 | 
						||
 | 
						||
                    //更新入药的药箱中的药品数量
 | 
						||
                    ChannelStock replace2ChannelStock = _connection.ChannelStock.AsQueryable()
 | 
						||
                         .Where(cs => cs.MachineId == stockList[i].MachineId && cs.DrawerNo == stockList[i].DrawerNo && cs.DrugId == stock.DrugId && cs.ManuNo == stock.ManuNo && cs.EffDate == stock.EffDate)
 | 
						||
                         .FirstOrDefault();
 | 
						||
                    if (replace2ChannelStock != null)
 | 
						||
                    {
 | 
						||
                        //如果有该批次的药品,则更新数量
 | 
						||
                        int r = _connection.ChannelStock.Where(cs => cs.Id == replace2ChannelStock.Id)
 | 
						||
                            .Set(cs => cs.Quantity, replace2ChannelStock.Quantity + stockList[i].AddQuantity)
 | 
						||
                            .Update();
 | 
						||
                        if (r <= 0)
 | 
						||
                        {
 | 
						||
                            logger.Info($"更新药箱药品数量失败,药箱号:{stockList[i].DrawerNo},药品ID:{stockList[i].DrugId}");
 | 
						||
                            flag = false;
 | 
						||
                            break;
 | 
						||
                        }
 | 
						||
                    }
 | 
						||
                    else
 | 
						||
                    {
 | 
						||
                        //如果没有该批次的药品,则新增一条记录
 | 
						||
                        int mid = _connection.Insert(new ChannelStock()
 | 
						||
                        {
 | 
						||
                            Id = Guid.NewGuid().ToString(),
 | 
						||
                            MachineId = stockList[i].MachineId,
 | 
						||
                            DrawerNo = stockList[i].DrawerNo,
 | 
						||
                            DrugId = stock.DrugId,
 | 
						||
                            ManuNo = stock.ManuNo,
 | 
						||
                            EffDate = stock.EffDate,
 | 
						||
                            Quantity = stockList[i].AddQuantity,
 | 
						||
                            BaseQuantity = stock.BaseQuantity,
 | 
						||
                            BoxState = 1
 | 
						||
                        });
 | 
						||
                        if (mid <= 0)
 | 
						||
                        {
 | 
						||
                            logger.Info($"新增药箱药品记录失败,药箱号:{stockList[i].DrawerNo},药品ID:{stockList[i].DrugId}");
 | 
						||
                            flag = false;
 | 
						||
                            break;
 | 
						||
                        }
 | 
						||
 | 
						||
                    }
 | 
						||
                    //记录操作记录
 | 
						||
                    int record2Id = _connection.InsertWithInt32Identity(new MachineRecord()
 | 
						||
                    {
 | 
						||
                        MachineId = stockList[i].MachineId,
 | 
						||
                        DrawerNo = stockList[i].DrawerNo,
 | 
						||
                        DrugId = stockList[i].DrugId,
 | 
						||
                        ManuNo = stockList[i].ManuNo,
 | 
						||
                        EffDate = !String.IsNullOrEmpty(stockList[i].EffDate) ? DateTime.ParseExact(stockList[i].EffDate, "yyyy-MM-dd", System.Globalization.CultureInfo.CurrentCulture) : null,
 | 
						||
                        OperationTime = DateTime.Now,
 | 
						||
                        Type = 69, //交换
 | 
						||
                        Quantity = stockList[i].AddQuantity,
 | 
						||
                        Operator = _globalStateService.Operator.Id,
 | 
						||
                        Reviewer = _globalStateService.Reviewer?.Id ?? _globalStateService.Operator.Id,
 | 
						||
                        InvoiceId = stock.Id.ToString(),
 | 
						||
                    });
 | 
						||
                    if (record2Id <= 0)
 | 
						||
                    {
 | 
						||
                        logger.Info($"药箱{stock.DrawerNo}交换药品新增操作记录失败,药箱号:{stockList[i].DrawerNo},药品ID:{stockList[i].DrugId}");
 | 
						||
                        flag = false;
 | 
						||
                        break;
 | 
						||
                    }
 | 
						||
                    //出库药箱减数量
 | 
						||
                    int updateQuantity = _connection.ChannelStock.Where(cs => cs.Id == stock.Id)
 | 
						||
                           .Set(cs => cs.Quantity, stock.Quantity - stockList[i].AddQuantity)
 | 
						||
                           .Update();
 | 
						||
                    if (updateQuantity <= 0)
 | 
						||
                    {
 | 
						||
                        logger.Info($"更新出库药箱药品减数量失败,药箱号:{stock.DrawerNo},药品ID:{stock.DrugId}");
 | 
						||
                        flag = false;
 | 
						||
                        break;
 | 
						||
                    }
 | 
						||
                    //入库药箱减数量
 | 
						||
                    int updateQuantity2 = _connection.ChannelStock.Where(cs => cs.Id == stockList[i].Id)
 | 
						||
                            .Set(cs => cs.Quantity, stockList[i].Quantity - stockList[i].AddQuantity)
 | 
						||
                            .Update();
 | 
						||
                    if (updateQuantity2 <= 0)
 | 
						||
                    {
 | 
						||
                        logger.Info($"更新入库药箱药品减数量失败,药箱号:{stockList[i].DrawerNo},药品ID:{stockList[i].DrugId}"); flag = false;
 | 
						||
                        break;
 | 
						||
                    }
 | 
						||
                }
 | 
						||
                if (flag)
 | 
						||
                {
 | 
						||
 | 
						||
                    _connection.CommitTransaction();
 | 
						||
                }
 | 
						||
                else
 | 
						||
                {
 | 
						||
                    _connection.RollbackTransaction();
 | 
						||
                }
 | 
						||
                return flag;
 | 
						||
 | 
						||
            }
 | 
						||
            catch (Exception ex)
 | 
						||
            {
 | 
						||
                logger.Info($"手术室药箱交换药品完成异常:{ex.Message}");
 | 
						||
                _connection.RollbackTransaction();
 | 
						||
                return false;
 | 
						||
            }
 | 
						||
        }
 | 
						||
        //手术室药箱移出药品完成
 | 
						||
        public async Task<bool> BoxRemoveFinish(ChannelStock stock, int SelectedDrawerNo, int removeQuantity)
 | 
						||
        {
 | 
						||
            try
 | 
						||
            {
 | 
						||
                bool flag = true;
 | 
						||
                _connection.BeginTransaction();
 | 
						||
                //查询移入的药品是否有库存
 | 
						||
                ChannelStock inChannelStock = _connection.ChannelStock.AsQueryable()
 | 
						||
                       .Where(cs => cs.MachineId == stock.MachineId && cs.DrawerNo == SelectedDrawerNo && cs.DrugId == stock.DrugId && cs.ManuNo == stock.ManuNo && cs.EffDate == stock.EffDate)
 | 
						||
                       .FirstOrDefault();
 | 
						||
                if (inChannelStock != null)
 | 
						||
                {
 | 
						||
                    //如果有该批次的药品,则更新数量
 | 
						||
                    int r = _connection.ChannelStock.Where(cs => cs.Id == inChannelStock.Id)
 | 
						||
                        .Set(cs => cs.Quantity, inChannelStock.Quantity + removeQuantity)
 | 
						||
                        .Update();
 | 
						||
                    if (r <= 0)
 | 
						||
                    {
 | 
						||
                        logger.Info($"手术室药箱移出药品更新数量失败,药箱号:{SelectedDrawerNo},药品ID:{stock.DrugId}");
 | 
						||
                    }
 | 
						||
                }
 | 
						||
                else
 | 
						||
                {
 | 
						||
                    //如果没有该批次的药品,先查询是否有未绑批次的,有则更新批次信息,无则新增一条记录
 | 
						||
                    ChannelStock inDrugChannelStock = _connection.ChannelStock.AsQueryable()
 | 
						||
                      .Where(cs => cs.MachineId == stock.MachineId && cs.DrawerNo == SelectedDrawerNo && cs.DrugId == stock.DrugId)
 | 
						||
                      .FirstOrDefault();
 | 
						||
                    if (inDrugChannelStock != null)
 | 
						||
                    {
 | 
						||
                        int mid = _connection.ChannelStock.Where(cs => cs.Id == inDrugChannelStock.Id)
 | 
						||
                        .Set(cs => cs.Quantity, removeQuantity)
 | 
						||
                        .Set(cs => cs.ManuNo, stock.ManuNo)
 | 
						||
                        .Set(cs => cs.EffDate, stock.EffDate)
 | 
						||
                        .Update();
 | 
						||
                        if (mid <= 0)
 | 
						||
                        {
 | 
						||
                            logger.Info($"更新药箱药品批次记录失败,药箱号:{stock.DrawerNo},药品ID:{stock.DrugId}");
 | 
						||
                            flag = false;
 | 
						||
                        }
 | 
						||
                    }
 | 
						||
                    else
 | 
						||
                    {
 | 
						||
                        int mid = _connection.Insert(new ChannelStock()
 | 
						||
                        {
 | 
						||
                            Id = Guid.NewGuid().ToString(),
 | 
						||
                            MachineId = stock.MachineId,
 | 
						||
                            DrawerNo = SelectedDrawerNo,
 | 
						||
                            DrugId = stock.DrugId,
 | 
						||
                            ManuNo = stock.ManuNo,
 | 
						||
                            EffDate = stock.EffDate,
 | 
						||
                            Quantity = removeQuantity,
 | 
						||
                            BaseQuantity = stock.BaseQuantity,
 | 
						||
                            BoxState = 1
 | 
						||
                        });
 | 
						||
                        if (mid <= 0)
 | 
						||
                        {
 | 
						||
                            logger.Info($"新增药箱药品记录失败,药箱号:{stock.DrawerNo},药品ID:{stock.DrugId}");
 | 
						||
                            flag = false;
 | 
						||
                        }
 | 
						||
                    }
 | 
						||
                }
 | 
						||
                if (flag)
 | 
						||
                {
 | 
						||
                    //减本库位库存数量
 | 
						||
                    int updateQuantity2 = _connection.ChannelStock.Where(cs => cs.Id == stock.Id)
 | 
						||
                            .Set(cs => cs.Quantity, stock.Quantity - removeQuantity)
 | 
						||
                            .Update();
 | 
						||
                    if (updateQuantity2 <= 0)
 | 
						||
                    {
 | 
						||
                        logger.Info($"更新入库药箱药品减数量失败,药箱号:{stock.DrawerNo},药品ID:{stock.DrugId}");
 | 
						||
                        flag = false;
 | 
						||
                    }
 | 
						||
                    else
 | 
						||
                    {
 | 
						||
                        int updateTotalQuantity = 0;
 | 
						||
                        int updateMoveToTotalQuantity = 0;
 | 
						||
                        //修改总库存数
 | 
						||
                        ChannelList channelList = _connection.ChannelList.Where(cl => cl.MachineId == stock.MachineId && cl.DrawerNo == stock.DrawerNo && cl.DrugId == stock.DrugId).FirstOrDefault();
 | 
						||
                        if (channelList != null)
 | 
						||
                        {
 | 
						||
                            updateTotalQuantity = _connection.ChannelList.Where(cl => cl.Id == channelList.Id)
 | 
						||
                                .Set(cl => cl.TotalQuantity, channelList.TotalQuantity - removeQuantity)
 | 
						||
                                .Update();
 | 
						||
                        }
 | 
						||
                        //修改移入药箱的总库存数
 | 
						||
                        ChannelList moveToChannelList = _connection.ChannelList.Where(cl => cl.MachineId == stock.MachineId && cl.DrawerNo == SelectedDrawerNo && cl.DrugId == stock.DrugId).FirstOrDefault();
 | 
						||
                        if (moveToChannelList != null)
 | 
						||
                        {
 | 
						||
                            updateTotalQuantity = _connection.ChannelList.Where(cl => cl.Id == moveToChannelList.Id)
 | 
						||
                                .Set(cl => cl.TotalQuantity, channelList.TotalQuantity + removeQuantity)
 | 
						||
                                .Update();
 | 
						||
                        }
 | 
						||
                        if (updateTotalQuantity > 0 && updateMoveToTotalQuantity > 0)
 | 
						||
                        {
 | 
						||
                            flag = true;
 | 
						||
                        }
 | 
						||
                        else
 | 
						||
                        {
 | 
						||
                            flag = false;
 | 
						||
                        }
 | 
						||
 | 
						||
                    }
 | 
						||
                    if (flag)
 | 
						||
                    {
 | 
						||
                        //记录操作记录
 | 
						||
                        int recordId = _connection.InsertWithInt32Identity(new MachineRecord()
 | 
						||
                        {
 | 
						||
                            MachineId = stock.MachineId,
 | 
						||
                            DrawerNo = stock.DrawerNo,
 | 
						||
                            ColNo = SelectedDrawerNo,//移入的药箱号
 | 
						||
                            DrugId = stock.DrugId,
 | 
						||
                            ManuNo = stock.ManuNo,
 | 
						||
                            EffDate = !String.IsNullOrEmpty(stock.EffDate) ? DateTime.ParseExact(stock.EffDate, "yyyy-MM-dd", System.Globalization.CultureInfo.CurrentCulture) : null,
 | 
						||
                            OperationTime = DateTime.Now,
 | 
						||
                            Type = 21, //交换
 | 
						||
                            Quantity = removeQuantity,
 | 
						||
                            Operator = _globalStateService.Operator.Id,
 | 
						||
                            Reviewer = _globalStateService.Reviewer?.Id ?? _globalStateService.Operator.Id,
 | 
						||
                            InvoiceId = stock.Id.ToString(),
 | 
						||
                        });
 | 
						||
                        if (recordId <= 0)
 | 
						||
                        {
 | 
						||
                            logger.Info($"药箱{stock.DrawerNo}交换药品新增操作记录失败,药箱号:{stock.DrawerNo},药品ID:{stock.DrugId}");
 | 
						||
                            flag = false;
 | 
						||
                        }
 | 
						||
                    }
 | 
						||
                }
 | 
						||
                if (flag)
 | 
						||
                {
 | 
						||
 | 
						||
                    _connection.CommitTransaction();
 | 
						||
                }
 | 
						||
                else
 | 
						||
                {
 | 
						||
                    _connection.RollbackTransaction();
 | 
						||
                }
 | 
						||
                return flag;
 | 
						||
 | 
						||
            }
 | 
						||
            catch (Exception ex)
 | 
						||
            {
 | 
						||
                logger.Info($"手术室药箱移出药品异常{ex.Message}");
 | 
						||
                _connection.RollbackTransaction();
 | 
						||
                return false;
 | 
						||
            }
 | 
						||
        }
 | 
						||
 | 
						||
    }
 | 
						||
}
 |