using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using JNPF.Common.Core.Manager;
using JNPF.Common.Enums;
using JNPF.Common.Extension;
using JNPF.Common.Security;
using JNPF.FriendlyException;
using JNPF.Systems.Interfaces.System;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using NPOI.SS.Formula;
using SqlSugar;
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.Interfaces;
using UAParser;
namespace Tnb.WarehouseMgr
{
///
/// 齐套分拣服务类
///
[ServiceModule(BizTypeId)]
public class WmsSetSortingService : BaseWareHouseService
{
private readonly ISqlSugarClient _db;
private readonly IWareHouseService _wareHouseService;
private readonly IBillRullService _billRullService;
private readonly IUserManager _userManager;
private const string BizTypeId = "26186830379045";
public WmsSetSortingService(ISqlSugarRepository repository, IWareHouseService wareHouseService, IUserManager userManager, IBillRullService billRullService)
{
_db = repository.AsSugarClient();
_wareHouseService = wareHouseService;
_billRullService = billRullService;
_userManager = userManager;
}
///
/// 齐套分拣(新增状态)
///
///
[HttpPost]
public async Task PackSortingByAdd(WmsCarryMat carryMat)
{
string firstLocationId = "27010980724501", secondLocationId = "27010987857941";
var endLocation = await _db.Queryable().SingleAsync(it => it.id == secondLocationId);
var setSortings = await _db.Queryable()
.Where(a => a.status == WmsWareHouseConst.BILLSTATUS_ADD_ID).OrderBy(a => a.seq)
.ToListAsync();
var items = await _db.Queryable().Where(it => it.status == WmsWareHouseConst.BILLSTATUS_ON_ID).ToListAsync();
var onFlag = items?.Count > 0;
try
{
await _db.Ado.BeginTranAsync();
var singleSorting = new WmsSetsortingH();
if (setSortings?.Count > 0)
{
singleSorting = setSortings[0];
var curCarry = await _db.Queryable().SingleAsync(it => it.id == singleSorting.carry_id);
var isMatch = await IsCarryAndLocationMatchByCarryStd(curCarry, endLocation);
if (!isMatch) throw new AppFriendlyException("库位与载具规格不匹配", 500);
}
if (setSortings?.Count > 0 && !onFlag)
{
var setSortingDList = await _db.Queryable().Where(it => it.bill_id == singleSorting.id).ToListAsync();
if (setSortingDList?.Count > 0)
{
List carryMats = new();
List carryCodes = new();
List carryIds = new();
var whereExpr = Expressionable.Create()
.And((a, b, c) => a.is_lock == 0)
.And((a, b, c) => !string.IsNullOrEmpty(a.location_id))
.And((a, b, c) => a.status == (int)EnumCarryStatus.占用);
foreach (var os in setSortingDList)
{
whereExpr.And((a, b, c) => b.material_id == os.material_id)
.And((a, b, c) => c.wh_id == os.warehouse_id)
.AndIF(!string.IsNullOrEmpty(os.code_batch), (a, b, c) => b.code_batch == os.code_batch);
var carryCodesPart = await _db.Queryable().InnerJoin((a, b) => a.id == b.carry_id).InnerJoin((a, b, c) => a.location_id == c.id)
.Where(whereExpr.ToExpression())
.Select()
.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 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.DeepCopy(carryCodesPart[i]);
curCarryCode.codeqty = os.pr_qty;
curCarryCodes.Add(curCarryCode);
break;
}
}
var partCarryMats = curCarryCodes.Adapt>();
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 _db.Insertable(carryMats).ExecuteCommandAsync();
carryIds = carryMats.Select(x => x.carry_id).Distinct().ToList();
await _db.Updateable().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().SetColumns(it => new WmsCarryH { out_status = ((int)EnumOutStatus.分拣出).ToString() }).Where(it => sortingOutIds.Contains(it.id)).ExecuteCommandAsync();
}
var carrys = await _db.Queryable().Where(it => carryIds.Contains(it.id)).ToArrayAsync();
if (carrys?.Length > 0)
{
List preTasks = new();
List 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 pretaskCodes = new();
foreach (var pt in preTasks)
{
var partCodes = carryCodes.FindAll(x => x.carry_id == pt.carry_id).Distinct().ToList();
var curPreTaskCodes = partCodes.Adapt>();
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 _db.Updateable().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(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 _db.Ado.CommitTranAsync();
}
catch (Exception)
{
await _db.Ado.RollbackTranAsync();
throw;
}
}
private async Task InnerGenPreTask(WmsCarryH[] carrys, List locIds, string eLocationId, string requireId, string requireCode, List preTasks, BasLocation endLocation)
{
foreach (var carry in carrys)
{
WmsPointH sPoint = await _db.Queryable().FirstAsync(it => it.location_id == carry.location_id);
WmsPointH ePoint = await _db.Queryable().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
};
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().Where(it => it.bill_id == input.requireId).ToListAsync();
var carryCodes = await _db.Queryable().Where(it => it.carry_id == carryId).ToListAsync();
var sortingCoees = carryCodes.Adapt>();
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 = _userManager.User.OrganizeId;
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().SetColumns(it => new WmsSetsortingH { status = WmsWareHouseConst.BILLSTATUS_COMPLETE_ID }).Where(it => it.id == input.requireId).ExecuteCommandAsync();
}
var carry = await _db.Queryable().SingleAsync(it => it.id == carryId);
if (carry != null)
{
if (carry.location_id?.IsNotEmptyOrNull() ?? false)
{
await _db.Updateable().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().Where(it => it.carry_id == carryId).ExecuteCommandAsync();
}
await _db.Ado.CommitTranAsync();
}
catch (Exception)
{
await _db.Ado.RollbackTranAsync();
throw;
}
}
}
}