M241218184844FixStoreAssociatedUser.php 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. <?php
  2. namespace addons\Store\console\migrations;
  3. use addons\Store\common\models\Store;
  4. use addons\Store\common\models\StoreAssociatedUser;
  5. use addons\Store\common\models\StoreGoodsCate;
  6. use addons\Store\common\models\StoreScoreStoreApply;
  7. use common\enums\StatusEnum;
  8. use common\models\user\UserPartitionRelationship;
  9. use yii\db\Migration;
  10. use Yii;
  11. /**
  12. * Class M241206074356NewAlterScoreRulesField
  13. */
  14. class M241218184844FixStoreAssociatedUser extends Migration
  15. {
  16. /**
  17. * {@inheritdoc}
  18. */
  19. public function safeUp()
  20. {
  21. try {
  22. echo '初始化关联门店数据' . PHP_EOL;
  23. // 获取当前系统所有可用的 mall_id
  24. $malls = \common\models\mall\Mall::getEnableMallList();
  25. $mall_ids = array_column($malls, 'id');
  26. foreach ($mall_ids as $mall_id) {
  27. echo '初始化关联门店数据' . $mall_id . PHP_EOL;
  28. //需要安装O2O插件
  29. $is_install = \common\models\mall\MallAddons::isInstall($mall_id, \common\enums\AddonsEnum::ADDONS_NAME_STORE);
  30. if (!$is_install) continue;
  31. $store_list = Store::lists(['where' => [['mall_id' => $mall_id], ['status' => StatusEnum::ENABLED]],'select'=>'id,user_id', 'index' => 'user_id']);
  32. //遍历所有商城下的用户
  33. $model = \common\models\user\User::find()->select('id')->where(['mall_id' => $mall_id]);
  34. foreach ($model->asArray()->batch(500) as $user_list) {
  35. $insertData = [];
  36. foreach ($user_list as $user) {
  37. echo '用户ID' . $user['id'] . PHP_EOL;
  38. $insert = $this->setStoreAssociated([
  39. 'mall_id' => $mall_id,
  40. 'user_id' => $user['id'],
  41. ], $store_list);
  42. if (!empty($insert)) $insertData[] = $insert;
  43. }
  44. !empty($insertData) && Yii::$app->db->createCommand()->batchInsert(StoreAssociatedUser::tableName(), array_keys($insertData[0]), $insertData)->execute();
  45. }
  46. }
  47. return true;
  48. } catch (\Exception $e) {
  49. var_dump($e->getMessage());
  50. return false;
  51. }
  52. }
  53. public function setStoreAssociated($params, $store_list)
  54. {
  55. $store_id = $insert_data = null;
  56. $user_id = $params['user_id'];
  57. $mall_id = $params['mall_id'];
  58. $store_associated_user = StoreAssociatedUser::findOne(['mall_id' => $mall_id, 'user_id' => $user_id, 'status' => StatusEnum::ENABLED]);
  59. if (!empty($store_associated_user)) {
  60. return [];
  61. }
  62. //没有绑定关联门店,根据用户最近上级的店长进行自动绑定门店
  63. $user_parent_arr = UserPartitionRelationship::getParentIdArr($user_id);
  64. $user_parent_arr[] = $user_id;
  65. $user_parent_arr = array_reverse($user_parent_arr);
  66. foreach ($user_parent_arr as $item) {
  67. // $manager = Store::findOne(['mall_id' => $mall_id, 'user_id' => $item, 'status' => StatusEnum::ENABLED]);
  68. if (isset($store_list[$item])) {
  69. $store_id = $store_list[$item]['id'];
  70. break;
  71. }
  72. }
  73. if (empty($store_id)) {
  74. //查直属上级绑定的店
  75. $user = \common\models\user\User::findOne([
  76. ['id' => $user_id]
  77. ]);
  78. if ($user->parent_id > 0) {
  79. $parent_store_associated_user = StoreAssociatedUser::findOne(['mall_id' => $mall_id, 'user_id' => $user->parent_id, 'status' => StatusEnum::ENABLED]);
  80. if ($parent_store_associated_user) {
  81. $store_id = $parent_store_associated_user->store_id;
  82. }
  83. }
  84. }
  85. !empty($store_id) && $insert_data = [
  86. 'mall_id' => $mall_id,
  87. 'store_id' =>$store_id,
  88. 'user_id' => $user_id,
  89. 'created_at' => time(),
  90. 'updated_at' => time(),
  91. ];
  92. if (!empty($insert_data)) {
  93. return $insert_data;
  94. // StoreAssociatedUser::setData($insert_data);
  95. }
  96. return [];
  97. }
  98. /**
  99. * {@inheritdoc}
  100. */
  101. public function safeDown()
  102. {
  103. echo "M241218184844FixStoreAssociatedUser cannot be reverted.\n";
  104. return false;
  105. }
  106. /*
  107. // Use up()/down() to run migration code without a transaction.
  108. public function up()
  109. {
  110. }
  111. public function down()
  112. {
  113. echo "M241206074356NewAlterScoreRulesField cannot be reverted.\n";
  114. return false;
  115. }
  116. */
  117. }