MailDataManager.cs 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using UnityEngine;
  5. namespace GFGGame
  6. {
  7. public class MailInfo
  8. {
  9. public long mailId;//邮件id
  10. public string title;//邮件标题
  11. public long timeSec;//邮件时间戳,单位秒
  12. public string content = "";//邮件内容
  13. public List<ItemData> rewards;
  14. public int state;//邮件状态 0未读, 1已读未领取,2已读已领取
  15. public bool hasItem;//是否有奖励
  16. }
  17. public class MailDataManager : SingletonBase<MailDataManager>
  18. {
  19. public int PageCount = 30;//每次请求数量
  20. public int CurPage = 0;//每次请求数量
  21. private int _totolCount = 0;
  22. public int TotolCount//邮件总数
  23. {
  24. get { return _totolCount; }
  25. set
  26. {
  27. _totolCount = value;
  28. }
  29. }
  30. private int _unreadCount = 0;
  31. public int UnreadCount////未读邮件数量
  32. {
  33. get { return _unreadCount; }
  34. set
  35. {
  36. if (_unreadCount != value)
  37. {
  38. _unreadCount = value;
  39. EventAgent.DispatchEvent(ConstMessage.RED_CHANGE);
  40. }
  41. }
  42. }
  43. private int _startIndex = 0;
  44. public int StartIndex//起始索引,从0开始
  45. {
  46. get { return _startIndex; }
  47. set { _startIndex = value; }
  48. }
  49. public Dictionary<long, MailInfo> _mailInfoDic = new Dictionary<long, MailInfo>();
  50. public Dictionary<long, MailInfo> MailInfoDic
  51. {
  52. get
  53. {
  54. return _mailInfoDic;
  55. }
  56. }
  57. private Dictionary<int, List<long>> _mailIdsDic = new Dictionary<int, List<long>>();
  58. public List<MailInfo> mailInfos = new List<MailInfo>();
  59. public void RefreshMailInfoDic(bool sort)
  60. {
  61. if (sort)
  62. {
  63. _mailInfoDic.Clear();
  64. _mailIdsDic.Clear();
  65. }
  66. }
  67. public void UpdateMainInfoDic(long mailId, MailInfo mailInfo)
  68. {
  69. if (!_mailInfoDic.ContainsKey(mailId))
  70. {
  71. _mailInfoDic.Add(mailId, mailInfo);
  72. }
  73. else
  74. {
  75. _mailInfoDic[mailId] = mailInfo;
  76. }
  77. }
  78. public void UpdateMailIdList(int page, long mailId)
  79. {
  80. if (!_mailIdsDic.ContainsKey(page))
  81. {
  82. _mailIdsDic.Add(page, new List<long>());
  83. }
  84. _mailIdsDic[page].Add(mailId);
  85. }
  86. public void UpdateMailContent(long mailId, int state, string content = "", List<ItemData> rewards = null)
  87. {
  88. _mailInfoDic[mailId].state = state;
  89. if (content != "") _mailInfoDic[mailId].content = content;
  90. if (rewards != null) _mailInfoDic[mailId].rewards = rewards;
  91. UnreadCount = UnreadCount - 1;
  92. }
  93. public long GetMailIdByIndex(int index)
  94. {
  95. int page = index / PageCount;
  96. int pageIndex = index % PageCount;
  97. if (_mailIdsDic.ContainsKey(page) && _mailIdsDic[page].Count > pageIndex)
  98. {
  99. return _mailIdsDic[page][pageIndex];
  100. }
  101. return -1;
  102. }
  103. public List<long> GetMailIdByPage(int page)
  104. {
  105. if (_mailIdsDic.ContainsKey(page))
  106. {
  107. return _mailIdsDic[page];
  108. }
  109. return null;
  110. }
  111. /// <summary>
  112. /// 根据邮件Id获取邮件信息
  113. /// </summary>
  114. /// <param name="mailId"></param>
  115. /// <returns></returns>
  116. public MailInfo GetMailInfoById(long mailId)
  117. {
  118. if (_mailInfoDic.ContainsKey(mailId))
  119. {
  120. return _mailInfoDic[mailId];
  121. }
  122. return null;
  123. }
  124. //0未读,1无奖已读,2有奖未领,3有奖已领
  125. public int GetMailState(MailInfo data)
  126. {
  127. if (data.hasItem == false)
  128. {
  129. if ((ConstMailStatus)data.state == ConstMailStatus.Unread) return 0;
  130. if ((ConstMailStatus)data.state == ConstMailStatus.ReadedButNotGet) return 1;
  131. if ((ConstMailStatus)data.state == ConstMailStatus.ReadedAndGot) return 1;
  132. }
  133. else
  134. {
  135. if ((ConstMailStatus)data.state == ConstMailStatus.Unread) return 2;
  136. if ((ConstMailStatus)data.state == ConstMailStatus.ReadedButNotGet) return 2;
  137. if ((ConstMailStatus)data.state == ConstMailStatus.ReadedAndGot) return 3;
  138. }
  139. return 0;
  140. }
  141. }
  142. }