|
@@ -1,12 +1,382 @@
|
|
-using System;
|
|
|
|
|
|
+using AutoMapper;
|
|
|
|
+using BizService;
|
|
|
|
+using Microsoft.Extensions.Logging;
|
|
|
|
+using MiniExcelLibs;
|
|
|
|
+using Model.Dto;
|
|
|
|
+using Model.Entities;
|
|
|
|
+using PLCTool.Common;
|
|
|
|
+using Prism.Commands;
|
|
|
|
+using Prism.Mvvm;
|
|
|
|
+using Prism.Services.Dialogs;
|
|
|
|
+using System;
|
|
using System.Collections.Generic;
|
|
using System.Collections.Generic;
|
|
|
|
+using System.Collections.ObjectModel;
|
|
using System.Linq;
|
|
using System.Linq;
|
|
using System.Text;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Threading.Tasks;
|
|
|
|
+using System.Windows;
|
|
|
|
|
|
namespace PLCTool.ViewModels.BasicConfigViewModel
|
|
namespace PLCTool.ViewModels.BasicConfigViewModel
|
|
{
|
|
{
|
|
- class DeviceViewModel
|
|
|
|
|
|
+
|
|
|
|
+ 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 ILogger _logger;
|
|
|
|
+ private List<BasDeviceDto> allDeviceList = new List<BasDeviceDto>();//所有方案
|
|
|
|
+ private List<BasDeviceDto> conditionDevices = new List<BasDeviceDto>();//符合条件的方案
|
|
|
|
+ public DeviceViewModel(IBasicDeviceService iBasicDeviceService, IBasicDeviceKindService iBasicDeviceKindService, IBasicProjectService iBasicProjectService, IMapper mapper, IDialogService dialog, ILogger logger)
|
|
|
|
+ {
|
|
|
|
+ _iBasicDeviceService = iBasicDeviceService;
|
|
|
|
+ _iBasicDeviceKindService = iBasicDeviceKindService;
|
|
|
|
+ _iBasicProjectService = iBasicProjectService;
|
|
|
|
+ _mapper = mapper;
|
|
|
|
+ _dialog = dialog;
|
|
|
|
+ _logger = logger;
|
|
|
|
+ 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);
|
|
|
|
+
|
|
|
|
+ GetPprojectConfig();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ #region 私有方法
|
|
|
|
+ private void OnLoad()
|
|
|
|
+ {
|
|
|
|
+ DeviceKindNameList = _iBasicDeviceKindService.FindAllDeviceKind();
|
|
|
|
+ ProjectNameList = _iBasicProjectService.FindAllProject();
|
|
|
|
+ }
|
|
|
|
+ private void Reset(object obj)
|
|
|
|
+ {
|
|
|
|
+ DeviceNo = string.Empty;
|
|
|
|
+ DeviceName = 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);
|
|
|
|
+ //弹出详情对话框
|
|
|
|
+ _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($"修改项目成功");
|
|
|
|
+ MessageBox.Show("修改项目成功", "确认", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ 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)
|
|
|
|
+ {
|
|
|
|
+ MessageBox.Show("删除成功!", "确认", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
|
|
+ GetPprojectConfig();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 查询
|
|
|
|
+ /// </summary>
|
|
|
|
+ /// <param name="obj"></param>
|
|
|
|
+ private void Query(object obj)
|
|
|
|
+ {
|
|
|
|
+ conditionDevices = (from a in allDeviceList
|
|
|
|
+ where (string.IsNullOrEmpty(DeviceName) ? true : (a.DeviceName == DeviceName))
|
|
|
|
+ && (string.IsNullOrEmpty(DeviceNo) ? true : (a.DeviceNo == DeviceNo))
|
|
|
|
+
|
|
|
|
+ select a).ToList();
|
|
|
|
+ //默认显示的第一页
|
|
|
|
+ conditionDevices = conditionDevices.OrderBy(x => x.DeviceId).ToList();
|
|
|
|
+ Getpage();
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ 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)
|
|
|
|
+ {
|
|
|
|
+
|
|
|
|
+ //弹出详情对话框
|
|
|
|
+ _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($"添加项目成功");
|
|
|
|
+ MessageBox.Show("添加项目成功!", "确认", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ });
|
|
|
|
+ }
|
|
|
|
+ /// <summary>
|
|
|
|
+ /// 获取所有项目
|
|
|
|
+ /// </summary>
|
|
|
|
+ private void GetPprojectConfig()
|
|
|
|
+ {
|
|
|
|
+ allDeviceList.Clear();
|
|
|
|
+ conditionDevices.Clear();
|
|
|
|
+ var projectlist = _iBasicDeviceService.QueryList();
|
|
|
|
+ var allDeviceKinds = _mapper.Map<List<bas_device>, List<BasDeviceDto>>(projectlist);
|
|
|
|
+ foreach (var plc in allDeviceKinds)
|
|
|
|
+ {
|
|
|
|
+ allDeviceList.Add(plc);
|
|
|
|
+ conditionDevices.Add(plc);
|
|
|
|
+ }
|
|
|
|
+ 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(); }
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+
|
|
|
|
+ private List<string> projectNameList;
|
|
|
|
+ public List<string> ProjectNameList
|
|
|
|
+ {
|
|
|
|
+ get { return projectNameList; }
|
|
|
|
+ set { projectNameList = value; RaisePropertyChanged(); }
|
|
|
|
+ }
|
|
|
|
+ private List<string> deviceKindNameList;
|
|
|
|
+ public List<string> DeviceKindNameList
|
|
|
|
+ {
|
|
|
|
+ get { return deviceKindNameList; }
|
|
|
|
+ set { deviceKindNameList = 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
|
|
}
|
|
}
|
|
}
|
|
}
|