DeviceTestViewModel.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384
  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. using System.Windows.Controls;
  19. namespace PLCTool.ViewModels.BusinessManageViewModel
  20. {
  21. public class DeviceTestViewModel : BindableBase
  22. {
  23. private readonly IBasicDeviceService _iBasicDeviceService;
  24. private readonly IBasicDeviceKindService _iBasicDeviceKindService;
  25. private readonly IBasicProjectService _iBasicProjectService;
  26. private readonly IBasicPlcTestSchemeService _basicPlcTestSchemeService;
  27. private readonly IMapper _mapper;
  28. private readonly IDialogService _dialog;
  29. private readonly ILogger _logger;
  30. private List<BasDeviceWithSchModel> allDeviceList = new List<BasDeviceWithSchModel>();//所有方案
  31. private List<BasDeviceWithSchModel> conditionDevices = new List<BasDeviceWithSchModel>();//符合条件的方案
  32. public DeviceTestViewModel(IBasicDeviceService iBasicDeviceService, IBasicDeviceKindService iBasicDeviceKindService, IBasicProjectService iBasicProjectService, IBasicPlcTestSchemeService basicPlcTestSchemeService, IMapper mapper, IDialogService dialog, ILogger logger)
  33. {
  34. _iBasicDeviceService = iBasicDeviceService;
  35. _iBasicDeviceKindService = iBasicDeviceKindService;
  36. _iBasicProjectService = iBasicProjectService;
  37. _basicPlcTestSchemeService = basicPlcTestSchemeService;
  38. _mapper = mapper;
  39. _dialog = dialog;
  40. _logger = logger;
  41. QueryCommand = new DelegateCommand<object>(Query);
  42. ExportCommand = new DelegateCommand<string>(Export);
  43. ResetCommand = new DelegateCommand<object>(Reset);
  44. OnLoadCommand = new DelegateCommand(OnLoad);
  45. //手动 自动测试
  46. AutoTestCommand = new DelegateCommand<object>(AutoDest);
  47. ManualTestCommand = new DelegateCommand<object>(ManualTest);
  48. }
  49. #region 私有方法
  50. /// <summary>
  51. /// 加载页面
  52. /// </summary>
  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. GetProjectConfig();
  72. }
  73. private void Reset(object obj)
  74. {
  75. DeviceNo = string.Empty;
  76. DeviceName = string.Empty;
  77. DeviceKindName = "---";
  78. ProjectName = "---";
  79. }
  80. /// <summary>
  81. /// 自动测试
  82. /// </summary>
  83. /// <param name="obj"></param>
  84. private void AutoDest(object obj)
  85. {
  86. object[] multiObj = obj as object[];
  87. //测试方案明细主键ID
  88. long id =(long) multiObj[0];
  89. long deviceId= (long)multiObj[1];
  90. DialogParameters parm = new DialogParameters();
  91. parm.Add("Key", id);
  92. parm.Add("Key2", deviceId);
  93. //弹出详情对话框
  94. //弹出详情对话框
  95. _dialog.ShowDialog("AutoTestView", parm, async callback =>
  96. {
  97. if (callback.Result == ButtonResult.OK)
  98. {
  99. //更新表格,重新获取
  100. }
  101. });
  102. }
  103. private void ManualTest(object obj)
  104. {
  105. object[] multiObj = obj as object[];
  106. //测试方案明细主键ID
  107. long id = (long)multiObj[0];
  108. long deviceId = (long)multiObj[1];
  109. DialogParameters parm = new DialogParameters();
  110. parm.Add("Key", id);
  111. parm.Add("Key2", deviceId);
  112. //弹出详情对话框
  113. //弹出详情对话框
  114. _dialog.ShowDialog("ManualTestView", parm, async callback =>
  115. {
  116. if (callback.Result == ButtonResult.OK)
  117. {
  118. //更新表格,重新获取
  119. }
  120. });
  121. }
  122. /// <summary>
  123. /// 查询
  124. /// </summary>
  125. /// <param name="obj"></param>
  126. private void Query(object obj)
  127. {
  128. conditionDevices = (from a in allDeviceList
  129. where (string.IsNullOrEmpty(DeviceName) ? true : (a.DeviceName == DeviceName))
  130. && (string.IsNullOrEmpty(DeviceNo) ? true : (a.DeviceNo == DeviceNo))
  131. && ((DeviceKindName == "---") ? true : (a.DeviceKindName == DeviceKindName))
  132. && ((ProjectName == "---") ? true : (a.ProjectName == ProjectName))
  133. select a).ToList();
  134. //默认显示的第一页
  135. conditionDevices = conditionDevices.OrderBy(x => x.DeviceId).ToList();
  136. Getpage();
  137. }
  138. private void Export(string obj)
  139. {
  140. using (System.Windows.Forms.SaveFileDialog saveFileDialog = new System.Windows.Forms.SaveFileDialog()
  141. {
  142. //设置文件类型
  143. //书写规则例如:txt files(*.txt)|*.txt
  144. Filter = "Excel files(*.xlsx)|*.xlsx|All files(*.*)|*.*",
  145. //设置默认文件名(可以不设置)
  146. FileName = "项目配置表",
  147. //获取或设置一个值,该值指示如果用户省略扩展名,文件对话框是否自动在文件名中添加扩展名。(可以不设置)
  148. AddExtension = true,
  149. //保存对话框是否记忆上次打开的目录
  150. RestoreDirectory = true
  151. })
  152. {
  153. // Show save file dialog box
  154. //点了保存按钮进入
  155. if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
  156. {
  157. try
  158. {
  159. //获得文件路径
  160. string localFilePath = saveFileDialog.FileName.ToString();
  161. //获取文件内容
  162. MiniExcel.SaveAs(localFilePath, DeviceItemList);
  163. }
  164. catch (Exception ex)
  165. {
  166. _logger.LogError(ex.ToString());
  167. }
  168. }
  169. }
  170. }
  171. /// <summary>
  172. /// 获取所有项目
  173. /// </summary>
  174. private void GetProjectConfig()
  175. {
  176. allDeviceList.Clear();
  177. conditionDevices.Clear();
  178. var devicelist = _iBasicDeviceService.QueryList();
  179. var allDeviceKinds = _mapper.Map<List<bas_device>, List<BasDeviceDto>>(devicelist);
  180. //所有测试方案
  181. var schlist = _basicPlcTestSchemeService.QueryList();
  182. var schDtoList = _mapper.Map<List<bas_plc_test_scheme>, List<BasicPlcTestSchemeDto>>(schlist);
  183. foreach (var item in allDeviceKinds)
  184. {
  185. string deviceKind =_iBasicDeviceKindService.Find((int)item.DeviceKindId)?.devicekind_name;
  186. //在测试方案中查找此设备类型的所有方案
  187. var schs= schDtoList.FindAll(x => x.DeviceKindName == deviceKind);
  188. //设备方案
  189. foreach (var sch in schs)
  190. {
  191. allDeviceList.Add(new BasDeviceWithSchModel()
  192. {
  193. DeviceId = item.DeviceId,
  194. DeviceNo = item.DeviceNo,
  195. DeviceName = item.DeviceName,
  196. DeviceKindName = _iBasicDeviceKindService.Find((int)item.DeviceKindId)?.devicekind_name,
  197. ProjectName = _iBasicProjectService.Find((int)item.ProjectId)?.project_name,
  198. SchemeName = sch.SchemeName,
  199. SchemeId=sch.SchemeId,
  200. }) ;
  201. conditionDevices.Add(new BasDeviceWithSchModel()
  202. {
  203. DeviceId = item.DeviceId,
  204. DeviceNo = item.DeviceNo,
  205. DeviceName = item.DeviceName,
  206. DeviceKindName = _iBasicDeviceKindService.Find((int)item.DeviceKindId)?.devicekind_name,
  207. ProjectName = _iBasicProjectService.Find((int)item.ProjectId)?.project_name,
  208. SchemeName = sch.SchemeName,
  209. SchemeId = sch.SchemeId,
  210. });
  211. }
  212. }
  213. conditionDevices = conditionDevices.OrderBy(x => x.DeviceId).ToList();
  214. Getpage();
  215. }
  216. /// <summary>
  217. /// 获取页面
  218. /// </summary>
  219. private void Getpage()
  220. {
  221. CurrentPage = 1;
  222. TotalCount = conditionDevices.Count;
  223. CurrentPageChanged();
  224. }
  225. /// <summary>
  226. /// 页面变化
  227. /// </summary>
  228. private void CurrentPageChanged()
  229. {
  230. DeviceItemList.Clear();
  231. foreach (var i in conditionDevices.Skip((CurrentPage - 1) * CountPerPage).Take(CountPerPage))
  232. {
  233. DeviceItemList.Add(i);
  234. }
  235. }
  236. #endregion
  237. #region 命令绑定
  238. public DelegateCommand<object> QueryCommand { set; get; }
  239. public DelegateCommand<string> ExportCommand { set; get; }
  240. public DelegateCommand<object> ResetCommand { set; get; }
  241. public DelegateCommand OnLoadCommand { set; get; }
  242. public DelegateCommand<object> AutoTestCommand { set; get; }
  243. public DelegateCommand<object> ManualTestCommand { set; get; }
  244. #endregion
  245. #region 数据绑定
  246. private ObservableCollection<BasDeviceWithSchModel> deviceItemList = new ObservableCollection<BasDeviceWithSchModel>();
  247. public ObservableCollection<BasDeviceWithSchModel> DeviceItemList
  248. {
  249. get { return deviceItemList; }
  250. set { deviceItemList = value; RaisePropertyChanged(); }
  251. }
  252. /// <summary>
  253. /// 设备编号
  254. /// </summary>
  255. private string devicedNo;
  256. public string DeviceNo
  257. {
  258. get { return devicedNo; }
  259. set { devicedNo = value; RaisePropertyChanged(); }
  260. }
  261. /// <summary>
  262. /// 设备类型名称
  263. /// </summary>
  264. private string deviceName;
  265. public string DeviceName
  266. {
  267. get { return deviceName; }
  268. set { deviceName = value; RaisePropertyChanged(); }
  269. }
  270. private string projectName;
  271. public string ProjectName
  272. {
  273. get { return projectName; }
  274. set { projectName = value; RaisePropertyChanged(); }
  275. }
  276. private string deviceKindName;
  277. public string DeviceKindName
  278. {
  279. get { return deviceKindName; }
  280. set { deviceKindName = value; RaisePropertyChanged(); }
  281. }
  282. /// <summary>
  283. /// 项目名称
  284. /// </summary>
  285. private ObservableCollection<string> projectNameList = new ObservableCollection<string>();
  286. public ObservableCollection<string> ProjectNameList
  287. {
  288. get { return projectNameList; }
  289. set { projectNameList = value; RaisePropertyChanged(); }
  290. }
  291. /// <summary>
  292. /// 设备类型
  293. /// </summary>
  294. private ObservableCollection<string> deviceKindNameList = new ObservableCollection<string>();
  295. public ObservableCollection<string> DeviceKindNameList
  296. {
  297. get { return deviceKindNameList; }
  298. set { deviceKindNameList = value; RaisePropertyChanged(); }
  299. }
  300. /// <summary>
  301. /// 总条数
  302. /// </summary>
  303. private int totalCount;
  304. public int TotalCount
  305. {
  306. get { return totalCount; }
  307. set { totalCount = value; RaisePropertyChanged(); CurrentPageChanged(); }
  308. }
  309. /// <summary>
  310. /// 每页数量
  311. /// </summary>
  312. private int countPerPage = 15;
  313. public int CountPerPage
  314. {
  315. get { return countPerPage; }
  316. set { countPerPage = value; RaisePropertyChanged(); CurrentPageChanged(); }
  317. }
  318. /// <summary>
  319. /// 单前页
  320. /// </summary>
  321. private int currentPage = 1;
  322. public int CurrentPage
  323. {
  324. get { return currentPage; }
  325. set { currentPage = value; RaisePropertyChanged(); CurrentPageChanged(); }
  326. }
  327. #endregion
  328. }
  329. }