DeviceViewModel.cs 16 KB

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