using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using Serilog;
using WMS_GIRAF_Interface.Models;
using WMS_GIRAF_Interface.Repositories.Interface;
namespace WMS_GIRAF_Interface.Controllers
{
[ApiController]
[Route("products")]
public class UpdateProductsController(
IStorageMainRepository storageMainRepository
) : ControllerBase
{
///
/// 更新产品信息
///
///
///
[HttpPut]
public UpdateProductsReturn UpdateProducts(UpdateProducts.RootObjectForUpdateProducts? updateProducts)
{
//根据提供的轮胎条码更新产品信息
UpdateProductsReturn returnJson = new UpdateProductsReturn();
if (updateProducts != null && !string.IsNullOrEmpty(updateProducts.updateRequestId))
{
if (updateProducts.products is{Length:>0})
{
int countNumber = updateProducts.products.Length;
foreach (var productInfo in updateProducts.products)
{
//检查轮胎条码是否
var checkBarcode = storageMainRepository.GetStorageMain("barcode", productInfo.identifier);
if (checkBarcode != null)
{
Log.Information($"{nameof(updateProducts)}: Record Tire Information: {JsonConvert.SerializeObject(checkBarcode)}");
//根据接口提供的信息进行更新
checkBarcode.IsBlocked = productInfo.isBlocked;
checkBarcode.IsPriority = productInfo.isPriority;
checkBarcode.PopulationNumber = productInfo.productClass;
checkBarcode.DryDateTimeInUtc = productInfo.maturationDate;
checkBarcode.OldDateTimeInUtc = productInfo.highPriorityDate;
checkBarcode.HdvDateTimeInUtc = productInfo.expirationDate;
if (string.IsNullOrEmpty(productInfo.newIdentifier) && checkBarcode.STOCK_BARCODE!=productInfo.identifier)
{
checkBarcode.STOCK_BARCODE = productInfo.identifier;
Log.Information($"{nameof(updateProducts)} change stock barcode {checkBarcode.STOCK_BARCODE} to {productInfo.identifier}");
}
storageMainRepository.UpdateStorageMain(checkBarcode);
Log.Information($"{nameof(updateProducts)}: Update tire ({checkBarcode.STOCK_BARCODE}) Information{JsonConvert.SerializeObject(checkBarcode)}");
countNumber--;
}
else
{
//未找到库存
Log.Information($"{nameof(updateProducts)}: tire ({productInfo.identifier}) have not found the storage Information ");
}
}
if (countNumber==0)
{
returnJson.status = "1";
returnJson.errorDetails = string.Empty;
}
else
{
returnJson.status = "2";
returnJson.errorDetails = "UpdatedPartially";
}
}
else
{
//无轮胎信息更新
Log.Information($"{nameof(updateProducts)}: Json didn't have Product parameters");
returnJson.status = "3";
returnJson.errorDetails = "RefusedBecauseEmptyProductParameter";
}
}
else
{
//json为空或者缺少必要参数
Log.Information($"{nameof(updateProducts)}: json is empty or missing necessary parameters");
returnJson.status = "3";
returnJson.errorDetails = "RefusedBecauseEmptyParameter";
}
return returnJson;
}
}
}