DeviceTestViewModel.cs 17 KB

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