422 lines
22 KiB
C#
422 lines
22 KiB
C#
using System;
|
|
using System.ArrayExtensions;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Security.Claims;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using JNPF;
|
|
using JNPF.Common.Const;
|
|
using JNPF.Common.Core.Manager;
|
|
using JNPF.Common.Dtos.VisualDev;
|
|
using JNPF.Common.Enums;
|
|
using JNPF.Common.Extension;
|
|
using JNPF.Common.Manager;
|
|
using JNPF.Common.Security;
|
|
using JNPF.FriendlyException;
|
|
using JNPF.Systems.Interfaces.System;
|
|
using JNPF.VisualDev;
|
|
using JNPF.VisualDev.Entitys;
|
|
using JNPF.VisualDev.Interfaces;
|
|
using Mapster;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using NPOI.SS.Formula;
|
|
using SqlSugar;
|
|
using TencentCloud.Common;
|
|
using Tnb.BasicData.Entities;
|
|
using Tnb.Common.Utils;
|
|
using Tnb.WarehouseMgr.Entities;
|
|
using Tnb.WarehouseMgr.Entities.Attributes;
|
|
using Tnb.WarehouseMgr.Entities.Consts;
|
|
using Tnb.WarehouseMgr.Entities.Dto;
|
|
using Tnb.WarehouseMgr.Entities.Enums;
|
|
using Tnb.WarehouseMgr.Entities.Exceptions;
|
|
using Tnb.WarehouseMgr.Interfaces;
|
|
using UAParser;
|
|
|
|
namespace Tnb.WarehouseMgr
|
|
{
|
|
/// <summary>
|
|
/// 齐套分拣服务类
|
|
/// </summary>
|
|
[ServiceModule(BizTypeId)]
|
|
[OverideVisualDev(ModuleConsts.MODULE_WMSSETSORTING_ID)]
|
|
public class WmsSetSortingService : BaseWareHouseService, IWmsSetSortingService
|
|
{
|
|
private readonly ISqlSugarClient _db;
|
|
private readonly IWareHouseService _wareHouseService;
|
|
private readonly IBillRullService _billRullService;
|
|
private readonly IUserManager _userManager;
|
|
private readonly ICacheManager _cacheManager;
|
|
private readonly IRunService _runService;
|
|
private readonly IVisualDevService _visualDevService;
|
|
private const string BizTypeId = "26186830379045";
|
|
|
|
public WmsSetSortingService(
|
|
ISqlSugarRepository<WmsSetsortingH> repository,
|
|
IWareHouseService wareHouseService,
|
|
IUserManager userManager, IBillRullService billRullService,
|
|
ICacheManager cacheManager,
|
|
IRunService runService,
|
|
IVisualDevService visualDevService,
|
|
ITaskMessageNotify taskMessageNotify
|
|
) : base(taskMessageNotify.Writer)
|
|
{
|
|
_db = repository.AsSugarClient();
|
|
_wareHouseService = wareHouseService;
|
|
_billRullService = billRullService;
|
|
_userManager = userManager;
|
|
_cacheManager = cacheManager;
|
|
_runService = runService;
|
|
_visualDevService = visualDevService;
|
|
//OverideFuncs.CreateAsync = Create;
|
|
}
|
|
|
|
private async Task<dynamic> Create(VisualDevModelDataCrInput input)
|
|
{
|
|
//在线开发
|
|
try
|
|
{
|
|
VisualDevEntity? templateEntity = await _visualDevService.GetInfoById(ModuleConsts.MODULE_WMSEMPTYINSTOCK_ID, true);
|
|
await _runService.Create(templateEntity, input);
|
|
|
|
await PackSortingByAdd();
|
|
}
|
|
catch (Exception)
|
|
{
|
|
throw;
|
|
}
|
|
return Task.FromResult(1);
|
|
|
|
}
|
|
|
|
/// <summary>
|
|
/// 齐套分拣(新增状态)
|
|
/// </summary>
|
|
/// <returns></returns>
|
|
[HttpPost, Timed(Name = nameof(PackSortingByAdd))]
|
|
public async Task PackSortingByAdd(CancellationTokenSource? cts = default)
|
|
{
|
|
//if (UserManager.AsscessToken.IsNullOrWhiteSpace()) return;
|
|
//var curUser = await GetUserIdentity();
|
|
|
|
//Console.WriteLine($"ThreadID:{Thread.CurrentThread.ManagedThreadId}\t Thread pool: {Thread.CurrentThread.IsThreadPoolThread}");
|
|
|
|
var curDb = _db.CopyNew();
|
|
string firstLocationId = "27010980724501", secondLocationId = "27010987857941";
|
|
var endLocation = await curDb.Queryable<BasLocation>().SingleAsync(it => it.id == secondLocationId);
|
|
|
|
var setSortings = await curDb.Queryable<WmsSetsortingH>()
|
|
.Where(a => a.status == WmsWareHouseConst.BILLSTATUS_ADD_ID)
|
|
.Select<WmsSetsortingH>()
|
|
.OrderBy(a => a.seq)
|
|
.ToListAsync();
|
|
var items = await curDb.Queryable<WmsSetsortingH>().Where(it => it.status == WmsWareHouseConst.BILLSTATUS_ON_ID).ToListAsync();
|
|
var onFlag = items?.Count > 0;
|
|
try
|
|
{
|
|
await curDb.Ado.BeginTranAsync();
|
|
|
|
if (setSortings?.Count > 0 && !onFlag)
|
|
{
|
|
var singleSorting = setSortings[0];
|
|
|
|
var setSortingDList = await curDb.Queryable<WmsSetsortingD>().Where(it => it.bill_id == singleSorting.id).ToListAsync();
|
|
if (setSortingDList?.Count > 0)
|
|
{
|
|
List<WmsCarryMat> carryMats = new();
|
|
List<WmsCarryCode> carryCodes = new();
|
|
List<string> carryIds = new();
|
|
|
|
foreach (var os in setSortingDList)
|
|
{
|
|
var OutStockStrategyInput = new OutStockStrategyQuery
|
|
{
|
|
material_id = os.material_id,
|
|
warehouse_id = os.warehouse_id,
|
|
code_batch = os.code_batch,
|
|
};
|
|
var outStkCarrys = await _wareHouseService.OutStockStrategy(OutStockStrategyInput);
|
|
var carryCodesPart = await curDb.Queryable<WmsCarryH>().InnerJoin<WmsCarryCode>((a, b) => a.id == b.carry_id).InnerJoin<BasLocation>((a, b, c) => a.location_id == c.id)
|
|
.Where((a, b) => outStkCarrys.Select(x => x.id).Contains(b.carry_id))
|
|
.Select<WmsCarryCode>()
|
|
.ToListAsync();
|
|
|
|
|
|
if (carryCodesPart?.Count > 0)
|
|
{
|
|
carryCodes.AddRange(carryCodesPart);
|
|
var codeQty = carryCodes.Sum(x => x.codeqty);
|
|
if (codeQty < os.pr_qty)
|
|
{
|
|
throw new AppFriendlyException($"需要出库[{os.pr_qty}],实际库存{codeQty},数量不足", 500);
|
|
}
|
|
List<WmsCarryCode> curCarryCodes = new();
|
|
for (int i = 0; i < carryCodesPart.Count; i++)
|
|
{
|
|
if (os.pr_qty > carryCodesPart[i].codeqty)
|
|
{
|
|
os.pr_qty -= carryCodesPart[i].codeqty;
|
|
curCarryCodes.Add(carryCodesPart[i]);
|
|
}
|
|
else if (os.pr_qty <= carryCodesPart[i].codeqty)
|
|
{
|
|
WmsCarryCode curCarryCode = DeepCopyHelper<WmsCarryCode>.DeepCopy(carryCodesPart[i]);
|
|
curCarryCode.codeqty = os.pr_qty;
|
|
curCarryCodes.Add(curCarryCode);
|
|
break;
|
|
}
|
|
}
|
|
var partCarryMats = curCarryCodes.Adapt<List<WmsCarryMat>>();
|
|
for (int i = 0; i < partCarryMats.Count; i++)
|
|
{
|
|
partCarryMats[i].need_qty = curCarryCodes[i].codeqty;
|
|
}
|
|
|
|
carryMats.AddRange(partCarryMats);
|
|
}
|
|
}
|
|
if (carryMats.Count > 0)
|
|
{
|
|
carryMats.ForEach(x => x.id = SnowflakeIdHelper.NextId());
|
|
carryMats = carryMats.OrderBy(o => o.create_time).GroupBy(g => new { g.carry_id, g.material_id, g.code_batch })
|
|
.Select(x =>
|
|
{
|
|
var arr = x.ToArray();
|
|
WmsCarryMat carryMat = arr[^arr.Length];
|
|
carryMat.need_qty = arr.Sum(d => d.need_qty);
|
|
return carryMat;
|
|
})
|
|
.ToList();
|
|
await curDb.Insertable(carryMats).ExecuteCommandAsync();
|
|
|
|
carryIds = carryMats.Select(x => x.carry_id).Distinct().ToList();
|
|
|
|
await curDb.Updateable<WmsCarryH>()
|
|
.SetColumns(it => new WmsCarryH
|
|
{
|
|
out_status = ((int)EnumOutStatus.全部出).ToString(),
|
|
collocation_scheme_id = singleSorting.collocation_scheme_id,
|
|
collocation_scheme_code = singleSorting.collocation_scheme_code,
|
|
source_id = singleSorting.source_id,
|
|
source_code = singleSorting.source_code
|
|
})
|
|
.Where(it => carryIds.Contains(it.id)).ExecuteCommandAsync();
|
|
|
|
//await curDb.Updateable<WmsCarryH>().SetColumns(it => new WmsCarryH { out_status = ((int)EnumOutStatus.全部出).ToString(), collocation_scheme_id = singleSorting.collocation_scheme_id, collocation_scheme_code = singleSorting.collocation_scheme_code }).Where(it => carryIds.Contains(it.id)).ExecuteCommandAsync();
|
|
|
|
//天益项目不需要
|
|
//await _db.Updateable<WmsCarryH>().SetColumns(it => new WmsCarryH { out_status = ((int)EnumOutStatus.分拣出).ToString() }).Where(it => sortingOutIds.Contains(it.id)).ExecuteCommandAsync();
|
|
}
|
|
var carrys = await curDb.Queryable<WmsCarryH>().Where(it => carryIds.Contains(it.id)).ToArrayAsync();
|
|
if (carrys?.Length > 0)
|
|
{
|
|
if (setSortings?.Count > 0)
|
|
{
|
|
var curCarry = carrys[^carrys.Length];
|
|
var isMatch = await IsCarryAndLocationMatchByCarryStd(curCarry, endLocation);
|
|
if (!isMatch) throw new AppFriendlyException("库位与载具规格不匹配", 500);
|
|
}
|
|
List<WmsPretaskH> preTasks = new();
|
|
List<string> locIds = new();
|
|
|
|
var mid = 6;
|
|
|
|
if (carrys.Length > mid)
|
|
{
|
|
var leftCarrys = carrys[..mid];
|
|
var rightCarrys = carrys[mid..];
|
|
await InnerGenPreTask(leftCarrys, locIds, firstLocationId, singleSorting.id, singleSorting.bill_code, preTasks, endLocation);
|
|
await InnerGenPreTask(rightCarrys, locIds, secondLocationId, singleSorting.id, singleSorting.bill_code, preTasks, endLocation);
|
|
}
|
|
else
|
|
{
|
|
await InnerGenPreTask(carrys, locIds, firstLocationId, singleSorting.id, singleSorting.bill_code, preTasks, endLocation);
|
|
}
|
|
List<WmsPretaskCode> pretaskCodes = new();
|
|
foreach (var pt in preTasks)
|
|
{
|
|
var partCodes = carryCodes.FindAll(x => x.carry_id == pt.carry_id).Distinct().ToList();
|
|
var curPreTaskCodes = partCodes.Adapt<List<WmsPretaskCode>>();
|
|
curPreTaskCodes.ForEach(x =>
|
|
{
|
|
x.id = SnowflakeIdHelper.NextId();
|
|
x.bill_id = pt.id;
|
|
x.create_time = DateTime.Now;
|
|
});
|
|
pretaskCodes.AddRange(curPreTaskCodes);
|
|
}
|
|
await _wareHouseService.GenPreTask(preTasks, pretaskCodes);
|
|
await curDb.Updateable<WmsSetsortingH>().SetColumns(it => new WmsSetsortingH { status = WmsWareHouseConst.BILLSTATUS_ON_ID }).Where(it => it.id == singleSorting.id).ExecuteCommandAsync();
|
|
GenPreTaskUpInput genPreTaskAfterUpInput = new();
|
|
genPreTaskAfterUpInput.CarryIds = preTasks.Select(x => x.carry_id).ToList();
|
|
genPreTaskAfterUpInput.LocationIds = new HashSet<string>(locIds).ToList();
|
|
await _wareHouseService.GenInStockTaskHandleAfter(genPreTaskAfterUpInput, it => new WmsCarryH { is_lock = 1, carry_status = ((int)EnumCarryStatus.齐套分拣).ToString() }, it => new BasLocation { is_use = ((int)EnumCarryStatus.齐套分拣).ToString(), is_lock = 1 });
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
await curDb.Ado.CommitTranAsync();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
JNPF.Logging.Log.Error("齐套分拣执行时出现错误", ex);
|
|
await curDb.Ado.RollbackTranAsync();
|
|
TimedTaskErrorInfo ei = new()
|
|
{
|
|
RequestURL = App.HttpContext?.Request?.Path,
|
|
RequestMethod = App.HttpContext?.Request?.Method,
|
|
userIdentity = await GetUserIdentity(_userManager.ToKen),
|
|
};
|
|
var timedTaskEx = ex.ToTimedTaskException(ei);
|
|
cts?.Cancel();
|
|
throw timedTaskEx;
|
|
}
|
|
finally
|
|
{
|
|
await Publish(nameof(IWareHouseService.GenTaskExecute));
|
|
}
|
|
}
|
|
|
|
private async Task InnerGenPreTask(WmsCarryH[] carrys, List<string> locIds, string eLocationId, string requireId, string requireCode, List<WmsPretaskH> preTasks, BasLocation endLocation)
|
|
{
|
|
foreach (var carry in carrys)
|
|
{
|
|
WmsPointH sPoint = await _db.Queryable<WmsPointH>().FirstAsync(it => it.location_id == carry.location_id);
|
|
WmsPointH ePoint = await _db.Queryable<WmsPointH>().FirstAsync(it => it.location_id == eLocationId);
|
|
|
|
if (sPoint != null && ePoint != null)
|
|
{
|
|
var points = await _wareHouseService.PathAlgorithms(sPoint.id, ePoint.id);
|
|
if (points?.Count > 0)
|
|
{
|
|
locIds.AddRange(points.Select(x => x.location_id).ToList()!);
|
|
}
|
|
//根据获取的路径点生成预任务,生成顺序必须预路径算法返回的起终点的顺序一致(预任务顺序)
|
|
if (points?.Count > 0)
|
|
{
|
|
if (points.Count <= 2) throw new AppFriendlyException("该路径不存在", 500);
|
|
var curPreTasks = points.Where(it => !it.location_id.IsNullOrEmpty()).GroupBy(g => g.area_code).Select(it =>
|
|
{
|
|
var sPoint = it.FirstOrDefault();
|
|
var 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(),
|
|
bill_code = _billRullService.GetBillNumber(WmsWareHouseConst.WMS_PRETASK_H_ENCODE).GetAwaiter().GetResult(),
|
|
status = WmsWareHouseConst.PRETASK_BILL_STATUS_DXF_ID,
|
|
biz_type = WmsWareHouseConst.BIZTYPE_WMSSETSORTING_ID,
|
|
task_type = WmsWareHouseConst.WMS_PRETASK_OUTSTOCK_TYPE_ID,
|
|
carry_id = carry.id,
|
|
carry_code = carry.carry_code,
|
|
area_id = sPoint?.area_id!,
|
|
area_code = it.Key,
|
|
require_id = requireId,
|
|
require_code = requireCode,
|
|
create_id = _userManager.UserId,
|
|
create_time = DateTime.Now,
|
|
source_id = carry.source_id,
|
|
source_code = carry.source_code,
|
|
};
|
|
return preTask;
|
|
}).ToList();
|
|
if (endLocation.is_sign == 0)
|
|
{
|
|
curPreTasks[^1].is_sign = 0; // 修改最后一个元素的是否签收值
|
|
}
|
|
preTasks.AddRange(curPreTasks);
|
|
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
|
|
public override async Task ModifyAsync(WareHouseUpInput input)
|
|
{
|
|
if (input == null) throw new ArgumentNullException(nameof(input));
|
|
//根据载具更新明细表状态
|
|
try
|
|
{
|
|
await _db.Ado.BeginTranAsync();
|
|
|
|
var carryId = input.carryIds[^input.carryIds.Count];
|
|
var ssds = await _db.Queryable<WmsSetsortingD>().Where(it => it.bill_id == input.requireId).ToListAsync();
|
|
var carryCodes = await _db.Queryable<WmsCarryCode>().Where(it => it.carry_id == carryId).ToListAsync();
|
|
|
|
var sortingCoees = carryCodes.Adapt<List<WmsSetsortingCode>>();
|
|
sortingCoees.ForEach(x =>
|
|
{
|
|
var billDId = ssds?.Find(xx => xx.material_id == x.material_id && xx.code_batch == x.code_batch)?.id;
|
|
if (billDId.IsNullOrEmpty())
|
|
{
|
|
billDId = ssds?.Find(xx => xx.material_id == x.material_id)?.id;
|
|
}
|
|
x.id = SnowflakeIdHelper.NextId();
|
|
x.bill_id = input.requireId;
|
|
x.bill_d_id = billDId!;
|
|
x.org_id = ssds?.Select(xx => xx.org_id).FirstOrDefault() ?? string.Empty;
|
|
x.create_id = _userManager.UserId;
|
|
x.create_time = DateTime.Now;
|
|
});
|
|
await _db.Insertable(sortingCoees).ExecuteCommandAsync();
|
|
var detailIds = sortingCoees.Select(x => x.bill_d_id).ToList();
|
|
var curSortingDetails = ssds.FindAll(x => detailIds.Contains(x.id));
|
|
var dic = sortingCoees.GroupBy(g => g.bill_d_id).ToDictionary(x => x.Key, x => x.Select(x => x.codeqty).ToList());
|
|
foreach (var osd in curSortingDetails)
|
|
{
|
|
if (dic.ContainsKey(osd.id))
|
|
{
|
|
osd.qty += dic[osd.id].Sum(d => d);
|
|
if (osd.qty >= osd.pr_qty)
|
|
{
|
|
osd.line_status = WmsWareHouseConst.BILLSTATUS_COMPLETE_ID;
|
|
}
|
|
else
|
|
{
|
|
osd.line_status = WmsWareHouseConst.BILLSTATUS_ON_ID;
|
|
}
|
|
}
|
|
}
|
|
await _db.Updateable(curSortingDetails).ExecuteCommandAsync();
|
|
if (curSortingDetails.All(it => it.line_status == WmsWareHouseConst.BILLSTATUS_COMPLETE_ID))
|
|
{
|
|
await _db.Updateable<WmsSetsortingH>().SetColumns(it => new WmsSetsortingH { status = WmsWareHouseConst.BILLSTATUS_COMPLETE_ID }).Where(it => it.id == input.requireId).ExecuteCommandAsync();
|
|
}
|
|
var carry = await _db.Queryable<WmsCarryH>().SingleAsync(it => it.id == carryId);
|
|
if (carry != null)
|
|
{
|
|
if (carry.location_id?.IsNotEmptyOrNull() ?? false)
|
|
{
|
|
await _db.Updateable<BasLocation>().SetColumns(it => new BasLocation { is_use = ((int)EnumCarryStatus.空闲).ToString() }).Where(it => it.id == carry.location_id).ExecuteCommandAsync();
|
|
}
|
|
carry.out_status = ((int)EnumOutStatus.正常).ToString();
|
|
carry.carry_status = ((int)EnumCarryStatus.齐套).ToString();
|
|
carry.location_id = null;
|
|
carry.location_code = null;
|
|
await _db.Updateable(carry).UpdateColumns(it => new { it.out_status, it.carry_status, it.location_id, it.location_code }).ExecuteCommandAsync();
|
|
await _db.Deleteable<WmsCarryMat>().Where(it => it.carry_id == carryId).ExecuteCommandAsync();
|
|
}
|
|
|
|
await _db.Ado.CommitTranAsync();
|
|
}
|
|
catch (Exception)
|
|
{
|
|
await _db.Ado.RollbackTranAsync();
|
|
throw;
|
|
|
|
}
|
|
}
|
|
|
|
|
|
}
|
|
}
|