using BaseOUDAL; using System.Data; using System.Globalization; using System.Text.RegularExpressions; using YLErp.Model; namespace YLErp.Modules.ClientCashModule { /// /// /// public class ClientCashInCashOutHistoryService : YLBaseService { public ClientCashInCashOutHistoryService(OptUserInfo userInfo) : base(userInfo) { } /// /// 导入交易 /// /// /// 当前文件中的目标期权总条数 /// 成功入库的数量 public void ImportClientCashInCashOutHistoryFromExcel(Stream streamIn, out int totalNum, out int successNum) { totalNum = 0; successNum = 0; var rowIndex = 0; try { var ds = Office.ExcelHelper.ReadExcelAsDataSet(streamIn, new[] { 0 }, 0); if (ds.Tables.Count < 1 || ds.Tables[0].Rows.Count < 2) { throw new ServiceException("读取导入数据失败:数据为空") { Tag = "111" }; } var table = ds.Tables[0]; var reader = new DataRowReader(table); rowIndex = 1; totalNum = table.Rows.Count - rowIndex; using (var trans = BeginTransaction()) { foreach (var row in table.Rows.Cast().Skip(1)) { rowIndex++; if (row.ItemArray.All(n => string.IsNullOrWhiteSpace(n?.ToString()))) { totalNum--; continue; } reader.SetDataRow(row); var clientName = reader.GetString("客户名称"); var client = ClientModule.ClientDataQueryService.GetClient(clientName); if (client == null) { throw new ServiceException($"客户{clientName}不存在"); } var clientCashInCashOutHistory = new clientcashincashout_history() { ClientId = client.id, ContractId = reader.GetString("交易编码"), Amount = reader.GetDouble("资金金额"), Action = reader.GetString("资金科目"), ValueDate = reader.GetDate("记录日期"), HappenDate = reader.GetDate("发生日期"), OptId = UserId, OptName = UserName, OptDate = DateTime.Now }; DbContext.clientcashincashout_history.Add(clientCashInCashOutHistory); DbContext.SaveChanges(); successNum++; } trans.Commit(); } } catch (ServiceException se) { if (se.Tag != null) { throw; } throw new ServiceException($"第{rowIndex}行,{se.Message}"); } catch (Exception ex) { LogFactory.GetLogger("导入历史资金").Error(ex); throw new ServiceException($"第{rowIndex}行,发生错误:{ex.Message}", ex); } } /// /// trade_swap_flow /// public SearchListResult SearchClientCashInCashOutHistoryList(ClientCashInCashOutHistoryReq req) { var query = from source in DbContext.clientcashincashout_history select source; if (req.ValueDateStart != DateTime.MinValue) { query = query.Where(d => d.ValueDate >= req.ValueDateStart); } if (req.ValueDateEnd != DateTime.MinValue) { var ValueDateTemp = req.ValueDateEnd.AddDays(1); query = query.Where(d => d.ValueDate < ValueDateTemp); } if (req.HappenDateStart != DateTime.MinValue) { query = query.Where(d => d.HappenDate >= req.HappenDateStart); } if (req.HappenDateEnd != DateTime.MinValue) { var HappenDateTemp = req.HappenDateEnd.AddDays(1); query = query.Where(d => d.HappenDate < HappenDateTemp); } if (req.ClientIds != null && req.ClientIds.Any()) { query = query.Where(d => req.ClientIds.Contains(d.ClientId)); } if (string.IsNullOrEmpty(req.sidx)) { req.sidx = "id"; req.sord = "desc"; } var retListResult = query.ToSearchList(req); return retListResult; } #region---内部业务类---- class DataRowReader { DataRow _row; readonly Dictionary _colMap; public DataRowReader(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; } else { continue; } _colMap[preCol1] = index; } } /// /// 设置datarow /// public void SetDataRow(DataRow row) { _row = row; } 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($"{fieldName} 必须填写"); } return 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($"{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($"{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($"{fieldName} 必须填写") : (double?)null; } return double.TryParse(str, out var num) ? num : throw new ServiceException($"{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($"{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($"{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($"{fieldName} 填写错误:{str}"); } } #endregion } }