Prechádzať zdrojové kódy

在测试操作页面增加项目选择界面

user_zyx 1 rok pred
rodič
commit
2597d9e86a

+ 1 - 0
BlankApp1/BlankApp1/App.xaml.cs

@@ -66,6 +66,7 @@ namespace BlankApp1
             containerRegistry.RegisterForNavigation<DeviceKindView, DeviceKindViewModel>();
             containerRegistry.RegisterForNavigation<DeviceView, DeviceViewModel>();
             containerRegistry.RegisterForNavigation<ProjectView, ProjectViewModel>();
+            containerRegistry.RegisterForNavigation<ProjectSelectView, ProjectSelectViewModel>();
             containerRegistry.RegisterForNavigation<DeviceTestView, DeviceTestViewModel>();
             containerRegistry.RegisterForNavigation<ResultQueryView, ResultQueryViewModel>();
             containerRegistry.RegisterForNavigation<RetryTestView, RetryTestViewModel>();

+ 64 - 0
BlankApp1/BlankApp1/Controls/ProjectPanel.xaml

@@ -0,0 +1,64 @@
+<UserControl x:Class="PLCTool.Controls.ProjectPanel"
+             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
+             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
+             xmlns:local="clr-namespace:PLCTool.Controls"
+             xmlns:sys="clr-namespace:System;assembly=mscorlib"
+             mc:Ignorable="d"
+             Margin="10 10 0 0">
+    <UserControl.Resources>
+        <sys:Double x:Key="MainHeight">300</sys:Double>
+        <sys:Double x:Key="MainWidth">400</sys:Double>
+        <sys:Double x:Key="TitleHeight">100</sys:Double>
+    </UserControl.Resources>
+    <Button BorderThickness="1" BorderBrush="{DynamicResource WD.PrimaryNormalSolidColorBrush}" 
+            Width="{StaticResource MainWidth}" Height="{StaticResource MainHeight}"
+            Padding="2"
+            Click="Button_Click">
+        <Grid Width="{StaticResource MainWidth}" Height="{StaticResource MainHeight}">
+            <Grid.RowDefinitions>
+                <RowDefinition Height="auto"/>
+                <RowDefinition Height="*"/>
+            </Grid.RowDefinitions>
+            <Grid>
+
+                <Label FontSize="50" FontWeight="Bold" 
+                       Background="{DynamicResource WD.PrimaryNormalSolidColorBrush}" Foreground="White" 
+                       Content="{Binding ProjectName,RelativeSource={RelativeSource AncestorType=local:ProjectPanel}}" 
+                       Height="{StaticResource TitleHeight}"
+                       Width="auto" HorizontalAlignment="Stretch"/>
+            </Grid>
+            <Grid Grid.Row="1">
+                <Grid.ColumnDefinitions>
+                    <ColumnDefinition Width="2*"/>
+                    <ColumnDefinition Width="3*"/>
+                </Grid.ColumnDefinitions>
+                <Grid.RowDefinitions>
+                    <RowDefinition/>
+                    <RowDefinition/>
+                    <RowDefinition/>
+                    <RowDefinition/>
+                </Grid.RowDefinitions>
+                <Grid.Resources>
+                    <Style TargetType="Label">
+                        <Setter Property="FontSize" Value="20"/>
+                        <Setter Property="Width" Value="auto"/>
+                        <Setter Property="HorizontalAlignment" Value="Right"/>
+                        <Setter Property="VerticalAlignment" Value="Center"/>
+                        <Setter Property="Margin" Value="10 0 0 0"/>
+                    </Style>
+                </Grid.Resources>
+                <Label Grid.Row="0" Content="项目代号:"/>
+                <Label Grid.Row="1" Content="负责人:"/>
+                <Label Grid.Row="2" Content="设备数量:"/>
+                <Label Grid.Row="3" Content="待测设备数量:"/>
+
+                <Label Grid.Row="0" Grid.Column="1" Content="{Binding ProjectCode,RelativeSource={RelativeSource AncestorType=local:ProjectPanel}}" HorizontalAlignment="Left"/>
+                <Label Grid.Row="1" Grid.Column="1" Content="负责人" HorizontalAlignment="Left"/>
+                <Label Grid.Row="2" Grid.Column="1" Content="设备数量" HorizontalAlignment="Left"/>
+                <Label Grid.Row="3" Grid.Column="1" Content="待测设备数量" HorizontalAlignment="Left"/>
+            </Grid>
+        </Grid>
+    </Button>
+</UserControl>

+ 59 - 0
BlankApp1/BlankApp1/Controls/ProjectPanel.xaml.cs

