ShareConfigJob.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. <?php
  2. namespace app\jobs\storeSync;
  3. use app\constants\OptionSetting;
  4. use app\models\Option;
  5. use app\models\Qrcode;
  6. use app\models\ShareLevel;
  7. use app\models\StoreSyncExtLog;
  8. use yii\base\BaseObject;
  9. use yii\db\Exception;
  10. use yii\queue\JobInterface;
  11. //分销
  12. class ShareConfigJob extends BaseObject implements JobInterface
  13. {
  14. public array $activity_id;
  15. public array $to_store_id;
  16. public int $from_store_id;
  17. public array $name;
  18. public function execute($queue)
  19. {
  20. // TODO: Implement execute() method.
  21. $from_store_id = $this->from_store_id;
  22. $to_store_id = $this->to_store_id;
  23. $name = $this->name[0];
  24. foreach ($to_store_id as $store_id_item) {
  25. //同步配置 基础配置 佣金配置 推广海报
  26. $result = $this->shareBasicConfig($from_store_id, $store_id_item, $name);
  27. //同步等级
  28. // $levelResult = $this->shareLevel($from_store_id, $store_id_item);
  29. }
  30. }
  31. //同步等级
  32. private function shareLevel($from_store_id, $to_store_id) {
  33. try {
  34. $shareLevelList = ShareLevel::find()->where(['store_id' => $from_store_id, 'is_delete' => 0])->asArray()->all();
  35. foreach ($shareLevelList as $item) {
  36. //判断是否为当前商城同步过此商品
  37. $share_level = ShareLevel::findOne(['store_id' => $to_store_id, 'is_delete' => 0, 'level' => $item['level']]);
  38. if (!$share_level) {
  39. $share_level = new ShareLevel();
  40. }
  41. $id = $item['id'];
  42. unset($item['id']);
  43. $share_level->attributes = $item;
  44. $share_level->store_id = $to_store_id;
  45. if (!$share_level->save()) {
  46. return [
  47. 'code' => 1,
  48. 'msg' => json_encode($share_level->errors, JSON_UNESCAPED_UNICODE)
  49. ];
  50. };
  51. (new StoreSyncExtLog())::handleData($from_store_id, $to_store_id, $id, $share_level->id, StoreSyncExtLog::TYPE_SHARE_CONFIG);
  52. }
  53. return [
  54. 'code' => 0,
  55. 'msg' => '同步成功'
  56. ];
  57. } catch (\Exception $e) {
  58. return [
  59. 'code' => 1,
  60. 'msg' => $e->getMessage()
  61. ];
  62. }
  63. }
  64. //同步配置 基础配置 佣金配置 推广海报
  65. private function shareBasicConfig($from_store_id, $to_store_id, $name) {
  66. try {
  67. if ($name === "share_basic_setting") {
  68. $share_basic_setting = Option::get('share_basic_setting', $from_store_id, OptionSetting::SHARE_GROUP_NAME)['value'];
  69. try {
  70. $share_basic_setting = json_decode($share_basic_setting, true);
  71. if (isset($share_basic_setting['share_goods_id']['value'])) {
  72. $ids = \explode(',', $share_basic_setting['share_goods_id']['value']);
  73. $arr = [];
  74. foreach ($ids as $id) {
  75. $storeSyncExtLog = StoreSyncExtLog::findOne([
  76. 'from_store_id' => $from_store_id,
  77. 'to_store_id' => $to_store_id,
  78. 'type' => StoreSyncExtLog::TYPE_PRODUCT,
  79. 'from_id' => $id,
  80. ]);
  81. if ($storeSyncExtLog) {
  82. $arr[] = $storeSyncExtLog->to_id;
  83. }
  84. }
  85. $share_basic_setting['share_goods_id']['value'] = \implode(',', $arr);
  86. }
  87. $share_basic_setting = json_encode($share_basic_setting, JSON_UNESCAPED_UNICODE);
  88. Option::set('share_basic_setting', $share_basic_setting, $to_store_id, OptionSetting::SHARE_GROUP_NAME);
  89. } catch (Exception $e) {
  90. }
  91. }
  92. if ($name === "share_money_setting") {
  93. $share_basic_setting = Option::get('share_money_setting', $from_store_id, OptionSetting::SHARE_GROUP_NAME)['value'];
  94. try {
  95. Option::set('share_money_setting', $share_basic_setting, $to_store_id, OptionSetting::SHARE_GROUP_NAME);
  96. } catch (Exception $e) {
  97. }
  98. }
  99. if ($name === "share_qrcode") {
  100. $qrcode = Qrcode::findOne(['store_id' => $from_store_id, 'is_delete' => 0, 'type' => Qrcode::TYPE_SHARE]);
  101. $to_qrcode = Qrcode::findOne(['store_id' => $to_store_id, 'is_delete' => 0, 'type' => Qrcode::TYPE_SHARE]);
  102. if (!$to_qrcode) {
  103. $to_qrcode = new Qrcode();
  104. }
  105. unset($qrcode['id']);
  106. $to_qrcode->attributes = $qrcode->attributes;
  107. $to_qrcode->store_id = $to_store_id;
  108. $to_qrcode->created_at = time();
  109. $to_qrcode->updated_at = time();
  110. if (!$to_qrcode->save()) {
  111. return [
  112. 'code' => 1,
  113. 'msg' => json_encode($to_qrcode->errors, JSON_UNESCAPED_UNICODE)
  114. ];
  115. }
  116. }
  117. return [
  118. 'code' => 0,
  119. 'msg' => '操作成功'
  120. ];
  121. } catch(\Exception $e) {
  122. return [
  123. 'code' => 1,
  124. 'msg' => $e->getMessage()
  125. ];
  126. }
  127. }
  128. /** 同步分销end **/
  129. }