using System.Collections; using System.Collections.Generic; using TMPro; using UnityEngine; using UnityEngine.UI; using System.Linq; /// /// 岗位职级数据存储类(只获取一遍组织机构) /// public class PostsData { private static PostsData _Instance; private PostsData() { } public static PostsData Instance { get { if (_Instance == null) { _Instance = new PostsData(); } return _Instance; } } private List AllData = new List(); public List GetOrgDatas() { lock (this) { return this.AllData; } } public void SetData(List datas) { lock (this) { foreach (var item in datas) { if (!AllData.Contains(item)) AllData.Add(item); } } } } public class PostsLoadPanel : MonoBehaviour { public Toggle Top; public Image ShowImage; public Image HideImage; public TMP_Text ShowOrgName; public GameObject Main; public Transform PostsParent; public GameObject PostsItem; [SerializeField] public List SelectDatas = new List(); public Color NormalColor; public Image NoneDataImage; private OrgItemData lastOrgData = null; private string PortsApi = "/api/Posts"; private List AllResultData = new List(); void Start() { if (AllResultData.Count == 0) { var Url = ""; if (HTTPRequestManager.Instance != null) { Url = $"{PortsApi}?PageNumber={1}&PageSize={10000}"; } //向服务端请求数据 HTTPRequestManager.Instance.GetJson>(Url, SkipPageSuccessed, SkipPageError); } //else //{ // DataBind(AllResultData); //} Top.onValueChanged.AddListener(Top_ValueChanged); } private void Top_ValueChanged(bool IsOn) { Main.SetActive(IsOn); ShowImage.gameObject.SetActive(IsOn); HideImage.gameObject.SetActive(!IsOn); if (lastOrgData != null) { AutoBind(); Main.SetActive(IsOn); } else { Main.SetActive(false); } } public void OrgDataBind(OrgItemData organizationsData) { if (lastOrgData != organizationsData) { lastOrgData = organizationsData; if (lastOrgData != null) { DataBind(AllResultData.Where(a => a.level == lastOrgData.level).ToList()); //AutoBind(); } SelectDatas.Clear(); ShowOrgName.text = "请先选择组织机构选择"; ShowOrgName.color = NormalColor; } if (Top.isOn) { Top.isOn = false; } } private void AutoBind() { StopAllCoroutines(); StartCoroutine(autoBind()); } IEnumerator autoBind() { yield return new WaitForSeconds(0.1f); float height = PostsParent.GetComponent().sizeDelta.y < 330 ? PostsParent.GetComponent().sizeDelta.y : 330; Main.GetComponent().sizeDelta = new Vector2( Main.GetComponent().sizeDelta.x, height); } private void DataBind(List allResultData) { foreach (Transform item in PostsParent) { if (item != NoneDataImage.transform) Destroy(item.gameObject); } if (allResultData.Count > 0) { NoneDataImage.gameObject.SetActive(false); foreach (var item in allResultData) { GameObject go = Instantiate(PostsItem, PostsParent); go.GetComponent().DataBind(item, this); } } else { NoneDataImage.gameObject.SetActive(true); } } private void SkipPageError(int code, string message) { //MessageNotifyBox.Instance.ShowDialogOK("获取岗位职级失败!"); Debug.LogError("获取岗位职级失败!"); } private void SkipPageSuccessed(List obj) { if (obj.Count > 0) { foreach (var item in obj) { AllResultData.Add(item); //Debug.Log(item.Name); } } //DataBind(AllResultData); } public void OnSelect(Posts data, bool IsOn) { if (IsOn) { if (!SelectDatas.Contains(data)) { SelectDatas.Add(data); } } else { if (SelectDatas.Contains(data)) { SelectDatas.Remove(data); } } if (SelectDatas.Count > 0) { string showName = ""; foreach (var item in SelectDatas) { showName += item.Name; if (SelectDatas.IndexOf(item) < SelectDatas.Count - 1) showName += ","; } if (showName.Length >= 17) { showName = showName.Substring(0, 16) + "..."; } ShowOrgName.text = showName; ShowOrgName.color = Color.white; } else { ShowOrgName.text = "请先选择组织机构选择"; ShowOrgName.color = NormalColor; } } public void Reset() { foreach (Transform item in PostsParent) { if (item.GetComponent()) item.gameObject.GetComponent().M_Toggle.isOn = false; } Top.isOn = false; ShowOrgName.text = "请先选择组织机构选择"; ShowOrgName.color = NormalColor; } }