|
@@ -0,0 +1,195 @@
|
|
|
+using AutoMapper;
|
|
|
+using BizService;
|
|
|
+using Model.Dto;
|
|
|
+using Model.Entities;
|
|
|
+using Prism.Commands;
|
|
|
+using Prism.Mvvm;
|
|
|
+using Prism.Services.Dialogs;
|
|
|
+using System;
|
|
|
+using System.Collections.Generic;
|
|
|
+using System.Collections.ObjectModel;
|
|
|
+using System.Diagnostics.Metrics;
|
|
|
+using System.Linq;
|
|
|
+using System.Text;
|
|
|
+using System.Threading.Tasks;
|
|
|
+using System.Windows;
|
|
|
+using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;
|
|
|
+
|
|
|
+namespace PLCTool.ViewModels.BasicConfigViewModel
|
|
|
+{
|
|
|
+ public class PLCConfigViewModel : BindableBase
|
|
|
+ {
|
|
|
+ private readonly IBasicPlcItemConfigService _iBasPlcItemConfigService;
|
|
|
+ private readonly IMapper _mapper;
|
|
|
+ private readonly IDialogService _dialog;
|
|
|
+ private List<BasPlcItemConfigDto> allPLCConfigList = new List<BasPlcItemConfigDto>();//所有方案
|
|
|
+ private List<BasPlcItemConfigDto> conditionConfig = new List<BasPlcItemConfigDto>();//符合条件的方案
|
|
|
+ public PLCConfigViewModel(IBasicPlcItemConfigService iBasPlcItemConfigService, IMapper mapper,IDialogService dialog)
|
|
|
+ {
|
|
|
+ _iBasPlcItemConfigService = iBasPlcItemConfigService;
|
|
|
+ _mapper = mapper;
|
|
|
+ _dialog = dialog;
|
|
|
+ QueryCommand = new DelegateCommand<object>(Query);
|
|
|
+ AddCommand= new DelegateCommand<object>(Add);
|
|
|
+ GetPLCConfig();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ #region 私有方法
|
|
|
+ /// <summary>
|
|
|
+ /// 查询
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="obj"></param>
|
|
|
+ private void Query(object obj)
|
|
|
+ {
|
|
|
+ conditionConfig = (from a in allPLCConfigList
|
|
|
+ where (string.IsNullOrEmpty(PLCItem) ? true : (a.PlcItem == PLCItem))
|
|
|
+ && (string.IsNullOrEmpty(PLCAddr) ? true : (a.PlcAddress == PLCAddr))
|
|
|
+ select a).ToList();
|
|
|
+ //默认显示的第一页
|
|
|
+ Getpage();
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 添加PLC变量
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="obj"></param>
|
|
|
+ /// <exception cref="NotImplementedException"></exception>
|
|
|
+ private void Add(object obj)
|
|
|
+ {
|
|
|
+ DialogParameters parm = new DialogParameters();
|
|
|
+ parm.Add("Key", "");
|
|
|
+ //弹出详情对话框
|
|
|
+ _dialog.ShowDialog("AddDetailView", parm, async callback =>
|
|
|
+ {
|
|
|
+ if (callback.Result == ButtonResult.OK)
|
|
|
+ {
|
|
|
+ BasPlcItemConfigDto returnValue = callback.Parameters.GetValue<BasPlcItemConfigDto>("ReturnValue");
|
|
|
+ if (returnValue != null)
|
|
|
+ {
|
|
|
+ var plcCon = _mapper.Map<BasPlcItemConfigDto,bas_plc_item_config >(returnValue);
|
|
|
+ var findPlc=allPLCConfigList.FirstOrDefault(x=>(x.PlcAddress==returnValue.PlcAddress)||(x.PlcItem==returnValue.PlcAddress));
|
|
|
+ if(findPlc != null)
|
|
|
+ {
|
|
|
+ MessageBox.Show("已有次PLC变量地址或名称,请更改名称!", "提示", MessageBoxButton.OKCancel, MessageBoxImage.Warning);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ bool isSucc=_iBasPlcItemConfigService.Add(plcCon);
|
|
|
+ if(isSucc)
|
|
|
+ {
|
|
|
+ //重新读取PLC
|
|
|
+ GetPLCConfig();
|
|
|
+ MessageBox.Show("添加PLC变量成功", "确认", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 获取PLC配置
|
|
|
+ /// </summary>
|
|
|
+ private void GetPLCConfig()
|
|
|
+ {
|
|
|
+ allPLCConfigList.Clear();
|
|
|
+ conditionConfig.Clear();
|
|
|
+ var plclist = _iBasPlcItemConfigService.QueryList();
|
|
|
+ var allPlc = _mapper.Map<List<bas_plc_item_config>, List<BasPlcItemConfigDto>>(plclist);
|
|
|
+ foreach (var plc in allPlc)
|
|
|
+ {
|
|
|
+ allPLCConfigList.Add(plc);
|
|
|
+ conditionConfig.Add(plc);
|
|
|
+ }
|
|
|
+ Getpage();
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 获取页面
|
|
|
+ /// </summary>
|
|
|
+ private void Getpage()
|
|
|
+ {
|
|
|
+ CurrentPage = 1;
|
|
|
+ TotalCount = conditionConfig.Count;
|
|
|
+ CurrentPageChanged();
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 页面变化
|
|
|
+ /// </summary>
|
|
|
+ private void CurrentPageChanged()
|
|
|
+ {
|
|
|
+
|
|
|
+ PLCItemList.Clear();
|
|
|
+
|
|
|
+ foreach (var i in conditionConfig.Skip((CurrentPage - 1) * CountPerPage).Take(CountPerPage))
|
|
|
+ {
|
|
|
+ PLCItemList.Add(i);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ #endregion
|
|
|
+ #region 命令绑定
|
|
|
+
|
|
|
+ public DelegateCommand<object> QueryCommand { set; get; }
|
|
|
+ public DelegateCommand<object> AddCommand { set; get; }
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ #region 数据绑定
|
|
|
+ private ObservableCollection<BasPlcItemConfigDto> plcItemList = new ObservableCollection<BasPlcItemConfigDto>();
|
|
|
+ public ObservableCollection<BasPlcItemConfigDto> PLCItemList
|
|
|
+ {
|
|
|
+ get { return plcItemList; }
|
|
|
+ set { plcItemList = value; RaisePropertyChanged(); }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// plc地址
|
|
|
+ /// </summary>
|
|
|
+ private string plcAddr;
|
|
|
+ public string PLCAddr
|
|
|
+ {
|
|
|
+ get { return plcAddr; }
|
|
|
+ set { plcAddr = value; RaisePropertyChanged(); }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// plc变量名
|
|
|
+ /// </summary>
|
|
|
+ private string plcItem;
|
|
|
+ public string PLCItem
|
|
|
+ {
|
|
|
+ get { return plcItem; }
|
|
|
+ set { plcItem = value; RaisePropertyChanged(); }
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 总条数
|
|
|
+ /// </summary>
|
|
|
+ private int totalCount;
|
|
|
+ public int TotalCount
|
|
|
+ {
|
|
|
+ get { return totalCount; }
|
|
|
+ set { totalCount = value; RaisePropertyChanged(); CurrentPageChanged(); }
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 每页数量
|
|
|
+ /// </summary>
|
|
|
+ private int countPerPage = 1;
|
|
|
+ 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
|
|
|
+ }
|
|
|
+}
|