Admin
2025-12-02 9e42f0dafa019f5ecf6b0ff425ecb966b002171e
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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
/*
 *所有关于Dt_outboundorder_detail类的业务代码应在此处编写
*可使用repository.调用常用方法,获取EF/Dapper等信息
*如果需要事务请使用repository.DbContextBeginTransaction
*也可使用DBServerProvider.手动获取数据库相关信息
*用户信息、权限、角色等使用UserContext.Current操作
*Dt_outboundorder_detailService对增、删、改查、导入、导出、审核业务代码扩展参照ServiceFunFilter
*/
 
using WIDESEA.Core.BaseProvider;
using WIDESEA.Core.Extensions.AutofacManager;
using WIDESEA.Entity.DomainModels;
using System.Linq;
using WIDESEA.Core.Utilities;
using System.Linq.Expressions;
using WIDESEA.Core.Extensions;
using Microsoft.EntityFrameworkCore;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.AspNetCore.Http;
using WIDESEA.Services.IRepositories;
using System;
using System.Collections.Generic;
using WIDESEA.Common.CustomModels;
 
namespace WIDESEA.Services.Services
{
    public partial class Dt_outboundorder_detailService
    {
        private readonly IHttpContextAccessor _httpContextAccessor;
        private readonly IDt_outboundorder_detailRepository _repository;//访问数据库
 
        [ActivatorUtilitiesConstructor]
        public Dt_outboundorder_detailService(
            IDt_outboundorder_detailRepository dbRepository,
            IHttpContextAccessor httpContextAccessor
            )
        : base(dbRepository)
        {
            _httpContextAccessor = httpContextAccessor;
            _repository = dbRepository;
            //多租户会用到这init代码,其他情况可以不用
            //base.Init(dbRepository);
        }
 
