浏览代码

重构本月统计逻辑,统一按自然月处理

将本月统计逻辑由传入时间范围改为以结束时间所在自然月为基准,自动计算月初至月末的区间,统一使用 monthDailyItems、monthGroupItems 变量进行本月相关统计和分组,避免与本年统计混淆。时间处理统一采用 DateTime 格式化,提升代码健壮性和可读性。
Lyyyi 3 天之前
父节点
当前提交
96b9a0ad15
共有 1 个文件被更改,包括 56 次插入55 次删除
  1. 56 55
      OASystem/OASystem.Api/Controllers/StatisticsController.cs

+ 56 - 55
OASystem/OASystem.Api/Controllers/StatisticsController.cs

@@ -8778,12 +8778,15 @@ WHERE
 
             #endregion
 
-            string beginDt = $"{dto.BeginDt} 00:00:00",
-                   endDt = $"{dto.EndDt} 23:59:59";
-            DateTime beginDate = DateTime.Parse(beginDt),
-                     endDate = DateTime.Parse(endDt);
-            string yearBeginDt = $"{beginDate.Year}-01-01 00:00:00",
-                   yearEndDt = $"{beginDate.Year}-12-31 23:59:59";
+            // 基础转换:解析传入的起止时间
+            DateTime beginDate = DateTime.Parse(dto.BeginDt).Date; // 强制取 00:00:00
+            DateTime endDate = DateTime.Parse(dto.EndDt).Date.AddDays(1).AddSeconds(-1); // 强制取 23:59:59
+
+            // 本月:按照时间范围的【最大月份】起止时间
+            // 逻辑:以 endDate 为基准,计算它所在月份的第一天和最后一天
+            DateTime monthBase = endDate;
+            DateTime monthBegin = new DateTime(monthBase.Year, monthBase.Month, 1);
+            DateTime monthEnd = monthBegin.AddMonths(1).AddSeconds(-1); // 自动处理 28/30/31 天及跨年
 
             //业务类型
             var priceTypeIds = new List<int>() {
@@ -8796,13 +8799,11 @@ WHERE
                 689, //689 团组签证保险
             };
 
-            var data = await CorporateProfitInfos(beginDt, endDt);
-            var groupItems = data.GroupItems;
-            var dailyItems = data.DailyItems;
-            //筛选不统计的费用类型
-            //if (dailyItems.Any()) dailyItems = dailyItems.Where(x => !priceTypeIds.Contains(x.PriceTypeId)).ToArray();
+            var montData = await CorporateProfitInfos(monthBegin.ToString("yyyy-MM-dd HH:mm:ss"), monthEnd.ToString("yyyy-MM-dd HH:mm:ss"));
+            var monthGroupItems = montData.GroupItems;
+            var monthDailyItems = montData.DailyItems;
 
-            var yearData = await CorporateProfitInfos(yearBeginDt, yearEndDt);
+            var yearData = await CorporateProfitInfos(beginDate.ToString("yyyy-MM-dd HH:mm:ss"), endDate.ToString("yyyy-MM-dd HH:mm:ss"));
             var yearGroupItems = yearData.GroupItems;
             var yearDailyItems = yearData.DailyItems;
             //筛选不统计的费用类型
@@ -8810,17 +8811,17 @@ WHERE
 
             #region 公共参数
             //团组收款、成本、利润 合计
-            decimal monthGroupCollectionTotal = groupItems.Sum(x => x.CollectionTotal),
-                    monthGroupCostTotal = groupItems.Sum(x => x.CostTotal),
+            decimal monthGroupCollectionTotal = monthGroupItems.Sum(x => x.CollectionTotal),
+                    monthGroupCostTotal = monthGroupItems.Sum(x => x.CostTotal),
                     yearGroupCollectionTotal = yearGroupItems.Sum(x => x.CollectionTotal),
                     yearGroupCostTotal = yearGroupItems.Sum(x => x.CostTotal);
             // 日付申请 48 人员费用、 49 办公费用、50 销售费用、51 其他费用、55	大运会、90 各部门基础固定费用明细
