PLCConfigViewModel.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  1. using AutoMapper;
  2. using BizService;
  3. using Microsoft.Extensions.Logging;
  4. using MiniExcelLibs;
  5. using Model.Dto;
  6. using Model.Entities;
  7. using Prism.Commands;
  8. using Prism.Mvvm;
  9. using Prism.Services.Dialogs;
  10. using System;
  11. using System.Collections.Generic;
  12. using System.Collections.ObjectModel;
  13. using System.Diagnostics.Metrics;
  14. using System.Linq;
  15. using System.Text;
  16. using System.Threading.Tasks;
  17. using System.Windows;
  18. using static System.Windows.Forms.VisualStyles.VisualStyleElement.StartPanel;
  19. namespace PLCTool.ViewModels.BasicConfigViewModel
  20. {
  21. public class PLCConfigViewModel : BindableBase
  22. {
  23. private readonly IBasicPlcItemConfigService _iBasPlcItemConfigService;
  24. private readonly IMapper _mapper;
  25. private readonly IDialogService _dialog;
  26. private readonly ILogger _logger;
  27. private List<BasPlcItemConfigDto> allPLCConfigList = new List<BasPlcItemConfigDto>();//所有方案
  28. private List<BasPlcItemConfigDto> conditionConfig = new List<BasPlcItemConfigDto>();//符合条件的方案
  29. public PLCConfigViewModel(IBasicPlcItemConfigService iBasPlcItemConfigService, IMapper mapper,IDialogService dialog, ILogger logger)
  30. {
  31. _iBasPlcItemConfigService = iBasPlcItemConfigService;
  32. _mapper = mapper;
  33. _dialog = dialog;
  34. _logger = logger;
  35. QueryCommand = new DelegateCommand<object>(Query);
  36. AddCommand= new DelegateCommand<object>(Add);
  37. ExportCommand = new DelegateCommand<string>(Export);
  38. EditCommand = new DelegateCommand<object>(Edit);
  39. DeleteCommand = new DelegateCommand<object>(Delete);
  40. GetPLCConfig();
  41. }
  42. private void Edit(object obj)
  43. {
  44. int id = Convert.ToInt32(obj);
  45. var findPlc = allPLCConfigList.FirstOrDefault(x => (x.Id ==id));
  46. DialogParameters parm = new DialogParameters();
  47. parm.Add("Key", findPlc);
  48. //弹出详情对话框
  49. _dialog.ShowDialog("AddDetailView", parm, async callback =>
  50. {
  51. if (callback.Result == ButtonResult.OK)
  52. {
  53. BasPlcItemConfigDto returnValue = callback.Parameters.GetValue<BasPlcItemConfigDto>("ReturnValue");
  54. if (returnValue != null)
  55. {
  56. var plcCon = _mapper.Map<BasPlcItemConfigDto, bas_plc_item_config>(returnValue);
  57. var findPlc = allPLCConfigList.FirstOrDefault(x => (x.PlcAddress == returnValue.PlcAddress) || (x.PlcItem == returnValue.PlcAddress));
  58. if (findPlc != null)
  59. {
  60. MessageBox.Show("已有此PLC变量地址或名称,请更改名称!", "提示", MessageBoxButton.OKCancel, MessageBoxImage.Warning);
  61. return;
  62. }
  63. //修改
  64. plcCon.id = id;
  65. bool isSucc = _iBasPlcItemConfigService.Edit(plcCon);
  66. if (isSucc)
  67. {
  68. //重新读取PLC
  69. GetPLCConfig();
  70. _logger.LogInformation($"修改PLC变量成功。变量名{PLCItem},地址{PLCAddr}");
  71. MessageBox.Show("修改PLC变量成功", "确认", MessageBoxButton.OK, MessageBoxImage.Information);
  72. }
  73. }
  74. }
  75. });
  76. }
  77. private void Delete(object obj)
  78. {
  79. int id = Convert.ToInt32(obj);
  80. MessageBoxResult boxResult = MessageBox.Show("确认删除此条数据?", "确认", MessageBoxButton.OKCancel, MessageBoxImage.Question);
  81. if (boxResult == MessageBoxResult.OK)
  82. {
  83. var del = _iBasPlcItemConfigService.Delete(id);
  84. if (del)
  85. {
  86. MessageBox.Show("删除成功!", "确认", MessageBoxButton.OK, MessageBoxImage.Information);
  87. GetPLCConfig();
  88. }
  89. }
  90. }
  91. #region 私有方法
  92. /// <summary>
  93. /// 查询
  94. /// </summary>
  95. /// <param name="obj"></param>
  96. private void Query(object obj)
  97. {
  98. conditionConfig = (from a in allPLCConfigList
  99. where (string.IsNullOrEmpty(PLCItem) ? true : (a.PlcItem == PLCItem))
  100. && (string.IsNullOrEmpty(PLCAddr) ? true : (a.PlcAddress == PLCAddr))
  101. select a).ToList();
  102. //默认显示的第一页
  103. Getpage();
  104. }
  105. private void Export(string obj)
  106. {
  107. using (System.Windows.Forms.SaveFileDialog saveFileDialog = new System.Windows.Forms.SaveFileDialog()
  108. {
  109. //设置文件类型
  110. //书写规则例如:txt files(*.txt)|*.txt
  111. Filter = "Excel files(*.xlsx)|*.xlsx|All files(*.*)|*.*",
  112. //设置默认文件名(可以不设置)
  113. FileName = "PLC配置表",
  114. //获取或设置一个值,该值指示如果用户省略扩展名,文件对话框是否自动在文件名中添加扩展名。(可以不设置)
  115. AddExtension = true,
  116. //保存对话框是否记忆上次打开的目录
  117. RestoreDirectory = true
  118. })
  119. {
  120. // Show save file dialog box
  121. //点了保存按钮进入
  122. if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  123. {
  124. try
  125. {
  126. //获得文件路径
  127. string localFilePath = saveFileDialog.FileName.ToString();
  128. //string filePathWithName = localFilePath + ".xlsx";
  129. //获取文件内容
  130. MiniExcel.SaveAs(localFilePath, PLCItemList);
  131. }
  132. catch (Exception ex)
  133. {
  134. }
  135. }
  136. }
  137. }
  138. /// <summary>
  139. /// 添加PLC变量
  140. /// </summary>
  141. /// <param name="obj"></param>
  142. /// <exception cref="NotImplementedException"></exception>
  143. private void Add(object obj)
  144. {
  145. //弹出详情对话框
  146. _dialog.ShowDialog("AddDetailView", async callback =>
  147. {
  148. if (callback.Result == ButtonResult.OK)
  149. {
  150. BasPlcItemConfigDto returnValue = callback.Parameters.GetValue<BasPlcItemConfigDto>("ReturnValue");
  151. if (returnValue != null)
  152. {
  153. var plcCon = _mapper.Map<BasPlcItemConfigDto,bas_plc_item_config >(returnValue);
  154. var findPlc=allPLCConfigList.FirstOrDefault(x=>(x.PlcAddress==returnValue.PlcAddress)||(x.PlcItem==returnValue.PlcAddress));
  155. if(findPlc != null)
  156. {
  157. MessageBox.Show("已有此PLC变量地址或名称,请更改名称!", "提示", MessageBoxButton.OKCancel, MessageBoxImage.Warning);
  158. return;
  159. }
  160. bool isSucc=_iBasPlcItemConfigService.Add(plcCon);
  161. if(isSucc)
  162. {
  163. //重新读取PLC
  164. GetPLCConfig();
  165. _logger.LogInformation($"添加PLC变量成功。变量名{PLCItem},地址{PLCAddr}");
  166. MessageBox.Show("添加PLC变量成功", "确认", MessageBoxButton.OK, MessageBoxImage.Information);
  167. }
  168. }
  169. }
  170. });
  171. }
  172. /// <summary>
  173. /// 获取PLC配置
  174. /// </summary>
  175. private void GetPLCConfig()
  176. {
  177. allPLCConfigList.Clear();
  178. conditionConfig.Clear();
  179. var plclist = _iBasPlcItemConfigService.QueryList();
  180. var allPlc = _mapper.Map<List<bas_plc_item_config>, List<BasPlcItemConfigDto>>(plclist);
  181. foreach (var plc in allPlc)
  182. {
  183. allPLCConfigList.Add(plc);
  184. conditionConfig.Add(plc);
  185. }
  186. Getpage();
  187. }
  188. /// <summary>
  189. /// 获取页面
  190. /// </summary>
  191. private void Getpage()
  192. {
  193. CurrentPage = 1;
  194. TotalCount = conditionConfig.Count;
  195. CurrentPageChanged();
  196. }
  197. /// <summary>
  198. /// 页面变化
  199. /// </summary>
  200. private void CurrentPageChanged()
  201. {
  202. PLCItemList.Clear();
  203. foreach (var i in conditionConfig.Skip((CurrentPage - 1) * CountPerPage).Take(CountPerPage))
  204. {
  205. PLCItemList.Add(i);
  206. }
  207. }
  208. #endregion
  209. #region 命令绑定
  210. public DelegateCommand<object> QueryCommand { set; get; }
  211. public DelegateCommand<object> AddCommand { set; get; }
  212. public DelegateCommand<string> ExportCommand { set; get; }
  213. /// <summary>
  214. /// 表格删除
  215. /// </summary>
  216. public DelegateCommand<Object> DeleteCommand { set; get; }
  217. /// <summary>
  218. /// 表格编辑按钮
  219. /// </summary>
  220. public DelegateCommand<Object> EditCommand { set; get; }
  221. #endregion
  222. #region 数据绑定
  223. private ObservableCollection<BasPlcItemConfigDto> plcItemList = new ObservableCollection<BasPlcItemConfigDto>();
  224. public ObservableCollection<BasPlcItemConfigDto> PLCItemList
  225. {
  226. get { return plcItemList; }
  227. set { plcItemList = value; RaisePropertyChanged(); }
  228. }
  229. /// <summary>
  230. /// plc地址
  231. /// </summary>
  232. private string plcAddr;
  233. public string PLCAddr
  234. {
  235. get { return plcAddr; }
  236. set { plcAddr = value; RaisePropertyChanged(); }
  237. }
  238. /// <summary>
  239. /// plc变量名
  240. /// </summary>
  241. private string plcItem;
  242. public string PLCItem
  243. {
  244. get { return plcItem; }
  245. set { plcItem = value; RaisePropertyChanged(); }
  246. }
  247. /// <summary>
  248. /// 总条数
  249. /// </summary>
  250. private int totalCount;
  251. public int TotalCount
  252. {
  253. get { return totalCount; }
  254. set { totalCount = value; RaisePropertyChanged(); CurrentPageChanged(); }
  255. }
  256. /// <summary>
  257. /// 每页数量
  258. /// </summary>
  259. private int countPerPage = 10;
  260. public int CountPerPage
  261. {
  262. get { return countPerPage; }
  263. set { countPerPage = value; RaisePropertyChanged(); CurrentPageChanged(); }
  264. }
  265. /// <summary>
  266. /// 单前页
  267. /// </summary>
  268. private int currentPage = 1;
  269. public int CurrentPage
  270. {
  271. get { return currentPage; }
  272. set { currentPage = value; RaisePropertyChanged(); CurrentPageChanged(); }
  273. }
  274. #endregion
  275. }
  276. }