| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- namespace Admin.NET.Plugin.AiDOP.Supply;
- /// <summary>
- /// 采购申请合并服务。P3 阶段仅合并本次生成且尚未入库的 PR 候选,不改动历史 PR。
- /// </summary>
- public class PurchaseRequestMergeService : ITransient
- {
- public PurchaseRequestMergeResult MergeGeneratedRequests(List<PurchaseRequestMain> requests)
- {
- if (requests.Count <= 1)
- {
- return new PurchaseRequestMergeResult
- {
- Requests = requests,
- OriginalCount = requests.Count,
- MergedCount = requests.Count
- };
- }
- var merged = requests
- .GroupBy(x => new
- {
- x.TenantId,
- x.CompanyId,
- x.FactoryId,
- x.IcitemId,
- x.PrPurchaseId,
- x.IsRequireGoods,
- SupplierType = x.SupplierType ?? string.Empty,
- WeekStart = GetWeekStart(x.PrSsendDate)
- })
- .Select(group =>
- {
- var rows = group.OrderBy(x => x.PrSsendDate).ThenBy(x => x.PrSarriveDate).ThenBy(x => x.Id).ToList();
- var first = rows.First();
- first.PrRqty = rows.Sum(x => x.PrRqty ?? 0);
- first.PrAqty = rows.Sum(x => x.PrAqty ?? 0);
- first.PrSqty = rows.Sum(x => x.PrSqty ?? 0);
- first.PrSsendDate = rows.Min(x => x.PrSsendDate);
- first.PrSarriveDate = rows.Min(x => x.PrSarriveDate);
- first.CreateTime = rows.Min(x => x.CreateTime);
- first.UpdateTime = DateTime.Now;
- return first;
- })
- .ToList();
- return new PurchaseRequestMergeResult
- {
- Requests = merged,
- OriginalCount = requests.Count,
- MergedCount = merged.Count
- };
- }
- private static DateTime? GetWeekStart(DateTime? value)
- {
- if (!value.HasValue) return null;
- var date = value.Value.Date;
- var diff = ((int)date.DayOfWeek + 6) % 7;
- return date.AddDays(-diff);
- }
- }
- public sealed class PurchaseRequestMergeResult
- {
- public List<PurchaseRequestMain> Requests { get; set; } = new();
- public int OriginalCount { get; set; }
- public int MergedCount { get; set; }
- public int ReducedCount => Math.Max(OriginalCount - MergedCount, 0);
- }
|