369 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			C#
		
	
	
	
			
		
		
	
	
			369 lines
		
	
	
		
			12 KiB
		
	
	
	
		
			C#
		
	
	
	
using Prism.Commands;
 | 
						||
using Prism.Mvvm;
 | 
						||
using Prism.Regions;
 | 
						||
using System;
 | 
						||
using System.Collections.Generic;
 | 
						||
using System.Linq;
 | 
						||
using System.Text;
 | 
						||
using System.Threading.Tasks;
 | 
						||
using System.Windows;
 | 
						||
using DM_Weight.Models;
 | 
						||
using DM_Weight.select;
 | 
						||
using DM_Weight.util;
 | 
						||
using log4net.Repository.Hierarchy;
 | 
						||
using log4net;
 | 
						||
using SqlSugar;
 | 
						||
using DM_Weight.msg;
 | 
						||
using Prism.Events;
 | 
						||
 | 
						||
namespace DM_Weight.ViewModels
 | 
						||
{
 | 
						||
    public class DrugListWindowViewModel : BindableBase, IConfirmNavigationRequest, IRegionMemberLifetime
 | 
						||
    {
 | 
						||
        private int _pageNum = 1;
 | 
						||
        public int PageNum
 | 
						||
        {
 | 
						||
            get => _pageNum;
 | 
						||
            set
 | 
						||
            {
 | 
						||
                SetProperty(ref _pageNum, value);
 | 
						||
                RequestData();
 | 
						||
            }
 | 
						||
        }
 | 
						||
 | 
						||
        private int _pageCount = 1;
 | 
						||
        public int PageCount
 | 
						||
        {
 | 
						||
            get => _pageCount;
 | 
						||
            set
 | 
						||
            {
 | 
						||
                SetProperty(ref _pageCount, value);
 | 
						||
            }
 | 
						||
        }
 | 
						||
 | 
						||
        private int _pageSize = 8;
 | 
						||
        public int PageSize
 | 
						||
        {
 | 
						||
            get => _pageSize;
 | 
						||
            set
 | 
						||
            {
 | 
						||
                SetProperty(ref _pageSize, value);
 | 
						||
            }
 | 
						||
        }
 | 
						||
 | 
						||
        private int _totalCount = 0;
 | 
						||
        public int TotalCount
 | 
						||
        {
 | 
						||
            get => _totalCount;
 | 
						||
            set
 | 
						||
            {
 | 
						||
                SetProperty(ref _totalCount, value);
 | 
						||
            }
 | 
						||
        }
 | 
						||
 | 
						||
        private List<DrugInfo> _drugInfos;
 | 
						||
        public List<DrugInfo> DrugInfos
 | 
						||
        {
 | 
						||
            get { return _drugInfos; }
 | 
						||
            set { SetProperty(ref _drugInfos, value); }
 | 
						||
        }
 | 
						||
 | 
						||
        private DrugInfo _selectedDrug = new();
 | 
						||
        public DrugInfo SelectedDrug
 | 
						||
        {
 | 
						||
            get { return _selectedDrug; }
 | 
						||
            set
 | 
						||
            {
 | 
						||
 | 
						||
                if (value != null)
 | 
						||
                {
 | 
						||
                    SetProperty(ref _selectedDrug, value);
 | 
						||
                    GetManuNos();
 | 
						||
                }
 | 
						||
                else
 | 
						||
                {
 | 
						||
                    SetProperty(ref _selectedDrug, new());
 | 
						||
                    Manunos = null;
 | 
						||
                }
 | 
						||
 | 
						||
            }
 | 
						||
        }
 | 
						||
        public static List<OrderTakeSelect> StaticSelects = new()
 | 
						||
        {
 | 
						||
            new OrderTakeSelect
 | 
						||
            {
 | 
						||
                Code = "DrugName",
 | 
						||
                Name = "药品名称"
 | 
						||
            },
 | 
						||
            new OrderTakeSelect
 | 
						||
            {
 | 
						||
                Code = "PyCode",
 | 
						||
                Name = "拼音码"
 | 
						||
            },
 | 
						||
            new OrderTakeSelect
 | 
						||
            {
 | 
						||
                Code = "DrugBarcode",
 | 
						||
                Name = "药品条码"
 | 
						||
            },
 | 
						||
            new OrderTakeSelect
 | 
						||
            {
 | 
						||
                Code = "DrugId",
 | 
						||
                Name = "药品编码"
 | 
						||
            }
 | 
						||
        };
 | 
						||
 | 
						||
        private List<OrderTakeSelect> _selects = StaticSelects;
 | 
						||
 | 
						||
        public List<OrderTakeSelect> Selects
 | 
						||
        {
 | 
						||
            get { return _selects; }
 | 
						||
            set
 | 
						||
            {
 | 
						||
                SetProperty(ref _selects, value);
 | 
						||
            }
 | 
						||
        }
 | 
						||
 | 
						||
        private OrderTakeSelect _selectedItem = StaticSelects[0];
 | 
						||
        /// <summary>
 | 
						||
        /// 查询条件 查询字段
 | 
						||
        /// </summary>
 | 
						||
        public OrderTakeSelect SelectedItem
 | 
						||
        {
 | 
						||
            get { return _selectedItem; }
 | 
						||
            set
 | 
						||
            {
 | 
						||
                SetProperty(ref _selectedItem, value);
 | 
						||
                RequestData();
 | 
						||
            }
 | 
						||
        }
 | 
						||
 | 
						||
        private string? _searchValue;
 | 
						||
 | 
						||
        /// <summary>
 | 
						||
        /// 查询条件 查询字段值
 | 
						||
        /// </summary>
 | 
						||
        public string? SearchValue
 | 
						||
        {
 | 
						||
            get { return _searchValue; }
 | 
						||
            set
 | 
						||
            {
 | 
						||
                SetProperty(ref _searchValue, value);
 | 
						||
                RequestData();
 | 
						||
            }
 | 
						||
        }
 | 
						||
 | 
						||
 | 
						||
        private List<DrugManuNo> _manunos;
 | 
						||
        public List<DrugManuNo> Manunos
 | 
						||
        {
 | 
						||
            get { return _manunos; }
 | 
						||
            set { SetProperty(ref _manunos, value); }
 | 
						||
        }
 | 
						||
 | 
						||
        private DrugManuNo _selectedManuno = new DrugManuNo();
 | 
						||
        public DrugManuNo SelectedManuno
 | 
						||
        {
 | 
						||
            get { return _selectedManuno; }
 | 
						||
            set { SetProperty(ref _selectedManuno, value ?? new()); }
 | 
						||
        }
 | 
						||
 | 
						||
        //private SqlSugarScope SqlSugarHelper.Db;
 | 
						||
 | 
						||
        IEventAggregator _eventAggregator;
 | 
						||
        public DrugListWindowViewModel(IEventAggregator eventAggregator)
 | 
						||
        {
 | 
						||
            _eventAggregator = eventAggregator;
 | 
						||
        }
 | 
						||
 | 
						||
        public DelegateCommand Query
 | 
						||
        {
 | 
						||
            get => new DelegateCommand(() =>
 | 
						||
            {
 | 
						||
                RequestData();
 | 
						||
            });
 | 
						||
        }
 | 
						||
 | 
						||
        public DelegateCommand AddDrugCommand
 | 
						||
        {
 | 
						||
            get => new DelegateCommand(() =>
 | 
						||
            {
 | 
						||
                try
 | 
						||
                {
 | 
						||
 | 
						||
                    //SelectedDrug.DrugId = 0;
 | 
						||
                    SqlSugarHelper.Db.Insertable(SelectedDrug).ExecuteCommand();
 | 
						||
                    RequestData();
 | 
						||
                }
 | 
						||
                catch (Exception ex)
 | 
						||
                {
 | 
						||
                    ILog logger = LogManager.GetLogger(typeof(DrugListWindowViewModel));
 | 
						||
                    logger.Info($"添加数据异常:{ex.ToString()}");
 | 
						||
                    return;
 | 
						||
                }
 | 
						||
            });
 | 
						||
        }
 | 
						||
 | 
						||
        public DelegateCommand EditDrugCommand
 | 
						||
        {
 | 
						||
            get => new DelegateCommand(() =>
 | 
						||
            {
 | 
						||
                SqlSugarHelper.Db.Updateable(SelectedDrug).UpdateColumns(d => new { d.DrugName, d.Manufactory, d.PyCode, d.PackUnit, d.MaxStock, d.DrugSpec }).ExecuteCommand();
 | 
						||
                RequestData();
 | 
						||
            }, () => SelectedDrug.DrugId !=null).ObservesProperty(() => SelectedDrug);
 | 
						||
        }
 | 
						||
 | 
						||
        public DelegateCommand AddManuCommand
 | 
						||
        {
 | 
						||
            get => new DelegateCommand(() =>
 | 
						||
            {
 | 
						||
                try
 | 
						||
                {
 | 
						||
 | 
						||
                    if (!string.IsNullOrEmpty(SelectedManuno.EffDate) && !string.IsNullOrEmpty(SelectedManuno.ManuNo))
 | 
						||
                    {
 | 
						||
                        string dt = DateTime.Parse(SelectedManuno.EffDate).ToString("yyyy-MM-dd");
 | 
						||
                        int repeatCount = SqlSugarHelper.Db.Queryable<DrugManuNo>().Where(dm => dm.DrugId == SelectedDrug.DrugId && dm.EffDate == dt && dm.ManuNo == SelectedManuno.ManuNo).Count();
 | 
						||
                        if (repeatCount > 0)
 | 
						||
                        {
 | 
						||
                            AlertMsg alertMsg = new AlertMsg
 | 
						||
                            {
 | 
						||
                                Message = "批次重复,请勿重复添加!",
 | 
						||
                                Type = MsgType.ERROR,
 | 
						||
                            };
 | 
						||
                            _eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
 | 
						||
                        }
 | 
						||
                        else
 | 
						||
                        {
 | 
						||
                            SelectedManuno.Id = Guid.NewGuid().ToString();
 | 
						||
                            SelectedManuno.DrugId = SelectedDrug.DrugId.ToString();
 | 
						||
                            SelectedManuno.EffDate = DateTime.Parse(SelectedManuno.EffDate).ToString("yyyy-MM-dd");
 | 
						||
                            SqlSugarHelper.Db.Insertable(SelectedManuno).ExecuteCommand(); GetManuNos();
 | 
						||
                            AlertMsg alertMsg = new AlertMsg
 | 
						||
                            {
 | 
						||
                                Message = "保存成功!",
 | 
						||
                                Type = MsgType.SUCCESS,
 | 
						||
                            };
 | 
						||
                            _eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
 | 
						||
                        }
 | 
						||
 | 
						||
                    }
 | 
						||
                    else
 | 
						||
                    {
 | 
						||
                        AlertMsg alertMsg = new AlertMsg
 | 
						||
                        {
 | 
						||
                            Message = "请输入批次及效期!",
 | 
						||
                            Type = MsgType.ERROR,
 | 
						||
                        };
 | 
						||
                        _eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
 | 
						||
                    }
 | 
						||
                }
 | 
						||
                catch (Exception ex)
 | 
						||
                {
 | 
						||
                    AlertMsg alertMsg = new AlertMsg
 | 
						||
                    {
 | 
						||
                        Message = $"添加失败{ex.Message}",
 | 
						||
                        Type = MsgType.ERROR,
 | 
						||
                    };
 | 
						||
                    _eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
 | 
						||
                }
 | 
						||
 | 
						||
            }, () => SelectedDrug.DrugId !=null).ObservesProperty(() => SelectedDrug);
 | 
						||
        }
 | 
						||
 | 
						||
        public DelegateCommand EditManuCommand
 | 
						||
        {
 | 
						||
            get => new DelegateCommand(() =>
 | 
						||
            {
 | 
						||
                try
 | 
						||
                {
 | 
						||
                    string dt = DateTime.Parse(SelectedManuno.EffDate).ToString("yyyy-MM-dd");
 | 
						||
                    int repeatCount = SqlSugarHelper.Db.Queryable<DrugManuNo>().Where(dm => dm.DrugId == SelectedDrug.DrugId && dm.EffDate == dt && dm.ManuNo == SelectedManuno.ManuNo).Count();
 | 
						||
                    if (repeatCount > 0)
 | 
						||
                    {
 | 
						||
                        AlertMsg alertMsg = new AlertMsg
 | 
						||
                        {
 | 
						||
                            Message = "批次重复,请勿重复添加!",
 | 
						||
                            Type = MsgType.ERROR,
 | 
						||
                        };
 | 
						||
                        _eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
 | 
						||
                    }
 | 
						||
                    else
 | 
						||
                    {
 | 
						||
                        SelectedManuno.EffDate = DateTime.Parse(SelectedManuno.EffDate).ToString("yyyy-MM-dd");
 | 
						||
                        SqlSugarHelper.Db.Updateable(SelectedManuno).UpdateColumns(m => new { m.ManuNo, m.EffDate }).ExecuteCommand();
 | 
						||
                        GetManuNos();
 | 
						||
                        AlertMsg alertMsg = new AlertMsg
 | 
						||
                        {
 | 
						||
                            Message = "修改成功!",
 | 
						||
                            Type = MsgType.SUCCESS,
 | 
						||
                        };
 | 
						||
                        _eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
 | 
						||
                    }
 | 
						||
                }
 | 
						||
                catch (Exception ex)
 | 
						||
                {
 | 
						||
 | 
						||
                    AlertMsg alertMsg = new AlertMsg
 | 
						||
                    {
 | 
						||
                        Message = "修改失败!",
 | 
						||
                        Type = MsgType.ERROR,
 | 
						||
                    };
 | 
						||
                    _eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
 | 
						||
                }
 | 
						||
            }, () => !string.IsNullOrEmpty(SelectedManuno.DrugId)).ObservesProperty(() => SelectedManuno);
 | 
						||
        }
 | 
						||
 | 
						||
        public bool KeepAlive => true;
 | 
						||
 | 
						||
 | 
						||
        //这个方法用于拦截请求,continuationCallback(true)就是不拦截,continuationCallback(false)拦截本次操作
 | 
						||
        public void ConfirmNavigationRequest(NavigationContext navigationContext, Action<bool> continuationCallback)
 | 
						||
        {
 | 
						||
            continuationCallback(true);
 | 
						||
        }
 | 
						||
 | 
						||
 | 
						||
        //接收导航传过来的参数
 | 
						||
        public void OnNavigatedTo(NavigationContext navigationContext)
 | 
						||
        {
 | 
						||
            //查询表格数据
 | 
						||
            RequestData();
 | 
						||
        }
 | 
						||
 | 
						||
        void GetManuNos()
 | 
						||
        {
 | 
						||
            Manunos = SqlSugarHelper.Db.Queryable<DrugManuNo>()
 | 
						||
                .WhereIF(SelectedDrug != null, (di) => di.DrugId.Equals(SelectedDrug.DrugId))
 | 
						||
                .Select(di => di)
 | 
						||
                .ToList();
 | 
						||
        }
 | 
						||
 | 
						||
        void RequestData()
 | 
						||
        {
 | 
						||
            int totalCount = 0;
 | 
						||
            DrugInfos = SqlSugarHelper.Db.Queryable<DrugInfo>()
 | 
						||
                .WhereIF(!String.IsNullOrEmpty(SearchValue) && SelectedItem.Code.Equals("DrugId"), (di) => di.DrugId.ToString().Contains(SearchValue))
 | 
						||
                .WhereIF(!String.IsNullOrEmpty(SearchValue) && SelectedItem.Code.Equals("DrugName"), (di) => di.DrugName.Contains(SearchValue))
 | 
						||
                .WhereIF(!String.IsNullOrEmpty(SearchValue) && SelectedItem.Code.Equals("PyCode"), (di) => di.PyCode.Contains(SearchValue))
 | 
						||
                .WhereIF(!String.IsNullOrEmpty(SearchValue) && SelectedItem.Code.Equals("DrugBarcode"), (di) => di.DrugBarcode.Contains(SearchValue))
 | 
						||
                .Select(di => di)
 | 
						||
                .ToPageList(PageNum, PageSize, ref totalCount)
 | 
						||
            ;
 | 
						||
            TotalCount = totalCount;
 | 
						||
            PageCount = (int)Math.Ceiling((double)TotalCount / PageSize);
 | 
						||
        }
 | 
						||
 | 
						||
        //每次导航的时候,该实列用不用重新创建,true是不重新创建,false是重新创建
 | 
						||
        public bool IsNavigationTarget(NavigationContext navigationContext)
 | 
						||
        {
 | 
						||
            return true;
 | 
						||
        }
 | 
						||
 | 
						||
        //这个方法用于拦截请求
 | 
						||
        public void OnNavigatedFrom(NavigationContext navigationContext)
 | 
						||
        {
 | 
						||
 | 
						||
        }
 | 
						||
    }
 | 
						||
}
 |