DeviceViewModel.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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 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, 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($"修改设备信息成功");
  129. MessageBox.Show("修改设备信息成功", "确认", MessageBoxButton.OK, MessageBoxImage.Information);
  130. }
  131. }
  132. }
  133. });
  134. }
  135. private void Delete(object obj)
  136. {
  137. int id = Convert.ToInt32(obj);
  138. MessageBoxResult boxResult = MessageBox.Show("确认删除此条数据?", "确认", MessageBoxButton.OKCancel, MessageBoxImage.Question);
  139. if (boxResult == MessageBoxResult.OK)
  140. {
  141. var del = _iBasicDeviceService.Delete(id);
  142. if (del)
  143. {
  144. MessageBox.Show("删除成功!", "确认", MessageBoxButton.OK, MessageBoxImage.Information);
  145. GetPprojectConfig();
  146. }
  147. }
  148. }
  149. /// <summary>
  150. /// 查询
  151. /// </summary>
  152. /// <param name="obj"></param>
  153. private void Query(object obj)
  154. {
  155. if ((!string.IsNullOrEmpty(StartTime)) && (!string.IsNullOrEmpty(EndTime)))
  156. {
  157. if (Convert.ToDateTime(StartTime) > Convert.ToDateTime(EndTime))
  158. {
  159. MessageBox.Show("起始时间大于结束时间,请重新输入", "确认", MessageBoxButton.OK, MessageBoxImage.Warning);
  160. return;
  161. }
  162. }
  163. conditionDevices = (from a in allDeviceList
  164. where (string.IsNullOrEmpty(DeviceName) ? true : (a.DeviceName == DeviceName))
  165. && (string.IsNullOrEmpty(DeviceNo) ? true : (a.DeviceNo == DeviceNo))
  166. && ((DeviceKindName == "---") ? true : (a.DeviceKindName == DeviceKindName))
  167. &&((ProjectName == "---") ? true : (a.ProjectName == ProjectName))
  168. && (EndTime == string.Empty ? true : (a.CreateTime < Convert.ToDateTime(EndTime)) && (Convert.ToDateTime(StartTime) < a.CreateTime))
  169. select a).ToList();
  170. //默认显示的第一页
  171. conditionDevices = conditionDevices.OrderBy(x => x.DeviceId).ToList();
  172. Getpage();
  173. }
  174. private void Export(string obj)
  175. {
  176. using (System.Windows.Forms.SaveFileDialog saveFileDialog = new System.Windows.Forms.SaveFileDialog()
  177. {
  178. //设置文件类型
  179. //书写规则例如:txt files(*.txt)|*.txt
  180. Filter = "Excel files(*.xlsx)|*.xlsx|All files(*.*)|*.*",
  181. //设置默认文件名(可以不设置)
  182. FileName = "项目配置表",
  183. //获取或设置一个值,该值指示如果用户省略扩展名,文件对话框是否自动在文件名中添加扩展名。(可以不设置)
  184. AddExtension = true,
  185. //保存对话框是否记忆上次打开的目录
  186. RestoreDirectory = true
  187. })
  188. {
  189. // Show save file dialog box
  190. //点了保存按钮进入
  191. if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  192. {
  193. try
  194. {
  195. //获得文件路径
  196. string localFilePath = saveFileDialog.FileName.ToString();
  197. //获取文件内容
  198. MiniExcel.SaveAs(localFilePath, DeviceItemList);
  199. }
  200. catch (Exception ex)
  201. {
  202. _logger.LogError(ex.ToString());
  203. }
  204. }
  205. }
  206. }
  207. /// <summary>
  208. /// 添加PLC变量
  209. /// </summary>
  210. /// <param name="obj"></param>
  211. /// <exception cref="NotImplementedException"></exception>
  212. private void Add(object obj)
  213. {
  214. //弹出详情对话框
  215. _dialog.ShowDialog("AddOrEditDeviceView", async callback =>
  216. {
  217. if (callback.Result == ButtonResult.OK)
  218. {
  219. BasDeviceDto returnValue = callback.Parameters.GetValue<BasDeviceDto>("ReturnValue");
  220. if (returnValue != null)
  221. {
  222. returnValue.CreateTime = DateTime.Now;
  223. returnValue.CreateBy = Appsession.UserName;
  224. returnValue.UpdateTime = DateTime.Now;
  225. returnValue.UpdateBy = Appsession.UserName;
  226. var deviceCon = _mapper.Map<BasDeviceDto, bas_device>(returnValue);
  227. var findPlc = allDeviceList.FirstOrDefault(x => (x.DeviceName == returnValue.DeviceName) || (x.DeviceNo == returnValue.DeviceNo));
  228. if (findPlc != null)
  229. {
  230. MessageBox.Show("已有此设备名称或编号,请更改!", "提示", MessageBoxButton.OKCancel, MessageBoxImage.Warning);
  231. return;
  232. }
  233. bool isSucc = _iBasicDeviceService.Add(deviceCon);
  234. if (isSucc)
  235. {
  236. //重新读取PLC
  237. GetPprojectConfig();
  238. _logger.LogInformation($"添加设备成功");
  239. MessageBox.Show("添加设备成功!", "确认", MessageBoxButton.OK, MessageBoxImage.Information);
  240. }
  241. }
  242. }
  243. });
  244. }
  245. /// <summary>
  246. /// 获取所有项目
  247. /// </summary>
  248. private void GetPprojectConfig()
  249. {
  250. allDeviceList.Clear();
  251. conditionDevices.Clear();
  252. var projectlist = _iBasicDeviceService.QueryList();
  253. var allDeviceKinds = _mapper.Map<List<bas_device>, List<BasDeviceDto>>(projectlist);
  254. foreach (var kind in allDeviceKinds)
  255. {
  256. //获取项目名和设备类型
  257. kind.DeviceKindName = _iBasicDeviceKindService.Find((int)kind.DeviceKindId)?.devicekind_name;
  258. kind.ProjectName = _iBasicProjectService.Find((int)kind.ProjectId)?.project_name;
  259. allDeviceList.Add(kind);
  260. conditionDevices.Add(kind);
  261. }
  262. conditionDevices = conditionDevices.OrderBy(x => x.DeviceId).ToList();
  263. Getpage();
  264. }
  265. /// <summary>
  266. /// 获取页面
  267. /// </summary>
  268. private void Getpage()
  269. {
  270. CurrentPage = 1;
  271. TotalCount = conditionDevices.Count;
  272. CurrentPageChanged();
  273. }
  274. /// <summary>
  275. /// 页面变化
  276. /// </summary>
  277. private void CurrentPageChanged()
  278. {
  279. DeviceItemList.Clear();
  280. foreach (var i in conditionDevices.Skip((CurrentPage - 1) * CountPerPage).Take(CountPerPage))
  281. {
  282. DeviceItemList.Add(i);
  283. }
  284. }
  285. #endregion
  286. #region 命令绑定
  287. public DelegateCommand<object> QueryCommand { set; get; }
  288. public DelegateCommand<object> AddCommand { set; get; }
  289. public DelegateCommand<string> ExportCommand { set; get; }
  290. /// <summary>
  291. /// 表格删除
  292. /// </summary>
  293. public DelegateCommand<Object> DeleteCommand { set; get; }
  294. /// <summary>
  295. /// 表格编辑按钮
  296. /// </summary>
  297. public DelegateCommand<Object> EditCommand { set; get; }
  298. public DelegateCommand<object> ResetCommand { set; get; }
  299. public DelegateCommand OnLoadCommand { set; get; }
  300. #endregion
  301. #region 数据绑定
  302. private ObservableCollection<BasDeviceDto> deviceItemList = new ObservableCollection<BasDeviceDto>();
  303. public ObservableCollection<BasDeviceDto> DeviceItemList
  304. {
  305. get { return deviceItemList; }
  306. set { deviceItemList = value; RaisePropertyChanged(); }
  307. }
  308. /// <summary>
  309. /// 设备编号
  310. /// </summary>
  311. private string devicedNo;
  312. public string DeviceNo
  313. {
  314. get { return devicedNo; }
  315. set { devicedNo = value; RaisePropertyChanged(); }
  316. }
  317. /// <summary>
  318. /// 设备类型名称
  319. /// </summary>
  320. private string deviceName;
  321. public string DeviceName
  322. {
  323. get { return deviceName; }
  324. set { deviceName = value; RaisePropertyChanged(); }
  325. }
  326. private string projectName;
  327. public string ProjectName
  328. {
  329. get { return projectName; }
  330. set { projectName = value; RaisePropertyChanged(); }
  331. }
  332. private string deviceKindName;
  333. public string DeviceKindName
  334. {
  335. get { return deviceKindName; }
  336. set { deviceKindName = value; RaisePropertyChanged(); }
  337. }
  338. /// <summary>
  339. /// 项目名称
  340. /// </summary>
  341. private ObservableCollection<string> projectNameList=new ObservableCollection<string>();
  342. public ObservableCollection<string> ProjectNameList
  343. {
  344. get { return projectNameList; }
  345. set { projectNameList = value; RaisePropertyChanged(); }
  346. }
  347. /// <summary>
  348. /// 设备类型
  349. /// </summary>
  350. private ObservableCollection<string> deviceKindNameList=new ObservableCollection<string>();
  351. public ObservableCollection<string> DeviceKindNameList
  352. {
  353. get { return deviceKindNameList; }
  354. set { deviceKindNameList = value; RaisePropertyChanged(); }
  355. }
  356. /// <summary>
  357. /// 开始时间
  358. /// </summary>
  359. private string startTime = DateTime.Now.AddDays(-1).ToString();
  360. public string StartTime
  361. {
  362. get { return startTime; }
  363. set { startTime = value; RaisePropertyChanged(); }
  364. }
  365. private string endTime = DateTime.Now.ToString();
  366. public string EndTime
  367. {
  368. get { return endTime; }
  369. set { endTime = value; RaisePropertyChanged(); }
  370. }
  371. /// <summary>
  372. /// 总条数
  373. /// </summary>
  374. private int totalCount;
  375. public int TotalCount
  376. {
  377. get { return totalCount; }
  378. set { totalCount = value; RaisePropertyChanged(); CurrentPageChanged(); }
  379. }
  380. /// <summary>
  381. /// 每页数量
  382. /// </summary>
  383. private int countPerPage = 15;
  384. public int CountPerPage
  385. {
  386. get { return countPerPage; }
  387. set { countPerPage = value; RaisePropertyChanged(); CurrentPageChanged(); }
  388. }
  389. /// <summary>
  390. /// 单前页
  391. /// </summary>
  392. private int currentPage = 1;
  393. public int CurrentPage
  394. {
  395. get { return currentPage; }
  396. set { currentPage = value; RaisePropertyChanged(); CurrentPageChanged(); }
  397. }
  398. #endregion
  399. }
  400. }