SetGoodsPriceJob.php 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. <?php
  2. namespace app\Jobs;
  3. use app\common\facades\Setting;
  4. use app\common\models\Goods;
  5. use Illuminate\Bus\Queueable;
  6. use Illuminate\Contracts\Queue\ShouldQueue;
  7. use Illuminate\Queue\InteractsWithQueue;
  8. use Illuminate\Queue\SerializesModels;
  9. class SetGoodsPriceJob implements ShouldQueue
  10. {
  11. use InteractsWithQueue, Queueable, SerializesModels;
  12. public $goodsIds;
  13. public $uniacid;
  14. public function __construct($goodsIds,$uniacid)
  15. {
  16. $this->goodsIds = $goodsIds;
  17. $this->uniacid = $uniacid;
  18. }
  19. public function handle()
  20. {
  21. if (!$this->goodsIds) {
  22. return;
  23. }
  24. Setting::$uniqueAccountId = $this->uniacid;
  25. \YunShop::app()->uniacid = $this->uniacid;
  26. $goods = Goods::uniacid()->whereIn('id',$this->goodsIds)
  27. ->with(['hasManyOptions' => function ($options) {
  28. $options->select('id','goods_id','product_price');
  29. }])
  30. ->get();
  31. $goods->map(function (Goods $good) {
  32. if ($good->has_option && !$good->hasManyOptions->isEmpty()) {//开启规格
  33. $good->min_price = $good->hasManyOptions->min('product_price');
  34. $good->max_price = $good->hasManyOptions->max('product_price');
  35. } else {
  36. $good->min_price = $good->price;
  37. $good->max_price = $good->price;
  38. }
  39. $good->save();
  40. });
  41. }
  42. }