Просмотр исходного кода

Merge branch 'master' of https://git.xxb.lttc.cn/wangbc/UniformMaterialManagementSystem

宝臣 王 3 месяцев назад
Родитель
Сommit
3bafca1f9f
1 измененных файлов с 39 добавлено и 2 удалено
  1. 39 2
      UniformMaterialManagementSystem/ViewModels/UsersViewModel.cs

+ 39 - 2
UniformMaterialManagementSystem/ViewModels/UsersViewModel.cs

@@ -1,9 +1,11 @@
 using System;
+using System.Collections;
 using System.Collections.Generic;
 using System.Collections.ObjectModel;
 using System.Data;
 using System.Linq;
 using System.Text;
+using System.Text.RegularExpressions;
 using System.Threading.Tasks;
 using System.Windows;
 using System.Windows.Controls;
@@ -96,7 +98,7 @@ namespace UniformMaterialManagementSystem.ViewModels
         private void Loaded(DataGrid dataGridUser)
         {
             // 加载时设置列标题、可读性
-            
+
             DataGridTextColumn? colIdNumber = dataGridUser.Columns.Where(x => x.Header.ToString()!.StartsWith("工号")).FirstOrDefault() as DataGridTextColumn;
             DataGridComboBoxColumn? colSupUnit = dataGridUser.Columns.Where(x => x.Header.ToString()!.StartsWith("监管单位")).FirstOrDefault() as DataGridComboBoxColumn;
             DataGridComboBoxColumn? colCompany = dataGridUser.Columns.Where(x => x.Header.ToString()!.StartsWith("生产企业")).FirstOrDefault() as DataGridComboBoxColumn;
@@ -332,7 +334,6 @@ namespace UniformMaterialManagementSystem.ViewModels
                 }
             }
 
-            //todo 保存时生产企业能不能为空
             // 检查新增行的必录项是否为空
             Dictionary<int, int> nullIndexes = new Dictionary<int, int>();
             StringBuilder nullMsg = new StringBuilder();
@@ -438,6 +439,42 @@ namespace UniformMaterialManagementSystem.ViewModels
                 }
             }
 
+            // 检查新增行的工号和登录名是否只有数字和字母
+            Regex regex = new Regex(@"^[0-9a-zA-Z]*$");
+            var illegalRows = UserModels.Where(x => x.IsNewRow && (!regex.IsMatch(x.IdNumber) || !regex.IsMatch(x.LoginName)));
+            if (illegalRows.Any())
+            {
+                StringBuilder illegalMsg = new StringBuilder();
+                Dictionary<int, int> illegalIndexes = new Dictionary<int, int>();
+                int firstIllegalRowIndex = UserModels.Count;
+                foreach (var illegalRow in illegalRows)
+                {
+                    int rowIndex = UserModels.IndexOf(illegalRow);
+                    int colIndex = -1;
+                    if (!regex.IsMatch(illegalRow.LoginName))
+                    {
+                        illegalMsg.Append($"\n第 {rowIndex} 行的登录名 '{illegalRow.LoginName}'");
+                        colIndex = 1;
+                    }
+                    if (!regex.IsMatch(illegalRow.IdNumber))
+                    {
+                        illegalMsg.Append($"\n第 {rowIndex} 行的工号 '{illegalRow.IdNumber}'");
+                        colIndex = 0;
+                    }
+                    illegalIndexes.Add(rowIndex, colIndex);
+
+                    firstIllegalRowIndex = firstIllegalRowIndex < rowIndex ? firstIllegalRowIndex : rowIndex;
+                }
+
+                // 提示用户
+                string idColCaption = _isCompanyUser ? "工号" : "工号/证件号码";
+                MessageBox.Show($"{idColCaption}、登录名列只接收数字或字母,以下数据不合法,请修改后再次保存:" + illegalMsg.ToString(), "错误", MessageBoxButton.OK, MessageBoxImage.Error);
+
+                // 定位到第一个非法的数据
+                SetCellEditing(dataGridUser, firstIllegalRowIndex, illegalIndexes[firstIllegalRowIndex]);
+                return;
+            }
+
             // 保存时检查前三列是否存在重复项
             var repeatIdNumbers = UserModels.GroupBy(x => x.IdNumber).Where(g => g.Count() > 1).Select(g => g.Key);
             var repeatLoginNames = UserModels.GroupBy(x => x.LoginName).Where(g => g.Count() > 1).Select(g => g.Key);