wanshenmean
2026-03-12 f56441867f2cc77567f97a92348a5d878f0dca05
Code/WMS/WIDESEA_WMSServer/WIDESEA_Core/BaseServices/ServiceBase.cs
@@ -1,4 +1,3 @@
using AutoMapper.Execution;
using Magicodes.ExporterAndImporter.Core;
using Magicodes.ExporterAndImporter.Core.Models;
using Magicodes.ExporterAndImporter.Excel;
@@ -33,7 +32,7 @@
            this.BaseDal = BaseDal;
        }
        public TRepository BaseDal { get; set; } //通过在子类的构造函数中注入,这里是基类,不用构造函数
        public TRepository BaseDal { get; set; } //通过在子类的构造函数中注入,这里是基类,不用构造函数
        public ISqlSugarClient Db => BaseDal.Db;
@@ -73,7 +72,7 @@
            }
            if (EnableWebOrderBy)
            {
                //获取排序字段
                //获取排序字段
                Dictionary<string, OrderByType> orderbyDic = GetPageDataSort(options, TProperties);
                foreach (var item in orderbyDic)
@@ -159,7 +158,7 @@
            parameter = parameter ?? Expression.Parameter(typeof(TEntity), "x");
            //创建节点的属性p=>p.name 属性name
            //创建节点的属性p=>p.name 属性name
            MemberExpression memberProperty = Expression.PropertyOrField(parameter, propertyName);
            if (expressionType == LinqExpressionType.In)
@@ -172,7 +171,7 @@
                    }
                    else
                    {
                        throw new Exception("属性值类型不正确");
                        throw new Exception("属性值类型不正确");
                    }
                }
                //
@@ -192,15 +191,15 @@
                //if (isStringValue)
                //{
                //    //string 类型的字段,如果值带有'单引号,EF会默认变成''两个单引号
                //    //string 类型的字段,如果值带有'单引号,EF会默认变成''两个单引号
                //    MethodInfo method = typeof(System.Collections.IList).GetMethod("Contains");
                //    //创建集合常量并设置为常量的值
                //    //创建集合常量并设置为常量的值
                //    ConstantExpression constantCollection = Expression.Constant(list);
                //    //创建一个表示调用带参数的方法的:new 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);
                //}
                //非string字段,按上面方式处理报异常Null TypeMapping in Sql Tree
                //非string字段,按上面方式处理报异常Null TypeMapping in Sql Tree
                BinaryExpression body = null;
                foreach (var values in list)
                {
@@ -217,7 +216,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));
@@ -273,7 +272,7 @@
        }
        /// <summary>
        /// 获取排序字段
        /// 获取排序字段
        /// </summary>
        /// <param name="pageData"></param>
        /// <param name="propertyInfo"></param>
@@ -311,7 +310,7 @@
        //    {
        //        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 +329,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 +348,7 @@
        //    }
        //    catch (Exception ex)
        //    {
        //        throw new Exception($"无权限,{ex.Message}");
        //        throw new Exception($"无权限,{ex.Message}");
        //    }
        //}
@@ -359,7 +358,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 +373,9 @@
        }
        /// <summary>
        /// 添加数据
        /// 添加数据
        /// </summary>
        /// <param name="entity">单个实体</param>
        /// <param name="entity">单个实体</param>
        /// <returns></returns>
        public virtual WebResponseContent AddData(TEntity entity)
        {
@@ -391,9 +390,9 @@
        }
        /// <summary>
        /// 添加数据
        /// 添加数据
        /// </summary>
        /// <param name="entities">实体集合</param>
        /// <param name="entities">实体集合</param>
        /// <returns></returns>
        public virtual WebResponseContent AddData(List<TEntity> entities)
        {
@@ -408,7 +407,7 @@
        }
        /// <summary>
        /// 添加数据
        /// 添加数据
        /// </summary>
        /// <param name="saveModel"></param>
        /// <returns></returns>
