using AutoMapper;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.UI;
using static GameManager;
///
/// 端口-新增
///
public class AddPort : MonoBehaviour
{
public Root URlreturn;
public Body mybody = new Body();
public Button save_bt;
///
/// 端口
///
public InputField port;
///
/// 编号
///
public InputField portCode;
///
/// 名称
///
public InputField portName;
public Dropdown portType;
//public Dropdown deviceId;
//public InputField portModel;
///
/// 状态
///
//public Dropdown status;
///
/// 对联机柜
///
public Dropdown conCabinet;
///
/// 对联设备
///
public Dropdown conDevice;
///
/// 对联端口
///
public Dropdown conPort;
///
/// 线缆名
///
public InputField cableName;
///
/// 线缆组名
///
public Dropdown cableGroupName;
//MapperConfiguration config;
//IMapper mapper;
PortQuery.PortList portlist;
private void Awake()
{
addconCabinet();
conCabinet.captionText.text = null;
gameObject.SetActive(false);
}
public void addconCabinet()
{
conCabinet.options.Clear();
List conCabinetname = new List();
for (int i = 0; i < GameManager.Inst.Cabinets_go.Count; i++)
conCabinetname.Add(GameManager.Inst.Cabinets_go[i].name);
conCabinetname.Add("");
conCabinet.AddOptions(conCabinetname);
}
private void OnEnable()
{
if (conCabinet.options.Count == 0)
{
addconCabinet();
conCabinet.captionText.text = null;
}
init();
port.text = null;
portCode.text = null;
portName.text = null;
portType.captionText.text = null;
conCabinet.captionText.text = null;
conDevice.captionText.text = null;
conPort.captionText.text = null;
cableName.text = null;
cableGroupName.captionText.text = null;
//config = new MapperConfiguration(cfg =>
//{
// cfg.CreateMap();
// cfg.CreateMap();
//});
//mapper = config.CreateMapper();
//try
//{
// if (!string.IsNullOrEmpty(GameManager.Inst.nowDeviceID))
// {
// for (int i = 0; i < GameManager.Inst.TmsPorts.Count; i++)
// {
// if (GameManager.Inst.TmsPorts[i].id == GameManager.Inst.nowDeviceID)
// {
// portlist = GameManager.Inst.TmsPorts[i];
// break;
// }
// }
// if (portlist != null)
// {
// // mybody = mapper.Map(portlist);
// }
// }
// //else
// //{
// // mybody = mapper.Map(GameManager.Inst.nowDevice.GetComponent().portList);
// //}
// mybody.deviceId = transform.parent.GetComponent().deviceList.id;
//}
//catch (Exception e)
//{
// Debug.Log(e.StackTrace + "**********" + e.Message);
//}
}
private void init()
{
conDevice.options.Clear();
conPort.options.Clear();
cableGroupName.options.Clear();
foreach (var item in LineQuery.Inst.keyValues.Keys)
{
Dropdown.OptionData optionData = new Dropdown.OptionData(item);
cableGroupName.options.Add(optionData);
}
//deviceId.options.Clear();
List deviceId_ops = new List();
deviceId_ops.Add("");
var list = Array.FindAll(GameObject.FindObjectsOfType(), itme =>
{
var n = itme.transform.parent.GetComponent().deviceList;
if (!string.IsNullOrEmpty(itme.portList.id) && n != null)
return true;
return false;
}).ToList();
Dictionary dic = new Dictionary();
for (int i = 0; i < list.Count; i++)
{
string s = list[i].transform.parent.GetComponent().deviceList.id;
if (!dic.ContainsKey(s))
{
dic.Add(s, null);
deviceId_ops.Add(s);
}
}
//deviceId.AddOptions(deviceId_ops);
conDevice.AddOptions(deviceId_ops);
}
private void Start()
{
//deviceId.onValueChanged.AddListener(deviceId_fuc);//所属设备
conDevice.onValueChanged.AddListener(conDevice_fuc);//对联设备
save_bt.onClick.AddListener(() =>
{
GameManager.Inst.editorMenu.gameObject.SetActive(false);
StartCoroutine(SaveJsonCoroutine());
});
}
private IEnumerator SaveJsonCoroutine()
{
yield return StartCoroutine(saveJson((y) =>
{
if (string.IsNullOrEmpty(y))
{
SecondConfirmPanel.DeleteConform(null, "添加端口失败");
refurbish();
}
else if (y == "2")
{
SecondConfirmPanel.DeleteConform(null, "该端口未配置所属设备");
refurbish();
}
else
{
if (URlreturn != null && URlreturn.message == "操作成功")
{
StartCoroutine(Succeed((x) =>
{
if (x)
{
GameManager.Inst.lastDeviceID = URlreturn.data;
refurbish();
}
}));
}
else
{
SecondConfirmPanel.DeleteConform(null, "添加端口失败");
refurbish();
}
}
}));
}
///
/// 刷新
///
public void refurbish()
{
Array.ForEach(GameManager.Inst.pop_ups.ToArray(), (itme) =>
{
itme.gameObject.SetActive(false);
});
}
public IEnumerator Succeed(Action callback)
{
GameManager.Inst.isLoading = false;
//TransparentGlowManage.Inst.addScript();
//yield return StartCoroutine(GameManager.Inst.init_Device());
//yield return StartCoroutine(GameManager.Inst.init_Port());
//yield return StartCoroutine(LineQuery.Inst.getJsonCoroutine());
//GameManager.Inst.LoadScene();
yield return null;
StartCoroutine(GameManager.Inst.Initialize((x) =>
{
if (!string.IsNullOrEmpty(x))
callback.Invoke(true);
}));
}
private void conDevice_fuc(int arg0)
{
List conPort_ops = new List();
//对联设备下所有端口
//conDevice.options[arg0].text//对联设备id
//Array.FindAll(FindObjectsOfType(), itme =>
//{
// if (itme.portList.deviceId== conDevice.options[arg0].text)
// return true;
// return false;
//}).ToList();
for (int i = 0; i < GameManager.Inst.TmsPorts_go.Count; i++)
{
var n = GameManager.Inst.TmsPorts_go[i].GetComponent().portList;
if (n.deviceId == conDevice.options[arg0].text)
conPort_ops.Add(n.port);
}
conPort.options.Clear();
conPort.AddOptions(conPort_ops);
}
private void deviceId_fuc(int arg0)
{
//deviceId.options[arg0].text//所属设备ID
}
private void Update()
{
}
public IEnumerator saveJson(Action callback = null)
{
if (string.IsNullOrEmpty(mybody.deviceId))
{
callback?.Invoke("2");
yield break;
}
try
{
mybody.port = port.text;
mybody.portCode = portCode.text;
mybody.portName = portName.text;
mybody.portType = portType.captionText.text;
//mybody.deviceId = deviceId.options[deviceId.value].text;
//mybody.portModel = portModel;
//mybody.status = status.value.ToString();
mybody.conDevice = conDevice.captionText.text;
mybody.conPort = conPort.captionText.text;
mybody.cableName = cableName.text;
if (!string.IsNullOrEmpty(conDevice.captionText.text) && !string.IsNullOrEmpty(conPort.captionText.text))
mybody.cableGroupName = cableGroupName.captionText.text;
}
catch (Exception e)
{
Debug.Log("新增端口错误:" + e.Message);
gameObject.SetActive(false);
callback.Invoke(null);
yield break;
}
var newData = JsonConvert.SerializeObject(mybody);
//var jsonResult = await CombineJSON.UpdateJson_POST(GameManager.Inst.Jk_URL.dk_xz, GameManager.Inst.token, newData);
//URlreturn = JsonConvert.DeserializeObject(jsonResult);
yield return StartCoroutine(
CombineJSON.UpdateJson_POST(GameManager.Inst.Jk_URL.dk_xz, GameManager.Inst.arguments.token, newData, (jsonResult) =>
{
try
{
URlreturn = JsonConvert.DeserializeObject(jsonResult);
callback?.Invoke("1");
}
catch (Exception e)
{
Debug.Log("新增端口接口错误:" + e.Message);
callback.Invoke(null);
}
})
);
}
#region JSON
[System.Serializable]
public class Body
{
///
/// 端口
///
public string port;
///
/// 编号
///
public string portCode;
///
/// 名称
///
public string portName;
///
/// 类型
///
public string portType;
///
/// 端口位置
///
public string portPosition;
///
/// 所属设备
///
public string deviceId;
///
/// 型号
///
public string portModel;
///
/// 状态:1-正常;0-禁用
///
public string status;
///
/// 对联设备
///
public string conDevice;
///
/// 对联端口
///
public string conPort;
///
/// 线缆名
///
[Tooltip("线缆名")] public string cableName;
///
/// 线缆组名
///
[Tooltip("线缆组名")] public string cableGroupName;
///
/// 备注
///
public string remark;
}
[System.Serializable]
public class Root
{
///
///
///
public string code;
///
/// 反馈结果
///
public string message;
///
/// 返回内容(ID)
///
public string data;
///
///
///
public string serverTime;
}
#endregion
}