MessageLogic.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. <?php
  2. /**
  3. * MessageLogic.php UTF-8
  4. * 消息逻辑处理
  5. *
  6. * @date : 2018/1/25 14:52
  7. *
  8. * @license 这不是一个自由软件,未经授权不许任何使用和传播。
  9. * @author : linjiebin <ljb@huosdk.com>
  10. * @version : HUOSDK 8.0
  11. */
  12. namespace web\pc\logic;
  13. use huo\model\common\CommonModel;
  14. use huo\model\message\MemMessageModel;
  15. use huo\model\message\MessageModel;
  16. class MessageLogic extends CommonModel {
  17. /**
  18. * 消息列表
  19. *
  20. * @param array $where
  21. * @param string $page
  22. * @param string $order
  23. *
  24. * @return array
  25. */
  26. public function getList($where = [], $page = '1,10', $order = '') {
  27. $_map = [];
  28. if (!empty($where['type'])) {
  29. $_map['type'] = $where['type'];
  30. }
  31. if (!empty($where['gameid'])) {
  32. $_map['app_id'] = $where['gameid'];
  33. }
  34. $_msg_ids = [];
  35. /* 获取已读消息列表 */
  36. if (!empty($where['mem_id']) && !empty($where['readed'])) {
  37. $_mem_map['mem_id'] = $where['mem_id'];
  38. $_mem_map['status'] = 2;
  39. $_msg_ids = MemMessageModel::where($_mem_map)->column('message_id');
  40. // $_map['id'] = ['in', $_msg_ids];
  41. } elseif (!empty($where['mem_id'])) {
  42. $_map['mem_id'] = ['in', '0,', $where['mem_id']];
  43. }
  44. $_order = $this->orderFilter($order);
  45. $_messages = MessageModel::where($_map)->page($page)->order($_order)->select()->toArray();
  46. $_list = [];
  47. $_types = ['1' => '活动消息',2=>'系统消息',3=>'卡券消息',4=>'优惠活动'];
  48. foreach ($_messages as $_message) {
  49. $_data = [];
  50. $_data['id'] = $_message['id'];
  51. $_data['title'] = $_message['title'];
  52. $_data['gameid'] = $_message['app_id'];
  53. $_data['type'] = $_message['type'];
  54. $_data['type_value'] = $_types[$_message['type']];
  55. $_data['create_time'] = $_message['create_time'];
  56. $_data['content'] = $_message['content'];
  57. $_data['readed'] = 1;
  58. if (in_array($_message['id'], $_msg_ids)) {
  59. $_data['readed'] = 2;
  60. }
  61. $_list[] = $_data;
  62. }
  63. return $_list;
  64. }
  65. /**
  66. * 获取消息数量
  67. *
  68. * @param $where
  69. *
  70. * @return int|string
  71. */
  72. public function getCnt($where) {
  73. $_map = [];
  74. if (!empty($where['type'])) {
  75. $_map['type'] = $where['type'];
  76. }
  77. if (!empty($where['gameid'])) {
  78. $_map['app_id'] = $where['gameid'];
  79. }
  80. if (!empty($where['mem_id']) && !empty($where['readed'])) {
  81. $_map['mm.status'] = 2;
  82. $_map['mm.mem_id'] = $where['mem_id'];
  83. /* 已看过的消息数量 */
  84. return MemMessageModel::where($_map)->count();
  85. } elseif (!empty($where['mem_id'])) {
  86. $_map['mem_id'] = ['in', '0,', $where['mem_id']];
  87. }
  88. return MessageModel::where($_map)->count();
  89. }
  90. /**
  91. * 添加消息
  92. *
  93. * @param int $mem_id
  94. * @param string $title
  95. * @param string $content
  96. * @param int $type
  97. * @param int $app_id
  98. *
  99. * @return bool
  100. */
  101. public function addMessage($mem_id, $title, $content, $type = 2, $app_id = 0) {
  102. $_msg_data['mem_id'] = $mem_id;
  103. $_msg_data['app_id'] = $app_id;
  104. $_msg_data['title'] = $title;
  105. $_msg_data['content'] = $content;
  106. $_msg_data['type'] = $type;
  107. $_msg_id = MessageModel::addMessage($_msg_data);
  108. if (false === $_msg_id) {
  109. return false;
  110. }
  111. if (!empty($mem_id)) {
  112. $_mm_data['mem_id'] = $mem_id;
  113. $_mm_data['message_id'] = $_msg_id;
  114. $_mm_data['type'] = $type;
  115. $_msg_id = MemMessageModel::addMessage($_mm_data);
  116. if (false === $_msg_id) {
  117. return false;
  118. }
  119. }
  120. return true;
  121. }
  122. /**
  123. * 获取消息详情
  124. *
  125. * @param int $msg_id
  126. * @param int $mem_id
  127. * @param bool $readed
  128. *
  129. * @return int
  130. */
  131. public function getDetail($msg_id = 0, $mem_id = 0, $readed = true) {
  132. if (empty($msg_id)) {
  133. return 2000;
  134. }
  135. $_message = MessageModel::get($msg_id);
  136. if (empty($_message)) {
  137. return 2000;
  138. }
  139. $_data['id'] = $_message->getAttr('id');
  140. $_data['title'] = $_message->getAttr('title');
  141. $_data['gameid'] = $_message->getAttr('app_id');
  142. $_data['type'] = $_message->getAttr('type');
  143. $_data['create_time'] = $_message->getAttr('create_time');
  144. $_data['content'] = $_message->getAttr('content');
  145. $_data['readed'] = 1;
  146. if (empty($mem_id) || false == $readed) {
  147. return $_data;
  148. }
  149. if (!empty($mem_id) && $readed) {
  150. $_map['mem_id'] = $mem_id;
  151. $_map['message_id'] = $msg_id;
  152. $_mem_message = MemMessageModel::get($_map);
  153. if (empty($_mem_message)) {
  154. $_mm_data['mem_id'] = $mem_id;
  155. $_mm_data['message_id'] = $msg_id;
  156. $_mm_data['type'] = $_message['type'];
  157. $_mm_data['status'] = 2;
  158. MemMessageModel::addMessage($_mm_data);
  159. } else if (1 == $_mem_message->getAttr('status')) {
  160. $_mem_message->setAttr('status', 2);
  161. $_mem_message->isUpdate(true)->save();
  162. }
  163. $_data['readed'] = 2;
  164. }
  165. return $_data;
  166. }
  167. }