后厨api
25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

83 lines
3.6 KiB

  1. using BPA.SAAS.KitChenManage.Comm.Const;
  2. using BPA.SAAS.KitChenManage.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.Data;
  10. using System.Linq;
  11. namespace BPA.SAAS.KitChenManage.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 = SqlSugar.DbType.MySql,//设置数据库类型
  29. IsAutoCloseConnection = true,//自动释放数据务,如果存在事务,在事务结束后释放
  30. InitKeyType = InitKeyType.Attribute, //从实体特性中读取主键自增列信息
  31. ConfigId = "default",
  32. };
  33. SqlSugarScope sqlSugar = new SqlSugarScope(configConnection,
  34. db =>
  35. {
  36. //过滤已删除数据
  37. db.QueryFilter.AddTableFilter<IDeleted>(it => it.IsDeleted == 0);
  38. //db.QueryFilter.AddTableFilter<IStatus>(it => it.Status == 0);
  39. if (App.User?.FindFirst(ClaimConst.CLAINM_SUPERADMIN)?.Value != null)
  40. {
  41. //非管理员账户过滤加盟商数据
  42. var groupId = App.User?.FindFirst(ClaimConst.GroupId)?.Value != null ? App.User?.FindFirst(ClaimConst.GroupId)?.Value : App.HttpContext.Request.Headers["groupId"].ToString();
  43. db.QueryFilter.AddTableFilter<IGroupId>(it => it.GroupId == groupId);
  44. }
  45. db.Aop.DataExecuting = (oldValue, entityInfo) =>
  46. {
  47. //新增添加加盟商id赋值
  48. var groupId = App.User?.FindFirst(ClaimConst.GroupId)?.Value != null ? App.User?.FindFirst(ClaimConst.GroupId)?.Value : App.HttpContext?.Request.Headers["groupId"].ToString();
  49. if (entityInfo.PropertyName == "GroupId" && entityInfo.OperationType == DataFilterType.InsertByObject)
  50. {
  51. if (oldValue == null || oldValue == Convert.DBNull)
  52. {
  53. entityInfo.SetValue(groupId);
  54. }
  55. }
  56. };
  57. //单例参数配置,所有上下文生效
  58. db.Aop.OnLogExecuting = (sql, pars) =>
  59. {
  60. //Console.WriteLine(sql);//输出sql
  61. Console.WriteLine($"当前SQL语句:【{sql}】,参数:【{string.Join(",", pars.Select(t => t.Value))}】");
  62. };
  63. });
  64. services.AddSingleton<ISqlSugarClient>(sqlSugar);//这边是SqlSugarScope用AddSingleton
  65. }
  66. // <summary>
  67. /// 判断是不是超级管理员
  68. /// </summary>
  69. /// <returns></returns>
  70. private static bool IsSuperAdmin()
  71. {
  72. if (App.User == null) return false;
  73. return App.User.FindFirst(ClaimConst.CLAINM_SUPERADMIN)?.Value == "1";
  74. }
  75. }
  76. }