DeviceViewModel.cs 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462
  1. using AutoMapper;
  2. using BizService;
  3. using Microsoft.Extensions.Logging;
  4. using MiniExcelLibs;
  5. using Model.Dto;
  6. using Model.Entities;
  7. using PLCTool.Common;
  8. using PLCTool.Events;
  9. using PLCTool.Models;
  10. using Prism.Commands;
  11. using Prism.Events;
  12. using Prism.Mvvm;
  13. using Prism.Services.Dialogs;
  14. using System;
  15. using System.Collections.Generic;
  16. using System.Collections.ObjectModel;
  17. using System.Linq;
  18. using System.Text;
  19. using System.Threading.Tasks;
  20. using System.Windows;
  21. namespace PLCTool.ViewModels.BasicConfigViewModel
  22. {
  23. public class DeviceViewModel : BindableBase
  24. {
  25. private readonly IBasicDeviceService _iBasicDeviceService;
  26. private readonly IBasicDeviceKindService _iBasicDeviceKindService;
  27. private readonly IBasicProjectService _iBasicProjectService;
  28. private readonly IMapper _mapper;
  29. private readonly IDialogService _dialog;
  30. private readonly Microsoft.Extensions.Logging.ILogger _logger;
  31. private readonly IEventAggregator _aggregator;
  32. private List<BasDeviceDto> allDeviceList = new List<BasDeviceDto>();//所有方案
  33. private List<BasDeviceDto> conditionDevices = new List<BasDeviceDto>();//符合条件的方案
  34. private List<CrumbViewModel> breadCrumbs = new List<CrumbViewModel>();
  35. public DeviceViewModel(IBasicDeviceService iBasicDeviceService, IBasicDeviceKindService iBasicDeviceKindService, IBasicProjectService iBasicProjectService, IMapper mapper, IDialogService dialog, Microsoft.Extensions.Logging.ILogger logger ,IEventAggregator aggregator)
  36. {
  37. _iBasicDeviceService = iBasicDeviceService;
  38. _iBasicDeviceKindService = iBasicDeviceKindService;
  39. _iBasicProjectService = iBasicProjectService;
  40. _mapper = mapper;
  41. _dialog = dialog;
  42. _logger = logger;
  43. _aggregator=aggregator;
  44. QueryCommand = new DelegateCommand<object>(Query);
  45. AddCommand = new DelegateCommand<object>(Add);
  46. ExportCommand = new DelegateCommand<string>(Export);
  47. EditCommand = new DelegateCommand<object>(Edit);
  48. DeleteCommand = new DelegateCommand<object>(Delete);
  49. ResetCommand = new DelegateCommand<object>(Reset);
  50. OnLoadCommand = new DelegateCommand(OnLoad);
  51. GetPprojectConfig();
  52. }
  53. #region 私有方法
  54. private void OnLoad()
  55. {
  56. DeviceKindNameList.Clear();
  57. DeviceKindNameList.Add("---");
  58. ProjectNameList.Clear();
  59. ProjectNameList.Add("---");
  60. var Kinds = _iBasicDeviceKindService.FindAllDeviceKind();
  61. foreach(var kind in Kinds)
  62. {
  63. DeviceKindNameList.Add(kind);
  64. }
  65. var projects= _iBasicProjectService.FindAllProject();
  66. foreach(var project in projects)
  67. {
  68. ProjectNameList.Add(project);
  69. }
  70. DeviceKindName = "---";
  71. ProjectName = "---";
  72. //发布面包靴
  73. breadCrumbs.Clear();
  74. breadCrumbs.Add(new CrumbViewModel { Name = "基础设置" });
  75. breadCrumbs.Add(new CrumbViewModel { Name = "设备管理" });
  76. _aggregator.GetEvent<BreadEvent>().Publish(breadCrumbs);
  77. }
  78. private void Reset(object obj)
  79. {
  80. DeviceNo = string.Empty;
  81. DeviceName = string.Empty;
  82. DeviceKindName = "---";
  83. ProjectName = "---";
  84. StartTime = string.Empty;
  85. EndTime = string.Empty;
  86. }
  87. /// <summary>
  88. /// 修改
  89. /// </summary>
  90. /// <param name="obj"></param>
  91. private void Edit(object obj)
  92. {
  93. int id = Convert.ToInt32(obj);
  94. var findDevice = allDeviceList.FirstOrDefault(x => (x.DeviceId == id));
  95. DialogParameters parm = new DialogParameters();
  96. parm.Add("Key", findDevice);
  97. //弹出详情对话框
  98. _dialog.ShowDialog("AddOrEditDeviceView", parm, async callback =>
  99. {
  100. if (callback.Result == ButtonResult.OK)
  101. {
  102. BasDeviceDto returnValue = callback.Parameters.GetValue<BasDeviceDto>("ReturnValue");
  103. if (returnValue != null)
  104. {
  105. //更新时间
  106. returnValue.UpdateTime = DateTime.Now;
  107. returnValue.UpdateBy = Appsession.UserName;
  108. //创建时间
  109. returnValue.CreateTime = findDevice.CreateTime;
  110. returnValue.CreateBy = findDevice?.CreateBy;
  111. var deviceKindCon = _mapper.Map<BasDeviceDto, bas_device>(returnValue);
  112. var findPlcs = allDeviceList.FindAll(x => (x.DeviceName == returnValue.DeviceName) || (x.DeviceNo == returnValue.DeviceNo));
  113. foreach (var item in findPlcs)
  114. {
  115. if (item.DeviceId != id)
  116. {
  117. MessageBox.Show("已有此设备编号,请更改名称!", "提示", MessageBoxButton.OKCancel, MessageBoxImage.Warning);
  118. return;
  119. }
  120. }
  121. //修改
  122. deviceKindCon.device_id = id;
  123. bool isSucc = _iBasicDeviceService.Edit(deviceKindCon);
  124. if (isSucc)
  125. {
  126. //重新读取PLC
  127. GetPprojectConfig();
  128. _logger.LogInformation($"修改设备信息成功,设备名称为{deviceKindCon.device_name}");
  129. _aggregator.GetEvent<LogEvent>().Publish (new LogMessage
  130. {
  131. LogTime= DateTime.Now,
  132. LogMsg= $"修改设备信息成功,设备名称为{deviceKindCon.device_name}"
  133. });
  134. MessageBox.Show("修改设备信息成功", "确认", MessageBoxButton.OK, MessageBoxImage.Information);
  135. }
  136. }
  137. }
  138. });
  139. }
  140. private void Delete(object obj)
  141. {
  142. int id = Convert.ToInt32(obj);
  143. MessageBoxResult boxResult = MessageBox.Show("确认删除此条数据?", "确认", MessageBoxButton.OKCancel, MessageBoxImage.Question);
  144. if (boxResult == MessageBoxResult.OK)
  145. {
  146. var del = _iBasicDeviceService.Delete(id);
  147. if (del)
  148. {
  149. _logger.LogInformation($"删除设备成功");
  150. _aggregator.GetEvent<LogEvent>().Publish(new LogMessage
  151. {
  152. LogTime = DateTime.Now,
  153. LogMsg = $"删除设备成功"
  154. });
  155. MessageBox.Show("删除成功!", "确认", MessageBoxButton.OK, MessageBoxImage.Information);
  156. GetPprojectConfig();
  157. }
  158. }
  159. }
  160. /// <summary>
  161. /// 查询
  162. /// </summary>
  163. /// <param name="obj"></param>
  164. private void Query(object obj)
  165. {
  166. if ((!string.IsNullOrEmpty(StartTime)) && (!string.IsNullOrEmpty(EndTime)))
  167. {
  168. if (Convert.ToDateTime(StartTime) > Convert.ToDateTime(EndTime))
  169. {
  170. MessageBox.Show("起始时间大于结束时间,请重新输入", "确认", MessageBoxButton.OK, MessageBoxImage.Warning);
  171. return;
  172. }
  173. }
  174. conditionDevices = (from a in allDeviceList
  175. where (string.IsNullOrEmpty(DeviceName) ? true : (a.DeviceName == DeviceName))
  176. && (string.IsNullOrEmpty(DeviceNo) ? true : (a.DeviceNo == DeviceNo))
  177. && ((DeviceKindName == "---") ? true : (a.DeviceKindName == DeviceKindName))
  178. &&((ProjectName == "---") ? true : (a.ProjectName == ProjectName))
  179. && (EndTime == string.Empty ? true : (a.CreateTime < Convert.ToDateTime(EndTime)) && (Convert.ToDateTime(StartTime) < a.CreateTime))
  180. select a).ToList();
  181. //默认显示的第一页
  182. conditionDevices = conditionDevices.OrderBy(x => x.DeviceId).ToList();
  183. Getpage();
  184. }
  185. private void Export(string obj)
  186. {
  187. using (System.Windows.Forms.SaveFileDialog saveFileDialog = new System.Windows.Forms.SaveFileDialog()
  188. {
  189. //设置文件类型
  190. //书写规则例如:txt files(*.txt)|*.txt
  191. Filter = "Excel files(*.xlsx)|*.xlsx|All files(*.*)|*.*",
  192. //设置默认文件名(可以不设置)
  193. FileName = "项目配置表",
  194. //获取或设置一个值,该值指示如果用户省略扩展名,文件对话框是否自动在文件名中添加扩展名。(可以不设置)
  195. AddExtension = true,
  196. //保存对话框是否记忆上次打开的目录
  197. RestoreDirectory = true
  198. })
  199. {
  200. // Show save file dialog box
  201. //点了保存按钮进入
  202. if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  203. {
  204. try
  205. {
  206. //获得文件路径
  207. string localFilePath = saveFileDialog.FileName.ToString();
  208. //获取文件内容
  209. MiniExcel.SaveAs(localFilePath, DeviceItemList);
  210. }
  211. catch (Exception ex)
  212. {
  213. _logger.LogError(ex.ToString());
  214. }
  215. }
  216. }
  217. }
  218. /// <summary>
  219. /// 添加PLC变量
  220. /// </summary>
  221. /// <param name="obj"></param>
  222. /// <exception cref="NotImplementedException"></exception>
  223. private void Add(object obj)
  224. {
  225. //弹出详情对话框
  226. _dialog.ShowDialog("AddOrEditDeviceView", async callback =>
  227. {
  228. if (callback.Result == ButtonResult.OK)
  229. {
  230. BasDeviceDto returnValue = callback.Parameters.GetValue<BasDeviceDto>("ReturnValue");
  231. if (returnValue != null)
  232. {
  233. returnValue.CreateTime = DateTime.Now;
  234. returnValue.CreateBy = Appsession.UserName;
  235. returnValue.UpdateTime = DateTime.Now;
  236. returnValue.UpdateBy = Appsession.UserName;
  237. var deviceCon = _mapper.Map<BasDeviceDto, bas_device>(returnValue);
  238. var findPlc = allDeviceList.FirstOrDefault(x => (x.DeviceName == returnValue.DeviceName) || (x.DeviceNo == returnValue.DeviceNo));
  239. if (findPlc != null)
  240. {
  241. MessageBox.Show("已有此设备名称或编号,请更改!", "提示", MessageBoxButton.OKCancel, MessageBoxImage.Warning);
  242. return;
  243. }
  244. bool isSucc = _iBasicDeviceService.Add(deviceCon);
  245. if (isSucc)
  246. {
  247. //重新读取PLC
  248. GetPprojectConfig();
  249. _logger.LogInformation($"添加设备成功,设备名为{returnValue.DeviceName}");
  250. _aggregator.GetEvent<LogEvent>().Publish(new LogMessage
  251. {
  252. LogTime = DateTime.Now,
  253. LogMsg = $"添加设备成功,设备名为{returnValue.DeviceName}"
  254. });
  255. MessageBox.Show("添加设备成功!", "确认", MessageBoxButton.OK, MessageBoxImage.Information);
  256. }
  257. }
  258. }
  259. });
  260. }
  261. /// <summary>
  262. /// 获取所有项目
  263. /// </summary>
  264. private void GetPprojectConfig()
  265. {
  266. allDeviceList.Clear();
  267. conditionDevices.Clear();
  268. var projectlist = _iBasicDeviceService.QueryList();
  269. var allDeviceKinds = _mapper.Map<List<bas_device>, List<BasDeviceDto>>(projectlist);
  270. foreach (var kind in allDeviceKinds)
  271. {
  272. //获取项目名和设备类型
  273. kind.DeviceKindName = _iBasicDeviceKindService.Find((int)kind.DeviceKindId)?.devicekind_name;
  274. kind.ProjectName = _iBasicProjectService.Find((int)kind.ProjectId)?.project_name;
  275. allDeviceList.Add(kind);
  276. conditionDevices.Add(kind);
  277. }
  278. conditionDevices = conditionDevices.OrderBy(x => x.DeviceId).ToList();
  279. Getpage();
  280. }
  281. /// <summary>
  282. /// 获取页面
  283. /// </summary>
  284. private void Getpage()
  285. {
  286. CurrentPage = 1;
  287. TotalCount = conditionDevices.Count;
  288. CurrentPageChanged();
  289. }
  290. /// <summary>
  291. /// 页面变化
  292. /// </summary>
  293. private void CurrentPageChanged()
  294. {
  295. DeviceItemList.Clear();
  296. foreach (var i in conditionDevices.Skip((CurrentPage - 1) * CountPerPage).Take(CountPerPage))
  297. {
  298. DeviceItemList.Add(i);
  299. }
  300. }
  301. #endregion
  302. #region 命令绑定
  303. public DelegateCommand<object> QueryCommand { set; get; }
  304. public DelegateCommand<object> AddCommand { set; get; }
  305. public DelegateCommand<string> ExportCommand { set; get; }
  306. /// <summary>
  307. /// 表格删除
  308. /// </summary>
  309. public DelegateCommand<Object> DeleteCommand { set; get; }
  310. /// <summary>
  311. /// 表格编辑按钮
  312. /// </summary>
  313. public DelegateCommand<Object> EditCommand { set; get; }
  314. public DelegateCommand<object> ResetCommand { set; get; }
  315. public DelegateCommand OnLoadCommand { set; get; }
  316. #endregion
  317. #region 数据绑定
  318. private ObservableCollection<BasDeviceDto> deviceItemList = new ObservableCollection<BasDeviceDto>();
  319. public ObservableCollection<BasDeviceDto> DeviceItemList
  320. {
  321. get { return deviceItemList; }
  322. set { deviceItemList = value; RaisePropertyChanged(); }
  323. }
  324. /// <summary>
  325. /// 设备编号
  326. /// </summary>
  327. private string devicedNo;
  328. public string DeviceNo
  329. {
  330. get { return devicedNo; }
  331. set { devicedNo = value; RaisePropertyChanged(); }
  332. }
  333. /// <summary>
  334. /// 设备类型名称
  335. /// </summary>
  336. private string deviceName;
  337. public string DeviceName
  338. {
  339. get { return deviceName; }
  340. set { deviceName = value; RaisePropertyChanged(); }
  341. }
  342. private string projectName;
  343. public string ProjectName
  344. {
  345. get { return projectName; }
  346. set { projectName = value; RaisePropertyChanged(); }
  347. }
  348. private string deviceKindName;
  349. public string DeviceKindName
  350. {
  351. get { return deviceKindName; }
  352. set { deviceKindName = value; RaisePropertyChanged(); }
  353. }
  354. /// <summary>
  355. /// 项目名称
  356. /// </summary>
  357. private ObservableCollection<string> projectNameList=new ObservableCollection<string>();
  358. public ObservableCollection<string> ProjectNameList
  359. {
  360. get { return projectNameList; }
  361. set { projectNameList = value; RaisePropertyChanged(); }
  362. }
  363. /// <summary>
  364. /// 设备类型
  365. /// </summary>
  366. private ObservableCollection<string> deviceKindNameList=new ObservableCollection<string>();
  367. public ObservableCollection<string> DeviceKindNameList
  368. {
  369. get { return deviceKindNameList; }
  370. set { deviceKindNameList = value; RaisePropertyChanged(); }
  371. }
  372. /// <summary>
  373. /// 开始时间
  374. /// </summary>
  375. private string startTime = DateTime.Now.AddDays(-1).ToString();
  376. public string StartTime
  377. {
  378. get { return startTime; }
  379. set { startTime = value; RaisePropertyChanged(); }
  380. }
  381. private string endTime = DateTime.Now.ToString();
  382. public string EndTime
  383. {
  384. get { return endTime; }
  385. set { endTime = value; RaisePropertyChanged(); }
  386. }
  387. /// <summary>
  388. /// 总条数
  389. /// </summary>
  390. private int totalCount;
  391. public int TotalCount
  392. {
  393. get { return totalCount; }
  394. set { totalCount = value; RaisePropertyChanged(); CurrentPageChanged(); }
  395. }
  396. /// <summary>
  397. /// 每页数量
  398. /// </summary>
  399. private int countPerPage = 15;
  400. public int CountPerPage
  401. {
  402. get { return countPerPage; }
  403. set { countPerPage = value; RaisePropertyChanged(); CurrentPageChanged(); }
  404. }
  405. /// <summary>
  406. /// 单前页
  407. /// </summary>
  408. private int currentPage = 1;
  409. public int CurrentPage
  410. {
  411. get { return currentPage; }
  412. set { currentPage = value; RaisePropertyChanged(); CurrentPageChanged(); }
  413. }
  414. #endregion
  415. }
  416. }