using System; using System.Collections.Generic; using System.Data; using System.Globalization; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace YLErp.Helpers { public class DataRowReaderHelper { DataRow _row; readonly Dictionary _colMap; public DataRowReaderHelper(DataTable table) { var colCount = table.Columns.Count; _colMap = new Dictionary(colCount, StringComparer.OrdinalIgnoreCase); var row1 = table.Rows[0]; var preCol1 = string.Empty; for (var index = 0; index < colCount; index++) { var col1 = row1[index]?.ToString()?.Trim(); if (!string.IsNullOrWhiteSpace(col1)) { preCol1 = col1; } _colMap[preCol1] = index; } } public bool ExistFieldName(string fieldName) { return _colMap.ContainsKey(fieldName); } public int RowIndex { get; private set; } /// /// 设置datarow /// public void SetDataRow(DataRow row, int rowIndex) { _row = row; RowIndex = rowIndex; } public string GetString(string fieldName, bool required = false) { var str = _colMap.TryGetValue(fieldName, out var colIndex) ? _row[colIndex]?.ToString()?.Trim() : null; if (required && string.IsNullOrWhiteSpace(str)) { throw new ServiceException($"第{RowIndex}行{fieldName} 必须填写"); } return str; } public int? GetInt(string fieldName, bool required) { var str = GetString(fieldName, required); if (!required && string.IsNullOrEmpty(str)) { return null; } return int.TryParse(str, out var num) ? num : throw new ServiceException($"第{RowIndex}行{fieldName} 填写错误:{str}"); } public double? GetDoubleOrPercent(string fieldName, bool required, bool percent) { var str = GetString(fieldName, required); if (!required && string.IsNullOrWhiteSpace(str)) { return null; } if (percent && (percent = str.EndsWith("%"))) { str = str.TrimEnd('%'); } return double.TryParse(str, out var num) ? (percent ? num / 100 : num) : throw new ServiceException($"第{RowIndex}行{fieldName} 填写错误:{str}"); } public decimal? GetDecimalOrPercent(string fieldName, bool required, bool percent) { var str = GetString(fieldName, required); if (!required && string.IsNullOrWhiteSpace(str)) { return null; } if (percent && (percent = str.EndsWith("%"))) { str = str.TrimEnd('%'); } return decimal.TryParse(str, out var num) ? (percent ? num / 100 : num) : throw new ServiceException($"第{RowIndex}行{fieldName} 填写错误:{str}"); } public double? GetDouble(string fieldName, bool required = false) { var str = GetString(fieldName, required); if (!required && string.IsNullOrWhiteSpace(str)) { return null; } return double.TryParse(str, out var num) ? num : throw new ServiceException($"第{RowIndex}行{fieldName} 填写错误:{str}"); } public decimal? GetDecimal(string fieldName, bool required = false) { var str = GetString(fieldName, required); if (!required && string.IsNullOrWhiteSpace(str)) { return null; } return decimal.TryParse(str, out var num) ? num : throw new ServiceException($"第{RowIndex}行{fieldName} 填写错误:{str}"); } //为了兼容模板修改导致的字段名称改变问题 public double? GetDouble(string fieldName, string fieldName2, bool required = false) { var str = GetString(fieldName, false) ?? GetString(fieldName2, false); if (string.IsNullOrWhiteSpace(str)) { return required ? throw new ServiceException($"第{RowIndex}行{fieldName} 必须填写") : (double?)null; } return double.TryParse(str, out var num) ? num : throw new ServiceException($"第{RowIndex}行{fieldName} 填写错误:{str}"); } public double? GetPercent(string fieldName, bool required = false) { var str = GetString(fieldName, required); if (!required && string.IsNullOrWhiteSpace(str)) { return null; } var percent = str.EndsWith("%"); if (percent) { str = str.TrimEnd('%'); } return double.TryParse(str, out var num) ? (percent ? num / 100 : num) : throw new ServiceException($"第{RowIndex}行{fieldName} 填写错误:{str}"); } /// /// 获取日期(不包括时间) /// public DateTime? GetDate(string fieldName, bool required = false) { var str = GetString(fieldName, required); if (!required && string.IsNullOrWhiteSpace(str)) { return null; } if (str.Length == 8 && Regex.IsMatch(str, @"^\d+$")) { return DateTime.TryParseExact(str, "yyyyMMdd", CultureInfo.InvariantCulture, DateTimeStyles.None, out var dt2) ? dt2 : throw new ServiceException($"{fieldName} 填写错误:{str}"); } return DateTime.TryParse(str, out var dt) ? dt.Date : throw new ServiceException($"第{RowIndex}行{fieldName} 填写错误:{str}"); } /// /// /// public int? GetInt32(string fieldName, bool required = false) { var str = GetString(fieldName, required); if (!required && string.IsNullOrWhiteSpace(str)) { return null; } return int.TryParse(str, out var num) ? num : throw new ServiceException($"第{RowIndex}行{fieldName} 填写错误:{str}"); } } }