|
- using BPA.SAAS.KitChenManageOrder.Core.Common;
- using BPA.SAAS.KitChenManageOrder.Core.Common.Const;
- using BPA.SAAS.KitChenManageOrder.Core.Entity.Base;
- using Furion;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.DependencyInjection;
- using SqlSugar;
- using System;
- using System.Collections.Generic;
- using System.Linq;
-
- namespace BPA.SAAS.KitChenManageOrder.Core
- {
- /// <summary>
- /// 数据库上下文对象
- /// </summary>
- public static class DbContext
- {
- public static SqlSugarScope Db { get; set; }
- /// <summary>
- /// SqlSugar 数据库实例
- /// </summary>
- public static void AddSqlsugarSetup(this IServiceCollection services, IConfiguration configuration)
- {
- var connectionConfigs = App.GetConfig<List<ConnectionConfig>>("ConnectionConfigs");
- //如果多个数数据库传 List<ConnectionConfig>
- var configConnection = new ConnectionConfig()
- {
- ConnectionString = connectionConfigs[0].ConnectionString,
- DbType = SqlSugar.DbType.MySql,//设置数据库类型
- IsAutoCloseConnection = true,//自动释放数据务,如果存在事务,在事务结束后释放
- InitKeyType = InitKeyType.Attribute, //从实体特性中读取主键自增列信息
- ConfigId = "default",
- };
- var connectionConfigslog = App.GetConfig<List<ConnectionConfig>>("ConnectionConfigslog");
- var configConnectionlog = new ConnectionConfig()
- {
- ConnectionString = connectionConfigslog[0].ConnectionString,
- DbType = SqlSugar.DbType.MySql,//设置数据库类型
- IsAutoCloseConnection = true,//自动释放数据务,如果存在事务,在事务结束后释放
- InitKeyType = InitKeyType.Attribute, //从实体特性中读取主键自增列信息
- ConfigId = "default",
- };
- SqlSugarDb.SqlSugarScope(configConnectionlog);
- SqlSugarScope sqlSugar = new SqlSugarScope(configConnection,
- db =>
- {
- //过滤已删除数据
- db.QueryFilter.AddTableFilter<IDeleted>(it => it.IsDeleted == 0);
- //db.QueryFilter.AddTableFilter<IStatus>(it => it.Status == 0);
- if (CurrentUser.GroupId != null || App.User?.FindFirst(ClaimConst.GroupId)?.Value!=null)
- {
- //非管理员账户过滤加盟商数据
- var groupId = CurrentUser.GroupId==null?App.User?.FindFirst(ClaimConst.GroupId)?.Value: CurrentUser.GroupId;
- db.QueryFilter.AddTableFilter<IGroupId>(it => it.GroupId == groupId);
- }
- db.Aop.DataExecuting = (oldValue, entityInfo) =>
- {
- //新增添加加盟商id赋值
- //var groupId = App.User?.FindFirst(ClaimConst.GroupId)?.Value;
- //if (entityInfo.PropertyName == "GroupId" && entityInfo.OperationType == DataFilterType.InsertByObject)
- //{
- // if (oldValue == null || oldValue == Convert.DBNull)
- // {
- // entityInfo.SetValue(groupId);
- // }
- //}
- DataExecuting(db);
- };
-
- //单例参数配置,所有上下文生效
- db.Aop.OnLogExecuting = (sql, pars) =>
- {
- //Console.WriteLine(sql);//输出sql
- Console.WriteLine($"当前SQL语句:【{sql}】,参数:【{string.Join(",", pars.Select(t => t.Value))}】");
- };
- });
-
- services.AddSingleton<ISqlSugarClient>(sqlSugar);//这边是SqlSugarScope用AddSingleton
- }
- /// <summary>
- /// 附默认值
- /// </summary>
- /// <param name="db"></param>
- private static void DataExecuting(SqlSugarClient db)
- {
- //全局字段赋值
- db.Aop.DataExecuting = (oldValue, entityInfo) =>
- {
- InsertByObject(entityInfo);
- //UpdateByObject(oldValue, entityInfo);
- };
-
- }
- /// <summary>
- ///插入数据时附默认值
- /// </summary>
- /// <param name="entityInfo"></param>
- private static void InsertByObject(DataFilterModel entityInfo)
- {
- if (entityInfo.OperationType != DataFilterType.InsertByObject) return;
- switch (entityInfo.PropertyName)
- {
- case "CreateAt":
- entityInfo.SetValue(DateTime.Now);
- break;
- case "CreateBy":
- entityInfo.SetValue("");
- break;
- case "GroupId":
- entityInfo.SetValue(App.User?.FindFirst(App.User?.FindFirst(ClaimConst.GroupId)?.Value == null?CurrentUser.GroupId: App.User?.FindFirst(ClaimConst.GroupId)?.Value));
- break;
- }
- }
- }
- }
|