123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484 |
- using AutoMapper;
- using BizService;
- using Microsoft.Extensions.Logging;
- using MiniExcelLibs;
- using Model.Dto;
- using Model.Entities;
- using PLCTool.Common;
- using PLCTool.Events;
- using PLCTool.Models;
- using Prism.Commands;
- using Prism.Events;
- using Prism.Mvvm;
- using Prism.Services.Dialogs;
- using System;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.Windows;
- namespace PLCTool.ViewModels.BasicConfigViewModel
- {
-
- public class DeviceViewModel : BindableBase
- {
- private readonly IBasicDeviceService _iBasicDeviceService;
- private readonly IBasicDeviceKindService _iBasicDeviceKindService;
- private readonly IBasicProjectService _iBasicProjectService;
- private readonly IMapper _mapper;
- private readonly IDialogService _dialog;
- private readonly Microsoft.Extensions.Logging.ILogger _logger;
- private readonly IEventAggregator _aggregator;
- private List<BasDeviceDto> allDeviceList = new List<BasDeviceDto>();//所有方案
- private List<BasDeviceDto> conditionDevices = new List<BasDeviceDto>();//符合条件的方案
- private List<CrumbViewModel> breadCrumbs = new List<CrumbViewModel>();
- public DeviceViewModel(IBasicDeviceService iBasicDeviceService, IBasicDeviceKindService iBasicDeviceKindService, IBasicProjectService iBasicProjectService, IMapper mapper, IDialogService dialog, Microsoft.Extensions.Logging.ILogger logger ,IEventAggregator aggregator)
- {
- _iBasicDeviceService = iBasicDeviceService;
- _iBasicDeviceKindService = iBasicDeviceKindService;
- _iBasicProjectService = iBasicProjectService;
- _mapper = mapper;
- _dialog = dialog;
- _logger = logger;
- _aggregator=aggregator;
- QueryCommand = new DelegateCommand<object>(Query);
- AddCommand = new DelegateCommand<object>(Add);
- ExportCommand = new DelegateCommand<string>(Export);
- EditCommand = new DelegateCommand<object>(Edit);
- DeleteCommand = new DelegateCommand<object>(Delete);
- ResetCommand = new DelegateCommand<object>(Reset);
- OnLoadCommand = new DelegateCommand(OnLoad);
-
-
- }
- #region 私有方法
- private void OnLoad()
- {
- DeviceKindNameList.Clear();
- DeviceKindNameList.Add("---");
- ProjectNameList.Clear();
- ProjectNameList.Add("---");
- var Kinds = _iBasicDeviceKindService.FindAllDeviceKind();
- foreach(var kind in Kinds)
- {
- DeviceKindNameList.Add(kind);
- }
- var projects= _iBasicProjectService.FindAllProject();
- foreach(var project in projects)
- {
- ProjectNameList.Add(project);
- }
- DeviceKindName = "---";
- ProjectName = "---";
- //发布面包靴
- breadCrumbs.Clear();
- breadCrumbs.Add(new CrumbViewModel { Name = "基础设置" });
- breadCrumbs.Add(new CrumbViewModel { Name = "设备管理" });
- _aggregator.GetEvent<BreadEvent>().Publish(breadCrumbs);
- //获取数据
- GetPprojectConfig();
- }
- private void Reset(object obj)
- {
- DeviceNo = string.Empty;
- DeviceName = string.Empty;
- DeviceKindName = "---";
- ProjectName = "---";
- StartTime = string.Empty;
- EndTime = string.Empty;
- }
- /// <summary>
- /// 修改
- /// </summary>
- /// <param name="obj"></param>
- private void Edit(object obj)
- {
- int id = Convert.ToInt32(obj);
- var findDevice = allDeviceList.FirstOrDefault(x => (x.DeviceId == id));
- DialogParameters parm = new DialogParameters();
- parm.Add("Key", findDevice);
- //蒙层显示
- _aggregator.GetEvent<MaskEvent>().Publish(true);
- //弹出详情对话框
- _dialog.ShowDialog("AddOrEditDeviceView", parm, async callback =>
- {
- if (callback.Result == ButtonResult.OK)
- {
- BasDeviceDto returnValue = callback.Parameters.GetValue<BasDeviceDto>("ReturnValue");
- if (returnValue != null)
- {
- //更新时间
- returnValue.UpdateTime = DateTime.Now;
- returnValue.UpdateBy = Appsession.UserName;
- //创建时间
- returnValue.CreateTime = findDevice.CreateTime;
- returnValue.CreateBy = findDevice?.CreateBy;
- var deviceKindCon = _mapper.Map<BasDeviceDto, bas_device>(returnValue);
- var findPlcs = allDeviceList.FindAll(x => (x.DeviceName == returnValue.DeviceName) || (x.DeviceNo == returnValue.DeviceNo));
- foreach (var item in findPlcs)
- {
- if (item.DeviceId != id)
- {
- MessageBox.Show("已有此设备编号,请更改名称!", "提示", MessageBoxButton.OKCancel, MessageBoxImage.Warning);
- return;
- }
- }
- //修改
- deviceKindCon.device_id = id;
- bool isSucc = _iBasicDeviceService.Edit(deviceKindCon);
- if (isSucc)
- {
- //重新读取PLC
- GetPprojectConfig();
- _logger.LogInformation($"修改设备信息成功,设备名称为{deviceKindCon.device_name}");
- SendLogToDis($"修改设备信息成功,设备名称为{deviceKindCon.device_name}");
-
- MessageBox.Show("修改设备信息成功", "确认", MessageBoxButton.OK, MessageBoxImage.Information);
- }
- }
- }
- });
- //蒙层显示
- _aggregator.GetEvent<MaskEvent>().Publish(false);
- }
- /// <summary>
- /// 删除
- /// </summary>
- /// <param name="obj"></param>
- private void Delete(object obj)
- {
- int id = Convert.ToInt32(obj);
- MessageBoxResult boxResult = MessageBox.Show("确认删除此条数据?", "确认", MessageBoxButton.OKCancel, MessageBoxImage.Question);
- if (boxResult == MessageBoxResult.OK)
- {
- var del = _iBasicDeviceService.Delete(id);
- if (del)
- {
- _logger.LogInformation($"删除设备成功");
- SendLogToDis($"删除设备成功");
-
- MessageBox.Show("删除成功!", "确认", MessageBoxButton.OK, MessageBoxImage.Information);
- GetPprojectConfig();
- }
- }
- }
- /// <summary>
- /// 查询
- /// </summary>
- /// <param name="obj"></param>
- private void Query(object obj)
- {
- if ((!string.IsNullOrEmpty(StartTime)) && (!string.IsNullOrEmpty(EndTime)))
- {
- if (Convert.ToDateTime(StartTime) > Convert.ToDateTime(EndTime))
- {
- MessageBox.Show("起始时间大于结束时间,请重新输入", "确认", MessageBoxButton.OK, MessageBoxImage.Warning);
- return;
- }
- }
- conditionDevices = (from a in allDeviceList
- where (string.IsNullOrEmpty(DeviceName) ? true : (a.DeviceName == DeviceName))
- && (string.IsNullOrEmpty(DeviceNo) ? true : (a.DeviceNo == DeviceNo))
- && ((DeviceKindName == "---") ? true : (a.DeviceKindName == DeviceKindName))
- &&((ProjectName == "---") ? true : (a.ProjectName == ProjectName))
- && (EndTime == string.Empty ? true : (a.CreateTime < Convert.ToDateTime(EndTime)) && (Convert.ToDateTime(StartTime) < a.CreateTime))
- select a).ToList();
- //默认显示的第一页
- conditionDevices = conditionDevices.OrderBy(x => x.DeviceId).ToList();
- Getpage();
- }
- /// <summary>
- /// 发送log之界面显示
- /// </summary>
- /// <param name="msg"></param>
- private void SendLogToDis(string msg)
- {
- _aggregator.GetEvent<LogEvent>().Publish(new LogMessage
- {
- LogTime = DateTime.Now,
- LogMsg = msg
- });
- }
- /// <summary>
- /// 导出
- /// </summary>
- /// <param name="obj"></param>
- private void Export(string obj)
- {
- using (System.Windows.Forms.SaveFileDialog saveFileDialog = new System.Windows.Forms.SaveFileDialog()
- {
- //设置文件类型
- //书写规则例如:txt files(*.txt)|*.txt
- Filter = "Excel files(*.xlsx)|*.xlsx|All files(*.*)|*.*",
- //设置默认文件名(可以不设置)
- FileName = "项目配置表",
- //获取或设置一个值,该值指示如果用户省略扩展名,文件对话框是否自动在文件名中添加扩展名。(可以不设置)
- AddExtension = true,
- //保存对话框是否记忆上次打开的目录
- RestoreDirectory = true
- })
- {
- // Show save file dialog box
- //点了保存按钮进入
- if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- try
- {
- //获得文件路径
- string localFilePath = saveFileDialog.FileName.ToString();
- //获取文件内容
- MiniExcel.SaveAs(localFilePath, DeviceItemList);
- }
- catch (Exception ex)
- {
- _logger.LogError(ex.ToString());
- }
- }
- }
- }
- /// <summary>
- /// 添加PLC变量
- /// </summary>
- /// <param name="obj"></param>
- /// <exception cref="NotImplementedException"></exception>
- private void Add(object obj)
- {
- //蒙层显示
- _aggregator.GetEvent<MaskEvent>().Publish(true);
- //弹出详情对话框
- _dialog.ShowDialog("AddOrEditDeviceView", async callback =>
- {
- if (callback.Result == ButtonResult.OK)
- {
- BasDeviceDto returnValue = callback.Parameters.GetValue<BasDeviceDto>("ReturnValue");
- if (returnValue != null)
- {
- returnValue.CreateTime = DateTime.Now;
- returnValue.CreateBy = Appsession.UserName;
- returnValue.UpdateTime = DateTime.Now;
- returnValue.UpdateBy = Appsession.UserName;
- var deviceCon = _mapper.Map<BasDeviceDto, bas_device>(returnValue);
- var findPlc = allDeviceList.FirstOrDefault(x => (x.DeviceName == returnValue.DeviceName) || (x.DeviceNo == returnValue.DeviceNo));
- if (findPlc != null)
- {
- MessageBox.Show("已有此设备名称或编号,请更改!", "提示", MessageBoxButton.OKCancel, MessageBoxImage.Warning);
- return;
- }
- bool isSucc = _iBasicDeviceService.Add(deviceCon);
- if (isSucc)
- {
- //重新读取PLC
- GetPprojectConfig();
- _logger.LogInformation($"添加设备成功,设备名为{returnValue.DeviceName}");
- SendLogToDis($"添加设备成功,设备名为{returnValue.DeviceName}");
- MessageBox.Show("添加设备成功!", "确认", MessageBoxButton.OK, MessageBoxImage.Information);
- }
- }
- }
- });
- //蒙层显示
- _aggregator.GetEvent<MaskEvent>().Publish(false);
- }
- /// <summary>
- /// 获取所有项目
- /// </summary>
- private void GetPprojectConfig()
- {
- int count = 0;
- allDeviceList.Clear();
- conditionDevices.Clear();
- var projectlist = _iBasicDeviceService.QueryList();
- var allDeviceKinds = _mapper.Map<List<bas_device>, List<BasDeviceDto>>(projectlist);
- foreach (var kind in allDeviceKinds)
- {
- //获取项目名和设备类型
- kind.DeviceKindName = _iBasicDeviceKindService.Find((int)kind.DeviceKindId)?.devicekind_name;
- kind.ProjectName = _iBasicProjectService.Find((int)kind.ProjectId)?.project_name;
- count++;
- kind.DisId = count;
- allDeviceList.Add(kind);
- conditionDevices.Add(kind);
- }
- conditionDevices = conditionDevices.OrderBy(x => x.DeviceId).ToList();
- Getpage();
- }
- /// <summary>
- /// 获取页面
- /// </summary>
- private void Getpage()
- {
- CurrentPage = 1;
- TotalCount = conditionDevices.Count;
- CurrentPageChanged();
- }
- /// <summary>
- /// 页面变化
- /// </summary>
- private void CurrentPageChanged()
- {
- DeviceItemList.Clear();
- foreach (var i in conditionDevices.Skip((CurrentPage - 1) * CountPerPage).Take(CountPerPage))
- {
- DeviceItemList.Add(i);
- }
- }
- #endregion
- #region 命令绑定
- public DelegateCommand<object> QueryCommand { set; get; }
- public DelegateCommand<object> AddCommand { set; get; }
- public DelegateCommand<string> ExportCommand { set; get; }
- /// <summary>
- /// 表格删除
- /// </summary>
- public DelegateCommand<Object> DeleteCommand { set; get; }
- /// <summary>
- /// 表格编辑按钮
- /// </summary>
- public DelegateCommand<Object> EditCommand { set; get; }
- public DelegateCommand<object> ResetCommand { set; get; }
- public DelegateCommand OnLoadCommand { set; get; }
- #endregion
- #region 数据绑定
- private ObservableCollection<BasDeviceDto> deviceItemList = new ObservableCollection<BasDeviceDto>();
- public ObservableCollection<BasDeviceDto> DeviceItemList
- {
- get { return deviceItemList; }
- set { deviceItemList = value; RaisePropertyChanged(); }
- }
- /// <summary>
- /// 设备编号
- /// </summary>
- private string devicedNo;
- public string DeviceNo
- {
- get { return devicedNo; }
- set { devicedNo = value; RaisePropertyChanged(); }
- }
- /// <summary>
- /// 设备类型名称
- /// </summary>
- private string deviceName;
- public string DeviceName
- {
- get { return deviceName; }
- set { deviceName = value; RaisePropertyChanged(); }
- }
- private string projectName;
- public string ProjectName
- {
- get { return projectName; }
- set { projectName = value; RaisePropertyChanged(); }
- }
- private string deviceKindName;
- public string DeviceKindName
- {
- get { return deviceKindName; }
- set { deviceKindName = value; RaisePropertyChanged(); }
- }
- /// <summary>
- /// 项目名称
- /// </summary>
- private ObservableCollection<string> projectNameList=new ObservableCollection<string>();
- public ObservableCollection<string> ProjectNameList
- {
- get { return projectNameList; }
- set { projectNameList = value; RaisePropertyChanged(); }
- }
- /// <summary>
- /// 设备类型
- /// </summary>
- private ObservableCollection<string> deviceKindNameList=new ObservableCollection<string>();
- public ObservableCollection<string> DeviceKindNameList
- {
- get { return deviceKindNameList; }
- set { deviceKindNameList = value; RaisePropertyChanged(); }
- }
- /// <summary>
- /// 开始时间
- /// </summary>
- private string startTime = DateTime.Now.AddDays(-1).ToString();
- public string StartTime
- {
- get { return startTime; }
- set { startTime = value; RaisePropertyChanged(); }
- }
- private string endTime = DateTime.Now.ToString();
- public string EndTime
- {
- get { return endTime; }
- set { endTime = value; RaisePropertyChanged(); }
- }
- /// <summary>
- /// 总条数
- /// </summary>
- private int totalCount;
- public int TotalCount
- {
- get { return totalCount; }
- set { totalCount = value; RaisePropertyChanged(); CurrentPageChanged(); }
- }
- /// <summary>
- /// 每页数量
- /// </summary>
- private int countPerPage = 15;
- public int CountPerPage
- {
- get { return countPerPage; }
- set { countPerPage = value; RaisePropertyChanged(); CurrentPageChanged(); }
- }
- /// <summary>
- /// 单前页
- /// </summary>
- private int currentPage = 1;
- public int CurrentPage
- {
- get { return currentPage; }
- set { currentPage = value; RaisePropertyChanged(); CurrentPageChanged(); }
- }
- #endregion
- }
- }
|