基础服务api
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

90 lines
4.0 KiB

  1. using BPA.SAAS.Manage.Comm.Const;
  2. using BPA.SAAS.Manage.Core.Base;
  3. using Furion;
  4. using Microsoft.Extensions.Configuration;
  5. using Microsoft.Extensions.DependencyInjection;
  6. using SqlSugar;
  7. using System;
  8. using System.Collections.Generic;
  9. using System.Linq;
  10. using System.Text.RegularExpressions;
  11. namespace BPA.SAAS.Manage.Core
  12. {
  13. /// <summary>
  14. /// 数据库上下文对象
  15. /// </summary>
  16. public static class DbContext
  17. {
  18. /// <summary>
  19. /// SqlSugar 数据库实例
  20. /// </summary>
  21. public static void AddSqlsugarSetup(this IServiceCollection services, IConfiguration configuration)
  22. {
  23. var connectionConfigs = App.GetConfig<List<ConnectionConfig>>("ConnectionConfigs");
  24. //如果多个数数据库传 List<ConnectionConfig>
  25. var configConnection = new ConnectionConfig()
  26. {
  27. ConnectionString = connectionConfigs[0].ConnectionString,
  28. DbType = DbType.MySql,//设置数据库类型
  29. IsAutoCloseConnection = true,//自动释放数据务,如果存在事务,在事务结束后释放
  30. InitKeyType = InitKeyType.Attribute, //从实体特性中读取主键自增列信息
  31. ConfigId = "default",
  32. };
  33. SqlSugarScope sqlSugar = new SqlSugarScope(configConnection,
  34. db =>
  35. {
  36. db.QueryFilter.AddTableFilter<IDeleted>(it => it.IsDeleted == 0);
  37. //db.QueryFilter.AddTableFilter<IStatus>(it => it.Status == 0);
  38. if (!IsSuperAdmin() && App.User.FindFirst(ClaimConst.CLAINM_SUPERADMIN)?.Value != null)
  39. {
  40. //非管理员账户过滤加盟商数据
  41. var groupId = App.User?.FindFirst(ClaimConst.GroupId)?.Value;
  42. db.QueryFilter.AddTableFilter<IGroupId>(it => it.GroupId == groupId);
  43. }
  44. db.Aop.DataExecuting = (oldValue, entityInfo) =>
  45. {
  46. //新增添加加盟商id赋值
  47. var groupId = App.User?.FindFirst(ClaimConst.GroupId)?.Value;
  48. if (entityInfo.PropertyName == "GroupId" && entityInfo.OperationType == DataFilterType.InsertByObject)
  49. {
  50. if (oldValue == null || oldValue == Convert.DBNull)
  51. {
  52. entityInfo.SetValue(groupId);
  53. }
  54. }
  55. };
  56. //单例参数配置,所有上下文生效
  57. db.Aop.OnLogExecuting = (sql, pars) =>
  58. {
  59. //过滤已删除数据
  60. //db.QueryFilter.AddTableFilter<IDeleted>(it => it.IsDeleted == 0);
  61. ////db.QueryFilter.AddTableFilter<IStatus>(it => it.Status == 0);
  62. //if (!IsSuperAdmin())
  63. //{
  64. // //非管理员账户过滤加盟商数据
  65. // var groupId = App.User?.FindFirst(ClaimConst.GroupId)?.Value;
  66. // db.QueryFilter.AddTableFilter<IGroupId>(it => it.GroupId == groupId);
  67. //}
  68. //Console.WriteLine(sql);//输出sql
  69. Console.WriteLine($"当前SQL语句:【{sql}】,参数:【{string.Join(",", pars.Select(t => t.Value))}】");
  70. };
  71. });
  72. services.AddSingleton<ISqlSugarClient>(sqlSugar);//这边是SqlSugarScope用AddSingleton
  73. }
  74. // <summary>
  75. /// 判断是不是超级管理员
  76. /// </summary>
  77. /// <returns></returns>
  78. private static bool IsSuperAdmin()
  79. {
  80. if (App.User == null) return false;
  81. var sd = App.User.FindFirst(ClaimConst.CLAINM_SUPERADMIN)?.Value;
  82. return App.User.FindFirst(ClaimConst.CLAINM_SUPERADMIN)?.Value == "1";
  83. }
  84. }
  85. }