ProductStockTips.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  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\StoreProductServices;
  13. use app\services\product\sku\StoreProductAttrValueServices;
  14. use app\webscoket\SocketPush;
  15. use crmeb\basic\BaseJobs;
  16. use crmeb\traits\QueueTrait;
  17. /**
  18. * 商品库存警戒提示
  19. * Class ProductStockTips
  20. * @package app\jobs\product
  21. */
  22. class ProductStockTips extends BaseJobs
  23. {
  24. use QueueTrait;
  25. public function doJob($productId, $send = 1)
  26. {
  27. /** @var StoreProductServices $make */
  28. $make = app()->make(StoreProductServices::class);
  29. $product = $make->get(['id' => $productId], ['stock', 'id', 'is_police', 'is_sold']);
  30. $store_stock = sys_config('store_stock') ?? 0;//库存预警界限
  31. /** @var StoreProductAttrValueServices $storeValueService */
  32. $storeValueService = app()->make(StoreProductAttrValueServices::class);
  33. $count = $storeValueService->getPolice([
  34. ['type', '=', 0],
  35. ['stock', '<=', $store_stock],
  36. ['product_id', '=', $productId]
  37. ]);
  38. $product->is_sold = $storeValueService->value(['type' => 0, 'product_id' => $productId], 'stock') === 0 ? 1 : 0;
  39. if ($store_stock >= $product['stock'] || $count) {
  40. $product->is_police = 1;
  41. if ($send) {
  42. try {
  43. SocketPush::admin()->type('STORE_STOCK')->data(['id' => $productId])->push();
  44. } catch (\Exception $e) {
  45. }
  46. }
  47. } else {
  48. $product->is_police = 0;
  49. }
  50. $product->save();
  51. return true;
  52. }
  53. }