        /// <summary>
        /// 分配库存
        /// </summary>
        /// <returns></returns>
        public WebResponseContent AllocateInventory(List<LocationContainer> containers, decimal outneedQty)
        {
            WebResponseContent content = new WebResponseContent();
            List<LocationContainer> respContainers = new List<LocationContainer>();
            List<LocationContainer> countContainers = containers;
            try
            {
                containers = containers.OrderBy(x => x.InboundDatetime).ToList();
                decimal sumqty = 0;
                int count = Convert.ToInt32(Math.Ceiling(outneedQty / 2500));
                int outcount = count;
                int cunt = 1;
                foreach (var item in containers)
                {
                    if (count <= outcount / 2)
                    {
                        LocationContainer Container = countContainers.OrderBy(x => Math.Abs((x.text1.GetValueOrDefault() + sumqty) - (outneedQty - sumqty))).FirstOrDefault();
                        respContainers.Add(Container);
                        countContainers.Remove(Container);
                        sumqty += Container.text1.GetValueOrDefault();
                        if (count == 1)
                        {
                            break;
                        }
                        count--;
                        cunt++;
                    }
                    else
                    {
                        LocationContainer Container = countContainers.FirstOrDefault();
                        respContainers.Add(Container);
                        countContainers.Remove(Container);
                        sumqty += Container.text1.GetValueOrDefault();
                        count--;
                        cunt++;
                    }
                }
                content.OK(data: respContainers);
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
 
            return content;
        }
 
        public WebResponseContent AllocateInventorys(List<LocationContainer> containers, decimal outneedQty)
        {
            WebResponseContent content = new WebResponseContent();
            List<LocationContainer> respContainers = new List<LocationContainer>();
            List<LocationContainer> countContainers = containers;
            try
            {
                containers = containers.OrderBy(x => x.InboundDatetime).ToList();
                decimal sumqty = 0;
                int count = Convert.ToInt32(outneedQty / 2500);
                int outcount = count;
                int cunt = 1;
                #region
                //foreach (var item in containers)
                //{
                //    if (cunt > outcount / 2)
                //    {
                //        if (count < 2)
                //        {
                //            LocationContainer Container = countContainers.OrderBy(x => Math.Abs((x.text1.GetValueOrDefault() + sumqty) - outneedQty - 200)).FirstOrDefault();
                //            respContainers.Add(Container);
                //            countContainers.Remove(Container);
                //            sumqty += Container.text1.GetValueOrDefault();
                //            break;
                //        }
                //        else
                //        {
                //            LocationContainer Container = countContainers.OrderBy(x => x.text1.GetValueOrDefault()).FirstOrDefault();
                //            respContainers.Add(Container);
                //            countContainers.Remove(Container);
                //            sumqty += Container.text1.GetValueOrDefault();
                //            count--;
                //            cunt++;
                //        }
                //    }
                //    else
                //    {
                //        LocationContainer Container = countContainers.OrderByDescending(x => x.text1.GetValueOrDefault()).FirstOrDefault();
                //        respContainers.Add(Container);
                //        countContainers.Remove(Container);
                //        sumqty += Container.text1.GetValueOrDefault();
                //        count--;
                //        cunt++;
                //    }
                //}
                #endregion
                foreach (var item in containers)
                {
                    if (count <= outcount / 2)
                    {
                        LocationContainer Container = countContainers.OrderBy(x => Math.Abs((x.text1.GetValueOrDefault() + sumqty) - (outneedQty - sumqty))).FirstOrDefault();
                        respContainers.Add(Container);
                        countContainers.Remove(Container);
                        sumqty += Container.text1.GetValueOrDefault();
                        if (count == 1)
                        {
                            break;
                        }
                        count--;
                        cunt++;
                    }
                    else
                    {
                        LocationContainer Container = countContainers.FirstOrDefault();
                        respContainers.Add(Container);
                        countContainers.Remove(Container);
                        sumqty += Container.text1.GetValueOrDefault();
                        count--;
                        cunt++;
                    }
                }
                content.OK(data: respContainers);
            }
            catch (Exception ex)
            {
                content.Error(ex.Message);
            }
 
            return content;
        }
 
        public List<decimal> numGroupRows(List<decimal> arr, decimal sum, int start = 0)
        {
            List<decimal> find = new List<decimal>();
 
            for (int i = start; i < arr.Count; i++)
                find.Add(arr[i]);
 
            find = find.FindAll(S => S == sum);
            bool flag = false;
            if (find.Count > 0)
            {
                find = new List<decimal> { find[0] };
                flag = true;
                return find;
            }
            else
            {
                for (int i = start; i < arr.Count; i++)
                {
                    decimal qty = arr[i];
                    if (qty < sum)
 
                    {
                        find.Add(arr[i]);
                        sum -= qty;
                    }
 
                    List<decimal> findTem = numGroupRows(arr, sum, i + 1);
                    if (findTem.Count > 0)
                    {
                        find.AddRange(findTem);
                    }
                    else
                    {
                        find.Remove(arr[i]);
                        sum += qty;
                    }
 
                    if (flag)
                    {
                        break;
                    }
                }
            }
            return find;
        }
 
        /// <summary>
        /// 货车分配
        /// </summary>
        /// <param name="args"></param>
        /// <param name="argsum"></param>
        /// <returns></returns>
        public List<decimal> MC_combination(List<decimal> args, decimal argsum)
        {
            #region
            List<List<decimal>> list = new List<List<decimal>>();
            List<decimal> result = new List<decimal>();
            List<decimal> List2 = args;
 
            Console.WriteLine(123);
            for (int i = 0; i < List2.Count; i++)
            {
                List<decimal> List3 = new List<decimal>();
                List3.Add(List2[i]);
                List2.RemoveAt(i);
                for (int j = 0; j < List2.Count; j++)
                {
                    if ((decimal)List2[j] <= argsum - List3.Sum())
                    {
                        List3.Add(List2[j]);
                        List2.RemoveAt(j);
                        j = -1;
                    }
                }
                list.Add(List3);
                i = -1;
            }
            foreach (var item in list)
            {
                Console.WriteLine(item.Sum());
                if (item.Sum() >= argsum - 500)
                {
                    result = item;
                    break;
                }
            }
            return result;
            #endregion
        }
 
        /// <summary>
        /// 集装箱分配
        /// </summary>
        /// <param name="args"></param>
        /// <param name="argsum"></param>
        /// <returns></returns>
        public List<decimal> MC_combinationJ(List<decimal> args, decimal argsum)
        {
            #region 获取数组中(多个数)相加和等于(<=)给定值的算法
            List<List<decimal>> list = new List<List<decimal>>();
            List<decimal> result = new List<decimal>();
            List<decimal> List2 = args;//整型数组转list
            for (int i = 0; i < List2.Count; i++)
            {
                List<decimal> List3 = new List<decimal>();
                List3.Add(List2[i]);
                List2.RemoveAt(i);
                for (int j = 0; j < List2.Count; j++)
                {
                    if ((decimal)List2[j] <= argsum - List3.Sum())
                    {
                        List3.Add(List2[j]);
                        List2.RemoveAt(j);
                        j = -1;
                    }
                }
                list.Add(List3);
                i = -1;
            }
            foreach (var item in list)
            {
                Console.WriteLine(item.Sum());
                if (item.Sum() >= argsum - 1000)
                {
                    result = item;
                    break;
                }
            }
            return result;
            #endregion
        }
 
        private decimal ToTalNum = 0;
 
        public List<LocationContainer> MC_Hcombination(List<LocationContainer> containers, List<LocationContainer> totalContainers, decimal outneedQty, bool flag)
        {
            List<LocationContainer> locationContainers = totalContainers;
            List<LocationContainer> locations = containers.OrderBy(x => x.InboundDatetime).ToList();
            int count = Convert.ToInt32(outneedQty / 2500);
            if (!flag)
            {
                for (int i = 0; i < count; i++)
                {
                    locationContainers.Add(locations[0]);
                    locations.Remove(locations[0]);
                }
            }
            decimal totalSum = locationContainers.Select(x => x.text1.GetValueOrDefault()).Sum();
            if (totalSum == ToTalNum)
            {
                throw new Exception("分配出问题啦!");
            }
            if (totalSum > outneedQty)
            {
                ToTalNum = locationContainers.Select(x => x.text1.GetValueOrDefault()).Sum();
                List<LocationContainer> locations1 = locationContainers.OrderByDescending(x => x.text1.GetValueOrDefault()).ToList();
                locations = locations.OrderBy(x => x.text1.GetValueOrDefault()).ToList();
                LocationContainer bigcontainer = locations1[1];
                LocationContainer smillcontainer = locations[1];
                locationContainers.Remove(bigcontainer);
                locationContainers.Add(smillcontainer);
                locations.Remove(smillcontainer);
                locations.Add(bigcontainer);
                locationContainers = MC_Hcombination(locations, locationContainers, outneedQty, true);
                totalSum = locationContainers.Select(x => x.text1.GetValueOrDefault()).Sum();
                ToTalNum = 0;
            }
            else if (totalSum < outneedQty - 500)
            {
                ToTalNum = locationContainers.Select(x => x.text1.GetValueOrDefault()).Sum();
                List<LocationContainer> locations1 = locationContainers.OrderBy(x => x.text1.GetValueOrDefault()).ToList();
                locations = locations.OrderByDescending(x => x.text1.GetValueOrDefault()).ToList();
                LocationContainer smillcontainer = locations1[1];
                LocationContainer bigcontainer = locations[1];
                locationContainers.Add(bigcontainer);
                locationContainers.Remove(smillcontainer);
                locations.Add(smillcontainer);
                locations.Remove(bigcontainer);
                locationContainers = MC_Hcombination(locations, locationContainers, outneedQty, true);
                totalSum = locationContainers.Select(x => x.text1.GetValueOrDefault()).Sum();
                ToTalNum = 0;
            }
            return locationContainers;
        }
 
    }
}