using Newtonsoft.Json;
using SimpleJSON;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
///
/// 线缆组
///
[AddComponentMenu("LineQuery/线缆组")]
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)
public IEnumerator getJsonCoroutine(Action 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("1");
}
else
{
keyValues.Clear();
dic.Clear();
callback?.Invoke("1");
}
}
else
{
Debug.Log("查询线缆组失败: " + root.message);
SecondConfirmPanel.DeleteConform(null, "查询线缆组失败");
callback.Invoke(null);
}
}));
}
///
/// 删除线缆组
///
public IEnumerator deleteJsonCoroutine(string id, Action 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.message);
SecondConfirmPanel.DeleteConform(null, "删除线缆组失败");
callback.Invoke(null);
}
}));
}
///
/// 新增线缆组
///
public IEnumerator SaveJsonCoroutine(string lineContentJson, Action 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);
}
}));
}
///
/// 新增
///
public IEnumerator saveJson(Root_add 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.LogError("新增线缆组接口错误:" + e.Message + e.StackTrace);
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 void editJsonCoroutine(string _id, string _lineContentJson, Action 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(jsonResult);
if (root.message == "操作成功")
{
callback.Invoke(true, root);
}
else
{
callback.Invoke(false, root);
}
}
catch
{
callback.Invoke(false, null);
}
}));
}
///
/// 删除线缆
///
///
///
///
public void deleteCableName(string cableGroupName, string CableName, Action callback)
{
var xianlan = PatternChoose.Inst.xianlan.gameObject.GetComponentsInChildren(true);
var info = Array.Find(xianlan, (item) =>
{
return (item.cableGroupName == cableGroupName && item.cableName == CableName);
});
RedactPort redact = PatternChoose.Inst.dk_bj_page.GetComponent();
GameManager.Inst.nowDeviceID = info.lines[0].GetComponent().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(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);
}
}
));
}
///
/// 删除线缆
///
///
///
public void deleteCableName(LocatingCable locatingCable, Action callback)
{
var port_id = locatingCable.lines[0].GetComponent().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(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 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
{
///
///
///
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_add
{
///
///
///
public string code;
///
/// 反馈结果
///
public string message;
///
///
///
public string data;
///
///
///
public string serverTime;
}
public class Root_lineContentJson
{
///
///
///
public string lineContentJson;
}
public class Root_id
{
///
///
///
public string id;
}
public class Root_edit
{
public string id;
public string lineContentJson;
}
public class Root_deleteCableName
{
public string id;
}
#endregion
}