-            decimal monthStaffCosts = dailyItems.Where(x => x.PriceParentTypeId == 48).Sum(x => x.CNYTotal),
-                    monthOfficeExpenses = dailyItems.Where(x => x.PriceParentTypeId == 49).Sum(x => x.CNYTotal),
-                    monthCostSales = dailyItems.Where(x => x.PriceParentTypeId == 50).Sum(x => x.CNYTotal),
-                    monthOtherFees = dailyItems.Where(x => x.PriceParentTypeId == 51).Sum(x => x.CNYTotal),
-                    monthDyhFees = dailyItems.Where(x => x.PriceParentTypeId == 55).Sum(x => x.CNYTotal),
-                    monthDepartFixedExpenses = dailyItems.Where(x => x.PriceParentTypeId == 90).Sum(x => x.CNYTotal),
+            decimal monthStaffCosts = monthDailyItems.Where(x => x.PriceParentTypeId == 48).Sum(x => x.CNYTotal),
+                    monthOfficeExpenses = monthDailyItems.Where(x => x.PriceParentTypeId == 49).Sum(x => x.CNYTotal),
+                    monthCostSales = monthDailyItems.Where(x => x.PriceParentTypeId == 50).Sum(x => x.CNYTotal),
+                    monthOtherFees = monthDailyItems.Where(x => x.PriceParentTypeId == 51).Sum(x => x.CNYTotal),
+                    monthDyhFees = monthDailyItems.Where(x => x.PriceParentTypeId == 55).Sum(x => x.CNYTotal),
+                    monthDepartFixedExpenses = monthDailyItems.Where(x => x.PriceParentTypeId == 90).Sum(x => x.CNYTotal),
                     yearStaffCosts = yearDailyItems.Where(x => x.PriceParentTypeId == 48).Sum(x => x.CNYTotal),
                     yearOfficeExpenses = yearDailyItems.Where(x => x.PriceParentTypeId == 49).Sum(x => x.CNYTotal),
                     yearCostSales = yearDailyItems.Where(x => x.PriceParentTypeId == 50).Sum(x => x.CNYTotal),
@@ -8936,16 +8937,16 @@ WHERE
                 otherTypeIds.AddRange(inforTypeIds);
                 otherTypeIds.AddRange(competitionTypeIds);
 
-                decimal monthAllIncomes = groupItems.Sum(x => x.ReceivableAmount), //月-收入
-                        monthTzIncomes = groupItems.Where(x => groupTypeIds.Contains(x.TeamDid)).Sum(x => x.ReceivableAmount),
-                        monthHwIncomes = groupItems.Where(x => inforTypeIds.Contains(x.TeamDid)).Sum(x => x.ReceivableAmount),
-                        monthSsIncomes = groupItems.Where(x => competitionTypeIds.Contains(x.TeamDid)).Sum(x => x.ReceivableAmount),
-                        monthQtIncomes = groupItems.Where(x => !otherTypeIds.Contains(x.TeamDid)).Sum(x => x.ReceivableAmount),
-                        monthAllCost = groupItems.Sum(x => x.CostTotal), //月-成本
-                        monthTzCost = groupItems.Where(x => groupTypeIds.Contains(x.TeamDid)).Sum(x => x.CostTotal),
-                        monthHwCost = groupItems.Where(x => inforTypeIds.Contains(x.TeamDid)).Sum(x => x.CostTotal),
-                        monthSsCost = groupItems.Where(x => competitionTypeIds.Contains(x.TeamDid)).Sum(x => x.CostTotal),
-                        monthQtCost = groupItems.Where(x => !otherTypeIds.Contains(x.TeamDid)).Sum(x => x.CostTotal),
+                decimal monthAllIncomes = monthGroupItems.Sum(x => x.ReceivableAmount), //月-收入
+                        monthTzIncomes = monthGroupItems.Where(x => groupTypeIds.Contains(x.TeamDid)).Sum(x => x.ReceivableAmount),
+                        monthHwIncomes = monthGroupItems.Where(x => inforTypeIds.Contains(x.TeamDid)).Sum(x => x.ReceivableAmount),
+                        monthSsIncomes = monthGroupItems.Where(x => competitionTypeIds.Contains(x.TeamDid)).Sum(x => x.ReceivableAmount),
+                        monthQtIncomes = monthGroupItems.Where(x => !otherTypeIds.Contains(x.TeamDid)).Sum(x => x.ReceivableAmount),
+                        monthAllCost = monthGroupItems.Sum(x => x.CostTotal), //月-成本
+                        monthTzCost = monthGroupItems.Where(x => groupTypeIds.Contains(x.TeamDid)).Sum(x => x.CostTotal),
+                        monthHwCost = monthGroupItems.Where(x => inforTypeIds.Contains(x.TeamDid)).Sum(x => x.CostTotal),
+                        monthSsCost = monthGroupItems.Where(x => competitionTypeIds.Contains(x.TeamDid)).Sum(x => x.CostTotal),
+                        monthQtCost = monthGroupItems.Where(x => !otherTypeIds.Contains(x.TeamDid)).Sum(x => x.CostTotal),
                         monthAllGross = monthAllIncomes - monthAllCost, //月-毛利金额
                         monthTzGross = monthTzIncomes - monthTzCost,
                         monthHwGross = monthHwIncomes - monthHwCost,
