You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
94 lines
2.3 KiB
94 lines
2.3 KiB
<?php
|
|
|
|
declare (strict_types=1);
|
|
namespace App\Model\v3;
|
|
|
|
use App\Constants\v3\OssThumbnail;
|
|
use App\Constants\v3\SsdbKeys;
|
|
use App\Model\Model;
|
|
use App\Service\v3\Interfaces\AttachmentServiceInterface;
|
|
use App\TaskWorker\SSDBTask;
|
|
use Hyperf\Database\Model\Builder;
|
|
use Hyperf\Database\Model\SoftDeletes;
|
|
use Hyperf\Utils\ApplicationContext;
|
|
use App\Constants\v3\Store as StoreConstants;
|
|
use Hyperf\Di\Annotation\Inject;
|
|
|
|
/**
|
|
*/
|
|
class Store extends Model
|
|
{
|
|
use SoftDeletes;
|
|
|
|
/**
|
|
* The table associated with the model.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $table = 'lanzu_store_new';
|
|
/**
|
|
* The attributes that are mass assignable.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $fillable = [];
|
|
/**
|
|
* The attributes that should be cast to native types.
|
|
*
|
|
* @var array
|
|
*/
|
|
protected $casts = [];
|
|
|
|
protected $appends = [
|
|
'month_sales'
|
|
];
|
|
|
|
/**
|
|
* @Inject
|
|
* @var AttachmentServiceInterface
|
|
*/
|
|
protected $attachmentService;
|
|
|
|
protected function boot(): void
|
|
{
|
|
parent::boot();
|
|
self::addGlobalScope('normal', function (Builder $builder) {
|
|
$builder->where([$this->getTable().'.is_open' => StoreConstants::IS_OPEN_YES, $this->getTable().'.status' => StoreConstants::STATUS_PASS]);
|
|
});
|
|
}
|
|
|
|
public function scopeOrderByDefault($query, $sort)
|
|
{
|
|
return $query->orderBy($this->getTable().'.sort', $sort)->orderBy($this->getTable().'.id', $sort);
|
|
}
|
|
|
|
public function scopeOrderBySales($query, $sort)
|
|
{
|
|
return $query->orderBy($this->getTable().'.sales', $sort);
|
|
}
|
|
|
|
public function getMonthSalesAttribute() {
|
|
$ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
|
|
return (integer)$ssdb->exec('get', SsdbKeys::STORE_MONTH_SALES.date('Ym').'_'.$this->id);
|
|
}
|
|
|
|
public function getLogoAttribute($value)
|
|
{
|
|
return $this->attachmentService->switchImgToAliOss($value, OssThumbnail::THUMBNAIL_300_Q80);
|
|
}
|
|
|
|
public function goods()
|
|
{
|
|
return $this->hasMany(Goods::class, 'store_id', 'id')->limit(5);
|
|
}
|
|
|
|
public function shoppingCart()
|
|
{
|
|
return $this->hasMany(ShoppingCart::class, 'store_id', 'id');
|
|
}
|
|
|
|
public function market()
|
|
{
|
|
return $this->belongsTo(Market::class, 'market_id', 'id');
|
|
}
|
|
}
|