From 8e447a040f2f811dd53181abb6598f691bda6d4d Mon Sep 17 00:00:00 2001
From: KharchenkoVladimir <vv.kharchenko@autodrilling.ru>
Date: Thu, 14 Oct 2021 12:04:21 +0500
Subject: [PATCH] Added method for returning wells with drill params only (in
 DepositController)

---
 AsbCloudApp/Services/IClusterService.cs       |  2 +
 .../Services/ClusterService.cs                | 89 +++++++++++++------
 .../Controllers/DepositController.cs          | 19 ++++
 3 files changed, 81 insertions(+), 29 deletions(-)

diff --git a/AsbCloudApp/Services/IClusterService.cs b/AsbCloudApp/Services/IClusterService.cs
index dacd8f4f..66952f21 100644
--- a/AsbCloudApp/Services/IClusterService.cs
+++ b/AsbCloudApp/Services/IClusterService.cs
@@ -9,6 +9,8 @@ namespace AsbCloudApp.Services
     {
         Task<IEnumerable<DepositDto>> GetDepositsAsync(int idCompany,
             CancellationToken token);
+        Task<IEnumerable<DepositDto>> GetDepositsDrillParamsAsync(int idCompany,
+            CancellationToken token = default);
         Task<IEnumerable<ClusterDto>> GetClustersAsync(int idCompany,
             int depositId, CancellationToken token);
         Task<IEnumerable<ClusterDto>> GetClustersAsync(int idCompany,
diff --git a/AsbCloudInfrastructure/Services/ClusterService.cs b/AsbCloudInfrastructure/Services/ClusterService.cs
index e04e8ea9..88e8515e 100644
--- a/AsbCloudInfrastructure/Services/ClusterService.cs
+++ b/AsbCloudInfrastructure/Services/ClusterService.cs
@@ -31,36 +31,30 @@ namespace AsbCloudInfrastructure.Services
                                       select well).ToListAsync(token)
                                 .ConfigureAwait(false);
 
-            var gDepositEntities = wellEntities
-                .GroupBy(w => w.Cluster)
-                .GroupBy(c => c.Key.Deposit);
+            var gDepositEntities = groupWells(wellEntities);
 
-            var dtos = gDepositEntities.Select(gDeposit => new DepositDto
-            {
-                Id = gDeposit.Key.Id,
-                Caption = gDeposit.Key.Caption,
-                Latitude = gDeposit.Key.Latitude,
-                Longitude = gDeposit.Key.Longitude,
-                Description = "",
-                Clusters = gDeposit.Select(gCluster => new ClusterDto
-                {
-                    Id = gCluster.Key.Id,
-                    Caption = gCluster.Key.Caption,
-                    Latitude = gCluster.Key.Latitude,
-                    Longitude = gCluster.Key.Longitude,
-                    Description = "",
-                    Wells = gCluster.Select(well => new WellDto
-                    {
-                        Id = well.Id,
-                        Caption = well.Caption,
-                        Latitude = well.Latitude,
-                        Longitude = well.Longitude,
-                        WellType = well.WellType?.Caption,
-                        Cluster = gCluster.Key.Caption,
-                        Deposit = gDeposit.Key.Caption,
-                    }),
-                }),
-            });
+            var dtos = CreateDepositDto(gDepositEntities);
+
+            return dtos;
+        }
+        
+        public async Task<IEnumerable<DepositDto>> GetDepositsDrillParamsAsync(int idCompany,
+            CancellationToken token = default)
+        {
+            var wellEntities = await (from well in db.Wells
+                        .Include(w => w.RelationCompaniesWells)
+                        .Include(w => w.WellType)
+                        .Include(w => w.Cluster)
+                        .ThenInclude(c => c.Deposit)
+                            from p in db.DrillParams
+                            where well.RelationCompaniesWells.Any(r => r.IdCompany == idCompany) &&
+                                  well.Id == p.IdWell
+                            select well).ToListAsync(token)
+                        .ConfigureAwait(false);
+
+            var gDepositEntities = groupWells(wellEntities);
+
+            var dtos = CreateDepositDto(gDepositEntities);
 
             return dtos;
         }
@@ -117,5 +111,42 @@ namespace AsbCloudInfrastructure.Services
 
             return dtos;
         }
+
+        private IEnumerable<IGrouping<Deposit, IGrouping<Cluster, Well>>> groupWells(IEnumerable<Well> wellEntities)
+        {
+            return wellEntities
+                .GroupBy(w => w.Cluster)
+                .GroupBy(c => c.Key.Deposit);
+        }
+
+        private IEnumerable<DepositDto> CreateDepositDto(IEnumerable<IGrouping<Deposit,IGrouping<Cluster, Well>>>gDepositEntities)
+        {
+            return gDepositEntities.Select(gDeposit => new DepositDto
+            {
+                Id = gDeposit.Key.Id,
+                Caption = gDeposit.Key.Caption,
+                Latitude = gDeposit.Key.Latitude,
+                Longitude = gDeposit.Key.Longitude,
+                Description = "",
+                Clusters = gDeposit.Select(gCluster => new ClusterDto
+                {
+                    Id = gCluster.Key.Id,
+                    Caption = gCluster.Key.Caption,
+                    Latitude = gCluster.Key.Latitude,
+                    Longitude = gCluster.Key.Longitude,
+                    Description = "",
+                    Wells = gCluster.Select(well => new WellDto
+                    {
+                        Id = well.Id,
+                        Caption = well.Caption,
+                        Latitude = well.Latitude,
+                        Longitude = well.Longitude,
+                        WellType = well.WellType?.Caption,
+                        Cluster = gCluster.Key.Caption,
+                        Deposit = gDeposit.Key.Caption,
+                    }),
+                }),
+            });
+        }
     }
 }
diff --git a/AsbCloudWebApi/Controllers/DepositController.cs b/AsbCloudWebApi/Controllers/DepositController.cs
index 4fadce32..6bfac7f9 100644
--- a/AsbCloudWebApi/Controllers/DepositController.cs
+++ b/AsbCloudWebApi/Controllers/DepositController.cs
@@ -41,6 +41,25 @@ namespace AsbCloudWebApi.Controllers
                 token).ConfigureAwait(false);
             return Ok(result);
         }
+        
+        /// <summary>
+        /// Получает список доступных пользователю месторождений (только скважины с параметрами бурения) 
+        /// </summary>
+        /// <param name="token"> Токен отмены задачи </param>
+        /// <returns></returns>
+        [HttpGet("drillParamsWells")]
+        [ProducesResponseType(typeof(IEnumerable<DepositDto>), (int)System.Net.HttpStatusCode.OK)]
+        public async Task<IActionResult> GetDepositsDrillParamsAsync(CancellationToken token = default)
+        {
+            int? idCompany = User.GetCompanyId();
+
+            if (idCompany is null)
+                return Forbid();
+
+            var result = await clusterService.GetDepositsDrillParamsAsync((int)idCompany,
+                token).ConfigureAwait(false);
+            return Ok(result);
+        }
 
         /// <summary>
         /// Получает список доступных пользователю кустов месторождения