Ver Fonte

完成账单接口。

CrazyIter_Bin há 1 ano atrás
pai
commit
87cb3de4c9
1 ficheiros alterados com 205 adições e 7 exclusões
  1. 205 7
      TEAMModelOS/Controllers/XTest/BillController.cs

+ 205 - 7
TEAMModelOS/Controllers/XTest/BillController.cs

@@ -52,15 +52,47 @@ namespace TEAMModelOS.Controllers.XTest
             _configuration = configuration;
             _azureStorage = azureStorage;
         }
-
         [HttpPost("report")]
         [RequestSizeLimit(102_400_000_00)] //最大10000m左右
-        public async Task<IActionResult> Read(JsonElement json)
+        public async Task<IActionResult> Report(JsonElement json)
         {
             try {
-                Dictionary<string, IEnumerable<BillDetail>> billDict = new Dictionary<string, IEnumerable<BillDetail>>();
+
+                List<KeyBillDetail> monthData = new List<KeyBillDetail>();
+                List<DayBillDetail> dayData = new List<DayBillDetail>();
                 string BillToken = _configuration.GetValue<string>("Azure:Bill:Token");
                 List<string> times = json.GetProperty("times").ToObject<List<string>>();
+
+                List<string> ResourceGroup = new List<string>();
+                List<string> Product = new List<string>();
+                List<string> MeterCategory = new List<string>();
+                List<string> MeterSubCategory = new List<string>();
+                List<string> MeterName = new List<string>();
+                List<string> ConsumedService = new List<string>();
+                
+                if (json.TryGetProperty("ResourceGroup", out JsonElement _ResourceGroup)) {
+                    ResourceGroup= _ResourceGroup.ToObject<List<string>>();
+                }
+                if (json.TryGetProperty("Product", out JsonElement _Product))
+                {
+                    Product= _Product.ToObject<List<string>>();
+                }
+                if (json.TryGetProperty("MeterCategory", out JsonElement _MeterCategory))
+                {
+                    MeterCategory= _MeterCategory.ToObject<List<string>>();
+                }
+                if (json.TryGetProperty("MeterSubCategory", out JsonElement _MeterSubCategory))
+                {
+                    MeterSubCategory= _MeterSubCategory.ToObject<List<string>>();
+                }
+                if (json.TryGetProperty("MeterName", out JsonElement _MeterName))
+                {
+                    MeterName= _MeterName.ToObject<List<string>>();
+                }
+                if (json.TryGetProperty("ConsumedService", out JsonElement _ConsumedService))
+                {
+                    ConsumedService= _ConsumedService.ToObject<List<string>>();
+                }
                 var httpClient = _httpClient.CreateClient();
                 httpClient.DefaultRequestHeaders.Remove("Authorization");
                 httpClient.DefaultRequestHeaders.Remove("api-version");
@@ -76,6 +108,8 @@ namespace TEAMModelOS.Controllers.XTest
                 }
 
                 Dictionary<string, HashSet<string>> dict = new Dictionary<string, HashSet<string>>();
+                Dictionary<string, HashSet<string>> dictCond = new Dictionary<string, HashSet<string>>();
+             
                 var BlobClientDict = _azureStorage.GetBlobContainerClient("teammodelos").GetBlobClient($"bill/dict.json");
                 if (BlobClientDict.Exists())
                 {
@@ -232,22 +266,186 @@ namespace TEAMModelOS.Controllers.XTest
                             }
                         }
                     }
