qinchaoyue 1 månad sedan
förälder
incheckning
9f81211ba4

+ 12 - 4
src/Hotline.Api/Controllers/Snapshot/BiSnapshotController.cs

@@ -18,6 +18,9 @@ using Amazon.Runtime.Internal.Util;
 using System.Collections;
 using Mapster;
 using Hotline.Share.Dtos.Settings;
+using XF.Domain.Repository;
+using Hotline.Configurations;
+using Microsoft.Extensions.Options;
 
 namespace Hotline.Api.Controllers.Snapshot;
 
@@ -29,12 +32,16 @@ public class BiSnapshotController : BaseController
     private readonly IBiSnapshotApplication _biSnapshotApplication;
     private readonly IIndustryRepository _industryRepository;
     private readonly ISystemDicDataCacheManager _systemDicDataCacheManager;
+    private readonly IRepository<SystemArea> _areaRepository;
+    private readonly IOptionsSnapshot<AppConfiguration> _appOptions;
 
-    public BiSnapshotController(IBiSnapshotApplication biSnapshotApplication, IIndustryRepository industryRepository, ISystemDicDataCacheManager systemDicDataCacheManager)
+    public BiSnapshotController(IBiSnapshotApplication biSnapshotApplication, IIndustryRepository industryRepository, ISystemDicDataCacheManager systemDicDataCacheManager, IRepository<SystemArea> areaRepository, IOptionsSnapshot<AppConfiguration> appOptions)
     {
         _biSnapshotApplication = biSnapshotApplication;
         _industryRepository = industryRepository;
         _systemDicDataCacheManager = systemDicDataCacheManager;
+        _areaRepository = areaRepository;
+        _appOptions = appOptions;
     }
 
     /// <summary>
