| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144 |
- <?php
- namespace addons\Store\console\migrations;
- use addons\Store\common\models\Store;
- use addons\Store\common\models\StoreAssociatedUser;
- use addons\Store\common\models\StoreGoodsCate;
- use addons\Store\common\models\StoreScoreStoreApply;
- use common\enums\StatusEnum;
- use common\models\user\UserPartitionRelationship;
- use yii\db\Migration;
- use Yii;
- /**
- * Class M241206074356NewAlterScoreRulesField
- */
- class M241218184844FixStoreAssociatedUser extends Migration
- {
- /**
- * {@inheritdoc}
- */
- public function safeUp()
- {
- try {
- echo '初始化关联门店数据' . PHP_EOL;
- // 获取当前系统所有可用的 mall_id
- $malls = \common\models\mall\Mall::getEnableMallList();
- $mall_ids = array_column($malls, 'id');
- foreach ($mall_ids as $mall_id) {
- echo '初始化关联门店数据' . $mall_id . PHP_EOL;
- //需要安装O2O插件
- $is_install = \common\models\mall\MallAddons::isInstall($mall_id, \common\enums\AddonsEnum::ADDONS_NAME_STORE);
- if (!$is_install) continue;
- $store_list = Store::lists(['where' => [['mall_id' => $mall_id], ['status' => StatusEnum::ENABLED]],'select'=>'id,user_id', 'index' => 'user_id']);
- //遍历所有商城下的用户
- $model = \common\models\user\User::find()->select('id')->where(['mall_id' => $mall_id]);
- foreach ($model->asArray()->batch(500) as $user_list) {
- $insertData = [];
- foreach ($user_list as $user) {
- echo '用户ID' . $user['id'] . PHP_EOL;
- $insert = $this->setStoreAssociated([
- 'mall_id' => $mall_id,
- 'user_id' => $user['id'],
- ], $store_list);
- if (!empty($insert)) $insertData[] = $insert;
- }
- !empty($insertData) && Yii::$app->db->createCommand()->batchInsert(StoreAssociatedUser::tableName(), array_keys($insertData[0]), $insertData)->execute();
- }
- }
- return true;
- } catch (\Exception $e) {
- var_dump($e->getMessage());
- return false;
- }
- }
- public function setStoreAssociated($params, $store_list)
- {
- $store_id = $insert_data = null;
- $user_id = $params['user_id'];
- $mall_id = $params['mall_id'];
- $store_associated_user = StoreAssociatedUser::findOne(['mall_id' => $mall_id, 'user_id' => $user_id, 'status' => StatusEnum::ENABLED]);
- if (!empty($store_associated_user)) {
- return [];
- }
- //没有绑定关联门店,根据用户最近上级的店长进行自动绑定门店
- $user_parent_arr = UserPartitionRelationship::getParentIdArr($user_id);
- $user_parent_arr[] = $user_id;
- $user_parent_arr = array_reverse($user_parent_arr);
- foreach ($user_parent_arr as $item) {
- // $manager = Store::findOne(['mall_id' => $mall_id, 'user_id' => $item, 'status' => StatusEnum::ENABLED]);
- if (isset($store_list[$item])) {
- $store_id = $store_list[$item]['id'];
- break;
- }
- }
- if (empty($store_id)) {
- //查直属上级绑定的店
- $user = \common\models\user\User::findOne([
- ['id' => $user_id]
- ]);
- if ($user->parent_id > 0) {
- $parent_store_associated_user = StoreAssociatedUser::findOne(['mall_id' => $mall_id, 'user_id' => $user->parent_id, 'status' => StatusEnum::ENABLED]);
- if ($parent_store_associated_user) {
- $store_id = $parent_store_associated_user->store_id;
- }
- }
- }
- !empty($store_id) && $insert_data = [
- 'mall_id' => $mall_id,
- 'store_id' =>$store_id,
- 'user_id' => $user_id,
- 'created_at' => time(),
- 'updated_at' => time(),
- ];
- if (!empty($insert_data)) {
- return $insert_data;
- // StoreAssociatedUser::setData($insert_data);
- }
- return [];
- }
- /**
- * {@inheritdoc}
- */
- public function safeDown()
- {
- echo "M241218184844FixStoreAssociatedUser cannot be reverted.\n";
- return false;
- }
- /*
- // Use up()/down() to run migration code without a transaction.
- public function up()
- {
- }
- public function down()
- {
- echo "M241206074356NewAlterScoreRulesField cannot be reverted.\n";
- return false;
- }
- */
- }
|