using Microsoft.AspNetCore.Identity ;
using Microsoft.AspNetCore.Mvc ;
using Microsoft.Extensions.Logging ;
using Newtonsoft.Json ;
using Serilog ;
using WMS_GIRAF_Interface.Entities ;
using WMS_GIRAF_Interface.Models ;
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 ,
IIoControlRouteRepository ioControlRouteRepository ,
CellLogicService cellLogicService )
: ControllerBase
{
private readonly ILogger < InputRequestController > _l ogger = logger ;
/// <summary>
/// 入库任务请求
/// </summary>
/// <param name="inputRequest">轮胎入库任务请求json</param>
[HttpPost]
public InputRequestReturn . RootObjectForInputRequestReturn GetInputRequestFromGiraf ( InputRequest . RootObjectForInputRequest ? inputRequest )
{
bool bResult = true ;
string totalResult = "" ;
string sResult = string . Empty ;
//TODO:获取入库任务,并生成到巷道的任务
InputRequestReturn . RootObjectForInputRequestReturn returnParameter = new InputRequestReturn . RootObjectForInputRequestReturn ( ) ;
//优先判断products是否包含轮胎信息
if ( inputRequest ! = null )
{
if ( ! string . IsNullOrEmpty ( inputRequest . introductionPost ) )
{
//检查当前post number是否存在
var checkPost =
destinationIdRepository . GetDestinationId ( 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 ? ? "0000000000" ;
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 ) ;
Log . Information ( $"Update product information:{inputRequestProduct.identifier}" ) ;
}
else
{
ProductInformation newProductInfo = new ProductInformation ( )
{
//新增物料信息
BarCode = inputRequestProduct . identifier ,
LotNumber = inputRequest . sku ,
PopulationNumber = inputRequestProduct . productClass ? ? "0000000000" ,
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 ) ;
Log . Information ( $"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 ) ;
if ( cellInfoStart ! = null )
{
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 . keepRequestInMemoryEvenIfLackOfProductIssue = true ;
checkTireTask . keepRequestInMemoryEvenIfMechanicalIssue = true ;
bResult = manageMainRepository . UpdateManageMain ( checkTireTask ) ;
if ( bResult )
{
//任务更新成功:完成本次
returnParameter . status = "1" ;
returnParameter . introductionPost =
inputRequest . introductionPost ;
Log . Information (
$"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!" ;
Log . Error (
$"Update task info failed! tire barcode :{inputRequestProduct.identifier}; error reason : WMS can not get available input post number!" ) ;
}
}
else
{
//起始DP有问题,查不到起点DP
returnParameter . status = "4" ;
returnParameter . introductionPost = inputRequest . introductionPost ;
returnParameter . errorDetails + =
$"Update task info failed! reason: WMS can not get available input post number!" ;
Log . Error (
$"Update task info failed! tire barcode :{inputRequestProduct.identifier}; error reason : WMS can not get DP number!" ) ;
}
}
}
else
{
//无可用终点,返回错误信息
returnParameter . status = "4" ;
returnParameter . introductionPost = inputRequest . introductionPost ;
returnParameter . errorDetails + = $"Update task info failed! reason: Json string parameter availableIntroductionPosts is null!" ;
Log . Error ( $"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" )
{
if ( inputRequest . availableIntroductionPosts ! = null )
{
bResult = cellLogicService . GetLaneWayFromDecisionPoint ( cellInfoStart ,
inputRequest . support . type , inputRequestProduct . identifier ,
inputRequest . availableIntroductionPosts , inputRequest . sku ,
out endLaneWay , out sResult ) ;
}
else
{
//可选巷道为空
}
}
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! ;" ;
Log . Information ( $"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!" ;
Log . Error ( $"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!" ;
Log . Error ( $"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} " ;
Log . Error ( $"tire barcode :{inputRequest.introductionPost}; Error message:{returnParameter.errorDetails}" ) ;
}
}
}
else
{
//未收到有效信息
returnParameter . status = "2" ;
returnParameter . introductionPost = null ;
returnParameter . errorDetails = $"WMS didn't receive the right format input parameters! " ;
Log . Error ( returnParameter . errorDetails ) ;
}
return returnParameter ;
}
/// <summary>
/// 入库轮胎申请
/// /Input/ItemDropped
/// </summary>
/// <param name="itemDropped">取消入库请求报文</param>
[HttpPost("ItemDropped")]
public ItemDroppedReturn . RootObjectForItemDroppedReturn GetItemDroppedFromGiraf ( ItemDropped . RootObjectForItemDropped ? itemDropped )
{
//TODO:获取轮胎新的introduction post信息,并进行更新
var itemDroppedReturn = new ItemDroppedReturn . RootObjectForItemDroppedReturn ( ) ;
if ( itemDropped ! = null )
{
if ( ! string . IsNullOrEmpty ( itemDropped . inputRequestId ) )
{
if ( ! string . IsNullOrEmpty ( itemDropped . introductionPost ) )
{
//判断当前request ID是否有任务存在
var checkManageMain = manageMainRepository . GetManageMain ( "requestNumber" , itemDropped . inputRequestId ) ;
if ( checkManageMain is { MANAGE_STATUS : "WaitingSend" , MANAGE_TYPE_CODE : "ManageLineMove" } )
{
//判断当前的任务起点是否为报文给出位置
if ( checkManageMain . START_CELL_ID ! = 0 )
{
var startCell = whCellRepository . GetCell ( checkManageMain . START_CELL_ID ) ;
if ( startCell ! = null )
{
if ( startCell . CELL_CODE ! = itemDropped . introductionPost )
{
//获取新的post的信息,然后更新任务信息
var newCell = whCellRepository . GetCell ( itemDropped . introductionPost ) ;
if ( newCell ! = null )
{
var endCell = whCellRepository . GetCell ( checkManageMain . END_CELL_ID ) ;
if ( endCell ! = null )
{
//检查新的post与终点巷道是否有路径可用;
var getRoutes =
ioControlRouteRepository . GetEnableControlRoutes ( newCell . CELL_CODE ,
endCell . CELL_CODE ) ;
if ( getRoutes is { Count : > 0 } )
{
//有课+-\
checkManageMain . START_CELL_ID = newCell . CELL_ID ;
checkManageMain . MANAGE_BEGIN_TIME =
DateTime . Now . ToString ( "yyyy-MM-dd HH:mm:ss" ) ;
manageMainRepository . UpdateManageMain ( checkManageMain ) ;
itemDroppedReturn . status = "1" ;
itemDroppedReturn . errorDetails = null ;
Log . Information ( $"{checkManageMain.STOCK_BARCODE} Item Dropped at {newCell.CELL_CODE},end lane way :{endCell.CELL_CODE} " ) ;
}
else
{
//当前新起点无法到达指定终点巷道
itemDroppedReturn . status = "0" ;
itemDroppedReturn . errorDetails = $"the introduction post{newCell.CELL_CODE} can not reach the line{endCell.CELL_CODE}" ;
Log . Error ( $"requestId :{itemDropped.inputRequestId}; Error details:+ the introduction post{newCell.CELL_CODE} can not reach the line{endCell.CELL_CODE}" ) ;
}
}
else
{
//出库站台信息为空
itemDroppedReturn . status = "0" ;
itemDroppedReturn . errorDetails = $"Error details: the task have errors, the end line ID ({checkManageMain.END_CELL_ID}) can not get info from WMS " ;
Log . Error ( itemDroppedReturn . errorDetails ) ;
}
}
else
{
//报错:未查到新post信息
itemDroppedReturn . status = "2" ;
itemDroppedReturn . errorDetails = $"Error details: the task have errors, the new Post ID ({itemDropped.introductionPost}) can not get info from WMS" ;
Log . Error ( itemDroppedReturn . errorDetails ) ;
}
}
else
{
//如果相等不做任何更改
itemDroppedReturn . status = "4" ;
itemDroppedReturn . errorDetails = null ;
Log . Information ( $"{checkManageMain.STOCK_BARCODE} Item Dropped already done" ) ;
}
}
else
{
//起始站台信息不为空
itemDroppedReturn . status = "0" ;
itemDroppedReturn . errorDetails = $"Error details: the task have errors, the start post ID ({checkManageMain.START_CELL_ID}) can not get info from WMS " ;
Log . Error ( itemDroppedReturn . errorDetails ) ;
}
}
else
{
//起始站台id不为空
itemDroppedReturn . status = "0" ;
itemDroppedReturn . errorDetails = $"Error details: the task have errors, the start post ID ({checkManageMain.START_CELL_ID}) is null" ;
Log . Error ( itemDroppedReturn . errorDetails ) ;
}
}
else
{
//无任务或者任务已经执行
if ( checkManageMain = = null )
{
itemDroppedReturn . status = "3" ;
itemDroppedReturn . errorDetails = $"Error details: the task didn't find, the request ID ({itemDropped.inputRequestId}) can not find corresponding task" ;
Log . Error ( itemDroppedReturn . errorDetails ) ;
}
else if ( checkManageMain . MANAGE_STATUS ! = "WaitingSend" )
{
itemDroppedReturn . status = "0" ;
itemDroppedReturn . errorDetails = $"Error details: the task ({itemDropped.inputRequestId}) is already running. " ;
Log . Error ( itemDroppedReturn . errorDetails ) ;
}
else if ( checkManageMain . MANAGE_TYPE_CODE ! = "ManageLineMove" )
{
itemDroppedReturn . status = "0" ;
itemDroppedReturn . errorDetails = $"Error details: the task ({itemDropped.inputRequestId}) type code is not right. the task type code is {checkManageMain.MANAGE_TYPE_CODE} " ;
Log . Error ( itemDroppedReturn . errorDetails ) ;
}
}
}
else
{
//introduction post为空
itemDroppedReturn . status = "2" ;
itemDroppedReturn . errorDetails = $"Error details: the task have errors, the new Post ID ({itemDropped.introductionPost}) is null" ;
Log . Error ( itemDroppedReturn . errorDetails ) ;
}
}
else
{
//requestId为空
itemDroppedReturn . status = "0" ;
itemDroppedReturn . errorDetails = $"Error details: the task have errors, the request ID ({itemDropped.inputRequestId}) is null" ;
Log . Error ( itemDroppedReturn . errorDetails ) ;
}
}
else
{
//报文为空
itemDroppedReturn . status = "0" ;
itemDroppedReturn . errorDetails = $"Error details: the task have errors, the request ID ({itemDropped.inputRequestId}) is null" ;
Log . Error ( itemDroppedReturn . errorDetails ) ;
}
//返回信息
return itemDroppedReturn ;
}
/// <summary>
/// 取消入库请求
/// </summary>
/// <param name="inputRequest"></param>
[HttpPost("Cancel")]
public CancelInputRequestReturn CancelInputRequest ( CancelInputRequest . RootObjectForCancelInputRequest ? 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 " ;
Log . Error ( $"cannot delete input task [{inputTaskInfo.STOCK_BARCODE}] already have task {inputTaskInfo.STOCK_BARCODE};" ) ;
}
else
{
//可以删除任务
manageMainRepository . DeleteManageMain ( inputTaskInfo ) ;
returnJson . status = "1" ;
returnJson . errorDetails = "" ;
Log . Information ( $"delete task [{inputTaskInfo.STOCK_BARCODE}] success" ) ;
}
}
else
{
//未查到对应入库任务
returnJson . status = "2" ;
returnJson . errorDetails = $"WMS can not detect this task,please confirm request number [{inputRequest.inputRequestId}] " ;
Log . Error ( returnJson . errorDetails ) ;
}
}
else
{
//传入数据位null
returnJson . status = "2" ;
returnJson . errorDetails = "The input json string is null " ;
Log . Error ( returnJson . errorDetails ) ;
}
return returnJson ;
}
}