@@ -9053,13 +9054,13 @@ WHERE
                 var conmmentTexts = new List<CorporateProfitDailyCommentView>();
 
                 #region 员工费用 48
-                decimal monthStaffCostTotal = dailyItems.Where(x => x.PriceParentTypeId == 48).Sum(x => x.ItemTotal),
+                decimal monthStaffCostTotal = monthDailyItems.Where(x => x.PriceParentTypeId == 48).Sum(x => x.ItemTotal),
                         yearStaffCostTotal = yearDailyItems.Where(x => x.PriceParentTypeId == 48).Sum(x => x.ItemTotal);
 
                 dailyViewItems.Add(new CorporateProfitDailyView() { No = "一", PriceName = "人员费用", ThisMonthAmount = monthStaffCostTotal, ThisYearAmount = yearStaffCostTotal });
                 conmmentTexts.Add(new CorporateProfitDailyCommentView()
                 {
-                    MonthComment = DailyListToString(dailyItems.Where(x => x.PriceParentTypeId == 48).ToArray()),
+                    MonthComment = DailyListToString(monthDailyItems.Where(x => x.PriceParentTypeId == 48).ToArray()),
                     YearComment = DailyListToString(yearDailyItems.Where(x => x.PriceParentTypeId == 48).ToArray())
                 });
 
@@ -9067,12 +9068,12 @@ WHERE
                 foreach (var item in dailyStaffTypes)
                 {
                     string priceName = !string.IsNullOrEmpty(item.Remark) ? item.Remark : item.Name;
-                    decimal monthAmount = dailyItems.Where(x => x.PriceTypeId == item.Id).Sum(x => x.ItemTotal),
+                    decimal monthAmount = monthDailyItems.Where(x => x.PriceTypeId == item.Id).Sum(x => x.ItemTotal),
                             yearAmount = yearDailyItems.Where(x => x.PriceTypeId == item.Id).Sum(x => x.ItemTotal);
                     dailyViewItems.Add(new CorporateProfitDailyView() { PriceName = priceName, ThisMonthAmount = monthAmount, ThisYearAmount = yearAmount });
                     conmmentTexts.Add(new CorporateProfitDailyCommentView()
                     {
-                        MonthComment = DailyListToString(dailyItems.Where(x => x.PriceTypeId == item.Id).ToArray()),
+                        MonthComment = DailyListToString(monthDailyItems.Where(x => x.PriceTypeId == item.Id).ToArray()),
                         YearComment = DailyListToString(yearDailyItems.Where(x => x.PriceTypeId == item.Id).ToArray())
                     });
                 }
@@ -9081,13 +9082,13 @@ WHERE
 
                 #region 办公费用
 
