DeviceViewModel.cs 18 KB

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