Message.php 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. <?php
  2. namespace app\api\model\system;
  3. use app\api\model\BaseModel;
  4. class Message extends BaseModel
  5. {
  6. // 表名
  7. protected $name = 'system_message';
  8. // 自动写入时间戳字段
  9. protected $autoWriteTimestamp = 'integer';
  10. // 定义时间戳字段名
  11. protected $createTime = 'createtime';
  12. protected $updateTime = 'updatetime';
  13. protected $deleteTime = false;
  14. public function fetchUserSystemMessage($user_id, $page, $size)
  15. {
  16. return $this->where("identity_type", \E_IDENTITY_TYPE::User)
  17. ->where("to_user_id", $user_id)
  18. ->order("updatetime", "desc")
  19. ->page($page)
  20. ->paginate($size);
  21. }
  22. public function fetchMassagerSystemMessage($m_id, $page, $size)
  23. {
  24. return $this->where("identity_type", \E_IDENTITY_TYPE::Massager)
  25. ->where("to_massager_id", $m_id)
  26. ->order("updatetime", "desc")
  27. ->page($page)
  28. ->paginate($size);
  29. }
  30. public function fetchAgencySystemMessage($a_id, $page, $size)
  31. {
  32. return $this->where("identity_type", \E_IDENTITY_TYPE::Agency)
  33. ->where("to_agency_id", $a_id)
  34. ->order("updatetime", "desc")
  35. ->page($page)
  36. ->paginate($size);
  37. }
  38. /**
  39. * @param $m_id
  40. * @return int|string
  41. * @throws \think\Exception
  42. */
  43. public function getUnreadMessageCount($m_id)
  44. {
  45. return $this->where("identity_type", \E_IDENTITY_TYPE::Massager)
  46. ->where("to_massager_id", $m_id)
  47. ->where("is_read", 0)
  48. ->count();
  49. }
  50. /**
  51. * 给系统用户发送系统消息
  52. * @param $identity_type
  53. * @param array $to_target
  54. * @param $title
  55. * @param $content
  56. * @return Message
  57. */
  58. public static function sendSystemMessage($identity_type, array $to_target, $title, $content)
  59. {
  60. return self::create([
  61. "identity_type" => $identity_type,
  62. "to_user_id" => isset($to_target["to_user_id"]) ? $to_target["to_user_id"] : null,
  63. "to_massager_id" => isset($to_target["to_massager_id"]) ? $to_target["to_massager_id"] : null,
  64. "to_agency_id" => isset($to_target["to_agency_id"]) ? $to_target["to_agency_id"] : null,
  65. "title" => $title,
  66. "content" => $content,
  67. "is_read" => 0,
  68. "createtime" => time(),
  69. "updatetime" => time()
  70. ]);
  71. }
  72. /**
  73. * @param $msgs
  74. * @return bool|int|string
  75. */
  76. public static function sendSystemMessages($msgs)
  77. {
  78. return self::insertAll($msgs);
  79. }
  80. }