Files
tnb.server/WarehouseMgr/Tnb.WarehouseMgr/WmsRobotCallbackService.cs
2023-08-04 11:26:08 +08:00

197 lines
12 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Text;
using System.Threading.Tasks;
using JNPF.Common.Core.Manager;
using JNPF.Common.Dtos.VisualDev;
using JNPF.Common.Extension;
using JNPF.Common.Security;
using JNPF.FriendlyException;
using JNPF.Systems.Interfaces.System;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using NPOI.OpenXmlFormats;
using SqlSugar;
using Tnb.BasicData.Entities;
using Tnb.WarehouseMgr.Entities;
using Tnb.WarehouseMgr.Entities.Consts;
using Tnb.WarehouseMgr.Entities.Dto;
using Tnb.WarehouseMgr.Entities.Dto.Inputs;
using Tnb.WarehouseMgr.Entities.Enums;
using Tnb.WarehouseMgr.Interfaces;
namespace Tnb.WarehouseMgr
{
/// <summary>
/// 机器人回调服务类
/// </summary>
public class WmsRobotCallbackService : BaseWareHouseService
{
private readonly IWmsCarryBindService _wmsCarryBindService;
private readonly ISqlSugarClient _db;
private readonly IUserManager _userManager;
private readonly IWareHouseService _warehouseService;
private readonly IBillRullService _billRullService;
private readonly IWmsKittingInStkService _wmsKittingInStkService;
public WmsRobotCallbackService(
IWmsCarryBindService wmsCarryBindService,
ISqlSugarRepository<WmsCarrybindH> repository,
IUserManager userManager,
IWareHouseService warehouseService,
IBillRullService billRullService,
IWmsKittingInStkService wmsKittingInStkService)
{
_wmsCarryBindService = wmsCarryBindService;
_db = repository.AsSugarClient();
_userManager = userManager;
_warehouseService = warehouseService;
_billRullService = billRullService;
_wmsKittingInStkService = wmsKittingInStkService;
}
/// <summary>
/// 机器人完成任务后回调接口
/// </summary>
/// <param name="input"></param>
/// <returns></returns>
[HttpPost]
public async Task RobotCallback(RobotCallBackInput input)
{
var carry = await _db.Queryable<WmsCarryH>().SingleAsync(it => it.id == input.carry_id);
var subCarry = await _db.Queryable<WmsCarryH>().SingleAsync(it => it.id == input.membercarry_id);
if (carry != null && subCarry != null)
{
VisualDevModelDataCrInput visualDevModelCrInput = new() { data = new Dictionary<string, object>() };
visualDevModelCrInput.data[nameof(WmsCarrybindH.carry_id)] = input.carry_id;
visualDevModelCrInput.data[nameof(WmsCarrybindH.carry_code)] = input.carry_code;
visualDevModelCrInput.data[nameof(WmsCarrybindH.membercarry_id)] = input.membercarry_id;
visualDevModelCrInput.data[nameof(WmsCarrybindH.membercarry_code)] = input.membercarry_code;
visualDevModelCrInput.data[nameof(WmsCarrybindH.type)] = 0;
visualDevModelCrInput.data[nameof(WmsCarrybindH.carrystd_id)] = carry.carrystd_id;
visualDevModelCrInput.data[nameof(WmsCarrybindH.create_id)] = _userManager.UserId;
visualDevModelCrInput.data[nameof(WmsCarrybindH.create_time)] = DateTime.Now;
visualDevModelCrInput.data[nameof(WmsCarrybindH.loc)] = input.loc;
await _wmsCarryBindService.CarryBind(visualDevModelCrInput);
carry.collocation_scheme_id = subCarry.collocation_scheme_id;
carry.collocation_scheme_code = subCarry.collocation_scheme_code;
carry.carry_status = ((int)EnumCarryStatus.).ToString();
await _db.Updateable(carry).UpdateColumns(it => new { it.collocation_scheme_id, it.collocation_scheme_code, it.carry_status }).ExecuteCommandAsync();
if (input.isLast)
{
var kittingout = await _db.Queryable<WmsKittingoutH>().FirstAsync(it => it.collocation_scheme_id == carry.collocation_scheme_id && it.status == WmsWareHouseConst.BILLSTATUS_CALLED_ID);
if (kittingout != null)
{
visualDevModelCrInput.data = new Dictionary<string, object>();
visualDevModelCrInput.data[nameof(InStockStrategyQuery.warehouse_id)] = kittingout.warehouse_id;
visualDevModelCrInput.data[nameof(WmsKittingInstock.carry_id)] = input.carry_id;
visualDevModelCrInput.data[nameof(WmsKittingInstock.carry_code)] = input.carry_code;
visualDevModelCrInput.data[nameof(WmsKittingInstock.collocation_scheme_id)] = carry!.collocation_scheme_id!;
visualDevModelCrInput.data[nameof(WmsKittingInstock.collocation_scheme_code)] = carry!.collocation_scheme_code!;
visualDevModelCrInput.data[nameof(WmsKittingInstock.seq)] = kittingout.seq;
visualDevModelCrInput.data[nameof(WmsKittingInstock.biz_type)] = WmsWareHouseConst.BIZTYPE_WMSKITTINGINSTK_ID;
visualDevModelCrInput.data[nameof(WmsKittingInstock.status)] = WmsWareHouseConst.BILLSTATUS_ADD_ID;
visualDevModelCrInput.data[nameof(WmsKittingInstock.create_id)] = _userManager.UserId;
visualDevModelCrInput.data[nameof(WmsKittingInstock.create_time)] = DateTime.Now;
visualDevModelCrInput.data[nameof(WmsKittingInstock.location_id)] = carry!.location_id!;
visualDevModelCrInput.data[nameof(WmsKittingInstock.bill_code)] = _billRullService.GetBillNumber("WmsKittingInStk").GetAwaiter().GetResult();
visualDevModelCrInput.data[nameof(WmsKittingInstock.source_id)] = kittingout.id;
visualDevModelCrInput.data[nameof(WmsKittingInstock.source_code)] = kittingout.bill_code;
var location = await _db.Queryable<BasLocation>().SingleAsync(it => it.id == kittingout.location_id);
if (kittingout.location_id.IsNullOrWhiteSpace() || (location != null && location.is_type.ToEnum<EnumLocationType>() == EnumLocationType.))
{
//入库
await _wmsKittingInStkService.KittingInStk(visualDevModelCrInput);
}
else
{
if (location != null && location.is_use == ((int)EnumCarryStatus.).ToString() && location.is_lock == 0)
{
//出库
var sPoint = await _db.Queryable<WmsPointH>().FirstAsync(it => it.location_id == carry.location_id);
var ePoint = await _db.Queryable<WmsPointH>().FirstAsync(it => it.location_id == kittingout.location_id);
if (sPoint != null && ePoint != null)
{
var kittingOutObj = new { requireId = kittingout.id, requireCode = kittingout.bill_code };
var preTasks = await _genPreTask(sPoint, ePoint, carry, kittingOutObj);
var carryCodes = await _db.Queryable<WmsCarryCode>().Where(it => it.carry_id == carry.id).ToListAsync();
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);
kittingout.status = WmsWareHouseConst.BILLSTATUS_ON_ID;
await _db.Updateable(kittingout).UpdateColumns(it => it.status).ExecuteCommandAsync();
GenPreTaskUpInput genPreTaskAfterUpInput = new();
genPreTaskAfterUpInput.CarryIds = preTasks.Select(x => x.carry_id).ToList();
genPreTaskAfterUpInput.LocationIds = new List<string> { carry.location_id! };
await _warehouseService.GenInStockTaskHandleAfter(genPreTaskAfterUpInput, it => new WmsCarryH { is_lock = 1 }, it => new BasLocation { is_lock = 1 });
}
}
else
{
//入库
await _wmsKittingInStkService.KittingInStk(visualDevModelCrInput);
}
}
}
}
}
}
private async Task<List<WmsPretaskH>> _genPreTask(WmsPointH sPoint, WmsPointH ePoint, WmsCarryH carry, dynamic KittingOutObj)
{
List<WmsPretaskH> preTasks = new();
var points = await _warehouseService.PathAlgorithms(sPoint.id, ePoint.id);
//根据获取的路径点生成预任务,生成顺序必须预路径算法返回的起终点的顺序一致(预任务顺序)
if (points.Count <= 2) throw new AppFriendlyException("该路径不存在", 500);
if (points?.Count > 0)
{
preTasks = 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();
preTask.org_id = _userManager.User.OrganizeId;
preTask.startlocation_id = sPoint?.location_id!;
preTask.startlocation_code = sPoint?.location_code!;
preTask.endlocation_id = ePoint?.location_id!;
preTask.endlocation_code = ePoint?.location_code!;
preTask.start_floor = sPoint?.floor.ToString();
preTask.end_floor = ePoint?.floor.ToString();
preTask.bill_code = _billRullService.GetBillNumber(WmsWareHouseConst.WMS_PRETASK_H_ENCODE).GetAwaiter().GetResult();
preTask.status = WmsWareHouseConst.PRETASK_BILL_STATUS_DXF_ID;
preTask.biz_type = WmsWareHouseConst.BIZTYPE_WMSKITTINGOUTSTK_ID;
preTask.task_type = WmsWareHouseConst.WMS_PRETASK_OUTSTOCK_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 = KittingOutObj.requireId;
preTask.require_code = KittingOutObj.requireCode;
preTask.create_id = _userManager.UserId;
preTask.create_time = DateTime.Now;
return preTask;
}).ToList();
}
return preTasks;
}
}
}