ShareHolderConfigJob.php 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. <?php
  2. namespace app\jobs\storeSync;
  3. use app\models\ShareHolderLevel;
  4. use app\models\StoreSyncExtLog;
  5. use yii\base\BaseObject;
  6. use yii\queue\JobInterface;
  7. //股东
  8. class ShareHolderConfigJob extends BaseObject implements JobInterface
  9. {
  10. public array $activity_id;
  11. public array $to_store_id;
  12. public int $from_store_id;
  13. public function execute($queue)
  14. {
  15. // TODO: Implement execute() method.
  16. $from_store_id = $this->from_store_id;
  17. $to_store_id = $this->to_store_id;
  18. foreach ($to_store_id as $store_id_item) {
  19. //等级同步
  20. $level_result = $this->shareHolderLevelConfig($from_store_id, $store_id_item);
  21. }
  22. }
  23. //等级同步
  24. private function shareHolderLevelConfig($from_store_id, $to_store_id) {
  25. try {
  26. $shareHolderLevelList = ShareHolderLevel::find()->where(['store_id' => $from_store_id, 'is_delete' => 0])->orderBy(['level' => 'ASC'])->asArray()->all();
  27. $level_cache = [];
  28. foreach ($shareHolderLevelList as $item) {
  29. $share_holder_level = ShareHolderLevel::findOne(['store_id' => $to_store_id, 'is_delete' => 0, 'level' => $item['level']]);
  30. if (!$share_holder_level) {
  31. $share_holder_level = new ShareHolderLevel();
  32. }
  33. $share_holder_level_id = $item['id'];
  34. unset($item['id']);
  35. $share_holder_level->attributes = $item;
  36. $share_holder_level->store_id = $to_store_id;
  37. if ($share_holder_level->save()) {
  38. $level_cache[$item['id']] = $share_holder_level->id;
  39. }
  40. $condition = json_decode($share_holder_level->condition, true);
  41. if ($condition) {
  42. foreach ($condition as $condition_index => $condition_item) {
  43. //商品升级
  44. if ($condition_index === 'goods') {
  45. if ($condition_item['value']['id']) {
  46. $id = [];
  47. if ($condition_item['value']['id']) {
  48. if (is_array($condition_item['value']['id'])) {
  49. foreach ($condition_item['value']['id'] as $goods_id_item) {
  50. $storeSyncExtLog = StoreSyncExtLog::findOne([
  51. 'from_store_id' => $from_store_id,
  52. 'to_store_id' => $to_store_id,
  53. 'type' => StoreSyncExtLog::TYPE_PRODUCT,
  54. 'from_id' => $goods_id_item
  55. ]);
  56. if ($storeSyncExtLog && !$storeSyncExtLog->to_id) {
  57. continue;
  58. }
  59. $id[] = $storeSyncExtLog->to_id;
  60. }
  61. } else {
  62. $id = [$condition_item['value']['id']];
  63. }
  64. }
  65. $condition[$condition_index]['value']['id'] = $id;
  66. }
  67. }
  68. if ($condition_index === 'shareholder') {
  69. if ($condition_item['from_level_id']) {
  70. $condition[$condition_index]['from_level_id'] = $level_cache[$condition_item['from_level_id']];
  71. }
  72. if ($condition_item['to_level_id']) {
  73. $condition[$condition_index]['to_level_id'] = $level_cache[$condition_item['to_level_id']];
  74. }
  75. }
  76. }
  77. }
  78. $share_holder_level->condition = json_encode($condition, JSON_UNESCAPED_UNICODE);
  79. $share_holder_level->save();
  80. (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $share_holder_level_id, $share_holder_level->id, StoreSyncExtLog::TYPE_SHARE_HOLDER_CONFIG);
  81. }
  82. return [
  83. 'code' => 0,
  84. 'msg' => '同步成功',
  85. 'data' => [
  86. 'level_cache' => $level_cache
  87. ]
  88. ];
  89. } catch (\Exception $e) {
  90. return [
  91. 'code' => 1,
  92. 'msg' => $e->getMessage()
  93. ];
  94. }
  95. }
  96. }