AK056/Assets/script/application/InitStorageProcess.cs
2025-05-07 11:20:40 +08:00

118 lines
3.9 KiB
C#

using BestHTTP;
using EasyInject.Attributes;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Text;
using Unity.VisualScripting;
using UnityEngine;
using static UnityEngine.EventSystems.EventTrigger;
/// <summary>
/// 初始化库存流程
/// </summary>
[Component]
public class InitStorageProcess : IProcessState
{
[Autowired]
private readonly NetWorkComponent netWorkComponent;
[Autowired]
private readonly LocationStorageManage storageManage;
[Autowired]
private readonly ContainersManage containerManage;
public string Name => "InitStorageProcess";
public int Priority => 2;
private bool isExecuting;
public bool IsExecuteProcess { get => isExecuting; set => isExecuting = value; }
public void OnEnter()
{
IsExecuteProcess = true;
Debug.Log("进入库存初始化流程");
///平库
SendRequset(33, 50);
SendRequset(1, 32);
}
private void SendRequset(int startRow, int endRow)
{
List<string> ints = new List<string>();
for (int i = startRow; i <= endRow; i++)
{
ints.Add(i.ToString("D2"));
}
netWorkComponent.RemoteGetStorageDataByRows(ints, RsultHandle);
}
public void RsultHandle(HTTPRequest originalRequest, HTTPResponse response)
{
if (response == null)
{
//请求错误重试
if(originalRequest.State == HTTPRequestStates.Error)
{
Debug.LogWarning("异常重试");
originalRequest.Send();
}
return;
}
if (response.IsSuccess)
{
Debug.LogWarning("处理");
JObject keyValues = JObject.Parse(UTF8Encoding.UTF8.GetString(response.Data));
JToken containers = keyValues["object"]["containers"];
List<StorageResponseData> storageResponses = JsonConvert.DeserializeObject<List<StorageResponseData>>(containers.ToString());
if (storageResponses != null)
{
foreach (StorageResponseData responseData in storageResponses)
{
//无货的不往下处理了
if (string.IsNullOrEmpty(responseData.containerCode))
{
continue;
}
//库位号
string storageName = responseData.locationCode;
GameObject storageObj = storageManage.GetStorageByName(storageName);
if (storageObj != null)
{
//赋值容器数据
ContainerEntity containerEntity = new();
containerEntity.channelType = responseData.channelType;
containerEntity.location = responseData.locationCode;
containerEntity.containerCode = responseData.containerCode;
containerEntity.procedureCode = responseData.procedureCode;
containerEntity.containerType = responseData.containerType;
containerEntity.isEmpty = responseData.isEmpty;
containerEntity.isTool = responseData.isTool;
int row = int.Parse(storageObj.name.Split("-")[0]);
if (row >= storageManage.startRow && row <= storageManage.endRow)
{
containerManage.CreateContainer(new Vector3(0, -0.2472343f, 0.0006999969f), storageObj.transform, containerEntity, storageObj.layer);
}
else
{
containerManage.CreateContainer(Vector3.zero, storageObj.transform, containerEntity, storageObj.layer);
}
}
}
}
}
}
public void OnExit()
{
}
public void OnUpdate()
{
}
}