//using AutoMapper;
using Newtonsoft.Json;
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using Unity.VisualScripting;
using UnityEngine;
using UnityEngine.EventSystems;
using UnityEngine.UI;
using static GameManager;
///
/// 端口-新增
///
public class AddPort : MonoBehaviour
{
public Root URlreturn = new Root();
public Body mybody = new Body();
public Button save_bt;
public Image save_bt_img;
///
/// 端口
///
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;
///
/// 对联设备ID和名字关联字典
///
public Dictionary conDevice_dic = new Dictionary();
public string conDevice_dic_id;
public Button close_bt;
private void Awake()
{
save_bt_img = save_bt.GetComponent();
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()
{
//Debug.Log("版本号 1.1.1");
mybody = new Body();
conDevice_dic.Clear();
if (conCabinet.options.Count == 0)
{
addconCabinet();
conCabinet.captionText.text = null;
}
init();
findIndex(conCabinet, "");
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);
//}
tbinfo();
//Debug.Log("版本号 1.1.1");
}
public void tbinfo()
{
var p = GameManager.Inst.nowDevice.GetComponent();
if (p)
{
mybody.port = p.portList.port;
mybody.portCode = p.portList.portCode;
mybody.portName = p.portList.portName;
mybody.portType = p.portList.portType;
mybody.portPosition = p.portList.portPosition;
mybody.deviceId = p.portList.deviceId;
//mybody.deviceName = p.portList.deviceName;
mybody.portModel = p.portList.portModel;
mybody.status = p.portList.status;
mybody.conDevice = p.portList.conDevice;
mybody.conPort = p.portList.conPort;
// mybody.conDeviceName = p.portList.conDeviceName;
mybody.cableName = p.portList.cableName;
mybody.cableGroupName = p.portList.cableGroupName;
mybody.remark = p.portList.remark;
//mybody.createName = p.portList.createName;
//mybody.createTime = p.portList.createTime;
}
}
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);
}
Dropdown.OptionData optionData1 = new Dropdown.OptionData("");
cableGroupName.options.Add(optionData1);
//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);
List portType_list = new List();
foreach (var item in PatternChoose.Inst.xianlan.lineCode_dic.Keys)
{
portType_list.Add(item);
}
portType_list.Add("");
portType.options.Clear();
portType.AddOptions(portType_list);
portType.captionText.text = "";
}
private void Start()
{
//deviceId.onValueChanged.AddListener(deviceId_fuc);//所属设备
conDevice.onValueChanged.AddListener(conDevice_fuc);//对联设备
conCabinet.onValueChanged.AddListener(OnDropdownValueChanged_conCabinet);//对联机柜
save_bt.onClick.AddListener(() =>
{
GameManager.Inst.editorMenu.gameObject.SetActive(false);
StartCoroutine(SaveJsonCoroutine());
});
close_bt.onClick.AddListener(() =>
{
gameObject.SetActive(false);
});
}
public void OnDropdownValueChanged_conCabinet(int index)
{
List conDevice_list = new List();//对联设备
Dictionary conDevice_list_dict = new Dictionary();
if (index != conCabinet.options.Count - 1)
{
var _s = GameManager.Inst.Cabinets_go[index].GetComponentsInChildren();
Array.ForEach(_s, (item) =>
{
if (!conDevice_list_dict.ContainsKey(item.deviceList.id) && !string.IsNullOrEmpty(item.deviceList.id))
{
//conDevice_list_dict.Add(item.deviceList.id, "");
//conDevice_list.Add(item.deviceList.id);
conDevice_list_dict.Add(item.deviceList.id, item.deviceList.deviceName);
conDevice_list.Add(item.deviceList.deviceName);
}
});
conDevice_list.Add("");
conDevice.options.Clear();
conDevice.AddOptions(conDevice_list);
conDevice_dic.Clear();
conDevice_dic = conDevice_list_dict;
}
else
{
conDevice_dic.Clear();
conDevice.options.Clear();
conDevice.captionText.text = "";
}
}
///
/// 寻找下拉菜单选项
///
///
///
public string findIndex(Dropdown _dro, string _name)
{
int index = _dro.options.FindIndex(x => x.text == _name);
if (index != -1)
{
_dro.captionText.text = _dro.options[index].text;
_dro.value = index;
return index.ToString();
}
return null;
}
private IEnumerator SaveJsonCoroutine()
{
yield return StartCoroutine(saveJson((y) =>
{
if (string.IsNullOrEmpty(y))
{
SecondConfirmPanel.DeleteConform(null, "添加端口失败");
refurbish();
}
else if (y == "2")
{
Debug.Log("添加端口失败:该端口未配置所属设备,端口:" + mybody.port);
SecondConfirmPanel.DeleteConform(null, "该端口未配置所属设备");
refurbish();
}
else
{
if (URlreturn != null && URlreturn.message == "操作成功")
{
addInFo();
StartCoroutine(Succeed((x) =>
{
if (x)
{
GameManager.Inst.lastDeviceID = URlreturn.data;
refurbish();
}
else
{
SecondConfirmPanel.DeleteConform(null, "更新场景失败");
Debug.Log("接口获取数据失败(新增端口-初始化失败)");
}
}));
}
else
{
Debug.Log("添加端口失败:" + URlreturn.message);
SecondConfirmPanel.DeleteConform(null, "添加端口失败");
refurbish();
}
}
}));
}
///
/// 更新本地对象数据
///
public void addInFo()
{
}
///
/// 刷新
///
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);
else
callback.Invoke(false);
}, true));
}
private void conDevice_fuc(int arg0)
{
List> orderedList = conDevice_dic.ToList();
try
{
KeyValuePair element = orderedList.ElementAt(arg0);
conDevice_dic_id = element.Key;
}
catch (Exception)
{
conDevice_dic_id = null;
}
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 (string.IsNullOrEmpty(conDevice_dic_id))
{
if (n.deviceName == conDevice.options[arg0].text && !string.IsNullOrEmpty(n.port))
//if (n.deviceId == conDevice.options[arg0].text)
{
conPort_ops.Add(n.port);
}
}
else
{
if (n.deviceId == conDevice_dic_id && !string.IsNullOrEmpty(n.port))
{
conPort_ops.Add(n.port);
}
}
}
conPort.options.Clear();
conPort_ops.Add("");
conPort.AddOptions(conPort_ops);
}
private void deviceId_fuc(int arg0)
{
//deviceId.options[arg0].text//所属设备ID
}
private void Update()
{
if ((cableGroupName.transform.Find("Dropdown List") && cableGroupName.transform.Find("Dropdown List").gameObject.activeSelf) ||
(portType.transform.Find("Dropdown List") && portType.transform.Find("Dropdown List").gameObject.activeSelf))
{
// 下拉菜单处于展开状态。
//Color color = save_bt_img.color;
//color.a = 0;
//save_bt_img.color = color;
save_bt.gameObject.SetActive(false);
}
else
{
// 下拉菜单未展开。
//Color color = save_bt_img.color;
//color.a = 1;
//save_bt_img.color = color;
save_bt.gameObject.SetActive(true);
}
}
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;
var dp = GameManager.Inst.FindParent(GameManager.Inst.nowDevice, GameManager.Inst.IsFindRacks_goORMachineSlots_goORTmsCards_go);
if (dp)
{
var dp_d = dp.GetComponent();
mybody.deviceId = string.IsNullOrEmpty(dp_d.deviceList.id) ? null : dp_d.deviceList.id;
}
else
mybody.deviceId = null;
//mybody.deviceId = deviceId.options[deviceId.value].text;
//mybody.portModel = portModel;
//mybody.status = status.value.ToString();
//mybody.conDevice = conDevice.captionText.text;
//mybody.conDevice = conDevice_dic_id;
//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;
///
/// 所属设备(ID)
///
public string deviceId;
///
/// 型号
///
public string portModel;
///
/// 状态:1-正常;0-禁用
///
public string status;
///
/// 对联设备(ID)
///
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
}