Browse Source

id生成 雪花函数

CrazyIter 4 years ago
parent
commit
98381e1d0b

+ 5 - 4
TEAMModelOS/Controllers/Analysis/ChangeController.cs

@@ -24,13 +24,14 @@ namespace TEAMModelOS.Controllers.Analysis
     [ApiController]
     [ApiController]
     public class ChangeController : Controller
     public class ChangeController : Controller
     {
     {
-
+        private SnowflakeId SnowflakeId;
         private readonly IWebHostEnvironment _hostingEnvironment;
         private readonly IWebHostEnvironment _hostingEnvironment;
         public AzureCosmosFactory _cosmosrepository;
         public AzureCosmosFactory _cosmosrepository;
         public AzureStorageFactory azureBlobDB;
         public AzureStorageFactory azureBlobDB;
         private const string CacheCosmosPrefix = "Analysis:";
         private const string CacheCosmosPrefix = "Analysis:";
-        public ChangeController(IWebHostEnvironment hostingEnvironment, AzureCosmosFactory cosmosDBRepository, AzureStorageFactory _azureBlobDB)
+        public ChangeController(IWebHostEnvironment hostingEnvironment, AzureCosmosFactory cosmosDBRepository, AzureStorageFactory _azureBlobDB, SnowflakeId _SnowflakeId)
         {
         {
+            SnowflakeId= _SnowflakeId;
             _hostingEnvironment = hostingEnvironment;
             _hostingEnvironment = hostingEnvironment;
             _cosmosrepository = cosmosDBRepository;
             _cosmosrepository = cosmosDBRepository;
             azureBlobDB = _azureBlobDB;
             azureBlobDB = _azureBlobDB;
@@ -170,7 +171,7 @@ namespace TEAMModelOS.Controllers.Analysis
                 //考试基本信息
                 //考试基本信息
                 examInfo = new ExamInfo
                 examInfo = new ExamInfo
                 {
                 {
-                    id = System.Guid.NewGuid().ToString(),
+                    id = SnowflakeId.NextId()+"",
                     code = "88924E4C-F00D-4A1D-B2C3-234F930F29D4",
                     code = "88924E4C-F00D-4A1D-B2C3-234F930F29D4",
                     name = s.ex_name,
                     name = s.ex_name,
                     type = s.ex_type,
                     type = s.ex_type,
@@ -197,7 +198,7 @@ namespace TEAMModelOS.Controllers.Analysis
                 string schoolName = x.info.school_name;
                 string schoolName = x.info.school_name;
                 Classroom classInfo = new Classroom
                 Classroom classInfo = new Classroom
                 {
                 {
-                    id = System.Guid.NewGuid().ToString(),
+                    id = SnowflakeId.NextId() + "",
                     classroomName = key,
                     classroomName = key,
                     //classroomType = schoolName,
                     //classroomType = schoolName,
                     studentCount = k,
                     studentCount = k,

+ 4 - 2
TEAMModelOS/Controllers/Syllabus/KnowledgeController.cs

@@ -21,11 +21,13 @@ namespace TEAMModelOS.Controllers
     //[Authorize]
     //[Authorize]
     public class KnowledgeController : BaseController
     public class KnowledgeController : BaseController
     {
     {
+        private SnowflakeId SnowflakeId;
         private AzureStorageFactory _table;
         private AzureStorageFactory _table;
         private AzureCosmosFactory _cosmos; 
         private AzureCosmosFactory _cosmos; 
 
 
-        public KnowledgeController(AzureStorageFactory table, AzureCosmosFactory cosmos )
+        public KnowledgeController(AzureStorageFactory table, AzureCosmosFactory cosmos , SnowflakeId _SnowflakeId)
         {
         {
+            SnowflakeId = _SnowflakeId;
             _table = table;
             _table = table;
             _cosmos = cosmos;
             _cosmos = cosmos;
           
           
@@ -288,7 +290,7 @@ namespace TEAMModelOS.Controllers
             ResponseBuilder builder = ResponseBuilder.custom();
             ResponseBuilder builder = ResponseBuilder.custom();
             if (request.@params.IsNotEmpty())
             if (request.@params.IsNotEmpty())
             {
             {
-                List<Knowledge> ts = await KnowledgeService.SaveOrUpdateKnowledge(_cosmos,request.@params);
+                List<Knowledge> ts = await KnowledgeService.SaveOrUpdateKnowledge(_cosmos,SnowflakeId,request.@params);
                 if (ts.Count > 0) builder.Data(ts).Extend(new Dictionary<string, object> { { "count", ts.Count } });
                 if (ts.Count > 0) builder.Data(ts).Extend(new Dictionary<string, object> { { "count", ts.Count } });
                 else {
                 else {
                     return builder.Error(ResponseCode.FAILED, "失败!").build();
                     return builder.Error(ResponseCode.FAILED, "失败!").build();

+ 2 - 2
TEAMModelOS/Service/Syllabus/KnowledgeService.cs

@@ -26,13 +26,13 @@ namespace TEAMModelOS.Service.Services.Implement
             return idPks;
             return idPks;
         }
         }
 
 
-        public static async Task<List<Knowledge>> SaveOrUpdateKnowledge(this AzureCosmosFactory _cosmos, List<Knowledge> request)
+        public static async Task<List<Knowledge>> SaveOrUpdateKnowledge(this AzureCosmosFactory _cosmos, SnowflakeId snowflakeId, List<Knowledge> request)
         {
         {
             foreach (Knowledge item in request)
             foreach (Knowledge item in request)
             {
             {
                 if (item.id == null)
                 if (item.id == null)
                 {
                 {
-                    item.id = Guid.NewGuid().ToString();
+                    item.id = snowflakeId.NextId()+"";
                 }
                 }
                 if (item.type == 1 && item.points.Count > 0)
                 if (item.type == 1 && item.points.Count > 0)
                 {
                 {