schangxiang@126.com
2025-09-17 ff43ddf18764629ff875478e4e47a7281cbd230a
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
using Admin.NET.Application.Entity;
using Admin.NET.Application.Service.ReportCenter.WmsOtherReport.Dto;
 
namespace Admin.NET.Application;
/// <summary>
/// 收货完成统计 服务
/// </summary>
[ApiDescriptionSettings(ApplicationConst.ReportCenterGroupName, Order = 100)]
public class WmsTakeGoodStatsService : IDynamicApiController, ITransient
{
    private readonly SqlSugarRepository<WmsOrderAsnDetails> _rep;
 
    private readonly SqlSugarRepository<WmsRecordReceivingDelivery> _wmsRecordReceivingDeliveryRep;
 
    public WmsTakeGoodStatsService(
        SqlSugarRepository<WmsRecordReceivingDelivery> wmsRecordReceivingDeliveryRep,
        SqlSugarRepository<WmsOrderAsnDetails> rep)
 
    {
        _wmsRecordReceivingDeliveryRep = wmsRecordReceivingDeliveryRep;
        _rep = rep;
    }
 
    /// <summary>
    /// 收货完成情况
    /// </summary>
    /// <param name="input"></param>
    /// <returns></returns>
    [HttpGet]
    [ApiDescriptionSettings(Name = "Page")]
    [Description("WmsTakeGoodStats/Page")]
    public async Task<SqlSugarPagedList<WmsTakeGoodStatsOutput>> Page([FromQuery] WmsTakeGoodStatsInput input)
    {
 
 
        var today = DateTime.Today; // 获取当前日期
 
        // 查询当天的 ASN 单明细表,按照 ASN 单号、物料号、供应商名称进行分组
        var result = await Task.Run(() =>
        {
            var query = _rep.AsQueryable()
               .Where(a => a.CreateTime >= today) // 过滤条件,只取当天的记录
                .Select<WmsOrderAsnDetails>()
                .ToList();
 
            return query;
        });
 
        //查找今天收获的所有记录
        var records = await _wmsRecordReceivingDeliveryRep.AsQueryable().Where(x => x.CreateTime >= today).ToListAsync();
 
        var returnQuery = new SqlSugarPagedList<WmsTakeGoodStatsOutput>();
        var wmsTakeGoodStatsOutputList = new List<WmsTakeGoodStatsOutput>();
        foreach (var item in result)
        {
            var newWmsTakeGoodStatsOutput = new WmsTakeGoodStatsOutput();
 
            if (item.AsnStatus == OrderStatusEnum.新建)
            {
                newWmsTakeGoodStatsOutput.AsnNo = item.AsnNo;
                newWmsTakeGoodStatsOutput.MaterialCode = item.MaterialCode;
                newWmsTakeGoodStatsOutput.SupplierName = item.SupplierName;
                newWmsTakeGoodStatsOutput.ProjectNo = item.ProjectNo;
                // newWmsTakeGoodStatsOutput.ASNBeginTime = item.PlannedStartTime;
                //newWmsTakeGoodStatsOutput.ASNEndTime = item.PlannedEndTime;
                newWmsTakeGoodStatsOutput.ASNStatus = ReceiptStatusEnum.待收货;
                newWmsTakeGoodStatsOutput.ASNNumber = item.GoodsQuantity;
                newWmsTakeGoodStatsOutput.AllQty = item.Quantity;
                wmsTakeGoodStatsOutputList.Add(newWmsTakeGoodStatsOutput);
            }
            else if (item.AsnStatus == OrderStatusEnum.处理中)
            {
                newWmsTakeGoodStatsOutput.AsnNo = item.AsnNo;
                newWmsTakeGoodStatsOutput.MaterialCode = item.MaterialCode;
                newWmsTakeGoodStatsOutput.SupplierName = item.SupplierName;
                newWmsTakeGoodStatsOutput.ProjectNo = item.ProjectNo;
                newWmsTakeGoodStatsOutput.ASNBeginTime = item.PlannedStartTime;
                newWmsTakeGoodStatsOutput.ASNEndTime = item.PlannedEndTime;
                newWmsTakeGoodStatsOutput.ASNNumber = item.GoodsQuantity;
                newWmsTakeGoodStatsOutput.AllQty = item.Quantity;
 
                if (today < item.PlannedEndTime)
                {
                    newWmsTakeGoodStatsOutput.ASNStatus = ReceiptStatusEnum.收货中;
                    wmsTakeGoodStatsOutputList.Add(newWmsTakeGoodStatsOutput);
                }
                else if (today > item.PlannedEndTime)
                {
                    newWmsTakeGoodStatsOutput.ASNStatus = ReceiptStatusEnum.延迟未收货;
                    wmsTakeGoodStatsOutputList.Add(newWmsTakeGoodStatsOutput);
                }
            }
            else if (item.AsnStatus == OrderStatusEnum.已完成 || item.AsnStatus == OrderStatusEnum.已关闭)
            {
                if (item.AsnStatus == OrderStatusEnum.已完成)
                {
                    newWmsTakeGoodStatsOutput.AsnNo = item.AsnNo;
                    newWmsTakeGoodStatsOutput.MaterialCode = item.MaterialCode;
                    newWmsTakeGoodStatsOutput.SupplierName = item.SupplierName;
                    newWmsTakeGoodStatsOutput.ProjectNo = item.ProjectNo;
                    // 找到所有相同 ASN 单号的记录
                    var completedItems = result.Where(r => r.AsnNo == item.AsnNo).ToList();
                    newWmsTakeGoodStatsOutput.ASNNumber = item.GoodsQuantity;
 
                    // 找到最小和最大的 PlannedStartTime
                    var minStartTime = completedItems.Min(r => r.PlannedStartTime);
                    var maxEndTime = completedItems.Max(r => r.PlannedEndTime);
 
                    // 设置 ASNBeginTime 和 ASNEndTime
                    newWmsTakeGoodStatsOutput.ASNBeginTime = minStartTime;
                    newWmsTakeGoodStatsOutput.ASNEndTime = maxEndTime;
                    newWmsTakeGoodStatsOutput.ASNStatus = ReceiptStatusEnum.收货完成;
                    newWmsTakeGoodStatsOutput.AllQty = item.Quantity;
                    wmsTakeGoodStatsOutputList.Add(newWmsTakeGoodStatsOutput);
                }
                else
                {
                    newWmsTakeGoodStatsOutput.AsnNo = item.AsnNo;
                    newWmsTakeGoodStatsOutput.MaterialCode = item.MaterialCode;
                    newWmsTakeGoodStatsOutput.SupplierName = item.SupplierName;
                    newWmsTakeGoodStatsOutput.ProjectNo = item.ProjectNo;
                    newWmsTakeGoodStatsOutput.ASNBeginTime = item.PlannedStartTime;
                    newWmsTakeGoodStatsOutput.ASNEndTime = item.PlannedEndTime;
                    newWmsTakeGoodStatsOutput.ASNStatus = ReceiptStatusEnum.已关闭;
                    newWmsTakeGoodStatsOutput.ASNNumber = item.GoodsQuantity;
                    newWmsTakeGoodStatsOutput.AllQty = item.Quantity;
                    wmsTakeGoodStatsOutputList.Add(newWmsTakeGoodStatsOutput);
                }
 
            }
            else if (item.AsnStatus == OrderStatusEnum.已取消)
            {
                newWmsTakeGoodStatsOutput.AsnNo = item.AsnNo;
                newWmsTakeGoodStatsOutput.MaterialCode = item.MaterialCode;
                newWmsTakeGoodStatsOutput.SupplierName = item.SupplierName;
                newWmsTakeGoodStatsOutput.ProjectNo = item.ProjectNo;
                newWmsTakeGoodStatsOutput.ASNBeginTime = item.PlannedStartTime;
                newWmsTakeGoodStatsOutput.ASNEndTime = item.PlannedEndTime;
                newWmsTakeGoodStatsOutput.ASNStatus = ReceiptStatusEnum.已取消;
                newWmsTakeGoodStatsOutput.ASNNumber = item.GoodsQuantity;
                newWmsTakeGoodStatsOutput.AllQty = item.Quantity;
                wmsTakeGoodStatsOutputList.Add(newWmsTakeGoodStatsOutput);
            }
        }
        // 使用 LINQ 对 wmsTakeGoodStatsOutputList 进行分组
        var groupedResults = wmsTakeGoodStatsOutputList
            .GroupBy(x => new { x.AsnNo, x.MaterialCode, x.SupplierName })
            .Select(group => new WmsTakeGoodStatsOutput()
            {
                SupplierName = group.Key.SupplierName,
                AsnNo = group.Key.AsnNo,
                MaterialCode = group.Key.MaterialCode,
                //ASNStatus = group.Max(x => x.ASNStatus),
                ProjectNo = group.Min(x => x.ProjectNo),
                PlanBeginTime = group.Min(x => x.PlanBeginTime),
                AllQty = group.Sum(x => x.AllQty),
                ASNNumber = group.Sum(x => x.ASNNumber),
                //ASNBeginTime = group.Min(x => x.ASNBeginTime),
                //ASNEndTime = group.Max(x => x.ASNEndTime),
                IsCancel = group.Count(x => x.ASNStatus == ReceiptStatusEnum.已取消) == group.Count() ? true : false,
                IsClose = group.Count(x => x.ASNStatus == ReceiptStatusEnum.已关闭) == group.Count() ? true : false,
            }); ;
 
        input.Page = 1;
        input.PageSize = 10000;
        var ls = groupedResults.ToPagedList(input.Page, input.PageSize);
        foreach (var item in ls.Items)
        {
            //计算 开始收获时间+结束收获时间
            var myrecords = records.Where(x => x.OrderNo == item.AsnNo && x.MaterialCode == item.MaterialCode && x.SupplierName == item.SupplierName).OrderBy(x => x.Id).ToList();
            if (myrecords.Count > 0)
            {
                item.ASNBeginTime = myrecords.Select(x => x.CreateTime).Min();
                //判断他的状态
                if (item.AllQty == item.ASNNumber)
                {
                    item.ASNStatus = ReceiptStatusEnum.收货完成;
                    item.ASNEndTime = myrecords.Select(x => x.CreateTime).Max();
                }
                else if (item.AllQty > item.ASNNumber)
                {
                    if (item.ASNNumber > 0)
                    {
                        item.ASNStatus = ReceiptStatusEnum.收货中;
                    }
                    else
                    {
                        item.ASNBeginTime = null;
                        item.ASNStatus = ReceiptStatusEnum.待收货;
                        //判断是不是延迟未收货
                        if (item.PlanBeginTime < DateTime.Now)
                        {
                            item.ASNStatus = ReceiptStatusEnum.延迟未收货;
                        }
                    }
                }
            }
            else
            {
                item.ASNBeginTime = null;
                item.ASNStatus = ReceiptStatusEnum.待收货;
                //判断是不是延迟未收货
                if (item.PlanBeginTime < DateTime.Now)
                {
                    item.ASNStatus = ReceiptStatusEnum.延迟未收货;
                }
            }
 
            //判断是否取消或关闭
            if (item.IsCancel)
            {
                item.ASNStatus = ReceiptStatusEnum.已取消;
            }
            if (item.IsClose)
            {
                item.ASNStatus = ReceiptStatusEnum.已关闭;
            }
 
        }
 
 
        return ls;
    }
 
 
}