Files
tnb.server/WarehouseMgr/Tnb.WarehouseMgr/WmsSaleReleaseService.cs

768 lines
40 KiB
C#
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System.Security.Cryptography.X509Certificates;
using JNPF.Common.Core.Manager;
using JNPF.Common.Dtos.VisualDev;
using JNPF.Common.Enums;
using JNPF.Common.Extension;
using JNPF.Common.Security;
using JNPF.EventBus;
using JNPF.FriendlyException;
using JNPF.Systems.Interfaces.System;
using JNPF.VisualDev;
using JNPF.VisualDev.Entitys;
using JNPF.VisualDev.Interfaces;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.CodeAnalysis;
using Microsoft.Extensions.Logging;
using NPOI.HSSF.Record;
using NPOI.SS.Formula;
using SqlSugar;
using Tnb.BasicData.Entities;
using Tnb.BasicData.Interfaces;
using Tnb.WarehouseMgr.Entities;
using Tnb.WarehouseMgr.Entities.Attributes;
using Tnb.WarehouseMgr.Entities.Consts;
using Tnb.WarehouseMgr.Entities.Dto;
using Tnb.WarehouseMgr.Entities.Dto.Inputs;
using Tnb.WarehouseMgr.Entities.Entity;
using Tnb.WarehouseMgr.Entities.Enums;
using Tnb.WarehouseMgr.Interfaces;
namespace Tnb.WarehouseMgr
{
/// <summary>
/// 销售出库服务
/// </summary>
[OverideVisualDev(ModuleConsts.MODULE_WMSSALERELEASE_ID)]
[ServiceModule(BizTypeId)]
public class WmsSaleReleaseService : BaseWareHouseService, IPdaStroage
{
private const string BizTypeId = "25104446664213";
private readonly ISqlSugarClient _db;
private readonly IRunService _runService;
private readonly IVisualDevService _visualDevService;
private readonly IBasLocationService _basLocationService;
private readonly IWareHouseService _wareHouseService;
private readonly IBillRullService _billRullService;
private readonly IUserManager _userManager;
private readonly IWmsCarryBindService _wmsCarryBindService;
public WmsSaleReleaseService(
ISqlSugarRepository<WmsDelivery> repository,
IRunService runService,
IVisualDevService visualDevService,
IBasLocationService basLocationService,
IBillRullService billRullService,
IWareHouseService wareHouseService,
IUserManager userManager,
IEventPublisher eventPublisher,
IWmsCarryBindService wmsCarryBindService
)
{
_db = repository.AsSugarClient();
_runService = runService;
_visualDevService = visualDevService;
_basLocationService = basLocationService;
_billRullService = billRullService;
_wareHouseService = wareHouseService;
_userManager = userManager;
_wmsCarryBindService = wmsCarryBindService;
OverideFuncs.GetDetailsAsync = GetDetailsAsync;
//OverideFuncs.CreateAsync = Create;
}
[NonAction]
private async Task<dynamic> GetDetailsAsync(string id)
{
try
{
return await Task.FromResult(_db.Queryable<WmsSaleD>().Where(r => r.bill_id == id).ToList());
}
catch (Exception ex)
{
return Task.FromResult(ex);
}
}
/// <summary>
/// 根据载具编号获取起始库位点
/// </summary>
/// <param name="carryId"></param>
/// <returns></returns>
public async Task<dynamic> GetUnStoreLocationListByCarryId([FromRoute] string carryId)
{
var items = await _db!.Queryable<WmsCarryH>().LeftJoin<BasLocation>((a, b) => a.location_id == b.id)
.Where(a => a.id == carryId)
.Select((a, b) => new
{
a.carry_code,
b.location_code,
})
.ToListAsync();
return items;
}
private async Task<dynamic> Create(VisualDevModelDataCrInput input)
{
return await Task.FromResult(true);
}
public override async Task ModifyAsync(WareHouseUpInput input)
{
if (input == null)
{
throw new ArgumentNullException(nameof(input));
}
int row = await _db.Updateable<WmsDelivery>().SetColumns(it => new WmsDelivery { status = WmsWareHouseConst.BILLSTATUS_COMPLETE_ID }).Where(it => it.id == input.requireId).ExecuteCommandAsync();
if (row < 1)
{
throw Oops.Oh(ErrorCode.COM1001);
}
//try
//{
// WmsCarryH wmsCarryH = await _db.Queryable<WmsCarryH>().Where(r => r.id == input.carryIds[0]).FirstAsync();
// await _db.Ado.CommitTranAsync();
//}
//catch (Exception ex)
//{
// Logger.LogError("【WmsSaleService ModifyAsync】" + ex.Message);
// Logger.LogError("【WmsSaleService ModifyAsync】" + ex.StackTrace);
// await _db.Ado.RollbackTranAsync();
//}
}
/// <summary>
/// 下发 电梯第三次改动
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
/// <exception cref="AppFriendlyException"></exception>
[HttpPost, NonUnify, AllowAnonymous]
public async Task<Tnb.WarehouseMgr.Entities.Dto.Outputs.Result> Distribute(SaleReleaseDistributeInput input)
{
Logger.LogInformation($"【Distribute】 销售出库下发");
SemaphoreSlim semaphoreSlim = null;
try
{
semaphoreSlim = _wareHouseService.GetSemaphore("outstock", WmsWareHouseConst.WAREHOUSE_CP_ID);
await semaphoreSlim.WaitAsync();
if (input.qty <= 0)
{
throw new AppFriendlyException("数量必须大于0", 500);
}
if (string.IsNullOrEmpty(input.source_id))
{
throw new AppFriendlyException("来源单据id不可为空", 500);
}
WmsSaleD wmsOutstockD = await _db.Queryable<WmsSaleD>().FirstAsync(it => it.id == input.source_id);
await _db.Ado.BeginTranAsync();
//入库取终点 //出库起点
OutStockStrategyQuery outStockStrategyInput = new() { warehouse_id = WmsWareHouseConst.WAREHOUSE_CP_ID, material_id = wmsOutstockD.material_id, qty = input.qty, code_batch = wmsOutstockD.code_batch, Region_id = WmsWareHouseConst.REGION_CPOutstock_ID };
List<Tuple<string, WmsCarryH, WmsCarryCode, BasLocation>> items = await _wareHouseService.OutStockStrategy_saleRelease(outStockStrategyInput);
decimal canOutstockQty = items.Sum(r => r.Item3.codeqty).ParseToDecimal();
if (canOutstockQty < input.qty)
{
throw new AppFriendlyException($@"当前可出库数量只有 {canOutstockQty.ToString("G")}", 500);
}
List<Tuple<string, WmsCarryH, WmsCarryCode, BasLocation>> items_sorttask = items.Where(r => r.Item1 == "分拣任务").ToList();
List<Tuple<string, WmsCarryH, WmsCarryCode, BasLocation>> items_pretask = items.Where(r => r.Item1 == "预任务").ToList();
Logger.LogInformation($"【Distribute】 预计生成{items_pretask.Count}条预任务");
List<BasLocation> endLocations = null;
// 自动发货
if (!input.isManual)
{
//endLocations = await _db.Queryable<BasLocation>().Where(r => _wareHouseService.GetFloor1OutstockLocation().Contains(r.id) && r.is_lock == 0 && r.is_use == "0").ToListAsync();
//if (endLocations.Count < items_pretask.Count)
//{
// throw new AppFriendlyException("一楼没有足够的未锁定且空闲的出库工位", 500);
//}
//List<BasLocation> endLocations_temp = await _db.Queryable<BasLocation>().Where(r => r.region_id == WmsWareHouseConst.REGION_CPOutstockCache_ID && r.is_lock == 0 && r.is_use == "0").ToListAsync();
//if (endLocations_temp.Count < items_pretask.Count)
//{
// throw new AppFriendlyException($"三楼发货时,没有可用的暂存库位可以使用,需要下发任务{items_pretask.Count}条,可用的暂存库位只有{endLocations_temp.Count}条", 500);
//}
}
//人工发货
else
{
InStockStrategyQuery inStockStrategyInput = new() { warehouse_id = WmsWareHouseConst.WAREHOUSE_CP_ID, Size = items_pretask.Count, Region_id = WmsWareHouseConst.REGION_CPManualOutstock_ID };
endLocations = await _wareHouseService.InStockStrategy(inStockStrategyInput);
if (endLocations.Count < items_pretask.Count)
{
throw new AppFriendlyException("三楼人工出库区没有足够的未锁定且空闲的出库工位", 500);
}
}
// 预任务逻辑
foreach (Tuple<string, WmsCarryH, WmsCarryCode, BasLocation> item in items_pretask)
{
WmsCarryH carry = item.Item2;
WmsCarryCode carryCode = item.Item3;
BasLocation startLocation = item.Item4;
// 根据一楼工位任务数平均分配任务 确定一楼工位
BasLocation endLocation = null;
// 自动发货
if (!input.isManual)
{
#region
// 3-1 第三次改动
var YCLInnerTransfer = await _db.Queryable<BasFactoryConfig>().Where(p => p.key == "floor3CurInCacheLocation").FirstAsync();
Logger.Information($"当前发货区: {YCLInnerTransfer.value}");
if (YCLInnerTransfer.value == null || (YCLInnerTransfer.value != "3楼发货左" && YCLInnerTransfer.value != "3楼发货右"))
{
throw new AppFriendlyException($"工厂配置->三楼出库暂存区当前入库分区 项配置错误需要配置值为【3楼发货左】或【3楼发货右】", 500);
}
List<BasLocation> locations = await _db.Queryable<BasLocation>()
.InnerJoin<WmsElevatorcacheArea>((a, b) => a.id == b.location_id)
.Where((a, b) => b.name == YCLInnerTransfer.value).OrderBy("a.is_lock,a.is_use,carry_count,a.location_code").ToListAsync();
endLocation = locations.FirstOrDefault();
await _db.Updateable<BasFactoryConfig>().SetColumns(r => r.value == (YCLInnerTransfer.value == "3楼发货左" ? "3楼发货右" : "3楼发货左"))
.Where(r => r.key == "floor3CurInCacheLocation").ExecuteCommandAsync();
#endregion
}
//人工发货
else
{
InStockStrategyQuery inStockStrategyInput = new() { warehouse_id = WmsWareHouseConst.WAREHOUSE_CP_ID, Size = 1, Region_id = WmsWareHouseConst.REGION_CPManualOutstock_ID };
endLocations = await _wareHouseService.InStockStrategy(inStockStrategyInput);
if (endLocations.Count < 1)
{
throw new AppFriendlyException("三楼人工出库区没有足够的未锁定且空闲的出库工位", 500);
}
endLocation = endLocations[0];
}
if (endLocation == null)
{
throw new AppFriendlyException("一楼没有足够的未锁定且空闲的出库工位", 500);
}
WmsPointH sPoint = null!;
WmsPointH ePoint = null!;
sPoint = await _db.Queryable<WmsPointH>().FirstAsync(it => it.location_id == startLocation.id);
bool isMatch = await IsCarryAndLocationMatchByCarryStd(carry, endLocation);
if (!isMatch)
{
throw new AppFriendlyException("库位与载具规格不匹配", 500);
}
ePoint = await _db.Queryable<WmsPointH>().FirstAsync(it => it.location_id == endLocation.id);
if (ePoint != null)
{
}
else
{
throw new AppFriendlyException($"库位{endLocation.location_code}未在点位表中维护对应点位", 500);
}
string endLocationId = endLocation.id;
// 在线开发
//VisualDevEntity? templateEntity = await _visualDevService?.GetInfoById(ModuleConsts.MODULE_WMSDELIVERYPDA_ID, true)!;
//await _runService.Create(templateEntity, input);
// 计算路径,插入预任务申请
if (sPoint != null && ePoint != null)
{
List<WmsPointH> points = new List<WmsPointH>();
if (sPoint.area_code != ePoint.area_code)
{
points = await _wareHouseService.PathAlgorithms(sPoint.id, ePoint.id);
//if (points.Count <= 2)
//{
// throw new AppFriendlyException($"sPoint {sPoint.point_code} ePoint{ePoint.point_code}该路径不存在", 500);
//}
}
else
{
points.Add(sPoint);
points.Add(ePoint);
}
//根据获取的路径点生成预任务,生成顺序必须预路径算法返回的起终点的顺序一致(预任务顺序)
if (points?.Count > 0)
{
List<WmsPretaskH> preTasks = points.Where(it => !it.location_id.IsNullOrEmpty()).GroupBy(g => g.area_code).Select(it =>
{
WmsPointH? sPoint = it.FirstOrDefault();
WmsPointH? ePoint = it.LastOrDefault();
WmsPretaskH preTask = new()
{
org_id = _userManager!.User.OrganizeId,
startlocation_id = sPoint?.location_id!,
startlocation_code = sPoint?.location_code!,
endlocation_id = ePoint?.location_id!,
endlocation_code = ePoint?.location_code!,
start_floor = sPoint?.floor.ToString(),
end_floor = ePoint?.floor.ToString(),
startpoint_id = sPoint?.id!,
startpoint_code = sPoint?.point_code!,
endpoint_id = ePoint?.id!,
endpoint_code = ePoint?.point_code!,
bill_code = _billRullService!.GetBillNumber(WmsWareHouseConst.WMS_PRETASK_H_ENCODE).GetAwaiter().GetResult(),
status = WmsWareHouseConst.PRETASK_BILL_STATUS_DXF_ID,
biz_type = WmsWareHouseConst.BIZTYPE_WMSSALERELEASE_ID,
task_type = WmsWareHouseConst.WMS_PRETASK_TRANSFER_TYPE_ID
};
preTask.carry_id = carry.id;
preTask.carry_code = carry.carry_code;
preTask.area_id = sPoint?.area_id!;
preTask.area_code = it.Key;
preTask.require_id = input.source_id;
preTask.require_code = "";
preTask.create_id = _userManager.UserId;
preTask.create_time = DateTime.Now;
return preTask;
}).ToList();
bool isOk = await _wareHouseService.GenPreTask(preTasks, null!, _db);
if (isOk)
{
if (endLocationId != null)
{
//查询库位表
BasLocation location = await _db.Queryable<BasLocation>().SingleAsync(it => it.id == startLocation.id);
{
//载具加锁,增加库位信息
_ = await _db.Updateable<WmsCarryH>().SetColumns(it => new WmsCarryH
{
carry_status = ((int)EnumCarryStatus.).ToString(),
is_lock = 1,
location_id = startLocation.id,
location_code = location.location_code,
require_id = input.source_id,
biz_type = WmsWareHouseConst.BIZTYPE_WMSSALERELEASE_ID
}).Where(it => it.id == carry.id).ExecuteCommandAsync();
}
//所有库位加锁
string?[] ids = new[] { startLocation.id, preTasks[0].endlocation_id, endLocationId };
_ = await _db.Updateable<BasLocation>().SetColumns(it => new BasLocation { is_lock = 1 }).Where(it => ids.Contains(it.id)).ExecuteCommandAsync();
}
}
}
}
}
Logger.LogInformation($"【Distribute】 预计生成{items_sorttask.Count}条分拣任务");
// 分拣任务逻辑
foreach (Tuple<string, WmsCarryH, WmsCarryCode, BasLocation> item in items_sorttask)
{
WmsCarryH carry = item.Item2;
WmsCarryCode carryCode = item.Item3;
BasLocation startLocation = item.Item4;
// 锁定分拣载具
await _db.Updateable<WmsCarryH>().SetColumns(r => r.is_lock == 1).Where(r => r.id == carry.id).ExecuteCommandAsync();
Dictionary<string, object> dic = new()
{
[nameof(WmsSortingtask.id)] = SnowflakeIdHelper.NextId(),
[nameof(WmsSortingtask.bill_code)] = await _billRullService.GetBillNumber(WmsWareHouseConst.WMS_SORTINGTASK_ENCODE),
[nameof(WmsSortingtask.org_id)] = input.org_id ?? _userManager.User.OrganizeId,
[nameof(WmsSortingtask.qty)] = carryCode.codeqty,
[nameof(WmsSortingtask.carry_id)] = carry.id,
[nameof(WmsSortingtask.carry_code)] = carry.carry_code,
[nameof(WmsSortingtask.status)] = WmsWareHouseConst.TASK_BILL_STATUS_DZX_ID,
[nameof(WmsSortingtask.location_id)] = startLocation.id,
[nameof(WmsSortingtask.location_code)] = startLocation.location_code,
[nameof(WmsSortingtask.create_id)] = input.create_id ?? _userManager.UserId,
[nameof(WmsSortingtask.create_time)] = DateTime.Now,
[nameof(WmsSortingtask.source_id)] = input.source_id
};
VisualDevModelDataCrInput visualDevModelDataCrInput = new()
{
data = dic
};
//在线开发
VisualDevEntity? templateEntity = await _visualDevService.GetInfoById(ModuleConsts.MODULE_WMSSORTINGTASK_ID, false);
await _runService.Create(templateEntity, visualDevModelDataCrInput);
}
// 更新销售出库单子表已下发数量
await _db.Updateable<WmsSaleD>().SetColumns(r => r.purchase_arriveqty == r.purchase_arriveqty + input.qty).Where(r => r.id == input.source_id).ExecuteCommandAsync();
await _db.Ado.CommitTranAsync();
Logger.LogInformation($"【Distribute】 销售出库下发完成");
}
catch (Exception ex)
{
await _db.Ado.RollbackTranAsync();
Logger.LogError($"【Distribute】 销售出库 {ex.Message}");
Logger.LogError($"【Distribute】 销售出库 {ex.StackTrace}");
return await ToApiResult(HttpStatusCode.InternalServerError, ex.Message);
}
finally
{
semaphoreSlim.Release();
await InvokeGenPretaskExcute();
}
return await ToApiResult(HttpStatusCode.OK, "成功");
}
/// <summary>
/// 下发
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
/// <exception cref="AppFriendlyException"></exception>
//[HttpPost, NonUnify, AllowAnonymous]
//public async Task<Tnb.WarehouseMgr.Entities.Dto.Outputs.Result> Distribute(SaleReleaseDistributeInput input)
//{
// Logger.LogInformation($"【Distribute】 销售出库下发");
// SemaphoreSlim semaphoreSlim = null;
// try
// {
// semaphoreSlim = _wareHouseService.GetSemaphore("outstock", WmsWareHouseConst.WAREHOUSE_CP_ID);
// await semaphoreSlim.WaitAsync();
// if (input.qty <= 0)
// {
// throw new AppFriendlyException("数量必须大于0", 500);
// }
// if (string.IsNullOrEmpty(input.source_id))
// {
// throw new AppFriendlyException("来源单据id不可为空", 500);
// }
// WmsSaleD wmsOutstockD = await _db.Queryable<WmsSaleD>().FirstAsync(it => it.id == input.source_id);
// await _db.Ado.BeginTranAsync();
// //入库取终点 //出库起点
// OutStockStrategyQuery outStockStrategyInput = new() { warehouse_id = WmsWareHouseConst.WAREHOUSE_CP_ID, material_id = wmsOutstockD.material_id, qty = input.qty, code_batch = wmsOutstockD.code_batch, Region_id = WmsWareHouseConst.REGION_CPOutstock_ID };
// List<Tuple<string, WmsCarryH, WmsCarryCode, BasLocation>> items = await _wareHouseService.OutStockStrategy_saleRelease(outStockStrategyInput);
// decimal canOutstockQty = items.Sum(r => r.Item3.codeqty).ParseToDecimal();
// if (canOutstockQty < input.qty)
// {
// throw new AppFriendlyException($@"当前可出库数量只有 {canOutstockQty.ToString("G")}", 500);
// }
// List<Tuple<string, WmsCarryH, WmsCarryCode, BasLocation>> items_sorttask = items.Where(r => r.Item1 == "分拣任务").ToList();
// List<Tuple<string, WmsCarryH, WmsCarryCode, BasLocation>> items_pretask = items.Where(r => r.Item1 == "预任务").ToList();
// Logger.LogInformation($"【Distribute】 预计生成{items_pretask.Count}条预任务");
// List<BasLocation> endLocations = null;
// // 自动发货
// if (!input.isManual)
// {
// endLocations = await _db.Queryable<BasLocation>().Where(r => _wareHouseService.GetFloor1OutstockLocation().Contains(r.id) && r.is_lock == 0 && r.is_use == "0").ToListAsync();
// if (endLocations.Count < items_pretask.Count)
// {
// throw new AppFriendlyException("一楼没有足够的未锁定且空闲的出库工位", 500);
// }
// List<BasLocation> endLocations_temp = await _db.Queryable<BasLocation>().Where(r => r.region_id == WmsWareHouseConst.REGION_CPOutstockCache_ID && r.is_lock == 0 && r.is_use == "0").ToListAsync();
// if (endLocations_temp.Count < items_pretask.Count)
// {
// throw new AppFriendlyException($"三楼发货时,没有可用的暂存库位可以使用,需要下发任务{items_pretask.Count}条,可用的暂存库位只有{endLocations_temp.Count}条", 500);
// }
// }
// //人工发货
// else
// {
// InStockStrategyQuery inStockStrategyInput = new() { warehouse_id = WmsWareHouseConst.WAREHOUSE_CP_ID, Size = items_pretask.Count, Region_id = WmsWareHouseConst.REGION_CPManualOutstock_ID };
// endLocations = await _wareHouseService.InStockStrategy(inStockStrategyInput);
// if (endLocations.Count < items_pretask.Count)
// {
// throw new AppFriendlyException("三楼人工出库区没有足够的未锁定且空闲的出库工位", 500);
// }
// }
// // 预任务逻辑
// foreach (Tuple<string, WmsCarryH, WmsCarryCode, BasLocation> item in items_pretask)
// {
// WmsCarryH carry = item.Item2;
// WmsCarryCode carryCode = item.Item3;
// BasLocation startLocation = item.Item4;
// // 根据一楼工位任务数平均分配任务 确定一楼工位
// BasLocation endLocation = null;
// // 自动发货
// if (!input.isManual)
// {
// endLocation = await _db.Queryable<BasLocation>().Where(r => _wareHouseService.GetFloor1OutstockLocation().Contains(r.id) && r.is_lock == 0 && r.is_use == "0").OrderBy("is_lock, task_nums, location_code").FirstAsync();
// if (endLocation == null)
// {
// throw new AppFriendlyException("一楼没有足够的未锁定且空闲的出库工位", 500);
// }
// }
// //人工发货
// else
// {
// InStockStrategyQuery inStockStrategyInput = new() { warehouse_id = WmsWareHouseConst.WAREHOUSE_CP_ID, Size = 1, Region_id = WmsWareHouseConst.REGION_CPManualOutstock_ID };
// endLocations = await _wareHouseService.InStockStrategy(inStockStrategyInput);
// if (endLocations.Count < 1)
// {
// throw new AppFriendlyException("三楼人工出库区没有足够的未锁定且空闲的出库工位", 500);
// }
// endLocation = endLocations[0];
// }
// if (endLocation == null)
// {
// throw new AppFriendlyException("一楼没有足够的未锁定且空闲的出库工位", 500);
// }
// WmsPointH sPoint = null!;
// WmsPointH ePoint = null!;
// sPoint = await _db.Queryable<WmsPointH>().FirstAsync(it => it.location_id == startLocation.id);
// bool isMatch = await IsCarryAndLocationMatchByCarryStd(carry, endLocation);
// if (!isMatch)
// {
// throw new AppFriendlyException("库位与载具规格不匹配", 500);
// }
// ePoint = await _db.Queryable<WmsPointH>().FirstAsync(it => it.location_id == endLocation.id);
// if (ePoint != null)
// {
// }
// else
// {
// throw new AppFriendlyException($"库位{endLocation.location_code}未在点位表中维护对应点位", 500);
// }
// string endLocationId = endLocation.id;
// // 在线开发
// //VisualDevEntity? templateEntity = await _visualDevService?.GetInfoById(ModuleConsts.MODULE_WMSDELIVERYPDA_ID, true)!;
// //await _runService.Create(templateEntity, input);
// // 计算路径,插入预任务申请
// if (sPoint != null && ePoint != null)
// {
// List<WmsPointH> points = new List<WmsPointH>();
// if (sPoint.area_code != ePoint.area_code)
// {
// points = await _wareHouseService.PathAlgorithms(sPoint.id, ePoint.id);
// //if (points.Count <= 2)
// //{
// // throw new AppFriendlyException($"sPoint {sPoint.point_code} ePoint{ePoint.point_code}该路径不存在", 500);
// //}
// }
// else
// {
// points.Add(sPoint);
// points.Add(ePoint);
// }
// //根据获取的路径点生成预任务,生成顺序必须预路径算法返回的起终点的顺序一致(预任务顺序)
// if (points?.Count > 0)
// {
// List<WmsPretaskH> preTasks = points.Where(it => !it.location_id.IsNullOrEmpty()).GroupBy(g => g.area_code).Select(it =>
// {
// WmsPointH? sPoint = it.FirstOrDefault();
// WmsPointH? ePoint = it.LastOrDefault();
// WmsPretaskH preTask = new()
// {
// org_id = _userManager!.User.OrganizeId,
// startlocation_id = sPoint?.location_id!,
// startlocation_code = sPoint?.location_code!,
// endlocation_id = ePoint?.location_id!,
// endlocation_code = ePoint?.location_code!,
// start_floor = sPoint?.floor.ToString(),
// end_floor = ePoint?.floor.ToString(),
// startpoint_id = sPoint?.id!,
// startpoint_code = sPoint?.point_code!,
// endpoint_id = ePoint?.id!,
// endpoint_code = ePoint?.point_code!,
// bill_code = _billRullService!.GetBillNumber(WmsWareHouseConst.WMS_PRETASK_H_ENCODE).GetAwaiter().GetResult(),
// status = WmsWareHouseConst.PRETASK_BILL_STATUS_DXF_ID,
// biz_type = WmsWareHouseConst.BIZTYPE_WMSSALERELEASE_ID,
// task_type = WmsWareHouseConst.WMS_PRETASK_TRANSFER_TYPE_ID
// };
// preTask.carry_id = carry.id;
// preTask.carry_code = carry.carry_code;
// preTask.area_id = sPoint?.area_id!;
// preTask.area_code = it.Key;
// preTask.require_id = input.source_id;
// preTask.require_code = "";
// preTask.create_id = _userManager.UserId;
// preTask.create_time = DateTime.Now;
// return preTask;
// }).ToList();
// bool isOk = await _wareHouseService.GenPreTask(preTasks, null!, _db);
// if (isOk)
// {
// if (endLocationId != null)
// {
// //查询库位表
// BasLocation location = await _db.Queryable<BasLocation>().SingleAsync(it => it.id == startLocation.id);
// {
// //载具加锁,增加库位信息
// _ = await _db.Updateable<WmsCarryH>().SetColumns(it => new WmsCarryH
// {
// carry_status = ((int)EnumCarryStatus.占用).ToString(),
// is_lock = 1,
// location_id = startLocation.id,
// location_code = location.location_code,
// require_id = input.source_id,
// biz_type = WmsWareHouseConst.BIZTYPE_WMSSALERELEASE_ID
// }).Where(it => it.id == carry.id).ExecuteCommandAsync();
// }
// //所有库位加锁
// string?[] ids = new[] { startLocation.id, preTasks[0].endlocation_id, endLocationId };
// _ = await _db.Updateable<BasLocation>().SetColumns(it => new BasLocation { is_lock = 1 }).Where(it => ids.Contains(it.id)).ExecuteCommandAsync();
// }
// }
// }
// }
// }
// Logger.LogInformation($"【Distribute】 预计生成{items_sorttask.Count}条分拣任务");
// // 分拣任务逻辑
// foreach (Tuple<string, WmsCarryH, WmsCarryCode, BasLocation> item in items_sorttask)
// {
// WmsCarryH carry = item.Item2;
// WmsCarryCode carryCode = item.Item3;
// BasLocation startLocation = item.Item4;
// // 锁定分拣载具
// await _db.Updateable<WmsCarryH>().SetColumns(r => r.is_lock == 1).Where(r => r.id == carry.id).ExecuteCommandAsync();
// Dictionary<string, object> dic = new()
// {
// [nameof(WmsSortingtask.id)] = SnowflakeIdHelper.NextId(),
// [nameof(WmsSortingtask.bill_code)] = await _billRullService.GetBillNumber(WmsWareHouseConst.WMS_SORTINGTASK_ENCODE),
// [nameof(WmsSortingtask.org_id)] = input.org_id ?? _userManager.User.OrganizeId,
// [nameof(WmsSortingtask.qty)] = carryCode.codeqty,
// [nameof(WmsSortingtask.carry_id)] = carry.id,
// [nameof(WmsSortingtask.carry_code)] = carry.carry_code,
// [nameof(WmsSortingtask.status)] = WmsWareHouseConst.TASK_BILL_STATUS_DZX_ID,
// [nameof(WmsSortingtask.location_id)] = startLocation.id,
// [nameof(WmsSortingtask.location_code)] = startLocation.location_code,
// [nameof(WmsSortingtask.create_id)] = input.create_id ?? _userManager.UserId,
// [nameof(WmsSortingtask.create_time)] = DateTime.Now,
// [nameof(WmsSortingtask.source_id)] = input.source_id
// };
// VisualDevModelDataCrInput visualDevModelDataCrInput = new()
// {
// data = dic
// };
// //在线开发
// VisualDevEntity? templateEntity = await _visualDevService.GetInfoById(ModuleConsts.MODULE_WMSSORTINGTASK_ID, false);
// await _runService.Create(templateEntity, visualDevModelDataCrInput);
// }
// // 更新销售出库单子表已下发数量
// await _db.Updateable<WmsSaleD>().SetColumns(r => r.purchase_arriveqty == r.purchase_arriveqty + input.qty).Where(r => r.id == input.source_id).ExecuteCommandAsync();
// await _db.Ado.CommitTranAsync();
// Logger.LogInformation($"【Distribute】 销售出库下发完成");
// }
// catch (Exception ex)
// {
// await _db.Ado.RollbackTranAsync();
// Logger.LogError($"【Distribute】 销售出库 {ex.Message}");
// Logger.LogError($"【Distribute】 销售出库 {ex.StackTrace}");
// return await ToApiResult(HttpStatusCode.InternalServerError, ex.Message);
// }
// finally
// {
// semaphoreSlim.Release();
// await InvokeGenPretaskExcute();
// }
// return await ToApiResult(HttpStatusCode.OK, "成功");
//}
/// <summary>
/// 分拣
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost, NonUnify, AllowAnonymous]
public async Task<Tnb.WarehouseMgr.Entities.Dto.Outputs.Result> Sort(FinishproductOutstockSortInput input)
{
if (string.IsNullOrEmpty(input.source_id))
{
throw new AppFriendlyException("分拣任务id不能为空", 500);
}
await _db.Ado.BeginTranAsync();
try
{
WmsSortingtask wmsSortingtask = await _db.Queryable<WmsSortingtask>().Where(r => r.id == input.source_id).FirstAsync();
if (wmsSortingtask.status == WmsWareHouseConst.TASK_BILL_STATUS_COMPLE_ID)
{
throw new AppFriendlyException("此分拣任务已完成,不允许重复提交", 500);
}
// 解锁分拣载具
await _db.Updateable<WmsCarryH>().SetColumns(r => r.is_lock == 0).Where(r => r.id == wmsSortingtask.carry_id).ExecuteCommandAsync();
// 扣减载具物料库存
await _db.Updateable<WmsCarryCode>().SetColumns(r => r.codeqty == r.codeqty - wmsSortingtask.qty).Where(r => r.carry_id == wmsSortingtask.carry_id).ExecuteCommandAsync();
await _db.Updateable<WmsSortingtask>().SetColumns(r => new WmsSortingtask
{
status = WmsWareHouseConst.TASK_BILL_STATUS_COMPLE_ID,
complete_time = DateTime.Now
}).Where(r => r.id == wmsSortingtask.id).ExecuteCommandAsync();
// 改为销售出库单
await _db.Updateable<WmsSaleD>().SetColumns(r => r.purchase_prqty == r.purchase_prqty + wmsSortingtask.qty).Where(r => r.id == wmsSortingtask.source_id).ExecuteCommandAsync();
//await _db.Updateable<WmsCarryCode>().SetColumns(r => new WmsCarryCode
//{
// codeqty = input.qty,
//}).Where(r => r.carry_id == input.carry_id).ExecuteCommandAsync();
await _db.Ado.CommitTranAsync();
}
catch (Exception ex)
{
await _db.Ado.RollbackTranAsync();
return await ToApiResult(HttpStatusCode.InternalServerError, ex.Message);
}
return await ToApiResult(HttpStatusCode.OK, "成功");
}
}
}