123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 |
- <?php
- namespace App\Repositories\Models\Dwbs;
- use App\Repositories\Enums\ModelStatusEnum;
- use App\Repositories\Models\Model;
- use Illuminate\Support\Facades\Cache;
- class ShopGood extends Model
- {
- /**
- * @var string
- */
- protected $table = 'dwbs_shop_goods';
- protected $guarded = [];
- /**
- * The attributes excluded from the model's JSON form.
- *
- * @var array
- */
- protected $hidden = [];
- protected $casts = [];
- public function good()
- {
- return $this->belongsTo(ShopGood::class)->select(['id', 'name', 'cover']);
- }
- public function getCoverAttribute($val)
- {
- if (empty($val)) {
- return config('app.url') . '/default/headimg.png';
- }
- return path_to_url($val);
- }
- public function setCoverAttribute($val)
- {
- $this->attributes['cover'] = url_to_path($val);
- }
- public static function byIdGetOrder($id)
- {
- return self::query()->where('id', $id)->select(['id', 'name', 'nums', 'jifen'])->where('status', ModelStatusEnum::OK)->first();
- }
- public static function updateKuCun($id, $nums)
- {
- Cache::lock("lock:shopOrder:good:{$id}")->get(function () use ($id, $nums) {
- Cache::put("model:good:nums:{$id}", $nums);
- });
- }
- public static function reloadKuCun($good_id = 0)
- {
- $goods = self::query()->when($good_id, function ($query) use ($good_id) {
- return $query->where('id', $good_id);
- })->where('status', ModelStatusEnum::OK)->select(['id', 'nums'])->get();
- foreach ($goods as $good) {
- self::updateKuCun($good->id, $good->nums);
- }
- }
- }
|