GQ_Communicate/GQ_URP/GQ/Assets/script/接口/查询/LineQuery.cs

605 lines
18 KiB
C#

using Newtonsoft.Json;
using SimpleJSON;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
/// <summary>
/// 线缆组
/// </summary>
[AddComponentMenu("LineQuery/线缆组")]
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)
public IEnumerator getJsonCoroutine(Action<bool, string> callback = null)
{
Root root = new Root();
yield return StartCoroutine(getJson(root, (x) =>
{
root = x;
if (root != null && root.message == "操作成功")
{
keyValues.Clear();
dic.Clear();
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, root.data[i].id);
}
}
callback?.Invoke(true, "1");
}
else
{
keyValues.Clear();
dic.Clear();
callback?.Invoke(false, "1");
}
}
else
{
Debug.Log("查询线缆组失败: " + root != null ? root.message : "查询线缆组失败");
SecondConfirmPanel.DeleteConform(null, root != null ? root.message : "查询线缆组失败");
callback.Invoke(false, root != null ? root.message : "查询线缆组失败");
}
}));
}
/// <summary>
/// 删除线缆组
/// </summary>
public IEnumerator deleteJsonCoroutine(string id, Action<string> callback)
{
Root root = new Root();
yield return StartCoroutine(deleteJson(root, id, (x) =>
{
root = x;
if (root != null && root.message == "操作成功")
{
//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
{
Debug.Log("删除线缆组失败: " + root != null ? root.message : "删除线缆组失败");
SecondConfirmPanel.DeleteConform(null, root != null ? root.message : "删除线缆组失败");
callback.Invoke(null);
}
}));
}
/// <summary>
/// 新增线缆组
/// </summary>
public IEnumerator SaveJsonCoroutine(string lineContentJson, Action<bool> callback)
{
Root_add root = new Root_add();
yield return StartCoroutine(saveJson(root, lineContentJson, (x) =>
{
root = x;
if (x != null && root != null && root.message == "操作成功")
{
LineGroupManager.Instance.AddLineGroup(lineContentJson, root.data);
var d = new Dic();
d.id = root.data;
d.lineContentJson = lineContentJson;
dic.Add(d);
keyValues.Add(lineContentJson, root.data);
callback.Invoke(true);
}
else
{
Debug.Log("新增线缆组失败: " + root != null ? root.message : "null");
SecondConfirmPanel.DeleteConform(null, "新增线缆组失败: " + root != null ? root.message : "null");
//LineGroupManager.Instance.AddLineGroup("", false);
callback.Invoke(false);
}
}));
}
/// <summary>
/// 新增
/// </summary>
public IEnumerator saveJson(Root_add root, string lineContentJson, Action<Root_add> 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_add>(jsonResult);
callback?.Invoke(root);
}
catch (Exception e)
{
Debug.LogError("新增线缆组接口错误:" + e.Message + e.StackTrace);
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>
public void addxianlan_未分组()
{
PortQuery.Root root = new PortQuery.Root();
StartCoroutine(LineQuery.Inst.getJson_未分组(root, (x) =>
{
root = x;
if (root != null && root.message == "操作成功")
{
LineGroupManager.Instance.showRightPop_未分组(root);
if (root.data != null && root.data.Count > 0)
{
for (int i = 0; i < root.data.Count; i++)
{
Debug.Log(root.data[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, root.data[i].id);
}
*/
}
//callback?.Invoke(true, "1");
}
else
{
// keyValues.Clear();
// dic.Clear();
// callback?.Invoke(false, "1");
}
}
else
{
Debug.Log("查询线缆组失败: " + root != null ? root.message : "查询线缆组失败");
}
}));
}
/// <summary>
/// 查询未分组
/// </summary>
public IEnumerator getJson_未分组(PortQuery.Root root, Action<PortQuery.Root> callback)
{
yield return
StartCoroutine
(
CombineJSON.GetJson_POST(GameManager.Inst.Jk_URL.xl_wfz, GameManager.Inst.arguments.token, (jsonResult) =>
{
try
{
root = JsonConvert.DeserializeObject<PortQuery.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);
}
})
);
}
/// <summary>
/// 编辑线缆组
/// </summary>
/// <param name="_id"></param>
/// <param name="_lineContentJson"></param>
/// <param name="callback"></param>
public void editJsonCoroutine(string _id, string _lineContentJson, Action<bool, Root> callback)
{
Root_edit root_Edit = new Root_edit
{
id = _id,
lineContentJson = _lineContentJson
};
string newData = JsonConvert.SerializeObject(root_Edit);
StartCoroutine(CombineJSON.UpdateJson_POST(GameManager.Inst.Jk_URL.xlz_bj, GameManager.Inst.arguments.token, newData, (jsonResult) =>
{
try
{
var root = JsonConvert.DeserializeObject<Root>(jsonResult);
if (root.message == "操作成功")
{
callback.Invoke(true, root);
}
else
{
callback.Invoke(false, root);
}
}
catch
{
callback.Invoke(false, null);
}
}));
}
/// <summary>
/// 删除线缆
/// </summary>
/// <param name="cableGroupName"></param>
/// <param name="CableName"></param>
/// <param name="callback"></param>
public void deleteCableName(string cableGroupName, string CableName, Action<string> callback)
{
var xianlan = PatternChoose.Inst.xianlan.gameObject.GetComponentsInChildren<LineInfor>(true);
var info = Array.Find(xianlan, (item) =>
{
return (item.cableGroupName == cableGroupName && item.cableName == CableName);
});
RedactPort redact = PatternChoose.Inst.dk_bj_page.GetComponent<RedactPort>();
GameManager.Inst.nowDeviceID = info.lines[0].GetComponent<PortQuery>().portList.id;
redact.syncInfo(GameManager.Inst.nowDeviceID);
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);
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);
callback.Invoke(null);
}
if (redact.URlreturn != null && redact.URlreturn.message == "操作成功")
{
StartCoroutine(Succeed((x) =>
{
if (x)
callback.Invoke("1");
else
{
SecondConfirmPanel.DeleteConform(null, "更新场景失败");
Debug.Log("接口获取数据失败(线缆接口-初始化失败)");
callback.Invoke(null);
}
}));
Array.ForEach(GameManager.Inst.pop_ups.ToArray(), (itme) =>
{
itme.gameObject.SetActive(false);
});
}
else
{
SecondConfirmPanel.DeleteConform(null, "删除端口线缆名失败");
callback.Invoke(null);
}
}
));
}
/// <summary>
/// 删除线缆
/// </summary>
/// <param name="locatingCable"></param>
/// <param name="callback"></param>
public void deleteCableName(LocatingCable locatingCable, Action<string> callback)
{
var port_id = locatingCable.lines[0].GetComponent<PortQuery>().portList.id;
if (string.IsNullOrEmpty(port_id))
callback.Invoke(null);
else
{
Root root = new Root();
Root_deleteCableName root_DeleteCableName = new Root_deleteCableName { id = port_id };
var newData = JsonConvert.SerializeObject(root_DeleteCableName);
StartCoroutine(
CombineJSON.UpdateJson_POST(GameManager.Inst.Jk_URL.xl_sc, GameManager.Inst.arguments.token, newData, (jsonResult) =>
{
try
{
root = JsonConvert.DeserializeObject<Root>(jsonResult);
}
catch (Exception e)
{
Debug.Log("删除端口线缆名接口错误:" + e.Message);
callback.Invoke(null);
}
if (root != null && root.message == "操作成功")
{
StartCoroutine(Succeed((x) =>
{
if (x)
callback.Invoke("1");
else
{
SecondConfirmPanel.DeleteConform(null, "更新场景失败");
Debug.Log("接口获取数据失败(线缆删除接口-初始化失败)");
callback.Invoke(null);
}
}));
Array.ForEach(GameManager.Inst.pop_ups.ToArray(), (itme) =>
{
itme.gameObject.SetActive(false);
});
}
else
{
SecondConfirmPanel.DeleteConform(null, root != null ? root.message : "删除线缆失败");
callback.Invoke(null);
}
}));
}
}
public IEnumerator Succeed(Action<bool> callback)
{
GameManager.Inst.isLoading = false;
yield return null;
StartCoroutine(GameManager.Inst.Initialize((x) =>
{
if (!string.IsNullOrEmpty(x))
callback.Invoke(true);
else
callback.Invoke(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;
}
[System.Serializable]
public class Root_add
{
/// <summary>
///
/// </summary>
public string code;
/// <summary>
/// 反馈结果
/// </summary>
public string message;
/// <summary>
///
/// </summary>
public string data;
/// <summary>
///
/// </summary>
public string serverTime;
}
[System.Serializable]
public class Root_lineContentJson
{
/// <summary>
///
/// </summary>
public string lineContentJson;
}
[System.Serializable]
public class Root_id
{
/// <summary>
///
/// </summary>
public string id;
}
[System.Serializable]
public class Root_edit
{
public string id;
public string lineContentJson;
}
[System.Serializable]
public class Root_deleteCableName
{
public string id;
}
#endregion
}