NotificationEntity.php 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. declare(strict_types=1);
  3. namespace SixShop\Message\Entity;
  4. use think\Model;
  5. /**
  6. * 用户通知实体类
  7. */
  8. class NotificationEntity extends Model
  9. {
  10. // 设置表名
  11. protected $name = 'message_notifications';
  12. // 设置字段信息
  13. protected $schema = [
  14. 'id' => 'int',
  15. 'user_id' => 'int',
  16. 'title' => 'string',
  17. 'content' => 'string',
  18. 'type' => 'int',
  19. 'is_read' => 'int',
  20. /* 'read_time' => 'int',
  21. 'create_time' => 'int',
  22. 'update_time' => 'int',
  23. 'delete_time' => 'int',*/
  24. ];
  25. // 自动时间戳
  26. protected $autoWriteTimestamp = true;
  27. // 软删除
  28. protected $deleteTime = 'delete_time';
  29. /**
  30. * 获取通知类型文本
  31. */
  32. public function getTypeTextAttr()
  33. {
  34. $types = [
  35. 1 => '系统通知',
  36. 2 => '活动通知',
  37. 3 => '订单通知',
  38. 4 => '物流通知',
  39. 5 => '其他通知',
  40. ];
  41. return $types[$this->type] ?? '未知类型';
  42. }
  43. /**
  44. * 获取是否已读文本
  45. */
  46. public function getIsReadTextAttr()
  47. {
  48. $statuses = [
  49. 0 => '未读',
  50. 1 => '已读',
  51. ];
  52. return $statuses[$this->is_read] ?? '未知状态';
  53. }
  54. /**
  55. * 标记为已读
  56. */
  57. public function markAsRead()
  58. {
  59. if ($this->is_read == 0) {
  60. $this->is_read = 1;
  61. $this->read_time = time();
  62. $this->save();
  63. }
  64. return $this;
  65. }
  66. /**
  67. * 关联用户
  68. */
  69. public function user()
  70. {
  71. return $this->belongsTo('app\common\model\User', 'user_id');
  72. }
  73. }