PrivateMessageEntity.php 1.7 KB

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