144 lines
2.9 KiB
PHP
144 lines
2.9 KiB
PHP
<?php
|
|
|
|
namespace app\Model;
|
|
|
|
use support\Model;
|
|
|
|
/**
|
|
* 二手车信息模型
|
|
*/
|
|
class UsedCar extends Model
|
|
{
|
|
protected $table = 'used_car';
|
|
|
|
protected $fillable = [
|
|
'code',
|
|
'user_id',
|
|
'role',
|
|
'product_name',
|
|
'vin',
|
|
'price',
|
|
'source_place',
|
|
'plate_place',
|
|
'plate_time',
|
|
'displacement',
|
|
'mileage',
|
|
'power',
|
|
'body_color',
|
|
'images',
|
|
'condition_desc',
|
|
'attachment',
|
|
'pdf_src',
|
|
'source_url',
|
|
'crawl_title',
|
|
'crawl_brand',
|
|
'crawl_brand_id',
|
|
'crawl_series',
|
|
'crawl_series_id',
|
|
'crawl_year',
|
|
'crawl_msrp',
|
|
'crawl_car_id',
|
|
'status',
|
|
'sort',
|
|
];
|
|
|
|
protected $casts = [
|
|
'price' => 'integer',
|
|
'mileage' => 'integer',
|
|
'crawl_brand_id' => 'integer',
|
|
'crawl_series_id' => 'integer',
|
|
'crawl_msrp' => 'integer',
|
|
'crawl_car_id' => 'integer',
|
|
'status' => 'integer',
|
|
'sort' => 'integer',
|
|
'attachment' => 'array',
|
|
'created_at' => 'datetime',
|
|
'updated_at' => 'datetime',
|
|
];
|
|
|
|
/**
|
|
* 状态常量
|
|
*/
|
|
const STATUS_NORMAL = 1; // 正常
|
|
const STATUS_ARCHIVED = 2; // 归档
|
|
const STATUS_DELETED = 3; // 删除
|
|
|
|
/**
|
|
* 关联用户
|
|
*/
|
|
public function user()
|
|
{
|
|
return $this->belongsTo(User::class, 'user_id');
|
|
}
|
|
|
|
/**
|
|
* 关联PDF生成日志
|
|
*/
|
|
public function pdfLogs()
|
|
{
|
|
return $this->hasMany(UsedCarPdfLog::class, 'used_car_id')->orderBy('created_at', 'desc');
|
|
}
|
|
|
|
/**
|
|
* 判断是否可以编辑
|
|
*/
|
|
public function canEdit(): bool
|
|
{
|
|
return $this->status === self::STATUS_NORMAL;
|
|
}
|
|
|
|
/**
|
|
* 判断是否已删除
|
|
*/
|
|
public function isDeleted(): bool
|
|
{
|
|
return $this->status === self::STATUS_DELETED;
|
|
}
|
|
|
|
/**
|
|
* 判断是否已归档
|
|
*/
|
|
public function isArchived(): bool
|
|
{
|
|
return $this->status === self::STATUS_ARCHIVED;
|
|
}
|
|
|
|
/**
|
|
* 获取图片URL数组
|
|
*/
|
|
public function getImageUrlsAttribute(): array
|
|
{
|
|
if (empty($this->images)) {
|
|
return [];
|
|
}
|
|
return explode(',', $this->images);
|
|
}
|
|
|
|
/**
|
|
* 作用域:正常状态
|
|
*/
|
|
public function scopeNormal($query)
|
|
{
|
|
return $query->where('status', self::STATUS_NORMAL);
|
|
}
|
|
|
|
/**
|
|
* 作用域:按用户筛选
|
|
*/
|
|
public function scopeByUser($query, int $userId)
|
|
{
|
|
return $query->where('user_id', $userId);
|
|
}
|
|
|
|
/**
|
|
* 作用域:搜索关键词
|
|
*/
|
|
public function scopeSearch($query, string $keyword)
|
|
{
|
|
return $query->where(function ($q) use ($keyword) {
|
|
$q->where('product_name', 'like', "%{$keyword}%")
|
|
->orWhere('code', 'like', "%{$keyword}%");
|
|
});
|
|
}
|
|
}
|