using Microsoft.AspNetCore.Identity ;
using Microsoft.AspNetCore.Mvc ;
using Microsoft.Extensions.Logging ;
using Newtonsoft.Json ;
using WMS_GIRAF_Interface.Data ;
using WMS_GIRAF_Interface.Entities ;
using WMS_GIRAF_Interface.Models ;
using WMS_GIRAF_Interface.Repositories.Implement ;
using WMS_GIRAF_Interface.Repositories.Interface ;
using WMS_GIRAF_Interface.TaskServices ;
namespace WMS_GIRAF_Interface.Controllers ;
/// <summary>
/// 入库申请Giraf->WMS
/// </summary>
[ApiController, Route("/Input")]
public class InputRequestController (
ILogger < InputRequestController > logger ,
IdestinationIdRepository destinationIdRepository ,
ManageTaskService manageTaskService ,
IManageMainRepository manageMainRepository ,
IWhCellRepository whCellRepository ,
IProductInformationRepository productInformationRepository ,
IIoControlRepository ioControlRepository ,
CellLogicService cellLogicService )
: ControllerBase
{
private readonly ILogger < InputRequestController > _l ogger = logger ;
/// <summary>
/// 入库任务请求
/// </summary>
/// <param name="inputRequest">轮胎入库任务请求json</param>
[HttpPost]
public InputRequestReturn . RootObject GetInputRequestFromGiraf ( InputRequest . RootObject inputRequest )
{
bool bResult = true ;
string totalResult = "" ;
string sResult = string . Empty ;
//TODO:获取入库任务,并生成到巷道的任务
InputRequestReturn . RootObject returnParameter = new InputRequestReturn . RootObject ( ) ;
//优先判断products是否包含轮胎信息
if ( inputRequest ! = null )
{
if ( ! string . IsNullOrEmpty ( inputRequest . introductionPost ) )
{
//检查当前post number是否存在
var checkPost =
destinationIdRepository . GetDestinationId ( Convert . ToInt32 ( inputRequest . introductionPost ) ) ;
if ( checkPost ! = null )
{
//创建入库任务
if ( inputRequest is { products : { Count : > 0 } , support : not null } )
{
string lineWay = string . Empty ;
foreach ( var inputRequestProduct in inputRequest . products )
{
//更新产品信息
//判断当前入库胎号是否有任务
var checkTireTask =
manageMainRepository . GetManageMain ( "barcode" , inputRequestProduct . identifier ) ;
if ( checkTireTask ! = null )
{
//tlt:查看是否有物料信息,如果包含物料信息,就进行更新
var checkProductInfo = productInformationRepository . GetProductInformation (
inputRequestProduct . identifier ) ;
if ( checkProductInfo ! = null )
{
//更新物料信息
checkProductInfo . BarCode = inputRequestProduct . identifier ;
checkProductInfo . LotNumber = inputRequest . sku ;
checkProductInfo . PopulationNumber = inputRequestProduct . productClass ;
checkProductInfo . SupportType = [ inputRequest . support . type ] ;
checkProductInfo . FabricationDateTimeInUtc = ( DateTime ) inputRequestProduct . fabricationDate ! ;
checkProductInfo . DryDateTimeInUtc = ( DateTime ) inputRequestProduct . maturationDate ! ;
checkProductInfo . OldDateTimeInUtc = ( DateTime ) inputRequestProduct . highPriorityDate ! ;
checkProductInfo . HdvDateTimeInUtc = ( DateTime ) inputRequestProduct . expirationDate ! ;
checkProductInfo . IsBlocked = inputRequestProduct . isBlocked ;
checkProductInfo . IsPriority = inputRequestProduct . isPriority ;
productInformationRepository . UpdateProductInformation ( checkProductInfo ) ;
logger . LogInformation ( $"Update product information:{inputRequestProduct.identifier}" ) ;
}
else
{
ProductInformation newProductInfo = new ProductInformation ( )
{
//新增物料信息
BarCode = inputRequestProduct . identifier ,
LotNumber = inputRequest . sku ,
PopulationNumber = inputRequestProduct . productClass ,
SupportType = [ inputRequest . support . type ] ,
FabricationDateTimeInUtc = ( DateTime ) inputRequestProduct . fabricationDate ! ,
DryDateTimeInUtc = ( DateTime ) inputRequestProduct . maturationDate ! ,
OldDateTimeInUtc = ( DateTime ) inputRequestProduct . highPriorityDate ! ,
HdvDateTimeInUtc = ( DateTime ) inputRequestProduct . expirationDate ! ,
IsBlocked = inputRequestProduct . isBlocked ,
IsPriority = inputRequestProduct . isPriority
} ;
productInformationRepository . AddProductInformation ( newProductInfo ) ;
logger . LogInformation ( $"Add product information:{inputRequestProduct.identifier};/r/n Json String : {JsonConvert.SerializeObject(newProductInfo)}" ) ;
}
if ( checkTireTask . MANAGE_TYPE_CODE ! = "ManageLineMove" )
{
//出现问题 生成任务错误
returnParameter . status = "2" ;
returnParameter . introductionPost = inputRequest . introductionPost ;
returnParameter . errorDetails + = $"Tire barcode {inputRequestProduct.identifier} already have manual loading task! ;" ;
}
else
{
//更新任务信息:将当前的信息保存到任务信息中,根据新的起点来更新可用的入库路径信息,筛选一个可用的入库巷道
var cellInfoEnd = whCellRepository . GetCell ( checkTireTask . END_CELL_ID ) ;
//判断当前任务终点是否包含在接口可用的终点列表中
if ( inputRequest . availableIntroductionPosts ! = null )
{
if ( inputRequest . availableIntroductionPosts . Contains ( cellInfoEnd . CELL_CODE ) )
{
lineWay = cellInfoEnd . LANE_WAY ;
}
else
{
//获取当前申请的起点 decision point
var cellInfoStart = whCellRepository . GetCell ( inputRequest . introductionPost ) ;
bResult = cellLogicService . GetLaneWayFromDecisionPoint ( cellInfoStart ,
checkTireTask . CELL_MODEL , checkTireTask . STOCK_BARCODE ,
inputRequest . availableIntroductionPosts , checkTireTask . LOTNUMBER ,
out int endLaneWay , out sResult ) ;
if ( bResult )
{
//更新终点信息到任务中
checkTireTask . END_CELL_ID = endLaneWay ;
checkTireTask . requestNumber = inputRequest . inputRequestId ;
checkTireTask . CELL_MODEL = inputRequest . support . type ;
checkTireTask . START_CELL_ID = cellInfoStart . CELL_ID ;
checkTireTask . LOTNUMBER = inputRequest . sku ;
checkTireTask . POPULATIONNUMBER = inputRequestProduct . productClass ;
checkTireTask . keepRequestInMemoryEventIfLackOfProductIssue = true ;
checkTireTask . keepRequestInMemoryEventIfMechanicalIssue = true ;
bResult = manageMainRepository . UpdateManageMain ( checkTireTask ) ;
if ( bResult )
{
//任务更新成功:完成本次
returnParameter . status = "1" ;
returnParameter . introductionPost = inputRequest . introductionPost ;
logger . LogInformation ( $"Update task info success! tire barcode :{checkTireTask.STOCK_BARCODE};/r/n task info: {JsonConvert.SerializeObject(checkTireTask)} " ) ;
}
else
{
//更新任务信息失败
returnParameter . status = "2" ;
returnParameter . introductionPost = inputRequest . introductionPost ;
returnParameter . errorDetails + = $"Tire barcode {inputRequestProduct.identifier} error reason : Insert task failed! ;" ;
logger . LogError ( $"Update task info failed! tire barcode :{inputRequestProduct.identifier}; error reason : Insert task failed!" ) ;
}
}
else
{
returnParameter . status = "4" ;
returnParameter . introductionPost = inputRequest . introductionPost ;
returnParameter . errorDetails + = $"Update task info failed! reason: WMS can not get available input post number!" ;
logger . LogError ( $"Update task info failed! tire barcode :{inputRequestProduct.identifier}; error reason : WMS can not get available input post number!" ) ;
}
}
}
else
{
//无可用终点,返回错误信息
returnParameter . status = "4" ;
returnParameter . introductionPost = inputRequest . introductionPost ;
returnParameter . errorDetails + = $"Update task info failed! reason: Json string parameter availableIntroductionPosts is null!" ;
logger . LogError ( $"tire barcode :{inputRequestProduct.identifier} parameter availableIntroductionPosts is null ,can not create task " ) ;
}
}
}
else
{
var cellInfoStart = whCellRepository . GetCell ( inputRequest . introductionPost ) ;
if ( cellInfoStart ! = null )
{
int endLaneWay = 0 ;
//入库申请点位为Decision Point DP
if ( cellInfoStart . CELL_MODEL = = "DP" )
{
bResult = cellLogicService . GetLaneWayFromDecisionPoint ( cellInfoStart ,
checkTireTask . CELL_MODEL , checkTireTask . STOCK_BARCODE ,
inputRequest . availableIntroductionPosts , checkTireTask . LOTNUMBER ,
out endLaneWay , out sResult ) ;
}
else //一楼入库时,入库点为VP,CP,EP,CV
{
bResult = cellLogicService . GetCellLocationFromCheckPoint ( cellInfoStart ,
out endLaneWay , out sResult ) ;
}
if ( bResult )
{
//生成任务
bResult = manageTaskService . CreateInputTaskFromGiraf ( inputRequestProduct ,
inputRequest . introductionPost , endLaneWay , inputRequest . sku , inputRequest . support . type , inputRequest . inputRequestId ,
inputRequest . support . IsBlocked , false , false ,
out sResult ) ;
if ( bResult )
{
//生成任务成功
returnParameter . status = "1" ;
returnParameter . introductionPost = inputRequest . introductionPost ;
returnParameter . errorDetails + =
$"Tire barcode {inputRequestProduct.identifier} created task success! ;" ;
logger . LogInformation ( $"Create task info success! tire barcode :{inputRequestProduct.identifier};/r/n task info: {inputRequest.sku}|{inputRequest.inputRequestId} " ) ;
}
else
{
//生成任务错误
//出现问题 生成任务错误
returnParameter . status = "2" ;
returnParameter . introductionPost = inputRequest . introductionPost ;
returnParameter . errorDetails + =
$"Tire barcode {inputRequestProduct.identifier} create task failed! the reason: {sResult} ;" ;
}
}
else
{
//找不到可用巷道
returnParameter . status = "4" ;
returnParameter . introductionPost = inputRequest . introductionPost ;
returnParameter . errorDetails + = $"Update task info failed! reason: WMS can not get available input post number!" ;
logger . LogError ( $"Update task info failed! tire barcode :{inputRequestProduct.identifier}; error reason : WMS can not get available input post number!" ) ;
}
}
else
{
//找不到对应起点信息
returnParameter . status = "4" ;
returnParameter . introductionPost = inputRequest . introductionPost ;
returnParameter . errorDetails + = $"Update task info failed! reason: Json string parameter availableIntroductionPosts is null!" ;
logger . LogError ( $"tire barcode :{inputRequestProduct.identifier} parameter availableIntroductionPosts is null ,can not create task " ) ;
}
}
}
}
}
else
{
//出现问题:找不到对应终点
returnParameter . status = "5" ;
returnParameter . introductionPost = inputRequest . introductionPost ;
returnParameter . errorDetails = $"WMS didn't find the post number{inputRequest.introductionPost} " ;
logger . LogError ( $"tire barcode :{inputRequest.introductionPost}; Error message:{returnParameter.errorDetails}" ) ;
}
}
}
else
{
//未收到有效信息
returnParameter . status = "2" ;
returnParameter . introductionPost = inputRequest . introductionPost ;
returnParameter . errorDetails = $"WMS didn't receive the right format input parameters! " ;
logger . LogError ( returnParameter . errorDetails ) ;
}
return returnParameter ;
}
/// <summary>
/// 入库轮胎申请
/// /Input/ItemDropped
/// </summary>
/// <param name="itemDropped">取消入库请求报文</param>
[HttpPost("ItemDropped")]
public void GetItemDroppedFromGiraf ( ItemDropped . RootObject itemDropped )
{
//TODO:获取轮胎post信息,并进行更新
}
/// <summary>
/// 取消入库请求
/// </summary>
/// <param name="inputRequest"></param>
[HttpPost("Cancel")]
public CancelInputRequestReturn CancelInputRequest ( CancelInputRequest . RootObject ? inputRequest )
{
//TODO:取消入库请求,如果已经到入库口,无法进行取消
//根据requestNumber获取入库信息
CancelInputRequestReturn returnJson = new CancelInputRequestReturn ( ) ;
if ( inputRequest ! = null & & string . IsNullOrEmpty ( inputRequest . inputRequestId ) )
{
var inputTaskInfo = manageMainRepository . GetManageMain ( "requestNumber" , inputRequest . inputRequestId ) ;
if ( inputTaskInfo ! = null )
{
//排查任务状态,并查看是否已经有控制任务
var checkIoControlTask = ioControlRepository . GetIoControlTaskByManageId ( inputTaskInfo . MANAGE_ID ) ;
if ( checkIoControlTask ! = null )
{
//不可以删除当前任务
returnJson . status = "3" ;
returnJson . errorDetails = "this task has been already in progress,it cannot be deleted " ;
logger . LogError ( $"cannot delete input task [{inputTaskInfo.STOCK_BARCODE}] already have task {inputTaskInfo.STOCK_BARCODE};" ) ;
}
else
{
//可以删除任务
manageMainRepository . DeleteManageMain ( inputTaskInfo ) ;
returnJson . status = "1" ;
returnJson . errorDetails = "" ;
logger . LogInformation ( $"delete task [{inputTaskInfo.STOCK_BARCODE}] success" ) ;
}
}
else
{
//未查到对应入库任务
returnJson . status = "2" ;
returnJson . errorDetails = $"WMS can not detect this task,please confirm request number [{inputRequest.inputRequestId}] " ;
logger . LogError ( returnJson . errorDetails ) ;
}
}
else
{
//传入数据位null
returnJson . status = "2" ;
returnJson . errorDetails = "CancelInputRequest:WMS can not get the json string " ;
logger . LogError ( returnJson . errorDetails ) ;
}
return returnJson ;
}
}