海南旅游SAAS
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.

114 lines
3.5 KiB

  1. <?php
  2. namespace App\Jobs;
  3. use App\Models\AgentProduct;
  4. use App\Models\AgentProductSpec;
  5. use App\Models\ProductSpec;
  6. use Illuminate\Bus\Queueable;
  7. use Illuminate\Contracts\Queue\ShouldBeUnique;
  8. use Illuminate\Contracts\Queue\ShouldQueue;
  9. use Illuminate\Foundation\Bus\Dispatchable;
  10. use Illuminate\Queue\InteractsWithQueue;
  11. use Illuminate\Queue\SerializesModels;
  12. use Illuminate\Support\Facades\DB;
  13. class ProductSpecSync implements ShouldQueue
  14. {
  15. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  16. /**
  17. * 任务可尝试的次数
  18. * @var int
  19. */
  20. public $tries = 3;
  21. /**
  22. * 任务失败前允许的最大异常数
  23. * @var int
  24. */
  25. public $maxExceptions = 3;
  26. /**
  27. * Create a new job instance.
  28. *
  29. * @return void
  30. */
  31. public function __construct(private int $product_id)
  32. {
  33. //
  34. }
  35. /**
  36. * Execute the job.
  37. *
  38. * @return void
  39. */
  40. public function handle()
  41. {
  42. //获取当前供应商产品的所有规格
  43. $product_specs = ProductSpec::where('product_id', $this->product_id)->get();
  44. //获取当前供应商产品的所有代理商产品ID
  45. AgentProduct::query()
  46. ->where('product_id', $this->product_id)
  47. ->chunk(5, function ($agent_products) use ($product_specs) {
  48. //当前供应商产品所有规格的ID
  49. $product_spec_ids = $product_specs->pluck('id')->toArray();
  50. //删除供应商已经删除的规格
  51. AgentProductSpec::query()
  52. ->whereIn('agent_product_id', $agent_products->pluck('id')->toArray())
  53. ->whereNotIn('product_spec_id', $product_spec_ids)
  54. ->delete();
  55. //将代理商规格中销售价小于供应商销售价的,将代理商的销售价设置为供应商的销售价
  56. foreach ($product_specs as $spec) {
  57. AgentProductSpec::query()
  58. ->where([['product_spec_id', '=', $spec->id], ['price', '<', $spec->price]])
  59. ->update([
  60. 'price' => $spec->price,
  61. 'stock' => DB::raw("IF(`stock` > {$spec->stock}, {$spec->stock}, `stock`)"),
  62. 'original_price' => DB::raw("IF(`original_price` < {$spec->original_price}, {$spec->original_price}, `original_price`)"),
  63. ]);
  64. }
  65. //供应商新增,但代理商还没有的规格,同步给代理商
  66. foreach ($agent_products as $ap) {
  67. //代理商已有的规格(如果代理商删除过的规格不再同步过来,这里加上withTrashed即可)
  68. $ap_spec_ids = AgentProductSpec::where('agent_product_id', $ap->id)->pluck('product_spec_id')->toArray();
  69. //代理商没有的规格
  70. $agent_no_spec_ids = array_diff($product_spec_ids, $ap_spec_ids);
  71. AgentProductSpec::query()
  72. ->insert(
  73. array_map(function ($v) use ($ap, $product_specs) {
  74. $product_spec = $product_specs->find($v);
  75. return [
  76. 'agent_product_id' => $ap->id,
  77. 'product_spec_id' => $v,
  78. 'name' => '',
  79. 'date' => null,
  80. 'stock' => $product_spec->stock,
  81. 'original_price' => $product_spec->original_price,
  82. 'price' => $product_spec->price,
  83. ];
  84. }, $agent_no_spec_ids)
  85. );
  86. //设置产品的销售价(列表页显示的价格)
  87. $psArr = $product_specs->toArray();
  88. $ap->price = min(array_column($psArr, 'price'));
  89. $ap->original_price = min(array_column($psArr, 'original_price'));
  90. //如果代理商库存大于供应商库存,将代理商库存修改为供应商的库存
  91. $pStock = array_sum(array_column($psArr, 'stock'));
  92. if ($ap->stock > $pStock) {
  93. $ap->stock = $pStock;
  94. }
  95. $ap->save();
  96. }
  97. });
  98. }
  99. }