From f0a45608f5a60bb7d3bf0b9de4008bb141f7db84 Mon Sep 17 00:00:00 2001 From: shanji <18996038927@163.com> Date: Sun, 24 Dec 2023 02:02:49 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=8C=E5=96=84=E9=87=87=E8=B4=AD=E5=95=86?= =?UTF-8?q?=E5=88=97=E8=A1=A8=E6=9F=A5=E8=AF=A2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Purchaser/PurchaserBusiness.cs | 331 +++++++++++------- .../Purchaser/QueryPurchaserRequest.cs | 6 +- 2 files changed, 204 insertions(+), 133 deletions(-) diff --git a/BBWYB.Server.Business/Purchaser/PurchaserBusiness.cs b/BBWYB.Server.Business/Purchaser/PurchaserBusiness.cs index 96bd32a..dba87c5 100644 --- a/BBWYB.Server.Business/Purchaser/PurchaserBusiness.cs +++ b/BBWYB.Server.Business/Purchaser/PurchaserBusiness.cs @@ -3,6 +3,7 @@ using BBWYB.Common.Models; using BBWYB.Server.Model; using BBWYB.Server.Model.Db; using BBWYB.Server.Model.Dto; +using FreeSql; using Yitter.IdGenerator; namespace BBWYB.Server.Business @@ -32,73 +33,68 @@ namespace BBWYB.Server.Business public ListResponse QueryPurchaserList(QueryPurchaserRequest request) { - if (request.PageSize > 20) - request.PageSize = 20; - var purchaserList = fsql.Select() - .WhereIf(!string.IsNullOrEmpty(request.Spu), p => fsql.Select() - .Where(psp1 => psp1.PurchaserId == p.Id && - psp1.ProductId == request.Spu) - .Any()) - .WhereIf(!string.IsNullOrEmpty(request.Sku), p => fsql.Select() - .Where(psp2 => psp2.PurchaserId == p.Id && - psp2.SkuId == request.Sku) - .Any()) - .WhereIf(request.PurchaserNameList != null && request.PurchaserNameList.Count() > 0, p => request.PurchaserNameList.Contains(p.Name)) - .WhereIf(request.CategoryIdList != null && request.CategoryIdList.Count() > 0, p => fsql.Select() - .Where(per => per.PurchaserId == p.Id && - request.CategoryIdList.Contains(per.ExtendedInfoId.Value)).Any()) - .WhereIf(request.LocationList != null && request.LocationList.Count() > 0, p => request.LocationList.Contains(p.Location)) - .Page(request.PageIndex, request.PageSize) - .Count(out var count) - .ToList(); - - var purchaserIdList = purchaserList.Select(p => p.Id).ToList(); - - #region 查询SPU绑定数/SKU绑定数 - var bindList = fsql.Select() - .InnerJoin((psp, psc) => psp.SkuPurchaseSchemeId == psc.Id) - .Where((psp, psc) => psc.ShopId == request.ShopId && purchaserIdList.Contains(psp.PurchaserId)) - .GroupBy((psp, psc) => new { psp.PurchaserId, psp.ProductId, psp.SkuId }) - .ToList(g => new - { - g.Key.PurchaserId, - g.Key.ProductId, - g.Key.SkuId - }); - #endregion - - #region 查询SPU采购数/SKU采购数 - var purchasedList = fsql.Select() - .InnerJoin((spr, ps) => spr.SkuId == ps.Id) - .Where((spr, ps) => ps.ShopId == request.ShopId && purchaserIdList.Contains(spr.PurchaserId)) - .GroupBy((spr, ps) => new { spr.PurchaserId, spr.SkuId, ps.ProductId }) - .ToList(g => new - { - g.Key.PurchaserId, - g.Key.ProductId, - g.Key.SkuId - }); + #region 数据验证 + if (request.RecentDayCondition != null && request.RecentDayCondition.RecentDay > 0) + { + if (request.RecentDayCondition.PurchasedCountComparisonOperator != null) + { + if (request.RecentDayCondition.PurchasedCount == null || (request.RecentDayCondition.PurchasedCountComparisonOperator == Enums.ComparisonOperator.介于 && + request.RecentDayCondition.PurchasedCount2 == null)) + throw new BusinessException("采购量条件不完整"); + } + if (request.RecentDayCondition.PurchasedAmountComparisonOperator != null) + { + if (request.RecentDayCondition.PurchasedAmount == null || (request.RecentDayCondition.PurchasedAmountComparisonOperator == Enums.ComparisonOperator.介于 && + request.RecentDayCondition.PurchasedAmount2 == null)) + throw new BusinessException("采购金额条件不完整"); + } + } #endregion - #region 查询订单数 - var poList = fsql.Select() - .InnerJoin((opi, o) => opi.OrderId == o.Id) - .Where((opi, o) => opi.ShopId == request.ShopId && - opi.IsEnabled == true && - o.OrderState != Enums.OrderState.已取消 && - purchaserIdList.Contains(opi.PurchaserId)) - .GroupBy((opi, o) => opi.PurchaserId) - .ToList(g => new - { - PurchaserId = g.Key, - Count = g.Count() - }); + if (request.PageSize > 20) + request.PageSize = 20; - #endregion + var select = fsql.Select() + .WhereIf(!string.IsNullOrEmpty(request.Spu), p => fsql.Select() + .Where(psp1 => psp1.PurchaserId == p.Id && + psp1.ProductId == request.Spu) + .Any()) + .WhereIf(!string.IsNullOrEmpty(request.Sku), p => fsql.Select() + .Where(psp2 => psp2.PurchaserId == p.Id && + psp2.SkuId == request.Sku) + .Any()) + .WhereIf(request.PurchaserNameList != null && + request.PurchaserNameList.Count() > 0, p => request.PurchaserNameList.Contains(p.Name)) + .WhereIf(request.CategoryIdList != null && + request.CategoryIdList.Count() > 0, p => fsql.Select() + .Where(per => per.PurchaserId == p.Id && + request.CategoryIdList.Contains(per.ExtendedInfoId.Value)).Any()) + .WhereIf(request.LocationList != null && request.LocationList.Count() > 0, p => request.LocationList.Contains(p.Location)) + .WhereIf(request.ManagmentMode != null, p => p.ManagmentMode == request.ManagmentMode); - #region 查询采购金额 - var purchaseAmountList = fsql.Select() + if (request.RecentDayCondition != null && request.RecentDayCondition.RecentDay > 0) + { + var recentStartDay = DateTime.Now.Date.AddDays(request.RecentDayCondition.RecentDay.Value * -1); + if (request.RecentDayCondition.PurchasedCountComparisonOperator != null) + { + var childSelect = fsql.Select() + .InnerJoin((opi, o) => opi.OrderId == o.Id) + .Where((opi, o) => opi.ShopId == request.ShopId && + opi.IsEnabled == true && + o.OrderState != Enums.OrderState.已取消 && + opi.CreateTime >= recentStartDay) + .GroupBy((opi, o) => opi.PurchaserId) + .HavingIf(request.RecentDayCondition.PurchasedCountComparisonOperator == Enums.ComparisonOperator.等于, g => g.Count() == request.RecentDayCondition.PurchasedCount) + .HavingIf(request.RecentDayCondition.PurchasedCountComparisonOperator == Enums.ComparisonOperator.小于, g => g.Count() <= request.RecentDayCondition.PurchasedCount) + .HavingIf(request.RecentDayCondition.PurchasedCountComparisonOperator == Enums.ComparisonOperator.大于, g => g.Count() >= request.RecentDayCondition.PurchasedCount) + .HavingIf(request.RecentDayCondition.PurchasedCountComparisonOperator == Enums.ComparisonOperator.介于, g => g.Count() >= request.RecentDayCondition.PurchasedCount && g.Count() <= request.RecentDayCondition.PurchasedCount2) + .WithTempQuery(g => new { g.Value.Item1.PurchaserId, Count = g.Count() }).As("pcc"); + select = select.Where(p => childSelect.Where(pcc => pcc.PurchaserId == p.Id).Any()); + } + if (request.RecentDayCondition.PurchasedAmountComparisonOperator != null) + { + var childSelect = fsql.Select() .InnerJoin((ocd, o, ori, opi) => ocd.OrderId == o.Id) .InnerJoin((ocd, o, ori, opi) => ocd.OrderId == ori.OrderId && ocd.SkuId == ori.BelongSkuId) .InnerJoin((ocd, o, ori, opi) => ori.OrderId == opi.OrderId) @@ -106,89 +102,162 @@ namespace BBWYB.Server.Business o.OrderState != Enums.OrderState.已取消 && ocd.IsEnabled == true && opi.IsEnabled == true && - purchaserIdList.Contains(opi.PurchaserId)) + opi.CreateTime >= recentStartDay) .GroupBy((ocd, o, ori, opi) => opi.PurchaserId) - .ToList(g => new - { - PurchaserId = g.Key, - PurchaseAmount = g.Sum(g.Value.Item1.SkuAmount) + g.Sum(g.Value.Item1.PurchaseFreight) - }); - #endregion - - #region 查询最近采购时间 - var recentPurchaseTimeList = fsql.Select() - .InnerJoin((opi1, o) => o.OrderState != Enums.OrderState.已取消) - .Where((opi1, o) => opi1.ShopId == request.ShopId && - purchaserIdList.Contains(opi1.PurchaserId) && - opi1.IsEnabled == true) - .GroupBy((opi1, o) => opi1.PurchaserId) - .WithTempQuery(g => new { MaxId = g.Max(g.Value.Item1.Id) }) - .From() - .InnerJoin((opi1, opi2) => opi1.MaxId == opi2.Id) - .ToList((opi1, opi2) => new - { - opi2.PurchaserId, - opi2.CreateTime - }); - #endregion - - #region 查询标签/主营类目 - var purchaserExtendInfoList = fsql.Select() - .InnerJoin((pei, per) => pei.Id == per.ExtendedInfoId) - .Where((pei, per) => purchaserIdList.Contains(per.PurchaserId)) - .ToList((pei, per) => new - { - pei.Id, - pei.Name, - pei.Type, - per.PurchaserId - }); - #endregion + .HavingIf(request.RecentDayCondition.PurchasedAmountComparisonOperator == Enums.ComparisonOperator.等于, g => g.Sum(g.Value.Item1.SkuAmount + g.Value.Item1.PurchaseFreight) == request.RecentDayCondition.PurchasedAmount) + .HavingIf(request.RecentDayCondition.PurchasedAmountComparisonOperator == Enums.ComparisonOperator.小于, g => g.Sum(g.Value.Item1.SkuAmount + g.Value.Item1.PurchaseFreight) <= request.RecentDayCondition.PurchasedAmount) + .HavingIf(request.RecentDayCondition.PurchasedAmountComparisonOperator == Enums.ComparisonOperator.大于, g => g.Sum(g.Value.Item1.SkuAmount + g.Value.Item1.PurchaseFreight) >= request.RecentDayCondition.PurchasedAmount) + .HavingIf(request.RecentDayCondition.PurchasedAmountComparisonOperator == Enums.ComparisonOperator.介于, g => g.Sum(g.Value.Item1.SkuAmount + g.Value.Item1.PurchaseFreight) >= request.RecentDayCondition.PurchasedAmount && g.Sum(g.Value.Item1.SkuAmount + g.Value.Item1.PurchaseFreight) <= request.RecentDayCondition.PurchasedAmount2) + .WithTempQuery(g => new { g.Value.Item4.PurchaserId, PurchasedAmount = g.Sum(g.Value.Item1.SkuAmount + g.Value.Item1.PurchaseFreight) }).As("pca"); + select = select.Where(p => childSelect.Where(pca => pca.PurchaserId == p.Id).Any()); + } + } - foreach (var purchaser in purchaserList) + var pageSelect = select.Page(request.PageIndex, request.PageSize); + var sql = pageSelect.ToSql(); + var purchaserList = select.Page(request.PageIndex, request.PageSize) + .Count(out var count) + .ToList(); + if (purchaserList.Count() > 0) { - #region SPU绑定数/SKU绑定数 - var currentBindList = bindList.Where(x => x.PurchaserId == purchaser.Id).ToList(); - purchaser.BindingSpuCount = currentBindList.Select(x => x.ProductId).Distinct().Count(); - purchaser.BindingSkuCount = currentBindList.Select(x => x.SkuId).Count(); + var purchaserIdList = purchaserList.Select(p => p.Id).ToList(); + + #region 查询SPU绑定数/SKU绑定数 + var bindList = fsql.Select() + .InnerJoin((psp, psc) => psp.SkuPurchaseSchemeId == psc.Id) + .Where((psp, psc) => psc.ShopId == request.ShopId && purchaserIdList.Contains(psp.PurchaserId)) + .GroupBy((psp, psc) => new { psp.PurchaserId, psp.ProductId, psp.SkuId }) + .ToList(g => new + { + g.Key.PurchaserId, + g.Key.ProductId, + g.Key.SkuId + }); #endregion - #region SPU采购数/SKU采购数 - var currentPurchasedList = purchasedList.Where(x => x.PurchaserId == purchaser.Id).ToList(); - purchaser.PurchasedSpuCount = currentPurchasedList.Select(x => x.ProductId).Distinct().Count(); - purchaser.PurchasedSkuCount = currentPurchasedList.Select(x => x.SkuId).Count(); + #region 查询SPU采购数/SKU采购数 + var purchasedList = fsql.Select() + .InnerJoin((spr, ps) => spr.SkuId == ps.Id) + .Where((spr, ps) => ps.ShopId == request.ShopId && purchaserIdList.Contains(spr.PurchaserId)) + .GroupBy((spr, ps) => new { spr.PurchaserId, spr.SkuId, ps.ProductId }) + .ToList(g => new + { + g.Key.PurchaserId, + g.Key.ProductId, + g.Key.SkuId + }); + #endregion - #region 订单数 - purchaser.PurchasedCount = poList.FirstOrDefault(x => x.PurchaserId == purchaser.Id)?.Count ?? 0; + #region 查询订单数 + var poList = fsql.Select() + .InnerJoin((opi, o) => opi.OrderId == o.Id) + .Where((opi, o) => opi.ShopId == request.ShopId && + opi.IsEnabled == true && + o.OrderState != Enums.OrderState.已取消 && + purchaserIdList.Contains(opi.PurchaserId)) + .GroupBy((opi, o) => opi.PurchaserId) + .ToList(g => new + { + PurchaserId = g.Key, + Count = g.Count() + }); + #endregion - #region 采购金额 - purchaser.PurchaseAmount = purchaseAmountList.FirstOrDefault(x => x.PurchaserId == purchaser.Id)?.PurchaseAmount ?? 0; + #region 查询采购金额 + var purchaseAmountList = fsql.Select() + .InnerJoin((ocd, o, ori, opi) => ocd.OrderId == o.Id) + .InnerJoin((ocd, o, ori, opi) => ocd.OrderId == ori.OrderId && ocd.SkuId == ori.BelongSkuId) + .InnerJoin((ocd, o, ori, opi) => ori.OrderId == opi.OrderId) + .Where((ocd, o, ori, opi) => o.ShopId == request.ShopId && + o.OrderState != Enums.OrderState.已取消 && + ocd.IsEnabled == true && + opi.IsEnabled == true && + purchaserIdList.Contains(opi.PurchaserId)) + .GroupBy((ocd, o, ori, opi) => opi.PurchaserId) + .ToList(g => new + { + PurchaserId = g.Key, + PurchaseAmount = g.Sum(g.Value.Item1.SkuAmount) + g.Sum(g.Value.Item1.PurchaseFreight) + }); #endregion - #region 最近采购时间 - purchaser.LastPurchaseTime = recentPurchaseTimeList.FirstOrDefault(x => x.PurchaserId == purchaser.Id)?.CreateTime; + #region 查询最近采购时间 + var recentPurchaseTimeList = fsql.Select() + .InnerJoin((opi1, o) => o.OrderState != Enums.OrderState.已取消) + .Where((opi1, o) => opi1.ShopId == request.ShopId && + purchaserIdList.Contains(opi1.PurchaserId) && + opi1.IsEnabled == true) + .GroupBy((opi1, o) => opi1.PurchaserId) + .WithTempQuery(g => new { MaxId = g.Max(g.Value.Item1.Id) }) + .From() + .InnerJoin((opi1, opi2) => opi1.MaxId == opi2.Id) + .ToList((opi1, opi2) => new + { + opi2.PurchaserId, + opi2.CreateTime + }); #endregion - #region 主营类目/标签 - var currentExtendInfoList = purchaserExtendInfoList.Where(x => x.PurchaserId == purchaser.Id).ToList(); - purchaser.CategoryList = currentExtendInfoList.Where(x => x.Type == Enums.PurchaserBasicInfoType.主营类目) - .Select(x => new PurchaserExtendedInfoResponse() - { - Id = x.Id, - Name = x.Name, - Type = x.Type - }).ToList(); - - purchaser.TagList = currentExtendInfoList.Where(x => x.Type == Enums.PurchaserBasicInfoType.标签) - .Select(x => new PurchaserExtendedInfoResponse() - { - Id = x.Id, - Name = x.Name, - Type = x.Type - }).ToList(); + #region 查询标签/主营类目 + var purchaserExtendInfoList = fsql.Select() + .InnerJoin((pei, per) => pei.Id == per.ExtendedInfoId) + .Where((pei, per) => purchaserIdList.Contains(per.PurchaserId)) + .ToList((pei, per) => new + { + pei.Id, + pei.Name, + pei.Type, + per.PurchaserId + }); #endregion + + foreach (var purchaser in purchaserList) + { + #region SPU绑定数/SKU绑定数 + var currentBindList = bindList.Where(x => x.PurchaserId == purchaser.Id).ToList(); + purchaser.BindingSpuCount = currentBindList.Select(x => x.ProductId).Distinct().Count(); + purchaser.BindingSkuCount = currentBindList.Select(x => x.SkuId).Count(); + #endregion + + #region SPU采购数/SKU采购数 + var currentPurchasedList = purchasedList.Where(x => x.PurchaserId == purchaser.Id).ToList(); + purchaser.PurchasedSpuCount = currentPurchasedList.Select(x => x.ProductId).Distinct().Count(); + purchaser.PurchasedSkuCount = currentPurchasedList.Select(x => x.SkuId).Count(); + #endregion + + #region 订单数 + purchaser.PurchasedCount = poList.FirstOrDefault(x => x.PurchaserId == purchaser.Id)?.Count ?? 0; + #endregion + + #region 采购金额 + purchaser.PurchaseAmount = purchaseAmountList.FirstOrDefault(x => x.PurchaserId == purchaser.Id)?.PurchaseAmount ?? 0; + #endregion + + #region 最近采购时间 + purchaser.LastPurchaseTime = recentPurchaseTimeList.FirstOrDefault(x => x.PurchaserId == purchaser.Id)?.CreateTime; + #endregion + + #region 主营类目/标签 + var currentExtendInfoList = purchaserExtendInfoList.Where(x => x.PurchaserId == purchaser.Id).ToList(); + purchaser.CategoryList = currentExtendInfoList.Where(x => x.Type == Enums.PurchaserBasicInfoType.主营类目) + .Select(x => new PurchaserExtendedInfoResponse() + { + Id = x.Id, + Name = x.Name, + Type = x.Type + }).ToList(); + + purchaser.TagList = currentExtendInfoList.Where(x => x.Type == Enums.PurchaserBasicInfoType.标签) + .Select(x => new PurchaserExtendedInfoResponse() + { + Id = x.Id, + Name = x.Name, + Type = x.Type + }).ToList(); + #endregion + } } return new ListResponse() diff --git a/BBWYB.Server.Model/Dto/Request/Purchaser/QueryPurchaserRequest.cs b/BBWYB.Server.Model/Dto/Request/Purchaser/QueryPurchaserRequest.cs index 317ce24..b0e277f 100644 --- a/BBWYB.Server.Model/Dto/Request/Purchaser/QueryPurchaserRequest.cs +++ b/BBWYB.Server.Model/Dto/Request/Purchaser/QueryPurchaserRequest.cs @@ -31,6 +31,8 @@ namespace BBWYB.Server.Model.Dto /// public QueryPurchaserConditionByDayRequest RecentDayCondition { get; set; } + public Enums.ManagmentMode? ManagmentMode { get; set; } + /// /// 页码 从1开始 /// @@ -51,7 +53,7 @@ namespace BBWYB.Server.Model.Dto /// public int? RecentDay { get; set; } - public Enums.ComparisonOperator PurchasedCountComparisonOperator { get; set; } + public Enums.ComparisonOperator? PurchasedCountComparisonOperator { get; set; } /// /// 采购量,大于/小于/等于/介于时传该值,可空 @@ -63,7 +65,7 @@ namespace BBWYB.Server.Model.Dto /// public int? PurchasedCount2 { get; set; } - public Enums.ComparisonOperator PurchasedAmountComparisonOperator { get; set; } + public Enums.ComparisonOperator? PurchasedAmountComparisonOperator { get; set; } /// /// 采购额,大于/小于/等于/介于时传该值,可空