ProductSyncStoreJob.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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\branch\StoreBranchProductServices;
  13. use app\services\product\product\StoreProductServices;
  14. use crmeb\basic\BaseJobs;
  15. use crmeb\traits\QueueTrait;
  16. use think\facade\Log;
  17. /**
  18. * 商品同步到门店
  19. * Class ProductSyncStoreJob
  20. * @package app\jobs\product
  21. */
  22. class ProductSyncStoreJob extends BaseJobs
  23. {
  24. use QueueTrait;
  25. /**
  26. * 同步某个商品到某个门店
  27. * @param $product_id
  28. * @param $store_id
  29. * @return bool
  30. */
  31. public function syncProduct($product_id, $store_id)
  32. {
  33. $product_id = (int)$product_id;
  34. $store_id = (int)$store_id;
  35. if (!$product_id || !$store_id) {
  36. return true;
  37. }
  38. try {
  39. /** @var StoreBranchProductServices $storeBranchProductServices */
  40. $storeBranchProductServices = app()->make(StoreBranchProductServices::class);
  41. $storeBranchProductServices->syncProduct($product_id, $store_id);
  42. } catch (\Throwable $e) {
  43. Log::error('同步商品到门店发生错误,错误原因:' . $e->getMessage());
  44. }
  45. return true;
  46. }
  47. /**
  48. * 新增门店:同步平台商品(选择多个,或者所有)
  49. * @param $store_id
  50. * @param $product_id
  51. * @return bool
  52. */
  53. public function syncProducts($store_id, $product_id = [])
  54. {
  55. if (!$store_id) {
  56. return true;
  57. }
  58. try {
  59. $where = ['is_show' => 1, 'is_del' => 0, 'type' => 0, 'product_type' => [0, 4], 'is_verify' => 1, 'pid' => 0];
  60. if ($product_id) {//同步某些商品
  61. $where['id'] = is_array($product_id) ? $product_id : explode(',', $product_id);
  62. }
  63. /** @var StoreProductServices $productServices */
  64. $productServices = app()->make(StoreProductServices::class);
  65. $products = $productServices->getSearchList($where, 0, 0, ['id'], '', []);
  66. if ($products) {
  67. $productIds = array_column($products, 'id');
  68. foreach ($productIds as $id) {
  69. ProductSyncStoreJob::dispatchDo('syncProduct', [$id, $store_id]);
  70. }
  71. }
  72. } catch (\Throwable $e) {
  73. Log::error('同步商品到门店发生错误,错误原因:' . $e->getMessage());
  74. }
  75. return true;
  76. }
  77. /**
  78. * 同步一个商品到多个门店
  79. * @param $product_id
  80. * @param $store_ids
  81. * @return bool
  82. */
  83. public function syncProductToStores($product_id, $applicable_type = 0, $store_ids = [])
  84. {
  85. $product_id = (int)$product_id;
  86. if (!$product_id) {
  87. return true;
  88. }
  89. try {
  90. if ($store_ids) {//同步门店
  91. $store_ids = is_array($store_ids) ? $store_ids : explode(',', $product_id);
  92. }
  93. /** @var StoreBranchProductServices $storeBranchProductServices */
  94. $storeBranchProductServices = app()->make(StoreBranchProductServices::class);
  95. $storeBranchProductServices->syncProductToStores($product_id, $applicable_type, $store_ids);
  96. } catch (\Throwable $e) {
  97. Log::error('同步商品到门店发生错误,错误原因:' . $e->getMessage());
  98. }
  99. return true;
  100. }
  101. }