123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310 |
- using AutoMapper;
- using BizService;
- using Microsoft.Extensions.Logging;
- using MiniExcelLibs;
- 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 readonly ILogger _logger;
- private List<BasPlcItemConfigDto> allPLCConfigList = new List<BasPlcItemConfigDto>();//所有方案
- private List<BasPlcItemConfigDto> conditionConfig = new List<BasPlcItemConfigDto>();//符合条件的方案
- public PLCConfigViewModel(IBasicPlcItemConfigService iBasPlcItemConfigService, IMapper mapper,IDialogService dialog, ILogger logger)
- {
- _iBasPlcItemConfigService = iBasPlcItemConfigService;
- _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);
- GetPLCConfig();
- }
- private void Edit(object obj)
- {
- int id = Convert.ToInt32(obj);
- var findPlc = allPLCConfigList.FirstOrDefault(x => (x.Id ==id));
- DialogParameters parm = new DialogParameters();
- parm.Add("Key", findPlc);
- //弹出详情对话框
- _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;
- }
- //修改
- plcCon.id = id;
- bool isSucc = _iBasPlcItemConfigService.Edit(plcCon);
- if (isSucc)
- {
- //重新读取PLC
- GetPLCConfig();
- _logger.LogInformation($"修改PLC变量成功。变量名{PLCItem},地址{PLCAddr}");
- MessageBox.Show("修改PLC变量成功", "确认", 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 = _iBasPlcItemConfigService.Delete(id);
- if (del)
- {
- MessageBox.Show("删除成功!", "确认", MessageBoxButton.OK, MessageBoxImage.Information);
- 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();
- }
- 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 = "PLC配置表",
- //获取或设置一个值,该值指示如果用户省略扩展名,文件对话框是否自动在文件名中添加扩展名。(可以不设置)
- AddExtension = true,
- //保存对话框是否记忆上次打开的目录
- RestoreDirectory = true
- })
- {
- // Show save file dialog box
- //点了保存按钮进入
- if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
- {
- try
- {
- //获得文件路径
- string localFilePath = saveFileDialog.FileName.ToString();
- //string filePathWithName = localFilePath + ".xlsx";
- //获取文件内容
- MiniExcel.SaveAs(localFilePath, PLCItemList);
- }
- catch (Exception ex)
- {
- }
- }
- }
- }
- /// <summary>
- /// 添加PLC变量
- /// </summary>
- /// <param name="obj"></param>
- /// <exception cref="NotImplementedException"></exception>
- private void Add(object obj)
- {
-
- //弹出详情对话框
- _dialog.ShowDialog("AddDetailView", 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();
- _logger.LogInformation($"添加PLC变量成功。变量名{PLCItem},地址{PLCAddr}");
- 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; }
- public DelegateCommand<string> ExportCommand { set; get; }
- /// <summary>
- /// 表格删除
- /// </summary>
- public DelegateCommand<Object> DeleteCommand { set; get; }
- /// <summary>
- /// 表格编辑按钮
- /// </summary>
- public DelegateCommand<Object> EditCommand { 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 = 10;
- 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
- }
- }
|