|
@@ -1,12 +1,351 @@
|
|
|
-using System;
|
|
|
+using AutoMapper;
|
|
|
+using BizService;
|
|
|
+using Microsoft.Extensions.Logging;
|
|
|
+using MiniExcelLibs;
|
|
|
+using Model.Dto;
|
|
|
+using Model.Entities;
|
|
|
+using PLCTool.Common;
|
|
|
+using Prism.Commands;
|
|
|
+using Prism.Mvvm;
|
|
|
+using Prism.Services.Dialogs;
|
|
|
+using System;
|
|
|
using System.Collections.Generic;
|
|
|
+using System.Collections.ObjectModel;
|
|
|
using System.Linq;
|
|
|
using System.Text;
|
|
|
using System.Threading.Tasks;
|
|
|
+using System.Windows;
|
|
|
|
|
|
namespace PLCTool.ViewModels.BasicConfigViewModel
|
|
|
{
|
|
|
- class ProjectViewModel
|
|
|
+ class ProjectViewModel : BindableBase
|
|
|
{
|
|
|
+ private readonly IBasicProjectService _iBasicProjectService;
|
|
|
+ private readonly IMapper _mapper;
|
|
|
+ private readonly IDialogService _dialog;
|
|
|
+ private readonly ILogger _logger;
|
|
|
+ private List<BasProjectDto> allProjectList = new List<BasProjectDto>();//所有方案
|
|
|
+ private List<BasProjectDto> conditionProject = new List<BasProjectDto>();//符合条件的方案
|
|
|
+ public ProjectViewModel(IBasicProjectService iBasicProjectService, IMapper mapper, IDialogService dialog, ILogger logger)
|
|
|
+ {
|
|
|
+ _iBasicProjectService = iBasicProjectService;
|
|
|
+ _mapper = mapper;
|
|
|
+ _dialog = dialog;
|
|
|
+ _logger = logger;
|
|
|
+ QueryCommand = new DelegateCommand<object>(Query);
|
|
|
+ AddCommand = new DelegateCommand<object>(Add);
|
|
|
+ ExportCommand = new DelegateCommand<string>(Export);
|
|
|
+ EditCommand = new DelegateCommand<object>(Edit);
|
|
|
+ DeleteCommand = new DelegateCommand<object>(Delete);
|
|
|
+ ResetCommand = new DelegateCommand<object>(Reset);
|
|
|
+ GetPprojectConfig();
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+ #region 私有方法
|
|
|
+
|
|
|
+ private void Reset(object obj)
|
|
|
+ {
|
|
|
+ ProjectName = string.Empty;
|
|
|
+ ProjectNo = string.Empty;
|
|
|
+ ProjectLeader = string.Empty;
|
|
|
+
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 修改
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="obj"></param>
|
|
|
+ private void Edit(object obj)
|
|
|
+ {
|
|
|
+ int id = Convert.ToInt32(obj);
|
|
|
+ var finrProject = allProjectList.FirstOrDefault(x => (x.ProjectId == id));
|
|
|
+ DialogParameters parm = new DialogParameters();
|
|
|
+ parm.Add("Key", finrProject);
|
|
|
+ //弹出详情对话框
|
|
|
+ _dialog.ShowDialog("AddOrEditProjectView", parm, async callback =>
|
|
|
+ {
|
|
|
+ if (callback.Result == ButtonResult.OK)
|
|
|
+ {
|
|
|
+ BasProjectDto returnValue = callback.Parameters.GetValue<BasProjectDto>("ReturnValue");
|
|
|
+
|
|
|
+ if (returnValue != null)
|
|
|
+ {
|
|
|
+ //更新时间
|
|
|
+ returnValue.UpdateTime = DateTime.Now;
|
|
|
+ returnValue.UpdateBy = Appsession.UserName;
|
|
|
+ //创建时间
|
|
|
+ returnValue.CreateTime = finrProject.CreateTime;
|
|
|
+ returnValue.CreateBy = finrProject?.CreateBy;
|
|
|
+
|
|
|
+ var projectCon = _mapper.Map<BasProjectDto, bas_project>(returnValue);
|
|
|
+ var findPlcs = allProjectList.FindAll(x => (x.ProjectName == returnValue.ProjectName) || (x.ProjectNo == returnValue.ProjectNo));
|
|
|
+ foreach (var item in findPlcs)
|
|
|
+ {
|
|
|
+ if (item.ProjectId != id)
|
|
|
+ {
|
|
|
+ MessageBox.Show("已有此项目名称或编号,请更改名称!", "提示", MessageBoxButton.OKCancel, MessageBoxImage.Warning);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ //修改
|
|
|
+ projectCon.project_id = id;
|
|
|
+ bool isSucc = _iBasicProjectService.Edit(projectCon);
|
|
|
+ if (isSucc)
|
|
|
+ {
|
|
|
+ //重新读取PLC
|
|
|
+ GetPprojectConfig();
|
|
|
+ _logger.LogInformation($"修改项目成功");
|
|
|
+ MessageBox.Show("修改项目成功", "确认", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+ private void Delete(object obj)
|
|
|
+ {
|
|
|
+ int id = Convert.ToInt32(obj);
|
|
|
+ MessageBoxResult boxResult = MessageBox.Show("确认删除此条数据?", "确认", MessageBoxButton.OKCancel, MessageBoxImage.Question);
|
|
|
+ if (boxResult == MessageBoxResult.OK)
|
|
|
+ {
|
|
|
+ var del = _iBasicProjectService.Delete(id);
|
|
|
+ if (del)
|
|
|
+ {
|
|
|
+ MessageBox.Show("删除成功!", "确认", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
|
+ GetPprojectConfig();
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 查询
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="obj"></param>
|
|
|
+ private void Query(object obj)
|
|
|
+ {
|
|
|
+ conditionProject = (from a in allProjectList
|
|
|
+ where (string.IsNullOrEmpty(ProjectNo) ? true : (a.ProjectNo == ProjectNo))
|
|
|
+ && (string.IsNullOrEmpty(ProjectName) ? true : (a.ProjectName == ProjectName))
|
|
|
+ && (string.IsNullOrEmpty(ProjectLeader) ? true : (a.ProjectLeader == ProjectLeader))
|
|
|
+ select a).ToList();
|
|
|
+ //默认显示的第一页
|
|
|
+ conditionProject = conditionProject.OrderBy(x => x.ProjectId).ToList();
|
|
|
+ Getpage();
|
|
|
+ }
|
|
|
+
|
|
|
+ private void Export(string obj)
|
|
|
+ {
|
|
|
+ using (System.Windows.Forms.SaveFileDialog saveFileDialog = new System.Windows.Forms.SaveFileDialog()
|
|
|
+ {
|
|
|
+ //设置文件类型
|
|
|
+ //书写规则例如:txt files(*.txt)|*.txt
|
|
|
+ Filter = "Excel files(*.xlsx)|*.xlsx|All files(*.*)|*.*",
|
|
|
+ //设置默认文件名(可以不设置)
|
|
|
+ FileName = "项目配置表",
|
|
|
+
|
|
|
+ //获取或设置一个值,该值指示如果用户省略扩展名,文件对话框是否自动在文件名中添加扩展名。(可以不设置)
|
|
|
+ AddExtension = true,
|
|
|
+
|
|
|
+ //保存对话框是否记忆上次打开的目录
|
|
|
+ RestoreDirectory = true
|
|
|
+ })
|
|
|
+ {
|
|
|
+ // Show save file dialog box
|
|
|
+
|
|
|
+ //点了保存按钮进入
|
|
|
+ if (saveFileDialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
|
|
|
+ {
|
|
|
+ try
|
|
|
+ {
|
|
|
+ //获得文件路径
|
|
|
+ string localFilePath = saveFileDialog.FileName.ToString();
|
|
|
+
|
|
|
+ //获取文件内容
|
|
|
+ MiniExcel.SaveAs(localFilePath, ProjectItemList);
|
|
|
+ }
|
|
|
+ catch (Exception ex)
|
|
|
+ {
|
|
|
+ _logger.LogError(ex.ToString());
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ }
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 添加PLC变量
|
|
|
+ /// </summary>
|
|
|
+ /// <param name="obj"></param>
|
|
|
+ /// <exception cref="NotImplementedException"></exception>
|
|
|
+ private void Add(object obj)
|
|
|
+ {
|
|
|
+
|
|
|
+ //弹出详情对话框
|
|
|
+ _dialog.ShowDialog("AddOrEditProjectView", async callback =>
|
|
|
+ {
|
|
|
+ if (callback.Result == ButtonResult.OK)
|
|
|
+ {
|
|
|
+ BasProjectDto returnValue = callback.Parameters.GetValue<BasProjectDto>("ReturnValue");
|
|
|
+ if (returnValue != null)
|
|
|
+ {
|
|
|
+ returnValue.CreateTime = DateTime.Now;
|
|
|
+ returnValue.CreateBy = Appsession.UserName;
|
|
|
+ returnValue.UpdateTime = DateTime.Now;
|
|
|
+ returnValue.UpdateBy = Appsession.UserName;
|
|
|
+ var plcCon = _mapper.Map<BasProjectDto, bas_project>(returnValue);
|
|
|
+ var findPlc = allProjectList.FirstOrDefault(x => (x.ProjectName == returnValue.ProjectName) || (x.ProjectNo == returnValue.ProjectNo));
|
|
|
+ if (findPlc != null)
|
|
|
+ {
|
|
|
+ MessageBox.Show("已有此项目名称或编号,请更改!", "提示", MessageBoxButton.OKCancel, MessageBoxImage.Warning);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ bool isSucc = _iBasicProjectService.Add(plcCon);
|
|
|
+ if (isSucc)
|
|
|
+ {
|
|
|
+ //重新读取PLC
|
|
|
+ GetPprojectConfig();
|
|
|
+ _logger.LogInformation($"添加项目成功");
|
|
|
+ MessageBox.Show("添加项目成功!", "确认", MessageBoxButton.OK, MessageBoxImage.Information);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ });
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 获取所有项目
|
|
|
+ /// </summary>
|
|
|
+ private void GetPprojectConfig()
|
|
|
+ {
|
|
|
+ allProjectList.Clear();
|
|
|
+ conditionProject.Clear();
|
|
|
+ var projectlist = _iBasicProjectService.QueryList();
|
|
|
+ var allPlc = _mapper.Map<List<bas_project>, List<BasProjectDto>>(projectlist);
|
|
|
+ foreach (var plc in allPlc)
|
|
|
+ {
|
|
|
+ allProjectList.Add(plc);
|
|
|
+ conditionProject.Add(plc);
|
|
|
+ }
|
|
|
+ conditionProject = conditionProject.OrderBy(x => x.ProjectId).ToList();
|
|
|
+ Getpage();
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 获取页面
|
|
|
+ /// </summary>
|
|
|
+ private void Getpage()
|
|
|
+ {
|
|
|
+ CurrentPage = 1;
|
|
|
+ TotalCount = conditionProject.Count;
|
|
|
+ CurrentPageChanged();
|
|
|
+
|
|
|
+
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 页面变化
|
|
|
+ /// </summary>
|
|
|
+ private void CurrentPageChanged()
|
|
|
+ {
|
|
|
+
|
|
|
+ ProjectItemList.Clear();
|
|
|
+
|
|
|
+ foreach (var i in conditionProject.Skip((CurrentPage - 1) * CountPerPage).Take(CountPerPage))
|
|
|
+ {
|
|
|
+ ProjectItemList.Add(i);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ #endregion
|
|
|
+ #region 命令绑定
|
|
|
+
|
|
|
+ public DelegateCommand<object> QueryCommand { set; get; }
|
|
|
+ public DelegateCommand<object> AddCommand { set; get; }
|
|
|
+ public DelegateCommand<string> ExportCommand { set; get; }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 表格删除
|
|
|
+ /// </summary>
|
|
|
+ public DelegateCommand<Object> DeleteCommand { set; get; }
|
|
|
+ /// <summary>
|
|
|
+ /// 表格编辑按钮
|
|
|
+ /// </summary>
|
|
|
+ public DelegateCommand<Object> EditCommand { set; get; }
|
|
|
+ public DelegateCommand<object> ResetCommand { set; get; }
|
|
|
+ #endregion
|
|
|
+
|
|
|
+ #region 数据绑定
|
|
|
+ private ObservableCollection<BasProjectDto> projectItemList = new ObservableCollection<BasProjectDto>();
|
|
|
+ public ObservableCollection<BasProjectDto> ProjectItemList
|
|
|
+ {
|
|
|
+ get { return projectItemList; }
|
|
|
+ set { projectItemList = value; RaisePropertyChanged(); }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 项目编号
|
|
|
+ /// </summary>
|
|
|
+ private string projectNo;
|
|
|
+ public string ProjectNo
|
|
|
+ {
|
|
|
+ get { return projectNo; }
|
|
|
+ set { projectNo = value; RaisePropertyChanged(); }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 项目名称
|
|
|
+ /// </summary>
|
|
|
+ private string projectName;
|
|
|
+ public string ProjectName
|
|
|
+ {
|
|
|
+ get { return projectName; }
|
|
|
+ set { projectName = value; RaisePropertyChanged(); }
|
|
|
+ }
|
|
|
+
|
|
|
+ /// <summary>
|
|
|
+ /// 项目名称
|
|
|
+ /// </summary>
|
|
|
+ private string projectLeader;
|
|
|
+ public string ProjectLeader
|
|
|
+ {
|
|
|
+ get { return projectLeader; }
|
|
|
+ set { projectLeader = value; RaisePropertyChanged(); }
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 总条数
|
|
|
+ /// </summary>
|
|
|
+ private int totalCount;
|
|
|
+ public int TotalCount
|
|
|
+ {
|
|
|
+ get { return totalCount; }
|
|
|
+ set { totalCount = value; RaisePropertyChanged(); CurrentPageChanged(); }
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 每页数量
|
|
|
+ /// </summary>
|
|
|
+ private int countPerPage = 15;
|
|
|
+ public int CountPerPage
|
|
|
+ {
|
|
|
+ get { return countPerPage; }
|
|
|
+ set { countPerPage = value; RaisePropertyChanged(); CurrentPageChanged(); }
|
|
|
+ }
|
|
|
+ /// <summary>
|
|
|
+ /// 单前页
|
|
|
+ /// </summary>
|
|
|
+ private int currentPage = 1;
|
|
|
+ public int CurrentPage
|
|
|
+ {
|
|
|
+ get { return currentPage; }
|
|
|
+ set { currentPage = value; RaisePropertyChanged(); CurrentPageChanged(); }
|
|
|
+ }
|
|
|
+
|
|
|
+ #endregion
|
|
|
}
|
|
|
}
|
|
|
+
|