xiazhengtongxue
2026-03-02 6614be8c7eb806d6db88ae953998016d58e49165
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
using WIDESEA_Common.TaskEnum;
using WIDESEA_Core;
using WIDESEA_Core.BaseRepository;
using WIDESEA_Core.BaseServices;
using WIDESEA_ITaskInfoService;
using WIDESEA_Model.Models;
 
namespace WIDESEA_TaskInfoService;
 
public class Task_HtyService : ServiceBase<Dt_Task_Hty, IRepository<Dt_Task_Hty>>, ITask_HtyService
{
    public Task_HtyService(IRepository<Dt_Task_Hty> BaseDal) : base(BaseDal)
    {
    }
    public WebResponseContent GetInOutTypeStats()
    {
        // 查询所有任务数据
        var allTasks = BaseDal.QueryData()
            .Select(x => new
            {
                TaskType = (TaskTypeEnum)x.TaskType,
                Roadway = x.Roadway,
                x.WarehouseId
            })
            .ToList();
 
        // 如果没有数据,返回空列表
        if (allTasks == null || !allTasks.Any())
        {
            return WebResponseContent.Instance.OK("未找到任何任务数据", new List<object>());
        }
 
        // 计算各仓库的任务类型分布
        var result = allTasks
            .GroupBy(x => x.Roadway == "1" || x.Roadway == "2" ? 1 : 2) // Roadway "1"和"2"属于WarehouseId=1,其他属于2
            .Select(g => new
            {
                WarehouseId = g.Key,
                Stats = g.Where(t => Enum.IsDefined(typeof(TaskTypeEnum), t.TaskType))
                        .GroupBy(t => t.TaskType)
                        .Select(typeGroup => new
                        {
                            Type = typeGroup.Key.GetDescription(),
                            Count = typeGroup.Count(),
                            Percentage = (int)(Math.Round((double)typeGroup.Count() / g.Count() * 100, 2))
                        })
                        .OrderByDescending(x => x.Count)
                        .ToList(),
                TotalCount = g.Count()
            })
            .ToList();
 
        return WebResponseContent.Instance.OK("成功", result);
    }
    public WebResponseContent GetWarehouseOperationStatistics()
    {
        /// <summary>
        /// 原料仓,总出入库任务数量,今日入库,今日出库,成品仓,总出入库数量,今日入库,今日出库,
        /// </summary>
 
        // 获取今天的日期(不考虑时间部分)
        var today = DateTime.Today;
        var tomorrow = today.AddDays(1);
 
        // 查询所有任务数据,包含创建时间
        var allTasks = BaseDal.QueryData()
            .Select(x => new
            {
                TaskType = (TaskTypeEnum)x.TaskType,
                Roadway = x.Roadway,
                x.WarehouseId,
                CreateTime = x.CreateDate
            })
            .ToList();
 
        // 如果没有数据,返回默认值
        if (allTasks == null || !allTasks.Any())
        {
            var emptyResult = new
            {
                RawMaterialWarehouse = new
                {
                    TotalTasks = 0,
                    TodayInbound = 0,
                    TodayOutbound = 0
                },
                FinishedProductWarehouse = new
                {
                    TotalTasks = 0,
                    TodayInbound = 0,
                    TodayOutbound = 0
                },
                Summary = new
                {
                    TotalAllTasks = 0,
                    TotalInbound = 0,
                    TotalOutbound = 0
                }
            };
            return WebResponseContent.Instance.OK("未找到任何任务数据", emptyResult);
        }
 
        // 定义出入库任务类型
        var inboundTypes = new[] { TaskTypeEnum.Inbound };
        var outboundTypes = new[] { TaskTypeEnum.Outbound };
 
        // 过滤出有效的任务数据
        var validTasks = allTasks.Where(t => Enum.IsDefined(typeof(TaskTypeEnum), t.TaskType)).ToList();
 
        // 计算原料仓(WarehouseId = 1)的统计
        var rawMaterialTasks = validTasks.Where(t =>
            t.Roadway == "1" || t.Roadway == "2" || t.WarehouseId == 1).ToList();
 
        var rawMaterialStats = new
        {
            TotalTasks = rawMaterialTasks.Count,
            TodayInbound = rawMaterialTasks.Count(t =>
                inboundTypes.Contains(t.TaskType) &&
                t.CreateTime >= today && t.CreateTime < tomorrow),
            TodayOutbound = rawMaterialTasks.Count(t =>
                outboundTypes.Contains(t.TaskType) &&
                t.CreateTime >= today && t.CreateTime < tomorrow)
        };
 
        // 计算成品仓(WarehouseId = 2)的统计
        var finishedProductTasks = validTasks.Where(t =>
            (t.Roadway != "1" && t.Roadway != "2") || t.WarehouseId == 2).ToList();
 
        var finishedProductStats = new
        {
            TotalTasks = finishedProductTasks.Count,
            TodayInbound = finishedProductTasks.Count(t =>
                inboundTypes.Contains(t.TaskType) &&
                t.CreateTime >= today && t.CreateTime < tomorrow),
            TodayOutbound = finishedProductTasks.Count(t =>
                outboundTypes.Contains(t.TaskType) &&
                t.CreateTime >= today && t.CreateTime < tomorrow)
        };
 
        // 计算总的入库和出库数量(所有仓库,所有时间)
        var totalInbound = validTasks.Count(t => inboundTypes.Contains(t.TaskType));
        var totalOutbound = validTasks.Count(t => outboundTypes.Contains(t.TaskType));
 
        // 构建返回结果
        var result = new
        {
            RawMaterialWarehouse = rawMaterialStats,
            FinishedProductWarehouse = finishedProductStats,
            Summary = new
            {
                TotalAllTasks = validTasks.Count,
                TotalInbound = totalInbound,       // 总共入库数量
                TotalOutbound = totalOutbound      // 总共出库数量
            }
        };
 
        return WebResponseContent.Instance.OK("成功", result);
    }
    public WebResponseContent GetTodayInOutStats()
    {
        // 获取今天和7天前的日期
        var today = DateTime.Now.Date;
        var sevenDaysAgo = today.AddDays(-6); // 包含今天共7天
 
        // 查询7天内全天数据(0:00-24:00)
        var query = BaseDal.QueryData(x =>
            x.CreateDate >= sevenDaysAgo &&
            x.CreateDate < today.AddDays(1) // 包含今天全天
        );
 
        var tasks = query.Select(x => new
        {
            TaskType = (TaskTypeEnum)x.TaskType,
            CreateDate = x.CreateDate
        }).ToList();
 
        if (!tasks.Any())
        {
            return WebResponseContent.Instance.OK("未找到任何任务数据", new List<object>());
        }
 
        // 统计每天出入库数量
        var dailyStats = new List<object>();
 
        for (int i = 0; i < 7; i++)
        {
            var currentDate = sevenDaysAgo.AddDays(i);
            var nextDate = currentDate.AddDays(1); // 下一天的0:00
 
            var dayTasks = tasks.Where(t =>
                t.CreateDate >= currentDate &&
                t.CreateDate < nextDate
            ).ToList();
 
            var stats = new
            {
                Date = currentDate.ToString("yyyy-MM-dd"),
                InboundCount = dayTasks.Count(t => t.TaskType == TaskTypeEnum.Inbound),
                OutboundCount = dayTasks.Count(t => t.TaskType == TaskTypeEnum.Outbound),
                TotalCount = dayTasks.Count
            };
 
            dailyStats.Add(stats);
        }
 
        return WebResponseContent.Instance.OK("成功", dailyStats);
    }
 
}