ProductStockJob.php 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2023 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\product\product\OutStoreProductServices;
  13. use crmeb\basic\BaseJobs;
  14. use crmeb\traits\QueueTrait;
  15. use think\facade\Log;
  16. class ProductStockJob extends BaseJobs
  17. {
  18. use QueueTrait;
  19. /**
  20. * 拆分计算
  21. * @param array $data
  22. * @return bool
  23. */
  24. public function distribute(array $data): bool
  25. {
  26. try {
  27. foreach ($data as $key => $item) {
  28. ProductStockJob::dispatch('calcValueStock', [$key]);
  29. }
  30. } catch (\Exception $e) {
  31. Log::error(['msg' => '拆分计算失败,错误原因:' . $e->getMessage(), 'data' => $data]);
  32. }
  33. return true;
  34. }
  35. /**
  36. * 计算库存
  37. * @param int $id
  38. * @return bool
  39. */
  40. public function calcValueStock(int $id): bool
  41. {
  42. try {
  43. /** @var OutStoreProductServices $services */
  44. $services = app()->make(OutStoreProductServices::class);
  45. $services->calcStockByAttrValue($id);
  46. } catch (\Exception $e) {
  47. Log::error(['msg' => '计算商品库存失败,错误原因:' . $e->getMessage(), 'data' => $id]);
  48. }
  49. return true;
  50. }
  51. }