Files
hyperf_service/app/Model/OssObject.php
2024-11-12 15:40:33 +08:00

119 lines
2.8 KiB
PHP

<?php
declare(strict_types=1);
namespace App\Model;
use App\Constants\Common\OssObjectCode;
use Hyperf\Collection\Collection;
use Hyperf\Database\Model\Builder;
use Hyperf\DbConnection\Model\Model;
use function Hyperf\Config\config;
/**
* @property int $id
* @property string $url
* @property int $size
* @property int $height
* @property int $width
* @property int $audio_second
* @property int $video_duration
* @property int $is_enabled
* @property string $create_time
* @property int $type
*/
class OssObject extends Model
{
/**
* The table associated with the model.
*/
protected ?string $table = 'oss_object';
/**
* The attributes that are mass assignable.
*/
protected array $fillable = [];
protected array $guarded = [];
/**
* The attributes that should be cast to native types.
*/
protected array $casts = ['id' => 'integer', 'size' => 'integer','height' => 'integer', 'width' => 'integer', 'audio_second' => 'integer', 'video_duration' => 'integer', 'is_enabled' => 'integer', 'type' => 'integer'];
const CREATED_AT = 'create_time';
const UPDATED_AT = null;
/**
* url访问器
* @param $value
* @return string
*/
public function getUrlAttribute($value)
{
return config('ali.oss_url').$value;
}
/**
* 根据ids获取资源id列表
* @param array $ids
* @return Collection
*/
public function getIdListByIds(array $ids): Collection
{
return $this->whereIn('id', $ids)->pluck('id');
}
/**
* 根据ids更新oss Enable状态
*/
public function updateEnabledByIds(array $ids): int
{
return $this->whereIn('id', $ids)->update(['is_enabled' => OssObjectCode::ENABLE]);
}
/**
* 根据ids 更新oss Disable状态
* @param array $ids
* @return int
*/
public function updateDisableByIds(array $ids): int
{
return $this->whereIn('id', $ids)->update(['is_enabled' => OssObjectCode::DISABLE]);
}
/**
* 根据id获取信息
* @param array $ids
* @return Collection
*/
public function getInfoByOssIds(array $ids): Collection
{
return $this->whereIn('id', $ids)->get();
}
/**
* @param int $id
* @return Builder|\Hyperf\Database\Model\Model|null
*/
public function getInfoById(int $id): \Hyperf\Database\Model\Model|Builder|null
{
return $this->where('id', $id)->first();
}
/**
* 根据is_enabled获取传入时间前无效资源id
* @param $time
* @return Collection|false
*/
public function getOssIdListByIsEnabled($time): Collection|false
{
return $this->where([
['is_enabled', '=', OssObjectCode::DISABLE],
['create_time', '<',$time]
])->pluck('id') ?? false;
}
}