基础服务api
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

92 rindas
4.1 KiB

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