diff --git a/CoreCms.Net.Core/Config/SqlSugarSetup.cs b/CoreCms.Net.Core/Config/SqlSugarSetup.cs index 4e45849c..52a2ff5e 100644 --- a/CoreCms.Net.Core/Config/SqlSugarSetup.cs +++ b/CoreCms.Net.Core/Config/SqlSugarSetup.cs @@ -17,6 +17,7 @@ using CoreCms.Net.Configuration; using CoreCms.Net.Loging; using Microsoft.Extensions.DependencyInjection; using SqlSugar; +using SqlSugar.IOC; namespace CoreCms.Net.Core.Config { @@ -25,70 +26,49 @@ namespace CoreCms.Net.Core.Config /// public static class SqlSugarSetup { + public static void AddSqlSugarSetup(this IServiceCollection services) { if (services == null) throw new ArgumentNullException(nameof(services)); - string connectionString = AppSettingsConstVars.DbSqlConnection; - string dbTypeString = AppSettingsConstVars.DbDbType; - //获取数据类型 - var dbType = dbTypeString == DbType.MySql.ToString() ? DbType.MySql : DbType.SqlServer; - //判断是否开启redis设置二级缓存方式 - ICacheService myCache = AppSettingsConstVars.RedisUseCache - ? (ICacheService)new SqlSugarRedisCache() - : new SqlSugarMemoryCache(); - - var connectionConfig = new ConnectionConfig() + //注入 ORM + SugarIocServices.AddSqlSugar(new IocConfig() { - ConnectionString = connectionString, //必填 - DbType = dbType, //必填 - IsAutoCloseConnection = false, - InitKeyType = InitKeyType.Attribute, + //数据库连接 + ConnectionString = AppSettingsConstVars.DbSqlConnection, + //判断数据库类型 + DbType = AppSettingsConstVars.DbDbType == IocDbType.MySql.ToString() ? IocDbType.MySql : IocDbType.SqlServer, + //是否开启自动关闭数据库连接-//不设成true要手动close + IsAutoCloseConnection = true, + }); - ConfigureExternalServices = new ConfigureExternalServices() + //设置参数 + services.ConfigurationSugar(db => + { + db.CurrentConnectionConfig.InitKeyType = InitKeyType.Attribute; + db.CurrentConnectionConfig.ConfigureExternalServices = new ConfigureExternalServices() { - DataInfoCacheService = myCache - }, - }; + //判断是否开启redis设置二级缓存方式 + DataInfoCacheService = AppSettingsConstVars.RedisUseCache ? (ICacheService)new SqlSugarRedisCache() : new SqlSugarMemoryCache() + }; + //执行SQL 错误事件,可监控sql(暂时屏蔽,需要可开启) + //db.Aop.OnLogExecuting = (sql, p) => + //{ + // NLogUtil.WriteFileLog(NLog.LogLevel.Error, LogType.Other, "SqlSugar执行SQL错误事件打印Sql", sql); + //}; - services.AddScoped(o => + //执行SQL 错误事件 + db.Aop.OnError = (exp) => { + NLogUtil.WriteFileLog(NLog.LogLevel.Error, LogType.Other, "SqlSugar", "执行SQL错误事件", exp); + }; - var db = new SqlSugarClient(connectionConfig); //默认SystemTable - - //日志处理 - ////SQL执行前 可以修改SQL - //db.Aop.OnLogExecuting = (sql, pars) => - //{ - // //获取sql - // Console.WriteLine(sql + "\r\n" + db.Utilities.SerializeObject(pars.ToDictionary(it => it.ParameterName, it => it.Value))); - // Console.WriteLine(); - - // //通过TempItems这个变量来算出这个SQL执行时间(1) - // if (db.TempItems == null) db.TempItems = new Dictionary(); - // db.TempItems.Add("logTime", DateTime.Now); - // //通过TempItems这个变量来算出这个SQL执行时间(2) - // var startingTime = db.TempItems["logTime"]; - // db.TempItems.Remove("time"); - // var completedTime = DateTime.Now; - - - //}; - //db.Aop.OnLogExecuted = (sql, pars) => //SQL执行完事件 - //{ - - //}; - //db.Aop.OnLogExecuting = (sql, pars) => //SQL执行前事件 - //{ - - //}; - db.Aop.OnError = (exp) =>//执行SQL 错误事件 - { - NLogUtil.WriteFileLog(NLog.LogLevel.Error, LogType.Other, "SqlSugar", "执行SQL错误事件", exp); - }; - return db; - }); + //设置更多连接参数 + //db.CurrentConnectionConfig.XXXX=XXXX + //db.CurrentConnectionConfig.MoreSetting=new MoreSetting(){} + //读写分离等都在这儿设置 + }); } } diff --git a/CoreCms.Net.Core/CoreCms.Net.Core.csproj b/CoreCms.Net.Core/CoreCms.Net.Core.csproj index dfae7f49..a476580e 100644 --- a/CoreCms.Net.Core/CoreCms.Net.Core.csproj +++ b/CoreCms.Net.Core/CoreCms.Net.Core.csproj @@ -25,6 +25,7 @@ + diff --git a/CoreCms.Net.IRepository/UnitOfWork/IUnitOfWork.cs b/CoreCms.Net.IRepository/UnitOfWork/IUnitOfWork.cs index 11cab486..0c2cb40a 100644 --- a/CoreCms.Net.IRepository/UnitOfWork/IUnitOfWork.cs +++ b/CoreCms.Net.IRepository/UnitOfWork/IUnitOfWork.cs @@ -15,7 +15,7 @@ namespace CoreCms.Net.IRepository.UnitOfWork { public interface IUnitOfWork { - SqlSugarClient GetDbClient(); + SqlSugarScope GetDbClient(); void BeginTran(); diff --git a/CoreCms.Net.Repository/BaseRepository.cs b/CoreCms.Net.Repository/BaseRepository.cs index c170af12..1b6bedc2 100644 --- a/CoreCms.Net.Repository/BaseRepository.cs +++ b/CoreCms.Net.Repository/BaseRepository.cs @@ -22,7 +22,7 @@ namespace CoreCms.Net.Repository public abstract class BaseRepository : IBaseRepository where T : class, new() { //private readonly IUnitOfWork _unitOfWork; - private readonly SqlSugarClient _dbBase; + private readonly SqlSugarScope _dbBase; protected BaseRepository(IUnitOfWork unitOfWork) { diff --git a/CoreCms.Net.Repository/Good/CoreCmsGoodsRepository.cs b/CoreCms.Net.Repository/Good/CoreCmsGoodsRepository.cs index 29d26e57..a1d2c72b 100644 --- a/CoreCms.Net.Repository/Good/CoreCmsGoodsRepository.cs +++ b/CoreCms.Net.Repository/Good/CoreCmsGoodsRepository.cs @@ -919,8 +919,8 @@ namespace CoreCms.Net.Repository } else { - var ids = DbClient.Queryable().Where(p => p.isDel == false && p.isMarketable == true) - .Select(p => p.id).ToArray(); + var ids = await DbClient.Queryable().Where(p => p.isDel == false && p.isMarketable == true) + .Select(p => p.id).ToArrayAsync(); var dbIds = new List(); if (ids.Any()) { diff --git a/CoreCms.Net.Repository/UnitOfWork/UnitOfWork.cs b/CoreCms.Net.Repository/UnitOfWork/UnitOfWork.cs index c713cada..d3efd188 100644 --- a/CoreCms.Net.Repository/UnitOfWork/UnitOfWork.cs +++ b/CoreCms.Net.Repository/UnitOfWork/UnitOfWork.cs @@ -14,6 +14,7 @@ using CoreCms.Net.IRepository.UnitOfWork; using CoreCms.Net.Loging; using NLog; using SqlSugar; +using SqlSugar.IOC; namespace CoreCms.Net.Repository.UnitOfWork { @@ -21,19 +22,24 @@ namespace CoreCms.Net.Repository.UnitOfWork { private readonly ISqlSugarClient _sqlSugarClient; - public UnitOfWork(ISqlSugarClient sqlSugarClient) + //public UnitOfWork(ISqlSugarClient sqlSugarClient) + //{ + // _sqlSugarClient = sqlSugarClient; + //} + + public UnitOfWork() { - _sqlSugarClient = sqlSugarClient; + _sqlSugarClient = DbScoped.SugarScope; } /// /// 获取DB,保证唯一性 /// /// - public SqlSugarClient GetDbClient() + public SqlSugarScope GetDbClient() { // 必须要as,后边会用到切换数据库操作 - return _sqlSugarClient as SqlSugarClient; + return _sqlSugarClient as SqlSugarScope; } public void BeginTran() diff --git a/CoreCms.Net.Services/Shop/CoreCmsPagesServices.cs b/CoreCms.Net.Services/Shop/CoreCmsPagesServices.cs index 5b6774e1..fe143487 100644 --- a/CoreCms.Net.Services/Shop/CoreCmsPagesServices.cs +++ b/CoreCms.Net.Services/Shop/CoreCmsPagesServices.cs @@ -421,7 +421,7 @@ namespace CoreCms.Net.Services } limit = limit > 0 ? limit : 10; - var goods = await _goodsServices.QueryListByClauseAsync(where, limit, p => p.createTime, OrderByType.Desc, false); + var goods = await _goodsServices.QueryPageAsync(where, " sort desc,id desc ", 1, limit, true); if (goods != null && goods.Any()) { JArray result = JArray.FromObject(goods); diff --git a/CoreCms.Net.Uni-App/CoreShop/components/coreshop-copyright/coreshop-copyright.vue b/CoreCms.Net.Uni-App/CoreShop/components/coreshop-copyright/coreshop-copyright.vue index aac5d63f..1dfa8b24 100644 --- a/CoreCms.Net.Uni-App/CoreShop/components/coreshop-copyright/coreshop-copyright.vue +++ b/CoreCms.Net.Uni-App/CoreShop/components/coreshop-copyright/coreshop-copyright.vue @@ -4,7 +4,7 @@ © {{shopName}} 品牌运营 - 备案号:{{shopBeiAn}} + 备案号:{{shopBeiAn}} Powered by CoreShop diff --git a/CoreCms.Net.Uni-App/CoreShop/components/coreshop-page/coreshop-imgSingle.vue b/CoreCms.Net.Uni-App/CoreShop/components/coreshop-page/coreshop-imgSingle.vue index c3f18f08..664c74c2 100644 --- a/CoreCms.Net.Uni-App/CoreShop/components/coreshop-page/coreshop-imgSingle.vue +++ b/CoreCms.Net.Uni-App/CoreShop/components/coreshop-page/coreshop-imgSingle.vue @@ -135,7 +135,7 @@