-                decimal monthWorkCostTotal = dailyItems.Where(x => x.PriceParentTypeId == 49).Sum(x => x.ItemTotal), //总费用
+                decimal monthWorkCostTotal = monthDailyItems.Where(x => x.PriceParentTypeId == 49).Sum(x => x.ItemTotal), //总费用
                         yearWorkCostTotal = yearDailyItems.Where(x => x.PriceParentTypeId == 49).Sum(x => x.ItemTotal);
 
                 dailyViewItems.Add(new CorporateProfitDailyView() { No = "二", PriceName = "办公费用", ThisMonthAmount = monthWorkCostTotal, ThisYearAmount = yearWorkCostTotal });
                 conmmentTexts.Add(new CorporateProfitDailyCommentView()
                 {
-                    MonthComment = DailyListToString(dailyItems.Where(x => x.PriceParentTypeId == 49).ToArray()),
+                    MonthComment = DailyListToString(monthDailyItems.Where(x => x.PriceParentTypeId == 49).ToArray()),
                     YearComment = DailyListToString(yearDailyItems.Where(x => x.PriceParentTypeId == 49).ToArray())
                 });
 
@@ -9095,12 +9096,12 @@ WHERE
                 foreach (var item in dailyWorkTypes)
                 {
                     string priceName = !string.IsNullOrEmpty(item.Remark) ? item.Remark : item.Name;
-                    decimal monthAmount = dailyItems.Where(x => x.PriceTypeId == item.Id).Sum(x => x.ItemTotal),
+                    decimal monthAmount = monthDailyItems.Where(x => x.PriceTypeId == item.Id).Sum(x => x.ItemTotal),
                             yearAmount = yearDailyItems.Where(x => x.PriceTypeId == item.Id).Sum(x => x.ItemTotal);
                     dailyViewItems.Add(new CorporateProfitDailyView() { PriceName = priceName, ThisMonthAmount = monthAmount, ThisYearAmount = yearAmount });
                     conmmentTexts.Add(new CorporateProfitDailyCommentView()
                     {
-                        MonthComment = DailyListToString(dailyItems.Where(x => x.PriceTypeId == item.Id).ToArray()),
+                        MonthComment = DailyListToString(monthDailyItems.Where(x => x.PriceTypeId == item.Id).ToArray()),
                         YearComment = DailyListToString(yearDailyItems.Where(x => x.PriceTypeId == item.Id).ToArray())
                     });
                 }
@@ -9108,13 +9109,13 @@ WHERE
                 #endregion
 
                 #region 销售费用
-                decimal monthSalesCostTotal = dailyItems.Where(x => x.PriceParentTypeId == 50).Sum(x => x.ItemTotal), //总费用
+                decimal monthSalesCostTotal = monthDailyItems.Where(x => x.PriceParentTypeId == 50).Sum(x => x.ItemTotal), //总费用
                         yearSalesCostTotal = yearDailyItems.Where(x => x.PriceParentTypeId == 50).Sum(x => x.ItemTotal);
 
                 dailyViewItems.Add(new CorporateProfitDailyView() { No = "三", PriceName = "销售费用", ThisMonthAmount = monthSalesCostTotal, ThisYearAmount = yearSalesCostTotal });
                 conmmentTexts.Add(new CorporateProfitDailyCommentView()
                 {
-                    MonthComment = DailyListToString(dailyItems.Where(x => x.PriceParentTypeId == 50).ToArray()),
+                    MonthComment = DailyListToString(monthDailyItems.Where(x => x.PriceParentTypeId == 50).ToArray()),
                     YearComment = DailyListToString(yearDailyItems.Where(x => x.PriceParentTypeId == 50).ToArray())
                 });
 
@@ -9122,12 +9123,12 @@ WHERE
                 foreach (var item in dailySalesTypes)
                 {
                     string priceName = !string.IsNullOrEmpty(item.Remark) ? item.Remark : item.Name;
-                    decimal monthAmount = dailyItems.Where(x => x.PriceTypeId == item.Id).Sum(x => x.ItemTotal),
+                    decimal monthAmount = monthDailyItems.Where(x => x.PriceTypeId == item.Id).Sum(x => x.ItemTotal),
                             yearAmount = yearDailyItems.Where(x => x.PriceTypeId == item.Id).Sum(x => x.ItemTotal);
                     dailyViewItems.Add(new CorporateProfitDailyView() { PriceName = priceName, ThisMonthAmount = monthAmount, ThisYearAmount = yearAmount });
                     conmmentTexts.Add(new CorporateProfitDailyCommentView()
                     {
-                        MonthComment = DailyListToString(dailyItems.Where(x => x.PriceTypeId == item.Id).ToArray()),
+                        MonthComment = DailyListToString(monthDailyItems.Where(x => x.PriceTypeId == item.Id).ToArray()),
                         YearComment = DailyListToString(yearDailyItems.Where(x => x.PriceTypeId == item.Id).ToArray())
                     });
                 }
