瀏覽代碼

新增检测机构

LT069288 3 月之前
父節點
當前提交
714b355eb7

+ 2 - 0
UniformMaterialManagementSystem/App.xaml.cs

@@ -61,6 +61,7 @@ namespace UniformMaterialManagementSystem
             services.AddKeyedTransient<UserControl, DeliveryReceiptControl>("DeliveryReceiptControl");
             services.AddKeyedTransient<UserControl, InspectionReportPage>("InspectionReportPage");
             services.AddKeyedTransient<UserControl, SampleRegistrationPage>("SampleRegistrationPage");
+            services.AddKeyedTransient<UserControl, InspectionOrganizationPage>("InspectionOrganizationPage");
 
             // ViewModel 注册
             services.AddSingleton<MainWindowViewModel>();
@@ -81,6 +82,7 @@ namespace UniformMaterialManagementSystem
             services.AddTransient<DeliveryReceiptViewModel>();
             services.AddTransient<InspectionReportPageViewModel>();
             services.AddTransient<SampleRegistrationPageViewModel>();
+            services.AddTransient<InspectionOrganizationPageViewModel>();
 
             // Service 注册
             services.AddScoped(typeof(IDataBaseService<>), typeof(DataBaseService<>));

+ 150 - 0
UniformMaterialManagementSystem/ViewModels/InspectionOrganizationPageViewModel.cs

@@ -0,0 +1,150 @@
+using CommunityToolkit.Mvvm.ComponentModel;
+using CommunityToolkit.Mvvm.Input;
+using Microsoft.EntityFrameworkCore;
+using System.Collections.ObjectModel;
+using System.Text;
+using System.Windows;
+using UniformMaterialManagementSystem.Entities;
+using UniformMaterialManagementSystem.Services;
+
+namespace UniformMaterialManagementSystem.ViewModels
+{
+    public partial class InspectionOrganizationPageViewModel : ObservableObject
+    {
+        [ObservableProperty]
+        private InspectionOrganization? _selectedInspectionOrganization;
+
+        [ObservableProperty]
+        private ObservableCollection<InspectionOrganization> _inspectionOrganizations = [];
+
+        private readonly IDataBaseService<InspectionOrganization> _service;
+        public InspectionOrganizationPageViewModel(IDataBaseService<InspectionOrganization> service)
+        {
+            _service = service;
+
+            var inspectionOrganizations = _service.GetAll().ToList();
+            foreach (var organization in inspectionOrganizations)
+            {
+                InspectionOrganizations.Add(organization);
+            }
+        }
+
+        /// <summary>
+        /// 新增
+        /// </summary>
+        [RelayCommand]
+        private void AddInspectionOrganization()
+        {
+            InspectionOrganization organization = new InspectionOrganization();
+            organization.IsEnabled = true;
+            InspectionOrganizations.Add(organization);
+
+            //_service.Insert(organization);
+        }
+
+        /// <summary>
+        /// 删除
+        /// </summary>
+        [RelayCommand]
+        private void RemoveInspectionOrganization()
+        {
+            if (SelectedInspectionOrganization == null)
+            {
+                MessageBox.Show("请先选中一行,再执行删除操作", "提示", MessageBoxButton.OK, MessageBoxImage.Information);
+                return;
+            }
+
+            var state =_service.Entry(SelectedInspectionOrganization);
+            if (state == EntityState.Detached)
+            {
+                InspectionOrganizations.Remove(SelectedInspectionOrganization);
+                return;
+            }
+
+            var result = MessageBox.Show($"确定删除检测机构编号为:{SelectedInspectionOrganization.OrderNo}的检测机构吗?",
+                "提示", MessageBoxButton.OKCancel, MessageBoxImage.Question);
+            if (result != MessageBoxResult.OK) return;
+
+            //var state = _service.Entry(SelectedInspectionOrganization); //Added
+            _service.Delete(SelectedInspectionOrganization);
+            _service.SaveChanges();
+
+            InspectionOrganizations.Remove(SelectedInspectionOrganization);
+        }
+
+        /// <summary>
+        /// 保存
+        /// </summary>
+        [RelayCommand]
+        private void SaveInspectionOrganization()
+        {
+            //自定义校验
+            var dictionary = CustomValidate();
+            if (dictionary.ContainsKey("result") && !string.IsNullOrEmpty(dictionary["result"]))
+            {
+                MessageBox.Show(dictionary["result"], "提示", MessageBoxButton.OK, MessageBoxImage.Warning);
+                return;
+            }
+
+            foreach (var organization in InspectionOrganizations)
+            {
+                var state = _service.Entry(organization);
+                switch (state)
+                {
+                    case EntityState.Detached:
+                        _service.Insert(organization);
+                        break;
+                    case EntityState.Modified:
+                        _service.Update(organization);
+                        break;
+                }
+            }
+
+            var result = _service.SaveChanges();
+            if (result)
+            {
+                MessageBox.Show("保存成功!");
+            }
+        }
+
+        private Dictionary<string, string> CustomValidate()
+        {
+            Dictionary<string, string> dic = new Dictionary<string, string>();
+            StringBuilder sb = new StringBuilder();
+
+            for (int i = 0; i < InspectionOrganizations.Count; i++)
+            {
+                var organization = InspectionOrganizations[i];
+                var state = _service.Entry(organization);
+                if (state == EntityState.Unchanged) continue;
+                if (organization.OrderNo <= 0)
+                {
+                    sb.AppendLine($"第{i + 1}行,编号不允许小于等于0!");
+                }
+                if (string.IsNullOrEmpty(organization.Name))
+                {
+                    sb.AppendLine($"第{i + 1}行,检测机构名称不允许为空!");
+                }
+                if (string.IsNullOrEmpty(organization.Name))
+                {
+                    sb.AppendLine($"第{i + 1}行,地址不允许为空!");
+                }
+                if (string.IsNullOrEmpty(organization.Contacts))
+                {
+                    sb.AppendLine($"第{i + 1}行,联系人不允许为空!");
+                }
+                if (string.IsNullOrEmpty(organization.Telephone))
+                {
+                    sb.AppendLine($"第{i + 1}行,联系电话不允许为空!");
+                }
+            }
+            List<string> repeatName = InspectionOrganizations.GroupBy(g => string.Format("{0}", g.Name)).Where(w => w.Count() > 1).Select(s => s.Key).ToList();
+            if (repeatName.Count > 0)
+            {
+                sb.AppendLine($"检测机构名称{repeatName[0]}重复,请检查!");
+            }
+            dic.Add("result",sb.ToString());
+            return dic;
+        }
+    }
+}

