GQ_Communicate/GQ_TongXin/Assets/script/接口/查询/LineQuery.cs

346 lines
8.9 KiB
C#

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> dic;//创建结构体对应的数组
/// <summary>
/// key:lineContentJson Value:id
/// </summary>
public Dictionary<string, string> keyValues = new Dictionary<string, string>();
private void Awake()
{
if (Inst != this && Inst != null)
{
Destroy(this.gameObject);
}
else
{
Inst = this;
DontDestroyOnLoad(this.gameObject);
}
}
void Start()
{
//StartCoroutine(getJsonCoroutine());
}
void Update()
{
}
/// <summary>
/// 查询线缆组
/// </summary>
public IEnumerator getJsonCoroutine(Action<Dictionary<string, string>> 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
{
}
}
/// <summary>
/// 删除线缆组
/// </summary>
public IEnumerator deleteJsonCoroutine(string id,Action<string> 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
{
}
}
/// <summary>
/// 新增线缆组
/// </summary>
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
{
}
}
/// <summary>
/// 新增
/// </summary>
public IEnumerator saveJson(Root root, string lineContentJson, Action<Root> 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<Root>(jsonResult);
callback?.Invoke(root);
}
catch (Exception e)
{
Debug.Log("新增线缆组接口错误:" + e.Message);
callback?.Invoke(null);
}
})
);
}
/// <summary>
/// 查询
/// </summary>
public IEnumerator getJson(Root root, Action<Root> callback)
{
yield return
StartCoroutine(
CombineJSON.GetJson_POST(GameManager.Inst.Jk_URL.xlz_cx, GameManager.Inst.arguments.token, (jsonResult) =>
{
try
{
root = JsonConvert.DeserializeObject<Root>(jsonResult);
callback?.Invoke(root);
}
catch (Exception e)
{
Debug.Log("查询线缆组接口错误:" + e.Message);
callback?.Invoke(null);
}
})
);
}
/// <summary>
/// 删除
/// </summary>
/// <param name="root"></param>
/// <returns></returns>
public IEnumerator deleteJson(Root root, string id, Action<Root> 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<Root>(jsonResult);
callback?.Invoke(root);
}
catch (Exception e)
{
Debug.Log("删除线缆组接口错误:" + e.Message);
callback?.Invoke(null);
}
})
);
}
// 删除线缆
public IEnumerable deleteCableName(string cableGroupName, string CableName, Action<string> callback = null)
{
var xianlan = GameObject.Find("线缆").GetComponentsInChildren<LineInfor>();
var info = Array.Find(xianlan, (item) =>
{
return (item.cableGroupName == cableGroupName && item.cableName == CableName);
});
RedactPort redact = PatternChoose.Inst.transform.Find("端口类/端口配置").GetComponent<RedactPort>();
redact.syncInfo(info.lines[0].GetComponent<PortQuery>().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<RedactPort.Root>(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
{
/// <summary>
///
/// </summary>
public string id;
/// <summary>
///
/// </summary>
public string lineContentJson;
/// <summary>
///
/// </summary>
public string createTime;
}
[System.Serializable]
public class Root
{
/// <summary>
///
/// </summary>
public string code;
/// <summary>
/// 反馈结果
/// </summary>
public string message;
/// <summary>
///
/// </summary>
public List<LineList> data;
/// <summary>
///
/// </summary>
public string serverTime;
}
public class Root_lineContentJson
{
/// <summary>
///
/// </summary>
public string lineContentJson;
}
public class Root_id
{
/// <summary>
///
/// </summary>
public string id;
}
#endregion
}