ProjectViewModel.cs 14 KB

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