@@ -9136,13 +9137,13 @@ WHERE
 
                 #region 51 其他费用
 
-                decimal monthOtherCostTotal = dailyItems.Where(x => x.PriceParentTypeId == 51).Sum(x => x.ItemTotal), //总费用
+                decimal monthOtherCostTotal = monthDailyItems.Where(x => x.PriceParentTypeId == 51).Sum(x => x.ItemTotal), //总费用
                         yearOtherCostTotal = yearDailyItems.Where(x => x.PriceParentTypeId == 51).Sum(x => x.ItemTotal);
 
                 dailyViewItems.Add(new CorporateProfitDailyView() { No = "四", PriceName = "其他费用", ThisMonthAmount = monthOtherCostTotal, ThisYearAmount = yearOtherCostTotal });
                 conmmentTexts.Add(new CorporateProfitDailyCommentView()
                 {
-                    MonthComment = DailyListToString(dailyItems.Where(x => x.PriceParentTypeId == 51).ToArray()),
+                    MonthComment = DailyListToString(monthDailyItems.Where(x => x.PriceParentTypeId == 51).ToArray()),
                     YearComment = DailyListToString(yearDailyItems.Where(x => x.PriceParentTypeId == 51).ToArray())
                 });
 
@@ -9150,12 +9151,12 @@ WHERE
                 foreach (var item in dailyOtherTypes)
                 {
                     string priceName = !string.IsNullOrEmpty(item.Remark) ? item.Remark : item.Name;
-                    decimal monthAmount = dailyItems.Where(x => x.PriceTypeId == item.Id).Sum(x => x.ItemTotal),
+                    decimal monthAmount = monthDailyItems.Where(x => x.PriceTypeId == item.Id).Sum(x => x.ItemTotal),
                             yearAmount = yearDailyItems.Where(x => x.PriceTypeId == item.Id).Sum(x => x.ItemTotal);
                     dailyViewItems.Add(new CorporateProfitDailyView() { PriceName = priceName, ThisMonthAmount = monthAmount, ThisYearAmount = yearAmount });
                     conmmentTexts.Add(new CorporateProfitDailyCommentView()
                     {
-                        MonthComment = DailyListToString(dailyItems.Where(x => x.PriceTypeId == item.Id).ToArray()),
+                        MonthComment = DailyListToString(monthDailyItems.Where(x => x.PriceTypeId == item.Id).ToArray()),
                         YearComment = DailyListToString(yearDailyItems.Where(x => x.PriceTypeId == item.Id).ToArray())
                     });
                 }
@@ -9164,13 +9165,13 @@ WHERE
 
                 #region 55 大运会
 
-                decimal monthDyhCostTotal = dailyItems.Where(x => x.PriceParentTypeId == 55).Sum(x => x.ItemTotal), //总费用
+                decimal monthDyhCostTotal = monthDailyItems.Where(x => x.PriceParentTypeId == 55).Sum(x => x.ItemTotal), //总费用
                         yearDyhCostTotal = yearDailyItems.Where(x => x.PriceParentTypeId == 55).Sum(x => x.ItemTotal);
 
                 dailyViewItems.Add(new CorporateProfitDailyView() { No = "五", PriceName = "大运会", ThisMonthAmount = monthDyhCostTotal, ThisYearAmount = yearDyhCostTotal });
                 conmmentTexts.Add(new CorporateProfitDailyCommentView()
                 {
-                    MonthComment = DailyListToString(dailyItems.Where(x => x.PriceParentTypeId == 55).ToArray()),
+                    MonthComment = DailyListToString(monthDailyItems.Where(x => x.PriceParentTypeId == 55).ToArray()),
                     YearComment = DailyListToString(yearDailyItems.Where(x => x.PriceParentTypeId == 55).ToArray())
                 });
 
