using Newtonsoft.Json; using System; using System.Collections; using System.Collections.Generic; using UnityEngine; public class LineQuery : MonoBehaviour { public static LineQuery Inst; [System.Serializable] public struct Dic { public string lineContentJson; public string id; } public List dic;//创建结构体对应的数组 /// /// key:lineContentJson Value:id /// public Dictionary keyValues = new Dictionary(); private void Awake() { if (Inst != this && Inst != null) { Destroy(this.gameObject); } else { Inst = this; DontDestroyOnLoad(this.gameObject); } } void Start() { //StartCoroutine(getJsonCoroutine()); } void Update() { } /// /// 查询线缆组 /// public IEnumerator getJsonCoroutine(Action> callback = null) { Root root = new Root(); yield return StartCoroutine(getJson(root, (x) => { root = x; })); if (root != null && root.message == "操作成功") { if (root.data != null && root.data.Count > 0) { for (int i = 0; i < root.data.Count; i++) { if (!keyValues.ContainsKey(root.data[i].lineContentJson)) { keyValues.Add(root.data[i].lineContentJson, root.data[i].id); var d = new Dic(); d.id = root.data[i].id; d.lineContentJson = root.data[i].lineContentJson; dic.Add(d); LineGroupManager.Instance.AddLineGroup(d.lineContentJson); } } callback?.Invoke(keyValues); } else { keyValues.Clear(); } } else { } } /// /// 删除线缆组 /// public IEnumerator deleteJsonCoroutine(string id,Action callback = null) { Root root = new Root(); yield return StartCoroutine(deleteJson(root, id, (x) => { root = x; })); if (root != null && root.message == "操作成功") { yield return StartCoroutine(getJsonCoroutine((x) => { foreach (var item in keyValues.Keys) { if (keyValues[item] == id) { keyValues.Remove(item); for (int i = 0; i < dic.Count; i++) { if (dic[i].id == id) dic.Remove(dic[i]); } break; } } callback?.Invoke("1"); })); } else { } } /// /// 新增线缆组 /// public IEnumerator SaveJsonCoroutine(string lineContentJson) { Root root = new Root(); yield return StartCoroutine(saveJson(root, lineContentJson, (x) => { root = x; })); if (root != null && root.message == "操作成功") { yield return StartCoroutine(getJsonCoroutine((x) => { foreach (var item in x) { LineGroupManager.Instance.AddLineGroup(item.Key); } })); } else { } } /// /// 新增 /// public IEnumerator saveJson(Root root, string lineContentJson, Action callback) { var C = new Root_lineContentJson(); C.lineContentJson = lineContentJson; string newData = JsonConvert.SerializeObject(C); yield return StartCoroutine( CombineJSON.UpdateJson_POST(GameManager.Inst.Jk_URL.xlz_xz, GameManager.Inst.arguments.token, newData, (jsonResult) => { try { root = JsonConvert.DeserializeObject(jsonResult); callback?.Invoke(root); } catch (Exception e) { Debug.Log("新增线缆组接口错误:" + e.Message); callback?.Invoke(null); } }) ); } /// /// 查询 /// public IEnumerator getJson(Root root, Action callback) { yield return StartCoroutine( CombineJSON.GetJson_POST(GameManager.Inst.Jk_URL.xlz_cx, GameManager.Inst.arguments.token, (jsonResult) => { try { root = JsonConvert.DeserializeObject(jsonResult); callback?.Invoke(root); } catch (Exception e) { Debug.Log("查询线缆组接口错误:" + e.Message); callback?.Invoke(null); } }) ); } /// /// 删除 /// /// /// public IEnumerator deleteJson(Root root, string id, Action callback) { var C = new Root_id(); C.id = id; string newData = JsonConvert.SerializeObject(C); yield return StartCoroutine( CombineJSON.UpdateJson_POST(GameManager.Inst.Jk_URL.xlz_sc, GameManager.Inst.arguments.token, newData, (jsonResult) => { try { root = JsonConvert.DeserializeObject(jsonResult); callback?.Invoke(root); } catch (Exception e) { Debug.Log("删除线缆组接口错误:" + e.Message); callback?.Invoke(null); } }) ); } // 删除线缆 public IEnumerable deleteCableName(string cableGroupName, string CableName, Action callback = null) { var xianlan = GameObject.Find("线缆").GetComponentsInChildren(); var info = Array.Find(xianlan, (item) => { return (item.cableGroupName == cableGroupName && item.cableName == CableName); }); RedactPort redact = PatternChoose.Inst.transform.Find("端口类/端口配置").GetComponent(); redact.syncInfo(info.lines[0].GetComponent().portList.id); redact.mybody.conDevice = null; redact.mybody.conPort = null; redact.mybody.conDeviceName = null; redact.mybody.cableGroupName = null; redact.mybody.cableName = null; string newData = JsonConvert.SerializeObject(redact.mybody); yield return StartCoroutine( CombineJSON.UpdateJson_POST(GameManager.Inst.Jk_URL.dk_bj, GameManager.Inst.arguments.token, newData, (jsonResult) => { try { redact.URlreturn = JsonConvert.DeserializeObject(jsonResult); } catch (Exception e) { Debug.Log("删除端口线缆名接口错误:" + e.Message); } if (redact.URlreturn != null && redact.URlreturn.message == "操作成功") { StartCoroutine(redact.Succeed()); Array.ForEach(GameManager.Inst.pop_ups.ToArray(), (itme) => { itme.gameObject.SetActive(false); }); } } )); } #region JSON [System.Serializable] public class LineList { /// /// /// public string id; /// /// /// public string lineContentJson; /// /// /// public string createTime; } [System.Serializable] public class Root { /// /// /// public string code; /// /// 反馈结果 /// public string message; /// /// /// public List data; /// /// /// public string serverTime; } public class Root_lineContentJson { /// /// /// public string lineContentJson; } public class Root_id { /// /// /// public string id; } #endregion }