NotifyMemberService.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. <?php
  2. namespace addons\TinyShop\services\common;
  3. use common\enums\StatusEnum;
  4. use common\enums\NotifyTypeEnum;
  5. use addons\TinyShop\common\models\common\NotifyMember;
  6. /**
  7. * Class NotifyMemberService
  8. * @package addons\TinyShop\services\common
  9. * @author jianyan74 <751393839@qq.com>
  10. */
  11. class NotifyMemberService
  12. {
  13. /**
  14. * 未读数量(组别)
  15. *
  16. * @param $member_id
  17. * @return array|\yii\db\ActiveRecord[]
  18. */
  19. public function unReadCount($member_id)
  20. {
  21. $data = [
  22. 'count' => 0,
  23. 'announce' => 0,
  24. 'remind' => 0,
  25. 'message' => 0,
  26. ];
  27. $groups = NotifyMember::find()
  28. ->select(['type', 'count(id) as count'])
  29. ->where(['member_id' => $member_id, 'is_read' => 0])
  30. ->groupBy(['type'])
  31. ->asArray()
  32. ->all();
  33. foreach ($groups as $group) {
  34. $data['count'] += $group['count'];
  35. switch ($group['type']) {
  36. case NotifyTypeEnum::ANNOUNCE :
  37. $data['announce'] += $group['count'];
  38. break;
  39. case NotifyTypeEnum::REMIND :
  40. $data['remind'] += $group['count'];
  41. break;
  42. case NotifyTypeEnum::MESSAGE :
  43. $data['message'] += $group['count'];
  44. break;
  45. }
  46. }
  47. return $data;
  48. }
  49. /**
  50. * 更新指定的notify,把isRead属性设置为true
  51. *
  52. * @param $member_id
  53. */
  54. public function read($member_id, $notifyIds)
  55. {
  56. NotifyMember::updateAll(['is_read' => true, 'updated_at' => time()], ['and', ['member_id' => $member_id], ['in', 'notify_id', $notifyIds]]);
  57. }
  58. /**
  59. * 清空
  60. *
  61. * @param $member_id
  62. * @param $type
  63. */
  64. public function clear($member_id, $notifyIds)
  65. {
  66. NotifyMember::updateAll([
  67. 'is_read' => true,
  68. 'status' => StatusEnum::DELETE,
  69. 'updated_at' => time(),
  70. ], [
  71. 'and',
  72. ['member_id' => $member_id],
  73. ['in', 'notify_id', $notifyIds],
  74. ]
  75. );
  76. }
  77. /**
  78. * 清空
  79. *
  80. * @param $member_id
  81. * @param $type
  82. */
  83. public function clearAll($member_id, $type)
  84. {
  85. NotifyMember::updateAll([
  86. 'is_read' => true,
  87. 'status' => StatusEnum::DELETE,
  88. 'updated_at' => time(),
  89. ], [
  90. 'member_id' => $member_id,
  91. 'type' => $type,
  92. ]);
  93. }
  94. /**
  95. * 全部设为已读
  96. *
  97. * @param $member_id
  98. */
  99. public function readAll($member_id)
  100. {
  101. NotifyMember::updateAll(['is_read' => true, 'updated_at' => time()], ['member_id' => $member_id, 'is_read' => false]);
  102. }
  103. /**
  104. * @param $member_id
  105. * @return array|\yii\db\ActiveRecord|null
  106. */
  107. public function newest($member_id)
  108. {
  109. return NotifyMember::find()->where(['status' => StatusEnum::ENABLED])
  110. ->andWhere([
  111. 'type' => NotifyTypeEnum::REMIND,
  112. 'member_id' => $member_id,
  113. 'is_read' => StatusEnum::DISABLED
  114. ])
  115. ->with(['notify'])
  116. ->orderBy('id desc')
  117. ->asArray()
  118. ->one();
  119. }
  120. }
粤ICP备19079148号