From 3406bbd34469982efeefe164c207dcb5c4a0dac2 Mon Sep 17 00:00:00 2001
From: wanshenmean <cathay_xy@163.com>
Date: 星期四, 30 四月 2026 22:03:54 +0800
Subject: [PATCH] fix: 修复多个功能模块中的问题和优化逻辑
---
Code/WMS/WIDESEA_WMSServer/WIDESEA_Core/BaseServices/ServiceBase.cs | 161 +++++++++++++++++++++++++++++++++++------------------
1 files changed, 105 insertions(+), 56 deletions(-)
diff --git a/Code/WMS/WIDESEA_WMSServer/WIDESEA_Core/BaseServices/ServiceBase.cs b/Code/WMS/WIDESEA_WMSServer/WIDESEA_Core/BaseServices/ServiceBase.cs
index 6b3275f..a0c6e4e 100644
--- a/Code/WMS/WIDESEA_WMSServer/WIDESEA_Core/BaseServices/ServiceBase.cs
+++ b/Code/WMS/WIDESEA_WMSServer/WIDESEA_Core/BaseServices/ServiceBase.cs
@@ -1,4 +1,3 @@
-锘縰sing AutoMapper.Execution;
using Magicodes.ExporterAndImporter.Core;
using Magicodes.ExporterAndImporter.Core.Models;
using Magicodes.ExporterAndImporter.Excel;
@@ -17,6 +16,7 @@
using WIDESEA_Core.DB.Models;
using WIDESEA_Core.Enums;
using WIDESEA_Core.Helper;
+
//using WIDESEA_Core.HostedService;
using WIDESEA_Core.Utilities;
using ICacheService = WIDESEA_Core.Caches.ICacheService;
@@ -33,11 +33,57 @@
this.BaseDal = BaseDal;
}
- public TRepository BaseDal { get; set; } //閫氳繃鍦ㄥ瓙绫荤殑鏋勯�犲嚱鏁颁腑娉ㄥ叆锛岃繖閲屾槸鍩虹被锛屼笉鐢ㄦ瀯閫犲嚱鏁�
+ public TRepository BaseDal { get; set; } //通过在子类的构造函数中注入,这里是基类,不用构造函数
public ISqlSugarClient Db => BaseDal.Db;
+ protected async Task<WebResponseContent> ExecuteWithinTransactionAsync(Func<Task<WebResponseContent>> operation)
+ {
+ var db = Db as SqlSugarClient;
+ if (db == null)
+ {
+ return WebResponseContent.Instance.Error("Database context does not support transactions");
+ }
+
+ var ownsTransaction = db.Ado.Transaction == null;
+ try
+ {
+ if (ownsTransaction)
+ {
+ db.BeginTran();
+ }
+
+ var result = await operation();
+ if (result?.Status == true)
+ {
+ if (ownsTransaction)
+ {
+ db.CommitTran();
+ }
+
+ return result;
+ }
+
+ if (ownsTransaction)
+ {
+ db.RollbackTran();
+ }
+
+ return result ?? WebResponseContent.Instance.Error("Transaction failed");
+ }
+ catch
+ {
+ if (ownsTransaction)
+ {
+ db.RollbackTran();
+ }
+
+ throw;
+ }
+ }
+
private PropertyInfo[] _propertyInfo { get; set; } = null;
+
public PropertyInfo[] TProperties
{
get
@@ -73,7 +119,7 @@
}
if (EnableWebOrderBy)
{
- //鑾峰彇鎺掑簭瀛楁
+ //获取排序字段
Dictionary<string, OrderByType> orderbyDic = GetPageDataSort(options, TProperties);
foreach (var item in orderbyDic)
@@ -157,9 +203,8 @@
string? value = propertyValue?.ToString();
if (value == null) return p => true;
-
parameter = parameter ?? Expression.Parameter(typeof(TEntity), "x");
- //鍒涘缓鑺傜偣鐨勫睘鎬=>p.name 灞炴�ame
+ //创建节点的属性p=>p.name 属性name
MemberExpression memberProperty = Expression.PropertyOrField(parameter, propertyName);
if (expressionType == LinqExpressionType.In)
@@ -172,7 +217,7 @@
}
else
{
- throw new Exception("灞炴�у�肩被鍨嬩笉姝g‘");
+ throw new Exception("属性值类型不正确");
}
}
//
@@ -192,15 +237,15 @@
//if (isStringValue)
//{
- // //string 绫诲瀷鐨勫瓧娈碉紝濡傛灉鍊煎甫鏈�'鍗曞紩鍙�,EF浼氶粯璁ゅ彉鎴�''涓や釜鍗曞紩鍙�
+ // //string 类型的字段,如果值带有'单引号,EF会默认变成''两个单引号
// MethodInfo method = typeof(System.Collections.IList).GetMethod("Contains");
- // //鍒涘缓闆嗗悎甯搁噺骞惰缃负甯搁噺鐨勫��
+ // //创建集合常量并设置为常量的值
// ConstantExpression constantCollection = Expression.Constant(list);
- // //鍒涘缓涓�涓〃绀鸿皟鐢ㄥ甫鍙傛暟鐨勬柟娉曠殑锛歯ew string[]{"1","a"}.Contains("a");
+ // //创建一个表示调用带参数的方法的:new string[]{"1","a"}.Contains("a");
// MethodCallExpression methodCall = Expression.Call(constantCollection, method, memberProperty);
// return Expression.Lambda<Func<TEntity, bool>>(methodCall, parameter);
//}
- //闈瀞tring瀛楁锛屾寜涓婇潰鏂瑰紡澶勭悊鎶ュ紓甯窷ull TypeMapping in Sql Tree
+ //非string字段,按上面方式处理报异常Null TypeMapping in Sql Tree
BinaryExpression body = null;
foreach (var values in list)
{
@@ -217,7 +262,7 @@
ConstantExpression constant = proType.ToString() == "System.String"
? Expression.Constant(propertyValue) : Expression.Constant(value.ChangeType(proType));
- // DateTime鍙�夋嫨浜嗘棩鏈熺殑鏃跺�欒嚜鍔ㄥ湪缁撴潫鏃ユ湡鍔犱竴澶╋紝淇DateTime绫诲瀷浣跨敤鏃ユ湡鍖洪棿鏌ヨ鏃犳硶鏌ヨ鍒扮粨鏉熸棩鏈熺殑闂
+ // DateTime只选择了日期的时候自动在结束日期加一天,修复DateTime类型使用日期区间查询无法查询到结束日期的问题
if ((proType == typeof(DateTime) || proType == typeof(DateTime?)) && expressionType == LinqExpressionType.LessThanOrEqual && value.Length == 10)
{
constant = Expression.Constant(Convert.ToDateTime(value).AddDays(1));
@@ -230,21 +275,27 @@
case LinqExpressionType.Equal:
expression = Expression.Lambda<Func<TEntity, bool>>(Expression.Equal(member, constant), parameter);
break;
+
case LinqExpressionType.NotEqual:
expression = Expression.Lambda<Func<TEntity, bool>>(Expression.NotEqual(member, constant), parameter);
break;
+
case LinqExpressionType.GreaterThan:
expression = Expression.Lambda<Func<TEntity, bool>>(Expression.GreaterThan(member, constant), parameter);
break;
+
case LinqExpressionType.LessThan:
expression = Expression.Lambda<Func<TEntity, bool>>(Expression.LessThan(member, constant), parameter);
break;
+
case LinqExpressionType.ThanOrEqual:
expression = Expression.Lambda<Func<TEntity, bool>>(Expression.GreaterThanOrEqual(member, constant), parameter);
break;
+
case LinqExpressionType.LessThanOrEqual:
expression = Expression.Lambda<Func<TEntity, bool>>(Expression.LessThanOrEqual(member, constant), parameter);
break;
+
case LinqExpressionType.Contains:
case LinqExpressionType.NotContains:
MethodInfo? method = typeof(string).GetMethod("Contains", new[] { typeof(string) });
@@ -265,6 +316,7 @@
expression = p => true;
}
break;
+
default:
expression = p => false;
break;
@@ -273,7 +325,7 @@
}
/// <summary>
- /// 鑾峰彇鎺掑簭瀛楁
+ /// 获取排序字段
/// </summary>
/// <param name="pageData"></param>
/// <param name="propertyInfo"></param>
@@ -303,15 +355,13 @@
return new Dictionary<string, OrderByType> { { "CreateDate", pageData.Order?.ToLower() == OrderByType.Asc.ToString() ? OrderByType.Asc : OrderByType.Desc } };
}
-
-
//private string GetDataRole(Type type)
//{
// try
// {
// UserRole? userRole = PermissionDataHostService.UserRoles.FirstOrDefault(x => x.UserId == App.User.UserId);
// if (userRole == null)
- // throw new Exception($"鏃犳潈闄�");
+ // throw new Exception($"无权限");
// if (type.IsAssignableFrom(typeof(BaseWarehouseEntity)) || type.GetProperty(nameof(BaseWarehouseEntity.WarehouseId)) != null)
// {
@@ -330,7 +380,7 @@
// //UserRole? userRole = PermissionDataHostService.UserRoles.FirstOrDefault(x => x.UserId == App.User.UserId);
// //if (userRole == null)
- // // throw new Exception($"鏃犳潈闄�");
+ // // throw new Exception($"无权限");
// //if (userRole.AuthorityScope == (int)AuthorityScopeEnum.CurrentRole)
// //{
@@ -349,7 +399,7 @@
// }
// catch (Exception ex)
// {
- // throw new Exception($"鏃犳潈闄愶紝{ex.Message}");
+ // throw new Exception($"无权限,{ex.Message}");
// }
//}
@@ -359,7 +409,7 @@
if (pageData.Value == null) return new PageGridData<object>(total: 0, null);
string keyName = t.GetKeyName();
- ////鐢熸垚鏌ヨ鏉′欢
+ ////生成查询条件
//Expression<Func<TEntity, bool>> whereExpression = keyName.CreateExpression<TEntity>(pageData.Value, LinqExpressionType.Equal);
int totalCount = 0;
PropertyInfo propertyInfo = t.GetProperties().FirstOrDefault(x => x.GetCustomAttribute<Navigate>() != null);
@@ -374,9 +424,9 @@
}
/// <summary>
- /// 娣诲姞鏁版嵁
+ /// 添加数据
/// </summary>
- /// <param name="entity">鍗曚釜瀹炰綋</param>
+ /// <param name="entity">单个实体</param>
/// <returns></returns>
public virtual WebResponseContent AddData(TEntity entity)
{
@@ -391,9 +441,9 @@
}
/// <summary>
- /// 娣诲姞鏁版嵁
+ /// 添加数据
/// </summary>
- /// <param name="entities">瀹炰綋闆嗗悎</param>
+ /// <param name="entities">实体集合</param>
/// <returns></returns>
public virtual WebResponseContent AddData(List<TEntity> entities)
{
@@ -408,7 +458,7 @@
}
/// <summary>
- /// 娣诲姞鏁版嵁
+ /// 添加数据
/// </summary>
/// <param name="saveModel"></param>
/// <returns></returns>
@@ -416,9 +466,9 @@
{
try
{
- if (saveModel == null || saveModel.MainData == null || saveModel.MainData.Count == 0)//鍒ゆ柇鍙傛暟鏄惁浼犲叆
+ if (saveModel == null || saveModel.MainData == null || saveModel.MainData.Count == 0)//判断参数是否传入
{
- return WebResponseContent.Instance.Error("浼犲弬閿欒,鍙傛暟涓嶈兘涓虹┖");
+ return WebResponseContent.Instance.Error("传参错误,参数不能为空");
}
string validResult = typeof(TEntity).ValidateDicInEntity(saveModel.MainData, true, TProperties);
@@ -430,7 +480,7 @@
PropertyInfo keyPro = typeof(TEntity).GetKeyProperty();
if (keyPro == null)
{
- return WebResponseContent.Instance.Error("璇峰厛璁剧疆涓婚敭");
+ return WebResponseContent.Instance.Error("请先设置主键");
}
if (keyPro.PropertyType == typeof(Guid))
{
@@ -454,7 +504,7 @@
if (typeof(TEntity).GetNavigatePro() == null)
{
- return WebResponseContent.Instance.Error("鏈厤缃鑸睘鎬�");
+ return WebResponseContent.Instance.Error("未配置导航属性");
}
Type detailType = typeof(TEntity).GetDetailType();
@@ -506,9 +556,9 @@
}
/// <summary>
- /// 淇敼鏁版嵁
+ /// 修改数据
/// </summary>
- /// <param name="entity">鍗曚釜瀹炰綋</param>
+ /// <param name="entity">单个实体</param>
/// <returns></returns>
public virtual WebResponseContent UpdateData(TEntity entity)
{
@@ -523,9 +573,9 @@
}
/// <summary>
- /// 淇敼鏁版嵁
+ /// 修改数据
/// </summary>
- /// <param name="entities">瀹炰綋闆嗗悎</param>
+ /// <param name="entities">实体集合</param>
/// <returns></returns>
public virtual WebResponseContent UpdateData(List<TEntity> entities)
{
@@ -540,7 +590,7 @@
}
/// <summary>
- /// 淇敼鏁版嵁
+ /// 修改数据
/// </summary>
/// <param name="saveModel"></param>
/// <returns></returns>
@@ -549,9 +599,9 @@
try
{
List<string>? list = UpdateIgnoreColOnExecute?.Invoke(saveModel);
- if (saveModel == null || saveModel.MainData == null || saveModel.MainData.Count == 0)//鍒ゆ柇鍙傛暟鏄惁浼犲叆
+ if (saveModel == null || saveModel.MainData == null || saveModel.MainData.Count == 0)//判断参数是否传入
{
- return WebResponseContent.Instance.Error("浼犲弬閿欒,鍙傛暟涓嶈兘涓虹┖");
+ return WebResponseContent.Instance.Error("传参错误,参数不能为空");
}
string validResult = typeof(TEntity).ValidateDicInEntity(saveModel.MainData, false, TProperties, list?.ToArray() ?? null);
@@ -563,7 +613,7 @@
PropertyInfo keyPro = typeof(TEntity).GetKeyProperty();
if (keyPro == null)
{
- return WebResponseContent.Instance.Error("璇峰厛璁剧疆涓婚敭");
+ return WebResponseContent.Instance.Error("请先设置主键");
}
TEntity entity = saveModel.MainData.DicToModel<TEntity>();
@@ -594,7 +644,7 @@
if (typeof(TEntity).GetNavigatePro() == null)
{
- return WebResponseContent.Instance.Error("鏈厤缃鑸睘鎬�");
+ return WebResponseContent.Instance.Error("未配置导航属性");
}
Type detailType = typeof(TEntity).GetDetailType();
@@ -675,9 +725,8 @@
}
else
{
- content = WebResponseContent.Instance.Error("鏈壘鍒颁富琛ㄤ富閿��");
+ content = WebResponseContent.Instance.Error("未找到主表主键值");
}
-
}
catch (Exception ex)
{
@@ -688,9 +737,9 @@
}
/// <summary>
- /// 鍒犻櫎鏁版嵁
+ /// 删除数据
/// </summary>
- /// <param name="key">涓婚敭</param>
+ /// <param name="key">主键</param>
/// <returns></returns>
public virtual WebResponseContent DeleteData(object key)
{
@@ -705,9 +754,9 @@
}
/// <summary>
- /// 鍒犻櫎鏁版嵁
+ /// 删除数据
/// </summary>
- /// <param name="keys">涓婚敭鏁扮粍</param>
+ /// <param name="keys">主键数组</param>
/// <returns></returns>
public virtual WebResponseContent DeleteData(object[] keys)
{
@@ -740,7 +789,7 @@
}
else
{
- return WebResponseContent.Instance.Error("鍙傛暟閿欒");
+ return WebResponseContent.Instance.Error("参数错误");
}
}
}
@@ -752,9 +801,9 @@
}
/// <summary>
- /// 鍒犻櫎鏁版嵁
+ /// 删除数据
/// </summary>
- /// <param name="entity">鍗曚釜瀹炰綋</param>
+ /// <param name="entity">单个实体</param>
/// <returns></returns>
public virtual WebResponseContent DeleteData(TEntity entity)
{
@@ -769,9 +818,9 @@
}
/// <summary>
- /// 鍒犻櫎鏁版嵁
+ /// 删除数据
/// </summary>
- /// <param name="entities">瀹炰綋闆嗗悎</param>
+ /// <param name="entities">实体集合</param>
/// <returns></returns>
public virtual WebResponseContent DeleteData(List<TEntity> entities)
{
@@ -786,7 +835,7 @@
}
/// <summary>
- /// 瀵煎嚭鏁版嵁
+ /// 导出数据
/// </summary>
/// <param name="pageData"></param>
/// <returns></returns>
@@ -800,7 +849,7 @@
string savePath = AppDomain.CurrentDomain.BaseDirectory + $"ExcelExport";
IExporter exporter = new ExcelExporter();
string wheres = options.ValidatePageOptions(TProperties);
- //鑾峰彇鎺掑簭瀛楁
+ //获取排序字段
Dictionary<string, OrderByType> orderbyDic = options.GetPageDataSort(TProperties);
List<TEntity> entities = BaseDal.QueryData(wheres, orderbyDic);
@@ -830,7 +879,7 @@
}
/// <summary>
- /// 瀵煎叆鏁版嵁
+ /// 导入数据
/// </summary>
/// <param name="files"></param>
/// <returns></returns>
@@ -839,7 +888,7 @@
try
{
if (files == null || files.Count == 0)
- return new WebResponseContent { Status = true, Message = "璇烽�夋嫨涓婁紶鐨勬枃浠�" };
+ return new WebResponseContent { Status = true, Message = "请选择上传的文件" };
Microsoft.AspNetCore.Http.IFormFile formFile = files[0];
string dicPath = AppDomain.CurrentDomain.BaseDirectory + $"ExcelImprot/{DateTime.Now.ToString("yyyMMdd")}/{typeof(TEntity).Name}/";
if (!Directory.Exists(dicPath)) Directory.CreateDirectory(dicPath);
@@ -865,7 +914,7 @@
}
/// <summary>
- /// 涓婁紶鏂囦欢
+ /// 上传文件
/// </summary>
/// <param name="files"></param>
/// <returns></returns>
@@ -875,7 +924,7 @@
}
/// <summary>
- /// 妯℃澘涓嬭浇
+ /// 模板下载
/// </summary>
/// <returns></returns>
public virtual WebResponseContent DownLoadTemplate()
@@ -888,11 +937,11 @@
SugarTable sugarTable = t.GetCustomAttribute<SugarTable>();
if (sugarTable != null)
{
- fileName = sugarTable.TableDescription + "瀵煎叆妯℃澘.xlsx";
+ fileName = sugarTable.TableDescription + "导入模板.xlsx";
}
else
{
- fileName = nameof(TEntity) + "瀵煎叆妯℃澘.xlsx";
+ fileName = nameof(TEntity) + "导入模板.xlsx";
}
string savePath = AppDomain.CurrentDomain.BaseDirectory + $"ExcelImprotTemplate";
FileHelper.WriteFile(savePath, fileName, data);
@@ -920,4 +969,4 @@
return content;
}
}
-}
+}
\ No newline at end of file
--
Gitblit v1.9.3