-                   
+                    if (ResourceGroup.IsNotEmpty()) {
+                        billDetails=  billDetails.Where(x => ResourceGroup.Contains(x.ResourceGroup));
+                    }
+                    if (Product.IsNotEmpty())
+                    {
+                        billDetails=  billDetails.Where(x => Product.Contains(x.Product));
+                    }
+                    if (MeterCategory.IsNotEmpty())
+                    {
+                        billDetails=  billDetails.Where(x => MeterCategory.Contains(x.MeterCategory));
+                    }
+                    if (MeterSubCategory.IsNotEmpty())
+                    {
+                        billDetails=  billDetails.Where(x => MeterSubCategory.Contains(x.MeterSubCategory));
+                    }
+                    if (MeterName.IsNotEmpty())
+                    {
+                        billDetails=  billDetails.Where(x => MeterName.Contains(x.MeterName));
+                    }
+                    if (ConsumedService.IsNotEmpty())
+                    {
+                        billDetails=  billDetails.Where(x => ConsumedService.Contains(x.ConsumedService));
+                    }
+                    {
+                        //MeterCategory 服务
+                        {
+                            var newKey = billDetails.Select(x => x.MeterCategory).ToHashSet();
+                            if (dictCond.ContainsKey("MeterCategory"))
+                            {
+                                var moreKey = newKey.Except(dictCond["MeterCategory"]);
+                                foreach (var key in moreKey)
+                                {
+                                    change=true;
+                                    dictCond["MeterCategory"].Add(key);
+                                }
+                            }
+                            else
+                            {
+                                change=true;
+                                dictCond.Add("MeterCategory", newKey);
+                            }
+                        }
+                        //MeterSubCategory 服务类型
+                        {
+                            var newKey = billDetails.Select(x => x.MeterSubCategory).ToHashSet();
+                            if (dictCond.ContainsKey("MeterSubCategory"))
+                            {
+                                var moreKey = newKey.Except(dictCond["MeterSubCategory"]);
+                                foreach (var key in moreKey)
+                                {
+                                    change=true;
+                                    dictCond["MeterSubCategory"].Add(key);
+                                }
+                            }
+                            else
+                            {
+                                change=true;
+                                dictCond.Add("MeterSubCategory", newKey);
+                            }
+                        }
+                        //ConsumedService 服务信息
+                        {
+                            var newKey = billDetails.Select(x => x.ConsumedService).ToHashSet();
+                            if (dictCond.ContainsKey("ConsumedService"))
+                            {
+                                var moreKey = newKey.Except(dictCond["ConsumedService"]);
+                                foreach (var key in moreKey)
+                                {
+                                    change=true;
+                                    dictCond["ConsumedService"].Add(key);
+                                }
+                            }
+                            else
+                            {
+                                change=true;
+                                dictCond.Add("ConsumedService", newKey);
+                            }
+                        }
+                        //ResourceGroup 资源组
+                        {
+                            var newKey = billDetails.Select(x => x.ResourceGroup.ToUpper()).ToHashSet();
+                            if (dictCond.ContainsKey("ResourceGroup"))
+                            {
+                                var moreKey = newKey.Except(dictCond["ResourceGroup"]);
+                                foreach (var key in moreKey)
+                                {
+                                    change=true;
+                                    dictCond["ResourceGroup"].Add(key);
+                                }
+                            }
+                            else
+                            {
+                                change=true;
+                                dictCond.Add("ResourceGroup", newKey);
+                            }
+                        }
+                        //MeterName 服务资源
+                        {
+                            var newKey = billDetails.Select(x => x.MeterName).ToHashSet();
+                            if (dictCond.ContainsKey("MeterName"))
+                            {
+                                var moreKey = newKey.Except(dictCond["MeterName"]);
+                                foreach (var key in moreKey)
+                                {
+                                    change=true;
+                                    dictCond["MeterName"].Add(key);
+                                }
+                            }
+                            else
+                            {
+                                change=true;
+                                dictCond.Add("MeterName", newKey);
+                            }
+                        }
+                        //Product  产品
+                        {
+                            var newKey = billDetails.Select(x => x.Product).ToHashSet();
+                            if (dictCond.ContainsKey("Product"))
+                            {
+                                var moreKey = newKey.Except(dictCond["Product"]);
+                                foreach (var key in moreKey)
+                                {
+                                    change=true;
+                                    dictCond["Product"].Add(key);
+                                }
+                            }
+                            else
+                            {
+                                change=true;
+                                dictCond.Add("Product", newKey);
+                            }
+                        }
+                    }
+                    monthData.Add(new KeyBillDetail { key=time, 
+                        cost= billDetails.Sum(x=>x.ExtendedCost)
                     
-                    billDict.Add(time, billDetails);
+                    });
+
+                    DayBillDetail dayBillDetail = new DayBillDetail() { month= time};
+                    for (int i = 1; i<=31; i++)
+                    {
+                        var day = billDetails.Where(x => x.Day==i);
+                        if (day!=null  && day.Count()>0)
+                        {
+                            dayBillDetail.bills.Add(new KeyBillDetail
+                            {
+                                key=$"{i}",
+                                cost= day.Sum(x =>x.ExtendedCost)
+                            });
+                        }
+                        else
+                        {
+                            dayBillDetail.bills.Add(new KeyBillDetail { key=$"{i}",cost=0 });
+                        }
+                    }
+                    dayData.Add(dayBillDetail);
                 }
                 if (change)
                 {
                     await _azureStorage.GetBlobContainerClient("teammodelos").UploadFileByContainer(dict.ToJsonString(), "bill", $"dict.json", true);
                 }
-                return Ok(new { data = billDict,dict ,code=200});
+                return Ok(new {   monthData, dayData, dict, dictCond, code=200});
             } catch (Exception ex) {
                 return Ok(new { code = 500 });
             }
            
         }
 
-
+        public class KeyBillDetail {
+            public string key { get; set; }
+            public double cost { get; set; }
+        }
+        public class DayBillDetail
+        {
+            public string month { get; set; }
+            /// <summary>
+            /// 每天的数据
+            /// </summary>
+            public List<KeyBillDetail> bills { get; set; } = new List<KeyBillDetail>();
+        }
         public class BillDetail
         {
             /// <summary>