ProductBatchJob.php 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace app\jobs\product;
  12. use app\services\product\product\StoreProductBatchProcessServices;
  13. use crmeb\basic\BaseJobs;
  14. use crmeb\traits\QueueTrait;
  15. /**
  16. * 商品批量任务队列
  17. * Class ProductBatchJob
  18. * @package app\jobs\product
  19. */
  20. class ProductBatchJob extends BaseJobs
  21. {
  22. use QueueTrait;
  23. /**
  24. * @return mixed
  25. */
  26. public static function queueName()
  27. {
  28. $default = config('queue.default');
  29. return config('queue.connections.' . $default . '.batch_queue');
  30. }
  31. /**
  32. * 商品批量队列
  33. * @param $type
  34. * @param $ids
  35. * @param $data
  36. * @param $isBatch
  37. * @return bool
  38. */
  39. public function productBatch($type, $ids, $data, $isBatch = false)
  40. {
  41. if (!$type || !$ids || !$data) {
  42. return true;
  43. }
  44. //是否批量多个修改
  45. if ($isBatch) {
  46. $length = 30;
  47. } else {
  48. $length = 100;
  49. }
  50. //拆分大数组 分批加入二级队列
  51. $idsArr = array_chunk($ids, $length);
  52. foreach ($idsArr as $ids) {
  53. //加入分批队列
  54. self::dispatchDo('chunkProductBatch', [$type, $ids, $data, $isBatch]);
  55. }
  56. return true;
  57. }
  58. /**
  59. * 拆分分批队列
  60. * @param $type
  61. * @param $ids
  62. * @param $data
  63. * @param $isBatch
  64. * @return bool
  65. */
  66. public function chunkProductBatch($type, $ids, $data, $isBatch = false)
  67. {
  68. if (!$type || !$ids || !$data) {
  69. return true;
  70. }
  71. //是否批量多个修改
  72. if ($isBatch) {
  73. self::dispatchDo('runProductBatch', [$type, $ids, $data]);
  74. } else {//拆分id,单个队列执行
  75. foreach ($ids as $id) {
  76. self::dispatchDo('runProductBatch', [$type, $id, $data]);
  77. }
  78. }
  79. return true;
  80. }
  81. /**
  82. * 实际执行商品操作队列
  83. * @param $type
  84. * @param $ids
  85. * @param $data
  86. * @return bool
  87. */
  88. public function runProductBatch($type, $id, $data)
  89. {
  90. if (!is_array($id)) {
  91. $id = (int)$id;
  92. }
  93. if (!$type || !$id || !$data) {
  94. return true;
  95. }
  96. try {
  97. /** @var StoreProductBatchProcessServices $batchProcessServices */
  98. $batchProcessServices = app()->make(StoreProductBatchProcessServices::class);
  99. switch ($type) {
  100. case 1://分类
  101. $batchProcessServices->setPrdouctCate($id, $data);
  102. break;
  103. case 4://购买即送积分、优惠券
  104. $batchProcessServices->setGiveIntegralCoupon($id, $data);
  105. break;
  106. case 2://商品标签
  107. case 3://物流设置
  108. case 5://关联用户标签
  109. case 6://活动推荐
  110. case 7://自定义留言
  111. case 8://运费设置
  112. case 9://商品展示
  113. $batchProcessServices->runBatch($id, $data, (int)$type);
  114. break;
  115. default:
  116. break;
  117. }
  118. } catch (\Throwable $e) {
  119. response_log_write([
  120. 'message' => '批量操作商品,type:' . $type . ';状态失败' . ';参数:' . json_encode(['id' => $id, 'data' => $data]) . ',失败原因:' . $e->getMessage(),
  121. 'file' => $e->getFile(),
  122. 'line' => $e->getLine()
  123. ]);
  124. }
  125. return true;
  126. }
  127. }