@@ -266,12 +273,13 @@ public class BiSnapshotController : BaseController
     [HttpGet("industry-statistics")]
     public async Task<IndustryStatisticsOutDto> IndustryStatisticsAsync([FromQuery] IndustryStatisticsInDto dto)
     {
+        var areaCode = _appOptions.Value.AreaCode;
         return new IndustryStatisticsOutDto
         {
-            Headers = await _industryRepository.Queryable().Where(m => m.IsEnable == true).Select(m => new SystemDicDataOutDto { 
+            Headers = await _areaRepository.Queryable().Where(m => m.ParentId == areaCode).Select(m => new SystemDicDataOutDto { 
                 Id = m.Id,
-                DicDataName = m.Name,
-                DicDataValue = m.Name,
+                DicDataName = m.AreaName,
+                DicDataValue = m.AreaName,
             }).ToListAsync(),
             Data = _biSnapshotApplication.GetIndustryStatistics(dto)
         };

+ 80 - 16
src/Hotline.Application/Snapshot/BiSnapshotApplication.cs

@@ -766,20 +766,20 @@ public class BiSnapshotApplication : IBiSnapshotApplication, IScopeDependency
     {
         var rawData = _orderSnapshotRepository.Queryable(includeDeleted: true)
             .LeftJoin<Order>((snapshot, order) => order.Id == snapshot.Id)
-            .Where((snapshot, order) => snapshot.SpecialReasonId != null)
+            .Where((snapshot, order) => snapshot.SpecialReasonId != null && snapshot.CreationTime >= dto.StartTime && snapshot.CreationTime <= dto.EndTime)
             .GroupBy((snapshot, order) => new
             {
                 OrgCode = order.ActualHandleOrgCode.Substring(SqlFunc.MappingColumn<int>("0"), SqlFunc.MappingColumn<int>("6")),
                 SpecialReasonId = snapshot.SpecialReasonId,
                 snapshot.SpecialReasonName
             })
-            .Select((snapshot, order) => new 
-            { 
+            .Select((snapshot, order) => new
+            {
                 OrgCode = order.ActualHandleOrgCode.Substring(SqlFunc.MappingColumn<int>("0"), SqlFunc.MappingColumn<int>("6")),
                 OrderCountNum = SqlFunc.AggregateCount(snapshot.Id),
                 SpecialReasonId = snapshot.SpecialReasonId,
                 SpecialReasonName = snapshot.SpecialReasonName
-            }, true) 
+            }, true)
             .MergeTable()
             .LeftJoin<SystemOrganize>((it, o) => it.OrgCode == o.Id)
             .Select((it, o) => new
@@ -810,7 +810,7 @@ public class BiSnapshotApplication : IBiSnapshotApplication, IScopeDependency
 
             foreach (var reason in specialReasons)
             {
-                row[reason] = 0; 
+                row[reason] = 0;
             }
 
             foreach (var item in orgGroup)
@@ -837,7 +837,7 @@ public class BiSnapshotApplication : IBiSnapshotApplication, IScopeDependency
             .Where((snapshot, order, special) => order.CreationTime >= dto.StartTime && order.CreationTime <= dto.EndTime && order.ActualHandleOrgCode.Substring(SqlFunc.MappingColumn<int>("0"), SqlFunc.MappingColumn<int>("6")) == dto.OrgCode && special.Id != null && snapshot.SpecialReasonId != null && special.SpecialType == ESpecialType.ReTransact)
             .WhereIF(dto.FieldName.NotNullOrEmpty(), (snapshot, order) => dto.FieldName == snapshot.SpecialReasonId)
             .OrderByDescending((snapshot, order) => order.CreationTime)
-            .Select((snapshot, order, special, org) => new ReTransactStatisticsDetailsOutDto 
+            .Select((snapshot, order, special, org) => new ReTransactStatisticsDetailsOutDto
             {
                 ReTransactOrgName = special.OrgName, // 被重办部门
                 ReTransactOneOrgName = org.Name, // 被重办一级部门
@@ -853,17 +853,81 @@ public class BiSnapshotApplication : IBiSnapshotApplication, IScopeDependency
 
     public List<Dictionary<string, object>> GetIndustryStatistics(IndustryStatisticsInDto dto)
     {
-        var query = _industryRepository.Queryable(includeDeleted: true)
-            .LeftJoin<OrderSnapshot>((industry, snapshot) => snapshot.IndustryId == industry.Id && snapshot.CreationTime >= dto.StartTime && snapshot.CreationTime <= dto.EndTime)
-            .LeftJoin<Order>((industry, snapshot, order) => order.Id == snapshot.Id)
-            .Where((industry) => industry.IsEnable == true)
-            .Select((industry, snapshot, order) => new
-            {
-                OrderCount = SqlFunc.AggregateSum(snapshot.Id),
-                order.AllDuration,
+        var parentId = _appOptions.Value.AreaCode;
+        var query = _systemAreaRepository.Queryable(includeDeleted: true)
+            .LeftJoin<Order>((area, order) => order.County == area.AreaName && order.CreationTime >= dto.StartTime && order.CreationTime <= dto.EndTime && order.County != null)
+            .LeftJoin<OrderSnapshot>((area, order, snapshot) => order.Id == snapshot.Id)
+            .Where((area, order, snapshot) => area.ParentId == parentId)
+            .GroupBy((area, order, snapshot) => new
+            {
+                //OrgCode = order.ActualHandleOrgCode.Substring(SqlFunc.MappingColumn<int>("0"), SqlFunc.MappingColumn<int>("6")),
+                IndustryId = snapshot.IndustryId,
+                snapshot.IndustryName,
+                area.AreaName
+            })
+            .Select((area, order, snapshot) => new
+            {
+                //OrgCode = order.ActualHandleOrgCode.Substring(SqlFunc.MappingColumn<int>("0"), SqlFunc.MappingColumn<int>("6")),
+                OrgName = area.AreaName,
+                OrderCountNum = SqlFunc.AggregateCount(snapshot.Id),
+                AllDuration = SqlFunc.AggregateSum(order.AllDuration),
                 IsEmphasis = SqlFunc.AggregateSum(SqlFunc.IIF(snapshot.IsEmphasis == true, 1, 0)),
+                IndustryName = snapshot.IndustryName,
+                IndustryId = snapshot.IndustryId,
+            })
+            .LeftJoin<Industry>((it, o) => it.IndustryId == o.Id)
+            .Select((it, o) => new
+            {
+                IndustryName = o.Name,
+                IndustryId = it.IndustryId,
+                OrderCountNum = it.OrderCountNum,
+                AllDuration = it.AllDuration,
+                OrgName = it.OrgName,
+                //OrgCode = it.OrgCode
             });
-        var outDto = new List<Dictionary<string, object>>();
-        return outDto;
+#if DEBUG
+        var sql = query.ToSqlString();
+#endif
+        var rawData = query.ToList(); ;
+        var pivotResult = new List<Dictionary<string, object>>();
+
+        var industryGroups = rawData.GroupBy(x => new { x.IndustryId, x.IndustryName });
+
+        var orgNames = rawData
+            .Select(x => x.OrgName)
+            .Distinct()
+            .ToList();
+
+        foreach (var industryItem in industryGroups)
+        {
+            if (industryItem.Key.IndustryName == null) continue;
+            var row = new Dictionary<string, object>
+            {
+                ["IndustryName"] = industryItem.Key.IndustryName,
+                ["IndustryId"] = industryItem.Key.IndustryId,
+                ["AllDuration"] = 0,
+            };
+
+            foreach (var orgCode in orgNames)
+            {
+                if (orgCode != null)
+                {
+                    row[orgCode] = 0;
+                }
+            }
+
+            foreach (var item in industryItem)
+            {
+                if (item.IndustryId != null)
+                {
+                    row[item.OrgName] = item.OrderCountNum;
+                    row["AllDuration"] = item.AllDuration;
+                }
+            }
+
+            pivotResult.Add(row);
+        }
+        pivotResult.AddSumLine("IndustryName");
+        return pivotResult;
     }
 }

+ 1 - 0
src/Hotline.Share/Tools/ListExtensions.cs

@@ -65,6 +65,7 @@ public static class ListExtensions
 
     public static void AddSumLine<T>(this IList<T> list, string fieldName)
     {
+        if (list == null || list.Count == 0) return;
         var type = typeof(T);
         if (typeof(T) == typeof(Dictionary<string, object>))
         {

+ 4 - 0
test/Hotline.Tests/Application/BiSnapshotApplicationTest.cs

@@ -14,6 +14,7 @@ using Shouldly;
 using System;
 using System.Collections.Generic;
 using System.Linq;
+using System.Linq.Dynamic.Core;
 using System.Text;
 using System.Threading.Tasks;
 using XF.Domain.Cache;
@@ -104,5 +105,8 @@ public class BiSnapshotApplicationTest : TestBase
         bInDto.OrgCode = "001";
         var d = await _biSnapshotApplication.GetReTransactStatisticsDetail(bInDto).ToListAsync();
         d.ShouldNotBeNull();
+
+        var e = _biSnapshotApplication.GetIndustryStatistics(inDto.Adapt<IndustryStatisticsInDto>());
+        e.ShouldNotBeNull();
     }
 }

+ 3 - 0
test/Hotline.Tests/appsettings.Development.json

@@ -12,6 +12,7 @@
     "AppConfiguration": {
         "AppScope": "ZiGong",
         "YiBin": {
+            "AreaCode": "511500",
             "CallCenterType": "TianRun", //XunShi、WeiErXin、TianRun、XingTang
             //智能回访
             "AiVisit": {
@@ -33,9 +34,11 @@
             }
         },
         "ZiGong": {
+            "AreaCode": "510300",
             "CallCenterType": "XingTang"
         },
         "LuZhou": {
+            "AreaCode": "510500",
             "CallCenterType": "XingTang"
         }
     },