+ 267 - 0
UniformMaterialManagementSystem/Views/InspectionOrganizationPage.xaml

@@ -0,0 +1,267 @@
+<UserControl
+    x:Class="UniformMaterialManagementSystem.Views.InspectionOrganizationPage"
+    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
+    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
+    xmlns:behaviors="clr-namespace:UniformMaterialManagementSystem.Behaviors"
+    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
+    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
+    xmlns:utils="clr-namespace:UniformMaterialManagementSystem.Utils"
+    d:DesignHeight="450"
+    d:DesignWidth="800"
+    mc:Ignorable="d">
+    <UserControl.Resources>
+        <ControlTemplate x:Key="CustomColumnHeaderTemplate" TargetType="DataGridColumnHeader">
+            <Border BorderBrush="LightSlateGray" BorderThickness="0,0,1,1">
+                <Grid>
+                    <Grid.ColumnDefinitions>
+                        <ColumnDefinition />
+                        <ColumnDefinition Width="Auto" />
+                    </Grid.ColumnDefinitions>
+                    <ContentPresenter
+                        Grid.Column="0"
+                        HorizontalAlignment="Center"
+                        VerticalAlignment="Center" />
+                    <Path
+                        x:Name="SortArrow"
+                        Grid.Column="1"
+                        Margin="0,0,5,0"
+                        VerticalAlignment="Center"
+                        Data="M 0 0 L 10 0 L 5 5 Z"
+                        Fill="Gray"
+                        Visibility="Collapsed" />
+                </Grid>
+            </Border>
+            <ControlTemplate.Triggers>
+                <Trigger Property="SortDirection" Value="Ascending">
+                    <Setter TargetName="SortArrow" Property="Data" Value="M 0 5 L 10 5 L 5 0 Z" />
+                    <Setter TargetName="SortArrow" Property="Visibility" Value="Visible" />
+                </Trigger>
+                <Trigger Property="SortDirection" Value="Descending">
+                    <Setter TargetName="SortArrow" Property="Data" Value="M 0 0 L 10 0 L 5 5 Z" />
+                    <Setter TargetName="SortArrow" Property="Visibility" Value="Visible" />
+                </Trigger>
+            </ControlTemplate.Triggers>
+        </ControlTemplate>
+
+        <Style x:Key="CustomColumnHeaderStyle" TargetType="DataGridColumnHeader">
+            <Setter Property="Background" Value="White" />
+            <Setter Property="Foreground" Value="Black" />
+            <Setter Property="FontWeight" Value="Bold" />
+            <Setter Property="HorizontalContentAlignment" Value="Center" />
+            <Setter Property="VerticalContentAlignment" Value="Center" />
+            <Setter Property="Height" Value="40" />
+            <Setter Property="Template" Value="{StaticResource CustomColumnHeaderTemplate}" />
+
+        </Style>
+
+        <Style x:Key="CustomRowHeaderStyle" TargetType="DataGridRowHeader">
+            <Setter Property="Background" Value="White" />
+            <Setter Property="Foreground" Value="Black" />
+            <Setter Property="BorderThickness" Value="0,0,1,1" />
+            <Setter Property="BorderBrush" Value="LightSlateGray" />
+            <Setter Property="Padding" Value="10,0,10,0" />
+        </Style>
+
+        <Style x:Key="CustomRowStyle" TargetType="DataGridRow">
+            <Setter Property="Background" Value="White" />
+            <Setter Property="FontSize" Value="14" />
+            <Setter Property="Height" Value="30" />
+
+            <Style.Triggers>
+                <Trigger Property="AlternationIndex" Value="1">
+                    <Setter Property="Background" Value="WhiteSmoke" />
+                </Trigger>
+                <Trigger Property="IsSelected" Value="True">
+                    <Setter Property="Background" Value="LightBlue" />
+                </Trigger>
+            </Style.Triggers>
+        </Style>
+        <Style
+            x:Key="CustomCellStyle"
+            BasedOn="{StaticResource {x:Type DataGridCell}}"
+            TargetType="DataGridCell">
+            <Setter Property="Background" Value="White" />
+            <Setter Property="HorizontalContentAlignment" Value="Center" />
+            <Setter Property="VerticalContentAlignment" Value="Center" />
+            <Setter Property="Padding" Value="10" />
+            <Style.Triggers>
+                <Trigger Property="IsSelected" Value="True">
+                    <Setter Property="Background" Value="LightBlue" />
+                    <Setter Property="Foreground" Value="Black" />
+                </Trigger>
+            </Style.Triggers>
+        </Style>
+    </UserControl.Resources>
+
+    <Grid>
+        <Grid.RowDefinitions>
+            <RowDefinition Height="Auto" />
+            <RowDefinition Height="*" />
+        </Grid.RowDefinitions>
+        <!--  菜单项  -->
+        <Border
+            Grid.Row="0"
+            BorderBrush="Gray"
+            BorderThickness="1">
+            <ToolBarPanel>
+                <ToolBar Background="White" ToolBarTray.IsLocked="True">
+                    <Button HorizontalContentAlignment="Center" Command="{Binding AddInspectionOrganizationCommand}">
+                        <Button.Template>
+                            <ControlTemplate>
+                                <Border
+                                    Width="40"
+                                    Background="{TemplateBinding Background}"
+                                    CornerRadius="5">
+                                    <StackPanel HorizontalAlignment="Center" Orientation="Vertical">
+                                        <TextBlock
+                                            HorizontalAlignment="Center"
+                                            FontFamily="{StaticResource FluentSystemIconsRegular}"
+                                            FontSize="20"
+                                            Text="{x:Static utils:RegularFontUtil.Add_Circle_32}" />
+                                        <TextBlock Text="新增" />
+                                    </StackPanel>
+                                </Border>
+                                <ControlTemplate.Triggers>
+                                    <Trigger Property="IsMouseOver" Value="True">
+                                        <Setter Property="Background" Value="LightGray" />
+                                    </Trigger>
+                                </ControlTemplate.Triggers>
+                            </ControlTemplate>
+                        </Button.Template>
+                    </Button>
+
+                    <Button Command="{Binding RemoveInspectionOrganizationCommand}">
+                        <Button.Template>
+                            <ControlTemplate>
+                                <Border
+                                    Width="40"
+                                    Background="{TemplateBinding Background}"
+                                    CornerRadius="5">
+                                    <StackPanel HorizontalAlignment="Center" Orientation="Vertical">
+                                        <TextBlock
+                                            HorizontalAlignment="Center"
+                                            FontFamily="{StaticResource FluentSystemIconsRegular}"
+                                            FontSize="20"
+                                            Text="{x:Static utils:RegularFontUtil.Delete_48}" />
+                                        <TextBlock Text="删除" />
+                                    </StackPanel>
+                                </Border>
+                                <ControlTemplate.Triggers>
+                                    <Trigger Property="IsMouseOver" Value="True">
+                                        <Setter Property="Background" Value="LightGray" />
+                                    </Trigger>
+                                </ControlTemplate.Triggers>
+                            </ControlTemplate>
+                        </Button.Template>
+                    </Button>
+                    <Button Command="{Binding SaveInspectionOrganizationCommand}">
+                        <Button.Template>
+                            <ControlTemplate>
+                                <Border
+                                    Width="40"
+                                    Background="{TemplateBinding Background}"
+                                    CornerRadius="5">
+                                    <StackPanel HorizontalAlignment="Center" Orientation="Vertical">
+                                        <TextBlock
+                                            HorizontalAlignment="Center"
+                                            FontFamily="{StaticResource FluentSystemIconsRegular}"
+                                            FontSize="20"
+                                            Text="{x:Static utils:RegularFontUtil.Save_32}" />
+                                        <TextBlock Text="保存" />
+                                    </StackPanel>
+                                </Border>
+                                <ControlTemplate.Triggers>
+                                    <Trigger Property="IsMouseOver" Value="True">
+                                        <Setter Property="Background" Value="LightGray" />
+                                    </Trigger>
+                                </ControlTemplate.Triggers>
+                            </ControlTemplate>
+                        </Button.Template>
+                    </Button>
+                </ToolBar>
+            </ToolBarPanel>
+        </Border>
+
+        <!--  表格DataGird  -->
+        <DataGrid
+            x:Name="DataGridMain"
+            Grid.Row="1"
+            behaviors:DataGridBehavior.RowNumbers="True"
+            AutoGenerateColumns="False"
+            Background="White"
+            CanUserAddRows="False"
+            CanUserReorderColumns="True"
+            CanUserResizeColumns="True"
+            CellStyle="{StaticResource CustomCellStyle}"
+            ColumnHeaderStyle="{StaticResource CustomColumnHeaderStyle}"
+            HeadersVisibility="All"
+            HorizontalGridLinesBrush="LightSlateGray"
+            ItemsSource="{Binding InspectionOrganizations, Mode=TwoWay}"
+            RowHeaderStyle="{StaticResource CustomRowHeaderStyle}"
+            RowStyle="{StaticResource CustomRowStyle}"
+            SelectedItem="{Binding SelectedInspectionOrganization, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
+            SelectionMode="Single"
+            SelectionUnit="FullRow"
+            VerticalGridLinesBrush="LightSlateGray">
+
+            <DataGrid.Resources>
+                <!--  非编辑模式下文本居中的样式  -->
+                <Style x:Key="TextColumnElementStyle" TargetType="TextBlock">
+                    <Setter Property="HorizontalAlignment" Value="Center" />
+                    <Setter Property="VerticalAlignment" Value="Center" />
+                    <Setter Property="TextAlignment" Value="Center" />
+                </Style>
+
+                <!--  编辑模式下文本居中的样式  -->
+                <Style x:Key="TextColumnEditingElementStyle" TargetType="TextBox">
+                    <Setter Property="HorizontalAlignment" Value="Stretch" />
+                    <Setter Property="VerticalAlignment" Value="Stretch" />
+                    <Setter Property="VerticalContentAlignment" Value="Center" />
+                </Style>
+
+                <Style x:Key="CheckBoxColumnElementStyle" TargetType="CheckBox">
+                    <Setter Property="HorizontalAlignment" Value="Center" />
+                    <Setter Property="VerticalAlignment" Value="Center" />
+                </Style>
+            </DataGrid.Resources>
+            <DataGrid.Columns>
+                <DataGridTextColumn
+                    Width="50"
+                    Binding="{Binding OrderNo, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
+                    EditingElementStyle="{StaticResource TextColumnEditingElementStyle}"
+                    ElementStyle="{StaticResource TextColumnElementStyle}"
+                    Header="编号*" />
+                <DataGridTextColumn
+                    Width="300"
+                    Binding="{Binding Name, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
+                    EditingElementStyle="{StaticResource TextColumnEditingElementStyle}"
+                    ElementStyle="{StaticResource TextColumnElementStyle}"
+                    Header="检测机构名称*" />
+                <DataGridTextColumn
+                    Width="500"
+                    Binding="{Binding Address, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
+                    EditingElementStyle="{StaticResource TextColumnEditingElementStyle}"
+                    ElementStyle="{StaticResource TextColumnElementStyle}"
+                    Header="地址*" />
+                <DataGridTextColumn
+                    Width="150"
+                    Binding="{Binding Contacts, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
+                    EditingElementStyle="{StaticResource TextColumnEditingElementStyle}"
+                    ElementStyle="{StaticResource TextColumnElementStyle}"
+                    Header="联系人*" />
+                <DataGridTextColumn
+                    Width="150"
+                    Binding="{Binding Telephone, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
+                    EditingElementStyle="{StaticResource TextColumnEditingElementStyle}"
+                    ElementStyle="{StaticResource TextColumnElementStyle}"
+                    Header="联系电话*" />
+                <DataGridCheckBoxColumn
+                    Width="100"
+                    Binding="{Binding IsEnabled, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}"
+                    ElementStyle="{StaticResource CheckBoxColumnElementStyle}"
+                    Header="使用状态" />
+
+            </DataGrid.Columns>
+        </DataGrid>
+    </Grid>
+</UserControl>

