NotifyMemberService.php 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. <?php
  2. namespace services\common;
  3. use common\enums\NotifyTypeEnum;
  4. use common\helpers\ArrayHelper;
  5. use common\enums\StatusEnum;
  6. use common\models\common\NotifyMember;
  7. /**
  8. * Class NotifyMemberService
  9. * @package services\common
  10. */
  11. class NotifyMemberService
  12. {
  13. /**
  14. * 获取用户消息列表
  15. *
  16. * @param $merchant_id
  17. */
  18. public function getNotReadNotify($merchant_id, $is_read = 0)
  19. {
  20. $data = NotifyMember::find()
  21. ->select(['type', 'max(created_at) as created_at', 'count(id) as count'])
  22. ->where(['status' => StatusEnum::ENABLED, 'is_read' => $is_read])
  23. ->andWhere(['merchant_id' => $merchant_id])
  24. ->groupBy('type')
  25. ->asArray()
  26. ->all();
  27. $count = 0;
  28. foreach ($data as &$datum) {
  29. $count += $datum['count'];
  30. $datum['title'] = NotifyTypeEnum::getValue($datum['type']);
  31. }
  32. return [
  33. ArrayHelper::arrayKey($data, 'type'),
  34. $count
  35. ];
  36. }
  37. /**
  38. * 更新指定的notify,把isRead属性设置为true
  39. *
  40. * @param $member_id
  41. */
  42. public function readById($member_id, $merchant_id, $notifyIds)
  43. {
  44. NotifyMember::updateAll(['is_read' => 1, 'read_member_id' => $member_id, 'updated_at' => time()], [
  45. 'and',
  46. ['merchant_id' => $merchant_id, 'is_read' => 0],
  47. ['in', 'id', $notifyIds]
  48. ]);
  49. }
  50. /**
  51. * 删除指定的notify,把isRead属性设置为true
  52. *
  53. * @param $member_id
  54. */
  55. public function deleteById($merchant_id, $notifyIds)
  56. {
  57. NotifyMember::deleteAll([
  58. 'and',
  59. ['merchant_id' => $merchant_id],
  60. ['in', 'id', $notifyIds]
  61. ]);
  62. }
  63. /**
  64. * @param $member_id
  65. * @param $merchant_id
  66. * @param $notifyIds
  67. */
  68. public function readByNotifyId($member_id, $merchant_id, $notifyIds)
  69. {
  70. NotifyMember::updateAll(['is_read' => 1, 'read_member_id' => $member_id, 'updated_at' => time()], [
  71. 'and',
  72. ['merchant_id' => $merchant_id, 'is_read' => 0],
  73. ['in', 'notify_id', $notifyIds]
  74. ]);
  75. }
  76. /**
  77. * 全部设为已读
  78. *
  79. * @param $member_id
  80. */
  81. public function readAll($member_id, $merchant_id)
  82. {
  83. NotifyMember::updateAll(['is_read' => 1, 'read_member_id' => $member_id, 'updated_at' => time()], ['merchant_id' => $merchant_id, 'is_read' => 0]);
  84. }
  85. }
粤ICP备19079148号