123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <?php
- declare(strict_types=1);
- namespace App\Model\Arts;
- use App\Model\Model;
- class DriverMessageModel extends Model
- {
- /**
- * The table associated with the model.
- *
- * @var ?string
- */
- protected ?string $table = 'driver_message';
- protected ?string $dateFormat = 'U';
- public bool $timestamps = false;
- protected int $is_status_search = 1;// 是否使用 1=是 0=否 默认使用 status = 1 筛选
- protected int $is_delete_search = 0;// 是否使用 1=是 0=否 默认使用 is_delete = 0 筛选
- /**
- * 默认查询字段
- *
- * @var array|string[]
- */
- public array $select = [
- '*'
- ];
- public function searchDriverIdAttribute($query, $value, array $params): mixed
- {
- if (empty($value)) {
- return $query;
- }
- return $query->where('driver_id', $value);
- }
- public function searchIsReadAttribute($query, $value, array $params): mixed
- {
- if (empty($value)) {
- return $query;
- }
- return $query->where('is_read', $value);
- }
- public function searchIdAttribute($query, $value, array $params): mixed
- {
- if (empty($value)) {
- return $query;
- }
- return $query->where('id', $value);
- }
- public static function add(array $params)
- {
- $insert = array_merge($params,[
- 'status' => 1,
- 'create_time' => time()
- ]);
- return self::query()->insertGetId($insert);
- }
- public static function edit(int $id, array $params)
- {
- unset($params['id']);
- $insert = array_merge($params,[
- 'update_time' => time()
- ]);
- $query = self::query()->where('id',$id);
- return $query->update($insert);
- }
- /**
- * 获取未读消息数量
- * @param int $driver_id
- * @return int
- */
- public static function getNumByDriverId(int $driver_id)
- {
- return self::query()
- ->leftJoin('driver_message_read',function ($join) use ($driver_id){
- $join->on('driver_message.id','=','driver_message_read.message_id')
- ->where('driver_message_read.driver_id',$driver_id);
- })
- ->whereIn('driver_message.driver_id',[0,$driver_id])
- ->whereNull('driver_message_read.id')->count();
- }
- public static function read(int $driver_id)
- {
- $read = self::query()
- ->leftJoin('driver_message_read',function ($join) use ($driver_id){
- $join->on('driver_message.id','=','driver_message_read.message_id')
- ->where('driver_message_read.driver_id',$driver_id);
- })
- ->whereIn('driver_message.driver_id',[0,$driver_id])
- ->whereNull('driver_message_read.id')
- ->pluck('driver_message.id');
- $time = time();
- foreach ($read as $message_id) {
- $data[] = [
- 'driver_id' => $driver_id,
- 'message_id' => $message_id,
- 'create_time' => $time
- ];
- }
- !empty($data) && DriverMessageReadModel::query()->insert($data);
- }
- /**
- * 数据处理器
- * @param $value
- * @param $params
- * @return string
- */
- public function dataCreateTimeAttribute($value,$params)
- {
- if (empty($value)){
- return '---';
- }
- if (date('Y-m-d',$value) == date('Y-m-d')){
- return date('H:i:s',$value);
- }
- return date('Y-m-d H:i:s',$value);
- }
- public function driver()
- {
- return $this->hasOne(DriverModel::class,'id','driver_id');
- }
- public function reads()
- {
- return $this->hasOne(DriverMessageReadModel::class,'message_id','id');
- }
- }
|