DriverMessageModel.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Model\Arts;
  4. use App\Model\Model;
  5. class DriverMessageModel extends Model
  6. {
  7. /**
  8. * The table associated with the model.
  9. *
  10. * @var ?string
  11. */
  12. protected ?string $table = 'driver_message';
  13. protected ?string $dateFormat = 'U';
  14. public bool $timestamps = false;
  15. protected int $is_status_search = 1;// 是否使用 1=是 0=否 默认使用 status = 1 筛选
  16. protected int $is_delete_search = 0;// 是否使用 1=是 0=否 默认使用 is_delete = 0 筛选
  17. /**
  18. * 默认查询字段
  19. *
  20. * @var array|string[]
  21. */
  22. public array $select = [
  23. '*'
  24. ];
  25. public function searchDriverIdAttribute($query, $value, array $params): mixed
  26. {
  27. if (empty($value)) {
  28. return $query;
  29. }
  30. return $query->where('driver_id', $value);
  31. }
  32. public function searchIsReadAttribute($query, $value, array $params): mixed
  33. {
  34. if (empty($value)) {
  35. return $query;
  36. }
  37. return $query->where('is_read', $value);
  38. }
  39. public function searchIdAttribute($query, $value, array $params): mixed
  40. {
  41. if (empty($value)) {
  42. return $query;
  43. }
  44. return $query->where('id', $value);
  45. }
  46. public static function add(array $params)
  47. {
  48. $insert = array_merge($params,[
  49. 'status' => 1,
  50. 'create_time' => time()
  51. ]);
  52. return self::query()->insertGetId($insert);
  53. }
  54. public static function edit(int $id, array $params)
  55. {
  56. unset($params['id']);
  57. $insert = array_merge($params,[
  58. 'update_time' => time()
  59. ]);
  60. $query = self::query()->where('id',$id);
  61. return $query->update($insert);
  62. }
  63. /**
  64. * 获取未读消息数量
  65. * @param int $driver_id
  66. * @return int
  67. */
  68. public static function getNumByDriverId(int $driver_id)
  69. {
  70. return self::query()
  71. ->leftJoin('driver_message_read',function ($join) use ($driver_id){
  72. $join->on('driver_message.id','=','driver_message_read.message_id')
  73. ->where('driver_message_read.driver_id',$driver_id);
  74. })
  75. ->whereIn('driver_message.driver_id',[0,$driver_id])
  76. ->whereNull('driver_message_read.id')->count();
  77. }
  78. public static function read(int $driver_id)
  79. {
  80. $read = self::query()
  81. ->leftJoin('driver_message_read',function ($join) use ($driver_id){
  82. $join->on('driver_message.id','=','driver_message_read.message_id')
  83. ->where('driver_message_read.driver_id',$driver_id);
  84. })
  85. ->whereIn('driver_message.driver_id',[0,$driver_id])
  86. ->whereNull('driver_message_read.id')
  87. ->pluck('driver_message.id');
  88. $time = time();
  89. foreach ($read as $message_id) {
  90. $data[] = [
  91. 'driver_id' => $driver_id,
  92. 'message_id' => $message_id,
  93. 'create_time' => $time
  94. ];
  95. }
  96. !empty($data) && DriverMessageReadModel::query()->insert($data);
  97. }
  98. /**
  99. * 数据处理器
  100. * @param $value
  101. * @param $params
  102. * @return string
  103. */
  104. public function dataCreateTimeAttribute($value,$params)
  105. {
  106. if (empty($value)){
  107. return '---';
  108. }
  109. if (date('Y-m-d',$value) == date('Y-m-d')){
  110. return date('H:i:s',$value);
  111. }
  112. return date('Y-m-d H:i:s',$value);
  113. }
  114. public function driver()
  115. {
  116. return $this->hasOne(DriverModel::class,'id','driver_id');
  117. }
  118. public function reads()
  119. {
  120. return $this->hasOne(DriverMessageReadModel::class,'message_id','id');
  121. }
  122. }