BatchHandleJob.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  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;
  12. use app\services\other\queue\QueueServices;
  13. use app\services\product\product\StoreProductServices;
  14. use app\services\product\sku\StoreProductAttrServices;
  15. use crmeb\basic\BaseJobs;
  16. use crmeb\traits\QueueTrait;
  17. use think\facade\Log;
  18. /**
  19. * 批量任务队列
  20. * Class BatchHandleJob
  21. * @package app\jobs
  22. */
  23. class BatchHandleJob extends BaseJobs
  24. {
  25. use QueueTrait;
  26. /**
  27. * @return mixed
  28. */
  29. public static function queueName()
  30. {
  31. $default = config('queue.default');
  32. return config('queue.connections.' . $default . '.batch_queue');
  33. }
  34. /**
  35. * 批量任务队列
  36. * @param false $data
  37. * @param $type
  38. * @param array $other
  39. * @return bool
  40. * @throws \think\db\exception\DataNotFoundException
  41. * @throws \think\db\exception\DbException
  42. * @throws \think\db\exception\ModelNotFoundException
  43. */
  44. public function doJob($data = false, $type, array $other = [])
  45. {
  46. /** @var QueueServices $queueServices */
  47. $queueServices = app()->make(QueueServices::class);
  48. $re = true;
  49. try {
  50. switch ($type) {
  51. case 1://批量发放优惠券
  52. if (!$data) {
  53. return true;
  54. }
  55. $re = $queueServices->sendCoupon($data, $type);
  56. break;
  57. case 2://批量设置用户分组
  58. if (!$data) {
  59. return true;
  60. }
  61. $re = $queueServices->setUserGroup($data, $type);
  62. break;
  63. case 3://批量设置用户标签
  64. if (!$data) {
  65. return true;
  66. }
  67. $re = $queueServices->setUserLabel($data, $type, $other);
  68. break;
  69. case 4://批量上下架商品
  70. $re = $queueServices->setProductShow($data, $type);
  71. break;
  72. case 5://批量删除商品规格
  73. $re = $queueServices->delProductRule($type);
  74. break;
  75. case 6://批量删除用户已删除订单
  76. $re = $queueServices->delOrder($type);
  77. break;
  78. case 7://批量手动发货
  79. case 8://批量电子面单发货
  80. case 9://批量配送
  81. case 10://批量虚拟发货
  82. $re = $queueServices->orderDelivery($data, $other);
  83. break;
  84. default:
  85. $re = false;
  86. break;
  87. }
  88. } catch (\Throwable $e) {
  89. $queueName = $queueServices->queue_type_name[$type] ?? '';
  90. Log::error($queueName . '失败,原因' . $e->getMessage());
  91. $re = false;
  92. }
  93. if ($re === false) $queueServices->delWrongQueue(0, $type, false);
  94. //清除缓存
  95. /** @var StoreProductServices $productService */
  96. $productService = app()->make(StoreProductServices::class);
  97. /** @var StoreProductAttrServices $productAttrService */
  98. $productAttrService = app()->make(StoreProductAttrServices::class);
  99. $productService->cacheTag()->clear();
  100. $productAttrService->cacheTag()->clear();
  101. return true;
  102. }
  103. }