DeviceKindViewModel.cs 16 KB

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