/*
|
*所有关于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;
|
}
|
|
}
|
}
|