using Masuit.Tools;
using Newtonsoft.Json;
using SqlSugar.Extensions;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using WIDESEA_Common;
using WIDESEA_Core;
using WIDESEA_Core.Const;
using WIDESEA_Core.Enums;
using WIDESEA_Core.Helper;
using WIDESEA_DTO.WMS;
using WIDESEA_Model.Models;
using WIDESEA_StorageBasicRepository;
using WIDESEA_StorageOutOrderRepository;
namespace WIDESEA_StoragIntegrationServices
{
///
/// 总装工单信息
///
public partial class MESService
{
public WebResponseContent pushOrderInfo(object json)
{
WebResponseContent responseContent = new WebResponseContent();
try
{
if (string.IsNullOrEmpty(json.ToString())) throw new Exception("请求参数为空");
var result = JsonConvert.DeserializeObject(json.ToString());
if (result.plantCode != "1052") throw new Exception("非本工厂订单,请重试");
var ListTaskDto = new List();
var ListTask = new List();
var ListAssemblyOrderInfo = new List();
foreach (var item in result.data)
{
var palletStockInfos = _palletStockInfoRepository.QueryData(x => x.OrderID == item.workOrderNo);
//如存在预绑定工单的库存,则优先出库预绑定的工单
if (palletStockInfos != null || palletStockInfos.Count != 0)
{
foreach (var item1 in palletStockInfos)
{
Dt_Task task = new Dt_Task()
{
CurrentAddress = item1.LocationCode,
Grade = 2,
NextAddress = item1.LocationCode,
PalletCode = item1.PalletCode,
OrderNo = item1.OrderID,
PVI = item1.PVI,
Modifier = item1.Modifier,
Roadway = item1.RoadwayNo,
SourceAddress = item1.LocationCode,
TaskNum = _taskRepository.GetTaskNo().Result,
TaskState = (int)TaskOutStatusEnum.OutNew,
TaskType = (int)TaskOutboundTypeEnum.Outbound,
TargetAddress = "",
Creater = "System",
CreateDate = DateTime.Now
};
ListTask.Add(task);
WMSTaskDTO taskDTO = new WMSTaskDTO()
{
Grade = task.Grade.Value,
PalletCode = task.PalletCode,
SourceAddress = task.SourceAddress,
TargetAddress = task.TargetAddress,
RoadWay = task.Roadway,
TaskState = task.TaskState.Value,
TaskType = task.TaskType,
TaskNum = task.TaskNum.Value
};
ListTaskDto.Add(taskDTO);
}
}
else
{
//如不存在预绑定工单车辆,则按照工单信息匹配库存车辆
//todo 特征信息匹配
_palletStockInfoRepository.QueryData(x => x.PVI == item.pvi);
}
Dt_AssemblyOrderInfo assemblyOrderInfo = new Dt_AssemblyOrderInfo
{
biwMaterial = item.biwMaterial,
CreateDate = DateTime.Now,
Creater = "System",
orderType = item.orderType,
pbMaterial = item.pbMaterial,
plantCode = result.plantCode,
pvi = item.pvi,
sequenceNo = item.sequenceNo,
spare1 = item.spare1,
spare2 = item.spare2,
spare3 = item.spare3,
splitStrategy = result.splitStrategy,
vehicleCharacteristic = item.vehicleCharacteristic,
vehicleCode = item.vehicleCode,
vehicleOrderNo = item.vehicleOrderNo,
workOrderNo = item.workOrderNo,
workOrderSendStatus = item.workOrderSendStatus, //1-排产 2-撤回
};
ListAssemblyOrderInfo.Add(assemblyOrderInfo);
}
_unitOfWorkManage.BeginTran();
if (ListTaskDto.Count > 0)
{
var configs = _configService.GetConfigsByCategory(CateGoryConst.CONFIG_SYS_IPAddress);
var wmsBase = configs.FirstOrDefault(x => x.ConfigKey == SysConfigConst.WCSIPAddress)?.ConfigValue;
var ipAddress = configs.FirstOrDefault(x => x.ConfigKey == SysConfigConst.ReceiveTask)?.ConfigValue;
if (wmsBase == null || ipAddress == null)
{
throw new InvalidOperationException("WMS IP 未配置");
}
var wmsIpAddress = wmsBase + ipAddress;
var WCSresult = HttpHelper.PostAsync(wmsIpAddress, ListTaskDto.ToJsonString()).Result;
var content = JsonConvert.DeserializeObject(WCSresult);
_taskRepository.AddData(ListTask);
}
_assemblyOrderInfoRepository.AddData(ListAssemblyOrderInfo);
_unitOfWorkManage.CommitTran();
responseContent.OK();
}
catch (Exception ex)
{
_unitOfWorkManage.RollbackTran();
return responseContent.Error(ex.Message);
}
return responseContent;
}
}
}