@@ -416,9 +415,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 +429,7 @@
                PropertyInfo keyPro = typeof(TEntity).GetKeyProperty();
                if (keyPro == null)
                {
                    return WebResponseContent.Instance.Error("请先设置主键");
                    return WebResponseContent.Instance.Error("请先设置主键");
                }
                if (keyPro.PropertyType == typeof(Guid))
                {
@@ -454,7 +453,7 @@
                if (typeof(TEntity).GetNavigatePro() == null)
                {
                    return WebResponseContent.Instance.Error("未配置导航属性");
                    return WebResponseContent.Instance.Error("未配置导航属性");
                }
                Type detailType = typeof(TEntity).GetDetailType();
@@ -506,9 +505,9 @@
        }
        /// <summary>
        /// 修改数据
        /// 修改数据
        /// </summary>
        /// <param name="entity">单个实体</param>
        /// <param name="entity">单个实体</param>
        /// <returns></returns>
        public virtual WebResponseContent UpdateData(TEntity entity)
        {
@@ -523,9 +522,9 @@
        }
        /// <summary>
        /// 修改数据
        /// 修改数据
        /// </summary>
        /// <param name="entities">实体集合</param>
        /// <param name="entities">实体集合</param>
        /// <returns></returns>
        public virtual WebResponseContent UpdateData(List<TEntity> entities)
        {
@@ -540,7 +539,7 @@
        }
        /// <summary>
        /// 修改数据
        /// 修改数据
        /// </summary>
        /// <param name="saveModel"></param>
        /// <returns></returns>
@@ -549,9 +548,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 +562,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 +593,7 @@
                if (typeof(TEntity).GetNavigatePro() == null)
                {
                    return WebResponseContent.Instance.Error("未配置导航属性");
                    return WebResponseContent.Instance.Error("未配置导航属性");
                }
                Type detailType = typeof(TEntity).GetDetailType();
@@ -675,7 +674,7 @@
                }
                else
                {
                    content = WebResponseContent.Instance.Error("未找到主表主键值");
                    content = WebResponseContent.Instance.Error("未找到主表主键值");
                }
            }
@@ -688,9 +687,9 @@
        }
        /// <summary>
        /// 删除数据
        /// 删除数据
        /// </summary>
        /// <param name="key">主键</param>
        /// <param name="key">主键</param>
        /// <returns></returns>
        public virtual WebResponseContent DeleteData(object key)
        {
@@ -705,9 +704,9 @@
        }
        /// <summary>
        /// 删除数据
        /// 删除数据
        /// </summary>
        /// <param name="keys">主键数组</param>
        /// <param name="keys">主键数组</param>
        /// <returns></returns>
        public virtual WebResponseContent DeleteData(object[] keys)
        {
@@ -740,7 +739,7 @@
                    }
                    else
                    {
                        return WebResponseContent.Instance.Error("参数错误");
                        return WebResponseContent.Instance.Error("参数错误");
                    }
                }
            }
@@ -752,9 +751,9 @@
        }
        /// <summary>
        /// 删除数据
        /// 删除数据
        /// </summary>
        /// <param name="entity">单个实体</param>
        /// <param name="entity">单个实体</param>
        /// <returns></returns>
        public virtual WebResponseContent DeleteData(TEntity entity)
        {
@@ -769,9 +768,9 @@
        }
        /// <summary>
        /// 删除数据
        /// 删除数据
        /// </summary>
        /// <param name="entities">实体集合</param>
        /// <param name="entities">实体集合</param>
        /// <returns></returns>
        public virtual WebResponseContent DeleteData(List<TEntity> entities)
        {
@@ -786,7 +785,7 @@
        }
        /// <summary>
        /// 导出数据
        /// 导出数据
        /// </summary>
        /// <param name="pageData"></param>
        /// <returns></returns>
@@ -800,7 +799,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 +829,7 @@
        }
        /// <summary>
        /// 导入数据
        /// 导入数据
        /// </summary>
        /// <param name="files"></param>
        /// <returns></returns>
@@ -839,7 +838,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 +864,7 @@
        }
        /// <summary>
        /// 上传文件
        /// 上传文件
        /// </summary>
        /// <param name="files"></param>
        /// <returns></returns>
@@ -875,7 +874,7 @@
        }
        /// <summary>
        /// 模板下载
        /// 模板下载
        /// </summary>
        /// <returns></returns>
        public virtual WebResponseContent DownLoadTemplate()
@@ -888,11 +887,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);