348 lines
13 KiB
C#
348 lines
13 KiB
C#
using DM_Weight.Models;
|
|
using DM_Weight.msg;
|
|
using DM_Weight.Port;
|
|
using DM_Weight.util;
|
|
using MaterialDesignThemes.Wpf;
|
|
using Prism.Commands;
|
|
using Prism.Events;
|
|
using Prism.Mvvm;
|
|
using Prism.Regions;
|
|
using System;
|
|
using System.Collections;
|
|
using System.Collections.Generic;
|
|
using System.Configuration;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows;
|
|
using System.Windows.Media;
|
|
|
|
namespace DM_Weight.ViewModels
|
|
{
|
|
public class SettingBoxWindowViewModel : BindableBase, INavigationAware, IRegionMemberLifetime
|
|
{
|
|
private SolidColorBrush _colorBrush;
|
|
|
|
public SolidColorBrush SnackbarBackground
|
|
{
|
|
get => _colorBrush;
|
|
set => SetProperty(ref _colorBrush, value);
|
|
}
|
|
private ISnackbarMessageQueue _snackbarMessageQueue = new SnackbarMessageQueue(TimeSpan.FromSeconds(3));
|
|
public ISnackbarMessageQueue SnackbarMessageQueue
|
|
{
|
|
get => _snackbarMessageQueue;
|
|
set => SetProperty(ref _snackbarMessageQueue, value);
|
|
}
|
|
private int _drawerNo = 0;
|
|
|
|
public int DrawerNo
|
|
{
|
|
get => _drawerNo;
|
|
set => SetProperty(ref _drawerNo, value);
|
|
}
|
|
|
|
private bool _isEnable = true;
|
|
public bool IsEnable { get => _isEnable; set => SetProperty(ref _isEnable, value); }
|
|
private int _status = 0;
|
|
public int Status { get => _status; set { SetProperty(ref _status, value); } }
|
|
|
|
//选择用户 控件的Visibility值
|
|
private object _userStatus = Visibility.Collapsed;
|
|
public object UserStatus
|
|
{
|
|
get => _userStatus;
|
|
set => SetProperty(ref _userStatus, value);
|
|
}
|
|
public static List<BoxType> StaticBoxType = new()
|
|
{
|
|
new BoxType() { TypeValue = 1, TypeName = "公共药箱" },
|
|
new BoxType() { TypeValue = 0, TypeName = "归属药师" }
|
|
};
|
|
|
|
//药箱类型
|
|
private List<BoxType> _boxTypes = StaticBoxType;
|
|
public List<BoxType> BoxTypeList
|
|
{
|
|
get => _boxTypes;
|
|
set => SetProperty(ref _boxTypes, value);
|
|
}
|
|
|
|
|
|
private BoxType _boxTypeSelectedItem = StaticBoxType[0];
|
|
public BoxType BoxTypeSelectedItem
|
|
{
|
|
get => _boxTypeSelectedItem;
|
|
set
|
|
{
|
|
if (value != null && value.TypeValue == 1)
|
|
{
|
|
//药箱为公共药箱,把药师信息隐藏
|
|
UserStatus = Visibility.Collapsed;
|
|
}
|
|
else
|
|
{
|
|
|
|
//药箱为药师药箱
|
|
UserStatus = Visibility.Visible;
|
|
}
|
|
SetProperty(ref _boxTypeSelectedItem, value);
|
|
}
|
|
}
|
|
|
|
//static List<BindUserList> StaticUserList = SqlSugarHelper.Db.Queryable<UserList>().Where(us => us.MachineId == "DM3").Select(us => new BindUserList() { UserId =Convert.ToInt32(us.UserBarcode), UserName = us.Nickname }).ToList();
|
|
//药师
|
|
private List<BindUserList> _userLists;// = StaticUserList;
|
|
public List<BindUserList> UserLists
|
|
{
|
|
get => _userLists;
|
|
set => SetProperty(ref _userLists, value);
|
|
}
|
|
|
|
private BindUserList _userListSelectedItem;// = _userLists[0];
|
|
public BindUserList UserListSelectedItem { get => _userListSelectedItem; set => SetProperty(ref _userListSelectedItem, value); }
|
|
|
|
private PortUtil _portUtil;
|
|
IEventAggregator _eventAggregator;
|
|
public SettingBoxWindowViewModel(PortUtil portUtil, IEventAggregator eventAggregator)
|
|
{
|
|
|
|
_portUtil = portUtil;
|
|
_eventAggregator = eventAggregator;
|
|
BindUserList();
|
|
}
|
|
private void BindUserList()
|
|
{
|
|
if (_userLists == null)
|
|
{
|
|
_userLists = SqlSugarHelper.Db.Queryable<UserList>().Where(us => us.MachineId == (ConfigurationManager.AppSettings["machineId"] ?? "DM5")).Select(us => new BindUserList() { UserId = Convert.ToInt32(us.UserBarcode), UserName = us.Nickname }).ToList();
|
|
_userListSelectedItem = _userLists[0];
|
|
}
|
|
}
|
|
public bool KeepAlive => false;
|
|
|
|
public bool IsNavigationTarget(NavigationContext navigationContext)
|
|
{
|
|
return true;
|
|
}
|
|
|
|
public void OnNavigatedFrom(NavigationContext navigationContext)
|
|
{
|
|
|
|
// 取消消息订阅
|
|
//_eventAggregator.GetEvent<PortUtilEvent>().Unsubscribe(DoMyPrismEvent);
|
|
}
|
|
private void BindBoxTypeList()
|
|
{
|
|
BoxTypeList.Clear();
|
|
BoxTypeList = new List<BoxType>() { new BoxType() { TypeValue = 0, TypeName = "归属药师" }, new BoxType() { TypeValue = 1, TypeName = "公共药箱" } };
|
|
}
|
|
public void OnNavigatedTo(NavigationContext navigationContext)
|
|
{
|
|
//_eventAggregator.GetEvent<PortUtilEvent>().Subscribe(DoMyPrismEvent);
|
|
}
|
|
//点击药箱
|
|
public DelegateCommand<string> UpdateDrawerNo
|
|
{
|
|
get => new DelegateCommand<string>((DrawerNo) =>
|
|
{
|
|
this.DrawerNo = Convert.ToInt32(DrawerNo);
|
|
ChannelList cnl = SqlSugarHelper.Db.Queryable<ChannelList>().Where(cl => cl.MachineId == "DM5" && cl.DrawerNo.ToString() == DrawerNo).First();
|
|
if (cnl != null && cnl.DrawerType != null)
|
|
{
|
|
BoxType currentBoxType = StaticBoxType.Where(sbt => sbt.TypeValue == cnl.DrawerType).FirstOrDefault();
|
|
if (currentBoxType != null)
|
|
{
|
|
BoxTypeSelectedItem = currentBoxType;
|
|
if (BoxTypeSelectedItem.TypeValue == 0)
|
|
{
|
|
//药箱归属药师,把药师信息显示出来
|
|
UserStatus = Visibility.Visible;
|
|
UserListSelectedItem = _userLists.Where(sul => sul.UserId.ToString() == cnl.BelongUser).FirstOrDefault();
|
|
if (UserListSelectedItem == null)
|
|
{
|
|
if (UserLists.Where(us => us.UserName == "请选择药师").Count() <= 0)
|
|
{
|
|
BindUserList nullBind = new BindUserList();
|
|
nullBind.UserId = 0;
|
|
nullBind.UserName = "请选择药师";
|
|
_userLists.Add(nullBind);
|
|
|
|
}
|
|
UserListSelectedItem = UserLists.Where(us => us.UserName == "请选择药师").FirstOrDefault();
|
|
}
|
|
else
|
|
{
|
|
UserListSelectedItem = UserLists[0];
|
|
}
|
|
}
|
|
else
|
|
{
|
|
//药箱为公共药箱,把药师信息隐藏
|
|
UserStatus = Visibility.Collapsed;
|
|
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
);
|
|
}
|
|
//打开药箱
|
|
public DelegateCommand OpenBox
|
|
{
|
|
get => new DelegateCommand(() =>
|
|
{
|
|
if (DrawerNo > 0)
|
|
{
|
|
IsEnable = false;
|
|
Status = 1;
|
|
//_portUtil.SpeakAsync("正在打开药箱");
|
|
//_portUtil.DrawerNo = DrawerNo;
|
|
//_portUtil.OpenBox();
|
|
_portUtil.SpeakAsync($"正在打开{DrawerNo}号药箱");//记录开药箱日志
|
|
SqlSugarHelper.Db.Insertable(new MachineRecord()
|
|
{
|
|
MachineId = "DM5",
|
|
DrawerNo = DrawerNo,
|
|
Operator = HomeWindowViewModel.Operator?.Id,
|
|
OperationTime = DateTime.Now,
|
|
Type = 55,
|
|
InvoiceId = $"打开{DrawerNo}号药箱",
|
|
}).ExecuteCommand();
|
|
ModbusHelper.GetInstance().OpenBoxDoor(DrawerNo - 1);
|
|
}
|
|
});
|
|
}
|
|
|
|
public DelegateCommand SaveSetting
|
|
{
|
|
get => new DelegateCommand(() =>
|
|
{
|
|
if (DrawerNo > 0)
|
|
{
|
|
ChannelList channelList = SqlSugarHelper.Db.Queryable<ChannelList>().Where(cs => cs.DrawerNo == DrawerNo && cs.MachineId == "DM5").First();
|
|
if (BoxTypeSelectedItem != null)
|
|
{
|
|
if (BoxTypeSelectedItem.TypeValue == 0 && UserListSelectedItem != null)
|
|
{
|
|
//药箱 归属药师
|
|
|
|
SqlSugarHelper.Db.Updateable(new ChannelList() { BelongUser = UserListSelectedItem.UserId.ToString(), DrawerType = BoxTypeSelectedItem.TypeValue, Id = channelList.Id }).UpdateColumns(cl => new { cl.BelongUser, cl.DrawerType }).ExecuteCommand();
|
|
}
|
|
else
|
|
{
|
|
//药箱属于公共药箱
|
|
SqlSugarHelper.Db.Updateable(new ChannelList() { DrawerType = BoxTypeSelectedItem.TypeValue, Id = channelList.Id }).UpdateColumns(cl => new { cl.DrawerType }).ExecuteCommand();
|
|
|
|
}
|
|
}
|
|
}
|
|
else
|
|
{
|
|
//提示请选择药箱
|
|
AlertMsg alertMsg = new AlertMsg
|
|
{
|
|
Message = $"未选择药箱,请先选择药箱",
|
|
Type = MsgType.ERROR
|
|
};
|
|
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
|
|
}
|
|
});
|
|
}
|
|
|
|
void DoMyPrismEvent(DeviceMsg msg)
|
|
{
|
|
switch (msg.EventType)
|
|
{
|
|
// 药箱打开
|
|
case EventType.DRAWEROPEN:
|
|
if (Status == 1)
|
|
{
|
|
Status = 2;
|
|
}
|
|
//记录开药箱日志
|
|
SqlSugarHelper.Db.Insertable(new MachineRecord()
|
|
{
|
|
MachineId = "DM5",
|
|
DrawerNo = _portUtil.DrawerNo,
|
|
Operator = HomeWindowViewModel.Operator?.Id,
|
|
OperationTime = DateTime.Now,
|
|
Type = 55,
|
|
InvoiceId = "药箱打开",
|
|
}).ExecuteCommand();
|
|
_portUtil.GetBoxStatus();
|
|
break;
|
|
// 药箱关闭
|
|
case EventType.DRAWERCLOSE:
|
|
//记录药箱操作日志
|
|
SqlSugarHelper.Db.Insertable(new MachineRecord()
|
|
{
|
|
MachineId = "DM5",
|
|
DrawerNo = _portUtil.DrawerNo,
|
|
Operator = HomeWindowViewModel.Operator?.Id,
|
|
OperationTime = DateTime.Now,
|
|
Type = 55,
|
|
InvoiceId = "药箱关闭",
|
|
}).ExecuteCommand();
|
|
|
|
if (Status == 2)
|
|
{
|
|
Status = 3;
|
|
}
|
|
IsEnable = true;
|
|
DrawerNo = -1;
|
|
_portUtil.Operate = false;
|
|
break;
|
|
// 打开失败
|
|
case EventType.OPENERROR:
|
|
AlertMsg alertMsg = new AlertMsg
|
|
{
|
|
Message = msg.Message,
|
|
Type = MsgType.ERROR
|
|
};
|
|
_eventAggregator.GetEvent<SnackbarEvent>().Publish(alertMsg);
|
|
|
|
IsEnable = true;
|
|
DrawerNo = -1;
|
|
Status = 0;
|
|
|
|
//记录药箱操作日志
|
|
SqlSugarHelper.Db.Insertable(new MachineRecord()
|
|
{
|
|
MachineId = "DM5",
|
|
DrawerNo = _portUtil.DrawerNo,
|
|
Operator = HomeWindowViewModel.Operator?.Id,
|
|
OperationTime = DateTime.Now,
|
|
Type = 55,
|
|
InvoiceId = "药箱打开失败",
|
|
}).ExecuteCommand();
|
|
_portUtil.Operate = false;
|
|
break;
|
|
}
|
|
}
|
|
|
|
public DelegateCommand Query
|
|
{
|
|
get => new DelegateCommand(() =>
|
|
{
|
|
BindUserList();
|
|
});
|
|
}
|
|
}
|
|
/// <summary>
|
|
/// 药箱类型
|
|
/// </summary>
|
|
public class BoxType
|
|
{
|
|
public int TypeValue { get; set; }
|
|
public string TypeName { get; set; }
|
|
}
|
|
|
|
public class BindUserList
|
|
{
|
|
public int UserId { get; set; }
|
|
public string UserName { get; set; }
|
|
}
|
|
}
|