Files
zszq-trs/YLErpDAL/Modules/ClientModule/ClientCashInCashOutHistoryService.cs
T
2024-05-09 14:06:26 +08:00

301 lines
10 KiB
C#

using BaseOUDAL;
using System.Data;
using System.Globalization;
using System.Text.RegularExpressions;
using YLErp.Model;
namespace YLErp.Modules.ClientCashModule
{
/// <summary>
///
/// </summary>
public class ClientCashInCashOutHistoryService : YLBaseService
{
public ClientCashInCashOutHistoryService(OptUserInfo userInfo) : base(userInfo)
{
}
/// <summary>
/// 导入交易
/// </summary>
/// <param name="streamIn"></param>
/// <param name="totalNum">当前文件中的目标期权总条数</param>
/// <param name="successNum">成功入库的数量</param>
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<DataRow>().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);
}
}
/// <summary>
/// trade_swap_flow
/// </summary>
public SearchListResult<clientcashincashout_history> 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<string, int> _colMap;
public DataRowReader(DataTable table)
{
var colCount = table.Columns.Count;
_colMap = new Dictionary<string, int>(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;
}
}
/// <summary>
/// 设置datarow
/// </summary>
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}");
}
/// <summary>
/// 获取日期(不包括时间)
/// </summary>
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}");
}
/// <summary>
///
/// </summary>
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
}
}