ShopGood.php 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. namespace App\Repositories\Models\Dwbs;
  3. use App\Repositories\Enums\ModelStatusEnum;
  4. use App\Repositories\Models\Model;
  5. use Illuminate\Support\Facades\Cache;
  6. class ShopGood extends Model
  7. {
  8. /**
  9. * @var string
  10. */
  11. protected $table = 'dwbs_shop_goods';
  12. protected $guarded = [];
  13. /**
  14. * The attributes excluded from the model's JSON form.
  15. *
  16. * @var array
  17. */
  18. protected $hidden = [];
  19. protected $casts = [];
  20. public function good()
  21. {
  22. return $this->belongsTo(ShopGood::class)->select(['id', 'name', 'cover']);
  23. }
  24. public function getCoverAttribute($val)
  25. {
  26. if (empty($val)) {
  27. return config('app.url') . '/default/headimg.png';
  28. }
  29. return path_to_url($val);
  30. }
  31. public function setCoverAttribute($val)
  32. {
  33. $this->attributes['cover'] = url_to_path($val);
  34. }
  35. public static function byIdGetOrder($id)
  36. {
  37. return self::query()->where('id', $id)->select(['id', 'name', 'nums', 'jifen'])->where('status', ModelStatusEnum::OK)->first();
  38. }
  39. public static function updateKuCun($id, $nums)
  40. {
  41. Cache::lock("lock:shopOrder:good:{$id}")->get(function () use ($id, $nums) {
  42. Cache::put("model:good:nums:{$id}", $nums);
  43. });
  44. }
  45. public static function reloadKuCun($good_id = 0)
  46. {
  47. $goods = self::query()->when($good_id, function ($query) use ($good_id) {
  48. return $query->where('id', $good_id);
  49. })->where('status', ModelStatusEnum::OK)->select(['id', 'nums'])->get();
  50. foreach ($goods as $good) {
  51. self::updateKuCun($good->id, $good->nums);
  52. }
  53. }
  54. }