+ 32 - 0
UniformMaterialManagementSystem/Views/InspectionOrganizationPage.xaml.cs

@@ -0,0 +1,32 @@
+using Microsoft.Extensions.DependencyInjection;
+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;
+using UniformMaterialManagementSystem.ViewModels;
+
+namespace UniformMaterialManagementSystem.Views
+{
+    /// <summary>
+    /// InspectionOrganizationPage.xaml 的交互逻辑
+    /// </summary>
+    public partial class InspectionOrganizationPage : UserControl
+    {
+        public InspectionOrganizationPage()
+        {
+            InitializeComponent();
+
+            this.DataContext = App.Current.Services.GetService<InspectionOrganizationPageViewModel>();
+        }
+    }
+}

+ 7 - 17
UniformMaterialManagementSystem/Views/InspectionReportPage.xaml

@@ -7,7 +7,6 @@
     xmlns:control="http://FilterDataGrid.Control.com/2024"
     xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
     xmlns:filterDataGrid="http://FilterDataGrid.Control.com/2024"
-    xmlns:local="clr-namespace:UniformMaterialManagementSystem.Views"
     xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
     xmlns:utils="clr-namespace:UniformMaterialManagementSystem.Utils"
     d:DesignHeight="450"
@@ -104,6 +103,7 @@
             <Setter Property="Foreground" Value="Black" />
             <Setter Property="BorderThickness" Value="0,0,1,1" />
             <Setter Property="BorderBrush" Value="LightSlateGray" />