@@ -0,0 +1,59 @@
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Data;
+using System.Windows.Documents;
+using System.Windows.Input;
+using System.Windows.Media;
+using System.Windows.Media.Imaging;
+using System.Windows.Navigation;
+using System.Windows.Shapes;
+
+namespace PLCTool.Controls
+{
+    /// <summary>
+    /// ProjectPanel.xaml 的交互逻辑
+    /// </summary>
+    public partial class ProjectPanel : UserControl
+    {
+
+
+        public string ProjectName
+        {
+            get { return (string)GetValue(ProjectNameProperty); }
+            set { SetValue(ProjectNameProperty, value); }
+        }
+
+        // Using a DependencyProperty as the backing store for ProjectName.  This enables animation, styling, binding, etc...
+        public static readonly DependencyProperty ProjectNameProperty =
+            DependencyProperty.Register("ProjectName", typeof(string), typeof(ProjectPanel), new PropertyMetadata("Null"));
+
+
+
+        public string ProjectCode
+        {
+            get { return (string)GetValue(ProjectCodeProperty); }
+            set { SetValue(ProjectCodeProperty, value); }
+        }
+
+        // Using a DependencyProperty as the backing store for ProjectCode.  This enables animation, styling, binding, etc...
+        public static readonly DependencyProperty ProjectCodeProperty =
+            DependencyProperty.Register("ProjectCode", typeof(string), typeof(ProjectPanel), new PropertyMetadata(""));
+
+        public event EventHandler? Click;
+
+        public ProjectPanel()
+        {
+            InitializeComponent();
+        }
+
+        private void Button_Click(object sender, RoutedEventArgs e)
+        {
+            Click?.Invoke(this, e);
+        }
+    }
+}

+ 381 - 0
BlankApp1/BlankApp1/ViewModels/BasicConfigViewModel/ProjectSelectViewModel.cs

@@ -0,0 +1,381 @@
+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
+{
+    public class ProjectSelectViewModel : 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 ProjectSelectViewModel(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;
+            StartTime = string.Empty;
+            EndTime = 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)
+        {
+            if ((!string.IsNullOrEmpty(StartTime))&& (!string.IsNullOrEmpty(EndTime)))
+            {
+                if (Convert.ToDateTime(StartTime) > Convert.ToDateTime(EndTime))
+                {
+                    MessageBox.Show("起始时间大于结束时间,请重新输入", "确认", MessageBoxButton.OK, MessageBoxImage.Warning);
+                    return;
+                }
+              
+            }
+           
+            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))
+                                 && (EndTime == string.Empty ? true : (a.CreateTime < Convert.ToDateTime(EndTime)) && (Convert.ToDateTime(StartTime) < a.CreateTime))
+                                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 数据绑定
+
+
+        /// <summary>
+        /// 开始时间
+        /// </summary>
+        private string startTime=DateTime.Now.AddDays(-1).ToString();
+        public string StartTime
+        {
+            get { return startTime; }
+            set { startTime = value; RaisePropertyChanged(); }
+        }
+        private string endTime = DateTime.Now.ToString();
+        public string EndTime
+        {
+            get { return endTime; }
+            set { endTime = value; RaisePropertyChanged(); }
+        }
+        
+        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
+    }
+}
+

+ 13 - 0
BlankApp1/BlankApp1/Views/BasicConfigView/ProjectSelectView.xaml

@@ -0,0 +1,13 @@
+<UserControl x:Class="PLCTool.Views.BasicConfigView.ProjectSelectView"
+             xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+             xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+             xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" 
+             xmlns:d="http://schemas.microsoft.com/expression/blend/2008" 
+             xmlns:local="clr-namespace:PLCTool.Views.BasicConfigView" xmlns:controls="clr-namespace:PLCTool.Controls"
+             mc:Ignorable="d" 
+             d:DesignHeight="450" d:DesignWidth="800">
+    <Grid>
+        <WrapPanel x:Name="WrapPanel_Project" >
+        </WrapPanel>
+    </Grid>
+</UserControl>

+ 46 - 0
BlankApp1/BlankApp1/Views/BasicConfigView/ProjectSelectView.xaml.cs

@@ -0,0 +1,46 @@
+using PLCTool.Controls;
+using System;
+using System.Collections.Generic;
+using System.Linq;
+using System.Text;
+using System.Threading.Tasks;
+using System.Windows;
+using System.Windows.Controls;
+using System.Windows.Data;
+using System.Windows.Documents;
+using System.Windows.Input;
+using System.Windows.Media;
+using System.Windows.Media.Imaging;
+using System.Windows.Navigation;
+using System.Windows.Shapes;
+
+namespace PLCTool.Views.BasicConfigView
+{
+    /// <summary>
+    /// ProjectSelectView.xaml 的交互逻辑
+    /// </summary>
+    public partial class ProjectSelectView : UserControl
+    {
+        public ProjectSelectView()
+        {
+            InitializeComponent();
+
+            for (int i = 0; i < 9; i++)
+            {
+                ProjectPanel projectPanel = new() { 
+                    ProjectName = "测试"+i
+                };
+                projectPanel.Click += ProjectPanel_Click;
+                WrapPanel_Project.Children.Add(projectPanel);
+            }
+        }
+
+        private void ProjectPanel_Click(object sender, EventArgs e)
+        {
+            if (sender is ProjectPanel pp)
+            {
+                MessageBox.Show(pp.ProjectName);
+            }
+        }
+    }
+}