using AutoMapper;
using Kean.Domain.Stock.Commands;
using Kean.Domain.Stock.Models;
using Kean.Domain.Stock.Repositories;
using Kean.Domain.Stock.SharedServices.Proxies;
using Kean.Infrastructure.Soap.Hithium.Models;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
namespace Kean.Domain.Stock.CommandHandlers
{
///
/// 命令处理程序
///
public sealed class ChangeStoragePropertyCommandHandler : CommandHandler
{
private readonly ICommandBus _commandBus; // 命令总线
private readonly IStockRepository _stockRepository; // 存储仓库
private readonly INotification _notifications; // 总线通知
private readonly IRecordRepository _recordRepository; // 记录仓库
///
/// 依赖注入
///
public ChangeStoragePropertyCommandHandler(
ICommandBus commandBus,
IStockRepository stockRepository,
IRecordRepository recordRepository,
INotification notifications)
{
_commandBus = commandBus;
_stockRepository = stockRepository;
_notifications = notifications;
_recordRepository = recordRepository;
}
///
/// 处理程序
///
public override async Task Handle(ChangeStoragePropertyCommand command, CancellationToken cancellationToken)
{
if (command.ValidationResult.IsValid)
{
var stock = await this._stockRepository.GetStorageBill(command.Serialno);
if (stock == null)
{
await _commandBus.Notify(nameof(command.Serialno), $"膜卷码({command.Serialno})未找到库存", command.Serialno, cancellationToken: cancellationToken);
return;
}
StockLine updateLine = stock.Lines.Where(r => r.Bill == command.Serialno).First();
if (!string.IsNullOrEmpty(command.FreezenStatus) && command.FreezenStatus == "Y")
{
updateLine.Enabled = false;
}
if (!string.IsNullOrEmpty(command.FreezenStatus) && command.FreezenStatus == "N")
{
updateLine.Enabled = true;
}
if (!string.IsNullOrEmpty(command.Batch.unit))
{
updateLine.Unit = command.Batch.unit;
}
if (!string.IsNullOrEmpty(command.Batch.quantity))
{
updateLine.QualifiedNum = command.Batch.quantity;
}
if (!string.IsNullOrEmpty(command.Batch.workOrderNo))
{
updateLine.WorkorderNo = command.Batch.workOrderNo;
}
if (!string.IsNullOrEmpty(command.Batch.productDate))
{
updateLine.ManufacturingDate = Convert.ToDateTime(command.Batch.productDate);
}
if (!string.IsNullOrEmpty(command.Batch.qualityState))
{
updateLine.IsAgv = command.Batch.qualityState;
}
if (!string.IsNullOrEmpty(command.Batch.materialType))
{
updateLine.QualityState = command.Batch.materialType;
}
if (!string.IsNullOrEmpty(command.Batch.productBatchNo))
{
updateLine.LineCode = command.Batch.productBatchNo;
}
if (!string.IsNullOrEmpty(command.Batch.sapBatchNo))
{
updateLine.LineName = command.Batch.sapBatchNo;
}
if (!string.IsNullOrEmpty(updateLine.UploadNum))
{
string mouldNo = string.Empty;
string maturity = string.Empty;
string expDate = string.Empty;
JObject jObj = JObject.Parse(updateLine.UploadNum);
JToken jMouldNo = jObj.SelectToken("mouldNo");
if (jMouldNo != null)
{
mouldNo = jObj["mouldNo"].ToString();
}
JToken jMaturity = jObj.SelectToken("maturity");
if (jMaturity != null)
{
maturity = jObj["maturity"].ToString();
}
JToken jExpDate = jObj.SelectToken("expDate");
if (jExpDate != null)
{
expDate = jObj["expDate"].ToString();
}
if (string.IsNullOrEmpty(command.Batch.mouldNo))
{
mouldNo = command.Batch.mouldNo;
}
if (string.IsNullOrEmpty(command.Batch.maturity))
{
maturity = command.Batch.maturity;
}
if (string.IsNullOrEmpty(command.Batch.expDate))
{
expDate = command.Batch.expDate;
}
updateLine.UploadNum = JsonConvert.SerializeObject(new
{
mouldNo = mouldNo,
maturity = maturity,
expDate = expDate
});
}
await _stockRepository.UpdateStorageProperty(updateLine);
Record record = new Record();
record.Transaction = "Update";
record.Barcode = stock.Barcode;
record.Original = stock.Cell;
record.Destination = stock.Cell;
record.BeginTime = DateTime.Now;
record.EndTime = DateTime.Now;
record.Operator = -4;
record.Tag = JsonConvert.SerializeObject(new
{
Transaction = "ChangeStorageProperty"
});
record.Lines = new List() { updateLine };
record.RequestNo = command.RequestNo;
await _recordRepository.Append(record);
}
else
{
await _commandBus.Notify(command.ValidationResult,
cancellationToken: cancellationToken);
}
}
}
}