308 lines
		
	
	
		
			10 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			308 lines
		
	
	
		
			10 KiB
		
	
	
	
		
			C#
		
	
	
	
using LinqToDB;
 | 
						|
using log4net;
 | 
						|
using MasaBlazorApp3.DataAccess.Dao;
 | 
						|
using MasaBlazorApp3.Pojo;
 | 
						|
using MasaBlazorApp3.Pojo.Config;
 | 
						|
using Microsoft.Extensions.Options;
 | 
						|
using System;
 | 
						|
using System.Collections.Generic;
 | 
						|
using System.Data.Common;
 | 
						|
using System.Linq;
 | 
						|
using System.Linq.Dynamic.Core;
 | 
						|
using System.Numerics;
 | 
						|
using System.Runtime.CompilerServices;
 | 
						|
using System.Text;
 | 
						|
using System.Threading.Tasks;
 | 
						|
using System.Xml.Linq;
 | 
						|
using ZstdSharp.Unsafe;
 | 
						|
 | 
						|
namespace MasaBlazorApp3.DataAccess.Impl
 | 
						|
{
 | 
						|
    public class PlanDao : IPlanDao
 | 
						|
    {
 | 
						|
        private AppDataConnection _connection;
 | 
						|
        private readonly SettingConfig _setting;
 | 
						|
        private readonly ILog logger = LogManager.GetLogger(typeof(DrugInfoDao));
 | 
						|
        private GlobalStateService _globalStateService;
 | 
						|
        public PlanDao(AppDataConnection connection, IOptions<SettingConfig> setting, GlobalStateService globalStateService)
 | 
						|
        {
 | 
						|
            _connection = connection;
 | 
						|
            _setting = setting.Value;
 | 
						|
            _globalStateService = globalStateService;
 | 
						|
        }
 | 
						|
        /// <summary>
 | 
						|
        /// 获取所有套餐数据
 | 
						|
        /// </summary>
 | 
						|
        /// <returns></returns>
 | 
						|
        public async Task<PageMultiData<Plan, DrugInfo>> GetAllPlanInfo()
 | 
						|
        {
 | 
						|
            var query = _connection.Plan.AsQueryable();
 | 
						|
 | 
						|
            List<Plan> list = await query.Where(p=>p.UseState==1)
 | 
						|
                .LoadWith(p => p._PlanDetails.Where(pd=>pd.UseState==1))
 | 
						|
                //.ThenLoad(p=>p._DrugInfo)
 | 
						|
                .OrderBy(r => r.Id)
 | 
						|
                .ToListAsync();
 | 
						|
            if (list != null && list.Count > 0)
 | 
						|
            {
 | 
						|
                for (int i = 0; i < list.Count(); i++)
 | 
						|
                {
 | 
						|
                    for (int j = 0; j < list[i]._PlanDetails.Count(); j++)
 | 
						|
                    {
 | 
						|
                        list[i]._PlanDetails[j]._DrugInfo =
 | 
						|
                             _connection.DrugInfo.AsQueryable().Where(di => di.DrugId == list[i]._PlanDetails[j].DrugId).First();
 | 
						|
 | 
						|
                    }
 | 
						|
                }
 | 
						|
            }
 | 
						|
 | 
						|
            var other = _connection.DrugInfo.AsQueryable();
 | 
						|
            List<DrugInfo> drugInfos = await other
 | 
						|
                .LoadWith(di => di.Manus)
 | 
						|
                .OrderBy((di) => di.DrugId)
 | 
						|
                .ToListAsync();
 | 
						|
 | 
						|
            int pagedData = await query.CountAsync();
 | 
						|
            return new PageMultiData<Plan, DrugInfo>()
 | 
						|
            {
 | 
						|
 | 
						|
                TotalDesserts = pagedData,
 | 
						|
                Desserts = list,
 | 
						|
                Other = drugInfos
 | 
						|
            };
 | 
						|
        }
 | 
						|
        /// <summary>
 | 
						|
        /// 根据套餐ID获取套餐数据
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="Id"></param>
 | 
						|
        /// <returns></returns>
 | 
						|
        public async Task<Plan> GetPlanById(int Id)
 | 
						|
        {
 | 
						|
            var query = _connection.Plan.AsQueryable().Where(p => p.Id == Id&&p.UseState==1);
 | 
						|
            List<Plan> list = await query
 | 
						|
               .LoadWith(p => p._PlanDetails)
 | 
						|
               //.ThenLoad(p=>p._DrugInfo)
 | 
						|
               .OrderBy(r => r.Id)
 | 
						|
               .ToListAsync();
 | 
						|
            if (list != null && list.Count > 0)
 | 
						|
            {
 | 
						|
                for (int i = 0; i < list.Count(); i++)
 | 
						|
                {
 | 
						|
                    for (int j = 0; j < list[i]._PlanDetails.Count(); j++)
 | 
						|
                    {
 | 
						|
                        list[i]._PlanDetails[j]._DrugInfo =
 | 
						|
                             _connection.DrugInfo.AsQueryable().Where(di => di.DrugId == list[i]._PlanDetails[j].DrugId).First();
 | 
						|
 | 
						|
                    }
 | 
						|
                }
 | 
						|
            }
 | 
						|
 | 
						|
            return list[0];
 | 
						|
        }
 | 
						|
 | 
						|
        /// <summary>
 | 
						|
        /// 新增套餐
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="plan"></param>
 | 
						|
        /// <returns></returns>
 | 
						|
        public async Task<bool> InsertPlanInfo(Plan plan)
 | 
						|
        {
 | 
						|
            try
 | 
						|
            {
 | 
						|
                plan.AddTime = DateTime.Now;
 | 
						|
                plan.OperatorUser = _globalStateService.Operator.Id;
 | 
						|
                plan.ReviewerUser = _globalStateService.Reviewer?.Id ?? _globalStateService.Operator.Id;
 | 
						|
                return _connection.InsertWithInt32Identity(plan) > 0;
 | 
						|
            }
 | 
						|
            catch (Exception ex)
 | 
						|
            {
 | 
						|
 | 
						|
                logger.Error($"添加套餐{plan.Name}失败,错误:" + ex.Message);
 | 
						|
                return false;
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        /// <summary>
 | 
						|
        /// 更新套餐
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="plan"></param>
 | 
						|
        /// <returns></returns>
 | 
						|
        public async Task<bool> UpdatePlanInfo(Plan plan)
 | 
						|
        {
 | 
						|
            try
 | 
						|
            {
 | 
						|
                var iResult = _connection.Plan
 | 
						|
                  .Where(p => p.Id == plan.Id)
 | 
						|
                  .Set(p => p.Name, plan.Name)
 | 
						|
                  .Set(p => p.Description, plan.Description);
 | 
						|
                return iResult.Update() > 0;
 | 
						|
            }
 | 
						|
            catch (Exception ex)
 | 
						|
            {
 | 
						|
                logger.Error($"修改套餐{plan.Name}失败,错误:" + ex.Message);
 | 
						|
                return false;
 | 
						|
            }
 | 
						|
        }
 | 
						|
        /// <summary>
 | 
						|
        /// 删除套餐
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="planId"></param>
 | 
						|
        /// <returns></returns>
 | 
						|
        public async Task<bool> DeletePlanInfo(int planId)
 | 
						|
        {
 | 
						|
            try
 | 
						|
            {
 | 
						|
                _connection.BeginTransaction();
 | 
						|
                bool flag = false;
 | 
						|
                //查询是否有绑定,没有绑定可以删除,有绑定则不允许删除
 | 
						|
                int hasCount = _connection.ChannelList.Where(cl => cl.DrugId == planId.ToString() && cl.MachineId == _setting.machineId).Count();
 | 
						|
                if (hasCount > 0)
 | 
						|
                {
 | 
						|
                    return flag;
 | 
						|
                }
 | 
						|
                else
 | 
						|
                {
 | 
						|
                    logger.Error($"删除套餐{planId}");
 | 
						|
                    //查询该套餐下是否有药品,如果有则一并删除
 | 
						|
                    int iHasPd = _connection.PlanDetails.Where(pd => pd.PlanId == planId).Count();
 | 
						|
                    int pdResult = 1;
 | 
						|
                    if (iHasPd > 0)
 | 
						|
                    {
 | 
						|
                        pdResult = _connection.PlanDetails.Where(pd => pd.PlanId == planId).Set(pd => pd.UseState, 0).Update();
 | 
						|
                    }
 | 
						|
                    int pResult = _connection.Plan.Where(p => p.Id == planId).Set(pd => pd.UseState, 0).Update();
 | 
						|
                    if (pdResult > 0 && pResult > 0)
 | 
						|
                    {
 | 
						|
                        flag = true;
 | 
						|
                    }
 | 
						|
                }
 | 
						|
 | 
						|
                if (flag)
 | 
						|
                {
 | 
						|
                    _connection.CommitTransaction();
 | 
						|
                }
 | 
						|
                else
 | 
						|
                {
 | 
						|
                    _connection.RollbackTransaction();
 | 
						|
                }
 | 
						|
                return flag;
 | 
						|
            }
 | 
						|
            catch (Exception ex)
 | 
						|
            {
 | 
						|
                logger.Error($"修改套餐失败,错误:" + ex.Message);
 | 
						|
                _connection.RollbackTransaction();
 | 
						|
                return false;
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
 | 
						|
        /// <summary>
 | 
						|
        /// 向套餐中添加药品
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="details"></param>
 | 
						|
        /// <returns></returns>
 | 
						|
        public async Task<bool> AddPlanDetail(PlanDetails details)
 | 
						|
        {
 | 
						|
            try
 | 
						|
            {
 | 
						|
                if (!string.IsNullOrEmpty(details.DrugId))
 | 
						|
                {
 | 
						|
                    int id = _connection.InsertWithInt32Identity(details);
 | 
						|
                    details.Id = id;
 | 
						|
                    return id > 0;
 | 
						|
                }
 | 
						|
                else
 | 
						|
                {
 | 
						|
                    return false;
 | 
						|
                }
 | 
						|
            }
 | 
						|
            catch (Exception ex)
 | 
						|
            {
 | 
						|
                logger.Error($"添加药品{details._DrugInfo.DrugName}失败,错误:" + ex.Message);
 | 
						|
                return false;
 | 
						|
            }
 | 
						|
        }
 | 
						|
        /// <summary>
 | 
						|
        /// 修改套餐中的药品
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="details"></param>
 | 
						|
        /// <returns></returns>
 | 
						|
        public async Task<bool> UpdatePlanDetail(PlanDetails details)
 | 
						|
        {
 | 
						|
            try
 | 
						|
            {
 | 
						|
                var iResult = _connection.PlanDetails
 | 
						|
                  .Where(p => p.Id == details.Id)
 | 
						|
                  .Set(p => p.DrugId, details._DrugInfo.DrugId)
 | 
						|
                  .Set(p => p.BaseQuantity, details.BaseQuantity);
 | 
						|
                return iResult.Update() > 0;
 | 
						|
            }
 | 
						|
            catch (Exception ex)
 | 
						|
            {
 | 
						|
                logger.Error($"修改药品{details._DrugInfo.DrugName}失败,错误:" + ex.Message);
 | 
						|
                return false;
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        /// <summary>
 | 
						|
        /// 删除套餐中的药品
 | 
						|
        /// </summary>
 | 
						|
        /// <param name="details"></param>
 | 
						|
        /// <returns></returns>
 | 
						|
        public async Task<bool> DeletePlanDetail(PlanDetails detail)
 | 
						|
        {
 | 
						|
            try
 | 
						|
            {
 | 
						|
                logger.Error($"删除套餐中的药品{detail._DrugInfo.DrugName}");
 | 
						|
                return _connection.PlanDetails.Where(p => p.Id == detail.Id).Set(p=>p.UseState,0).Update() > 0;
 | 
						|
 | 
						|
            }
 | 
						|
            catch (Exception ex)
 | 
						|
            {
 | 
						|
                logger.Error($"添加药品失败,错误:" + ex.Message);
 | 
						|
                return false;
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
        public bool CheckDrugById(PlanDetails details)
 | 
						|
        {
 | 
						|
            if (details._DrugInfo.DrugId != null)
 | 
						|
            {
 | 
						|
                //查询该药品是否已在套餐中存在,存在则不再添加
 | 
						|
                PlanDetails pdDrug = _connection.PlanDetails.Where(p => p.PlanId == details.PlanId && p.DrugId == details._DrugInfo.DrugId&&p.UseState==1).FirstOrDefault();
 | 
						|
 | 
						|
                if (details.Id > 0)
 | 
						|
                {
 | 
						|
                    //修改原数据
 | 
						|
                    if (pdDrug != null && pdDrug.Id == details.Id)
 | 
						|
                    {
 | 
						|
                        return true;
 | 
						|
                    }
 | 
						|
                    else
 | 
						|
                    {
 | 
						|
                        return false;
 | 
						|
                    }
 | 
						|
                }
 | 
						|
                else
 | 
						|
                {
 | 
						|
                    //新增数据
 | 
						|
                    if (pdDrug != null)
 | 
						|
                    {
 | 
						|
                        return !(pdDrug.Id > 0);
 | 
						|
                    }
 | 
						|
                    else
 | 
						|
                    {
 | 
						|
                        return true;
 | 
						|
                    }
 | 
						|
                }
 | 
						|
                //return !(hasCount > 0);
 | 
						|
            }
 | 
						|
            else
 | 
						|
            {
 | 
						|
                return true;
 | 
						|
            }
 | 
						|
        }
 | 
						|
 | 
						|
    }
 | 
						|
}
 |