+            <Setter Property="Padding" Value="5,0,5,0" />
         </Style>
 
         <Style x:Key="CustomRowStyle" TargetType="DataGridRow">
@@ -226,7 +226,10 @@
                                         </ControlTemplate>
                                     </Button.Template>
                                 </Button>
-                                <Button Command="{Binding SaveInspectionReportCommand}" x:Name="SaveButton" Click="SaveButton_OnClick">
+                                <Button
+                                    x:Name="SaveButton"
+                                    Click="SaveButton_OnClick"
+                                    Command="{Binding SaveInspectionReportCommand}">
                                     <Button.Template>
                                         <ControlTemplate>
                                             <Border
@@ -289,6 +292,8 @@
                 Background="White"
                 CanUserAddRows="False"
                 CanUserReorderColumns="True"
+                CanUserResizeColumns="True"
+                CanUserResizeRows="False"
                 CellStyle="{StaticResource CustomCellStyle}"
                 ColumnHeaderStyle="{StaticResource CustomColumnHeaderStyle}"
                 FilterLanguage="SimplifiedChinese"
@@ -324,12 +329,6 @@
                         <Setter Property="VerticalAlignment" Value="Stretch" />
                         <Setter Property="VerticalContentAlignment" Value="Center" />
                     </Style>
-
-                    <Style x:Key="CheckBoxColumnElementStyle" TargetType="CheckBox">
-                        <Setter Property="HorizontalAlignment" Value="Center" />
-                        <Setter Property="VerticalAlignment" Value="Center" />
-                        <Setter Property="IsEnabled" Value="False" />
-                    </Style>
                 </control:FilterDataGrid.Resources>
 
                 <control:FilterDataGrid.Columns>
@@ -828,12 +827,6 @@
                                     <Setter Property="VerticalAlignment" Value="Stretch" />
                                     <Setter Property="VerticalContentAlignment" Value="Center" />
                                 </Style>
-
-                                <Style x:Key="CheckBoxColumnElementStyle" TargetType="CheckBox">
-                                    <Setter Property="HorizontalAlignment" Value="Center" />
-                                    <Setter Property="VerticalAlignment" Value="Center" />
-                                    <Setter Property="IsEnabled" Value="False" />
-                                </Style>
                             </DataGrid.Resources>
                             <DataGrid.Columns>
                                 <DataGridTemplateColumn Width="100" Header="检验组">
@@ -922,9 +915,6 @@
                         Text="{Binding SelectedInspectionReport.EditUser, Mode=TwoWay, UpdateSourceTrigger=PropertyChanged}" />
                 </Grid>
             </ScrollViewer>
-
-
         </Grid>
-
     </Grid>
 </UserControl>