Jelajahi Sumber

优化用户关联和移除企业信息接口

Li 2 tahun lalu
induk
melakukan
14e6bc59a4
1 mengubah file dengan 54 tambahan dan 26 penghapusan
  1. 54 26
      TEAMModelBI/Controllers/BINormal/BusinessUsersController.cs

+ 54 - 26
TEAMModelBI/Controllers/BINormal/BusinessUsersController.cs

@@ -6,6 +6,7 @@ using Microsoft.Extensions.Options;
 using System;
 using System.Collections.Generic;
 using System.Linq;
+using System.Reflection;
 using System.Text;
 using System.Text.Json;
 using System.Threading.Tasks;
@@ -148,7 +149,7 @@ namespace TEAMModelBI.Controllers.BINormal
             var cosmosClient = _azureCosmos.GetCosmosClient();
             if (!jsonElement.TryGetProperty("mobile", out JsonElement mobile)) return BadRequest();
 
-            string sql = $"select value(c) from c where c.mobile ={mobile}";
+            string sql = $"select value(c) from c where c.mobile ='{mobile}'";
 
             List<BizUsers> bizUsers = new();
             await foreach (var item in cosmosClient.GetContainer(Constant.TEAMModelOS,"Normal").GetItemQueryIterator<BizUsers>(queryText:sql,requestOptions:new QueryRequestOptions() { PartitionKey = new PartitionKey("BizUsers") }))
@@ -218,14 +219,14 @@ namespace TEAMModelBI.Controllers.BINormal
         [HttpPost("rel-biz")]
         public async Task<ActionResult> RelationBusiness(JsonElement jsonElement) 
         {
-            if(!jsonElement.TryGetProperty("id", out JsonElement id)) return BadRequest();
+            if(!jsonElement.TryGetProperty("id", out JsonElement _id)) return BadRequest();
             if (!jsonElement.TryGetProperty("bizs", out JsonElement _bizs)) return BadRequest();
             if (!jsonElement.TryGetProperty("type", out JsonElement type)) return BadRequest();
+            if (!jsonElement.TryGetProperty("userType", out JsonElement userType)) return BadRequest();
             //jsonElement.TryGetProperty("site", out JsonElement site);//分开部署,就不需要,一站多用时,取消注释
             var (tmdId, tmdName, pic, _, _, _) = HttpJwtAnalysis.JwtXAuthBI(HttpContext.GetXAuth("AuthToken"), _option);
 
             List<BizRel> bizRels = _bizs.ToObject<List<BizRel>>();
-
             var cosmosClient = _azureCosmos.GetCosmosClient();
             var tableClient = _azureStorage.GetCloudTableClient();
             var blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public");
@@ -236,8 +237,31 @@ namespace TEAMModelBI.Controllers.BINormal
             //    tableClient = _azureStorage.GetCloudTableClient(BIConst.Global);
             //    blobClient = _azureStorage.GetBlobContainerClient(containerName: "0-public", BIConst.Global);
             //}
-
+            string id = $"{_id}";
             StringBuilder strMsg = new($"{tmdName}[{tmdId}]给");
+
+            BizUsers bizUsers = new();
+
+            List<BizRel> noBizRel = new();
+
+            if ($"{userType}".Equals("tmdId"))
+            {
+                var coreUser = await _coreAPIHttpService.GetUserInfo(new Dictionary<string, string> { { "key", $"{id}" } }, _option.Location, _configuration);
+                string salt = Utils.CreatSaltString(8);
+                bizUsers = new()
+                {
+                    id = Guid.NewGuid().ToString(),
+                    mobile = long.Parse($"{coreUser.mobile}"),
+                    tmdId = coreUser.id,
+                    code = "BizUsers",
+                    name = string.IsNullOrEmpty(bizUsers.name) ? bizUsers.mobile.ToString() : bizUsers.name,
+                    salt = salt,
+                    pwd = string.IsNullOrEmpty(bizUsers.pwd) ? Utils.HashedPassword(bizUsers.mobile.ToString(), salt) : Utils.HashedPassword(bizUsers.pwd, salt),
+                };
+
+                id = bizUsers.id;
+            }
+
             if ($"{type}".Equals("add"))
             {
                 strMsg.Append($"用户【{id}】关联企业信息:添加企业:");
@@ -248,42 +272,46 @@ namespace TEAMModelBI.Controllers.BINormal
             }
             else { return Ok(new { state = RespondCode.ParamsError, msg = "类型错误" }); }
 
-            List<BizRel> noBizRel = new();
-            BizUsers bizUsers = new();
-
             var response = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReadItemStreamAsync($"{id}", new PartitionKey("BizUsers"));
             if (response.Status == RespondCode.Ok)
             {
                 using var json = await JsonDocument.ParseAsync(response.ContentStream);
                 bizUsers = json.ToObject<BizUsers>();
-                foreach (var item in bizRels)
+            }
+
+            foreach (var item in bizRels)
+            {
+                var temp = bizUsers.relation.Find(f => f.bizId == item.bizId);
+                if ($"{type}".Equals("add"))
                 {
-                    var temp = bizUsers.relation.Find(f => f.bizId == item.bizId);
-                    if ($"{type}".Equals("add"))
+                    if (temp != null)
+                        noBizRel.Add(item);
+                    else
                     {
-                        if (temp != null)
-                            noBizRel.Add(item);
-                        else
-                        {
-                            bizUsers.relation.Add(item);
-                            strMsg.Append($"{item.name}[{item.bizId}]|");
-                        }
+                        bizUsers.relation.Add(item);
+                        strMsg.Append($"{item.name}[{item.bizId}]|");
                     }
-                    else if ($"{type}".Equals("del"))
+                }
+                else if ($"{type}".Equals("del"))
+                {
+                    if (temp != null)
                     {
-                        if (temp != null)
-                        {
-                            bizUsers.relation.Add(item);
-                            strMsg.Append($"{item.name}[{item.bizId}]|");
-                        }
-                        else
-                            noBizRel.Add(item);
+                        bizUsers.relation.Add(item);
+                        strMsg.Append($"{item.name}[{item.bizId}]|");
                     }
+                    else
+                        noBizRel.Add(item);
                 }
+            }
 
+            if (response.Status == RespondCode.Ok) 
+            {
                 bizUsers = await cosmosClient.GetContainer("TEAMModelOS", "Normal").ReplaceItemAsync<BizUsers>(bizUsers, bizUsers.id, new PartitionKey("BizUsers"));
             }
-            else return Ok(new { state = RespondCode.NotFound, msg = "未找到该用户" });
+            else
+            {
+                bizUsers = await cosmosClient.GetContainer("TEAMModelOS", "Normal").CreateItemAsync<BizUsers>(bizUsers, new PartitionKey("BizUsers"));
+            }
 
             //保存操作记录
             await AzureStorageBlobExtensions.SaveBILog(blobClient, tableClient, "bizconfig-addSchool", strMsg.ToString(), _dingDing, httpContext: HttpContext);