DiyCommon.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. namespace app\jobs\storeSync;
  3. use app\models\StoreSyncExtLog;
  4. //店铺同步公用部分
  5. class DiyCommon
  6. {
  7. /**
  8. * 事件检测
  9. * @param $from_store_id integer 源店铺
  10. * @param $type string 事件类型
  11. * @param $params array 所需参数一般为id
  12. */
  13. public function JobBehaviors($from_store_id, $type, $params = [])
  14. {
  15. //当前店铺是源店铺
  16. $storeSyncLock = \app\models\StoreSyncLock::find()->where(['from_store_id' => $from_store_id, 'is_delete' => 0])
  17. ->select('to_store_id, imports')->asArray()->all();
  18. if ($storeSyncLock && in_array($type, StoreSyncExtLog::TYPE_ARR)) {
  19. //重构格式 将同一个商城的合并
  20. //[
  21. //'to_store_id' => imports//数组
  22. //]
  23. $importsArr = [];
  24. foreach ($storeSyncLock as $lock_item) {
  25. $importsArr[$lock_item['to_store_id']] = [];
  26. foreach ($storeSyncLock as $lock_import_item) {
  27. $imports = json_decode($lock_import_item['imports'], true);
  28. if ($lock_item['to_store_id'] === $lock_import_item['to_store_id']) {
  29. $importsArr[$lock_item['to_store_id']] = array_merge($importsArr[$lock_item['to_store_id']], $imports);
  30. }
  31. }
  32. }
  33. debug_log(['importsArr' => $importsArr, 'type' => $type], 'DeliveryRulesJob.log');
  34. $to_store_id = [];
  35. //获取到可以执行当前事件的store_id
  36. foreach ($importsArr as $import_store => $import_item) {
  37. if (in_array($type, $import_item)) {
  38. array_push($to_store_id, $import_store);
  39. }
  40. }
  41. //制作缓存key
  42. sort($to_store_id);
  43. sort($params);
  44. $cache = md5($type . implode(',', $to_store_id) . implode(',', $params) . $from_store_id);
  45. $id = \Yii::$app->cache->get($cache);
  46. if ($id && !\Yii::$app->queue->isDone($id)) {//\Yii::$app->queue->status($id)
  47. \Yii::$app->queue->remove($id);
  48. }
  49. $id = 0;
  50. switch ($type) {
  51. case StoreSyncExtLog::TYPE_ACTIVITY_CUT_PRICE:
  52. $id = \queue_push(new \app\jobs\storeSync\CutPriceActivityJob(['activity_id' => $params, 'to_store_id' => $to_store_id, 'from_store_id' => $from_store_id]));
  53. break;
  54. case StoreSyncExtLog::TYPE_DIY:
  55. $id = \queue_push(new \app\jobs\storeSync\DiyJob(['diy_id' => $params, 'to_store_id' => $to_store_id, 'from_store_id' => $from_store_id]));
  56. break;
  57. case StoreSyncExtLog::TYPE_PT:
  58. $id = \queue_push(new \app\jobs\storeSync\PtActivityJob(['pt_activity_id' => $params, 'to_store_id' => $to_store_id, 'from_store_id' => $from_store_id]));
  59. break;
  60. case StoreSyncExtLog::TYPE_SECKILL:
  61. $id = \queue_push(new \app\jobs\storeSync\SeckillActivityJob(['activity_id' => $params, 'to_store_id' => $to_store_id, 'from_store_id' => $from_store_id]));
  62. break;
  63. case StoreSyncExtLog::TYPE_SHARE_CONFIG:
  64. $id = \queue_push(new \app\jobs\storeSync\ShareConfigJob(['to_store_id' => $to_store_id, 'from_store_id' => $from_store_id, 'name' => $params]));
  65. break;
  66. case StoreSyncExtLog::TYPE_SHARE_HOLDER_CONFIG:
  67. //$id = \queue_push(new \app\jobs\storeSync\ShareHolderConfigJob(['to_store_id' => $to_store_id, 'from_store_id' => $from_store_id]));
  68. break;
  69. case StoreSyncExtLog::TYPE_VIDEO_GOODS:
  70. $id = \queue_push(new \app\jobs\storeSync\VideoGoodsJob(['to_store_id' => $to_store_id, 'from_store_id' => $from_store_id]));
  71. break;
  72. case StoreSyncExtLog::TYPE_INTEGRAL_STORE:
  73. $id = \queue_push(new \app\jobs\storeSync\IntegralStoreJob(['to_store_id' => $to_store_id, 'from_store_id' => $from_store_id]));
  74. break;
  75. case StoreSyncExtLog::TYPE_WORKER_CONFIG_GOODS:
  76. $id = \queue_push(new \app\jobs\storeSync\WorkerConfigJob(['to_store_id' => $to_store_id, 'from_store_id' => $from_store_id]));
  77. break;
  78. case StoreSyncExtLog::TYPE_BOOKING_GOODS:
  79. $id = \queue_push(new \app\jobs\storeSync\BookingHomeJob(['to_store_id' => $to_store_id, 'from_store_id' => $from_store_id]));
  80. break;
  81. case StoreSyncExtLog::TYPE_ARTICLE:
  82. $id = \queue_push(new \app\jobs\storeSync\ArticleSettingJob(['to_store_id' => $to_store_id, 'from_store_id' => $from_store_id]));
  83. break;
  84. case StoreSyncExtLog::TYPE_TOPIC:
  85. $id = \queue_push(new \app\jobs\storeSync\TopicSettingJob(['to_store_id' => $to_store_id, 'from_store_id' => $from_store_id]));
  86. break;
  87. case StoreSyncExtLog::TYPE_POSTAGE_RULES:
  88. $id = \queue_push(new \app\jobs\storeSync\DeliveryRulesJob(['to_store_id' => $to_store_id, 'from_store_id' => $from_store_id, 'delivery_rules_id' => $params]));
  89. break;
  90. case StoreSyncExtLog::TYPE_PRODUCT_CAT:
  91. $id = \queue_push(new \app\jobs\storeSync\GoodsCatJob(['to_store_id' => $to_store_id, 'from_store_id' => $from_store_id, 'cat_id' => $params]));
  92. break;
  93. default:
  94. $id = \queue_push(new \app\jobs\storeSync\GoodsJob(['goods_id' => $params, 'to_store_id' => $to_store_id, 'from_store_id' => $from_store_id]));
  95. break;
  96. }
  97. debug_log($id, 'queuePush.log');
  98. if ($id) {
  99. \Yii::$app->cache->set($cache, $id);
  100. }
  101. }
  102. }
  103. }