@@ -9178,12 +9179,12 @@ WHERE
                 foreach (var item in dailyDyhTypes)
                 {
                     string priceName = !string.IsNullOrEmpty(item.Remark) ? item.Remark : item.Name;
-                    decimal monthAmount = dailyItems.Where(x => x.PriceTypeId == item.Id).Sum(x => x.ItemTotal),
+                    decimal monthAmount = monthDailyItems.Where(x => x.PriceTypeId == item.Id).Sum(x => x.ItemTotal),
                             yearAmount = yearDailyItems.Where(x => x.PriceTypeId == item.Id).Sum(x => x.ItemTotal);
                     dailyViewItems.Add(new CorporateProfitDailyView() { PriceName = priceName, ThisMonthAmount = monthAmount, ThisYearAmount = yearAmount });
                     conmmentTexts.Add(new CorporateProfitDailyCommentView()
                     {
-                        MonthComment = DailyListToString(dailyItems.Where(x => x.PriceTypeId == item.Id).ToArray()),
+                        MonthComment = DailyListToString(monthDailyItems.Where(x => x.PriceTypeId == item.Id).ToArray()),
                         YearComment = DailyListToString(yearDailyItems.Where(x => x.PriceTypeId == item.Id).ToArray())
                     });
                 }
@@ -9192,13 +9193,13 @@ WHERE
 
                 #region 90 各部门基础固定费用明细
 
-                decimal monthBasicCostTotal = dailyItems.Where(x => x.PriceParentTypeId == 90).Sum(x => x.ItemTotal), //总费用
+                decimal monthBasicCostTotal = monthDailyItems.Where(x => x.PriceParentTypeId == 90).Sum(x => x.ItemTotal), //总费用
                         yearBasicCostTotal = yearDailyItems.Where(x => x.PriceParentTypeId == 90).Sum(x => x.ItemTotal);
 
                 dailyViewItems.Add(new CorporateProfitDailyView() { No = "六", PriceName = "各部门基础固定费用明细", ThisMonthAmount = monthBasicCostTotal, ThisYearAmount = yearBasicCostTotal });
                 conmmentTexts.Add(new CorporateProfitDailyCommentView()
                 {
-                    MonthComment = DailyListToString(dailyItems.Where(x => x.PriceParentTypeId == 90).ToArray()),
+                    MonthComment = DailyListToString(monthDailyItems.Where(x => x.PriceParentTypeId == 90).ToArray()),
                     YearComment = DailyListToString(yearDailyItems.Where(x => x.PriceParentTypeId == 90).ToArray())
                 });
 
@@ -9206,12 +9207,12 @@ WHERE
                 foreach (var item in dailyBasicTypes)
                 {
                     string priceName = item.Name;
-                    decimal monthAmount = dailyItems.Where(x => x.PriceTypeId == item.Id).Sum(x => x.ItemTotal),
+                    decimal monthAmount = monthDailyItems.Where(x => x.PriceTypeId == item.Id).Sum(x => x.ItemTotal),
                             yearAmount = yearDailyItems.Where(x => x.PriceTypeId == item.Id).Sum(x => x.ItemTotal);
                     dailyViewItems.Add(new CorporateProfitDailyView() { PriceName = priceName, ThisMonthAmount = monthAmount, ThisYearAmount = yearAmount });
                     conmmentTexts.Add(new CorporateProfitDailyCommentView()
                     {
-                        MonthComment = DailyListToString(dailyItems.Where(x => x.PriceTypeId == item.Id).ToArray()),
+                        MonthComment = DailyListToString(monthDailyItems.Where(x => x.PriceTypeId == item.Id).ToArray()),
                         YearComment = DailyListToString(yearDailyItems.Where(x => x.PriceTypeId == item.Id).ToArray())
                     });
                 }
@@ -9277,7 +9278,7 @@ WHERE
             }
             else if (excelType == 5)
             {
-                var view = await CorporateProfitItems(2, beginDt, endDt);
+                var view = await CorporateProfitItems(2, beginDate.ToString("yyyy-MM-dd HH:mm:ss"), endDate.ToString("yyyy-MM-dd HH:mm:ss"));
                 return Ok(JsonView(true, "操作成功!", view.Label));