From f540d5984146273f24802eedf98bcbe3ce4ddabe Mon Sep 17 00:00:00 2001 From: azizi Date: Thu, 17 Sep 2020 18:15:09 +0430 Subject: [PATCH] transfar user from temp_businesses to users and more tables --- app/Console/Commands/convertUsers.php | 250 ++++++++++++++++++++++++++ 1 file changed, 250 insertions(+) create mode 100644 app/Console/Commands/convertUsers.php diff --git a/app/Console/Commands/convertUsers.php b/app/Console/Commands/convertUsers.php new file mode 100644 index 0000000..2e473c4 --- /dev/null +++ b/app/Console/Commands/convertUsers.php @@ -0,0 +1,250 @@ +get(); + return $data; + } + + /** + * Insert a row in table users + */ + private function insertRowToUsers($data) + { + Database::table('users')->insertOrIgnore($data); + $lastUser = Database::select('SELECT * FROM `users` WHERE id=(SELECT max(id) FROM `users`)'); + + return $lastUser[0]->id; + } + + + /** + * Insert to wmuser_common phone + */ + private function insertPhone($phones,$timestamp,$userId) + { + $phones = json_decode($phones); + + foreach($phones as $phone) + { + + Database::table('wmuser_common.phones')->insertOrIgnore([ + 'phonable_id' => $userId, + 'number' => $phone->tel, + 'label' => $phone->label, + 'phonable_type' => 'user', + 'created_at' => $timestamp['created_at'], + 'updated_at' => $timestamp['updated_at'], + ]); + + } + + } + + + /** + * Insert address + */ + + private function InsertAddress($addressData,$tmp_id, $userId) + { + $userAddress = Database::select( + 'select * from `addresses` where addressable_id=? and addressable_type =?', + [ + $tmp_id, + 'App\TempBusiness' + ] + ); + + foreach ($userAddress as $address) + { + Database::table('wmuser_common.addresses')->insertOrIgnore([ + 'title' => $address->title, + 'city_id' => $addressData['city_id'], + 'district_id' => $addressData['district_id'], + 'postal_code' => $address->postal_code, + 'addressable_id' => $userId, + 'addressable_type' => 'user', + 'created_at' => Carbon::now(), + 'updated_at' => Carbon::now() + ]); + } + + + } + + + + /** + * Delete from table temp_businesses + */ + private function deleteTempBusiness() + { + Database::table('temp_businesses')->delete(); + } + + + /** + * insert Status to client status + * + */ + private function insertToClientStatuses($clientStatus) + { + + Database::table('wmuser_crm.client_statuses')->insertOrIgnore([ + 'name' => $clientStatus, + 'user_id' => 52, + 'business_id' => 3, + 'created_at' => Carbon::now(), + 'updated_at' => Carbon::now() + ]); + + } + + + /** + * Insert categories with Id + */ + private function categoriesConfig($categoryId, $client_id) + { + + $category = Database::select('select * from `categories` where id=? ' , [$categoryId]); + + + Database::table('wmuser_crm.client_categories')->insertOrIgnore([ + 'name_en' => $category[0]->name_en, + 'name' => $category[0]->name_fa, + 'parent_id' => $category[0]->parent_id, + 'business_id' => 3, + 'user_id' => $client_id, + 'level' => 1, + 'created_at' => Carbon::now(), + 'updated_at' => Carbon::now() + ]); + + $clientCategories = Database::table('wmuser_crm.client_categories')->latest()->first(); + + + Database::table('wmuser_crm.client_client_category')->insertOrIgnore([ + 'client_category_id' => $clientCategories->id, + 'client_id' => $client_id + + ]); + + + } + + /** + * Execute the console command. + * + * @return mixed + */ + public function handle() + { + + $count = 0; + foreach($this->getBusinessRows() as $userData) + { + + $phones = json_decode($userData->phones); + + $cellPhone = null; + + for( $i=0; $i <= count($phones)-1 ; $i++ ) + { + if($phones[$i]->label == 'Mobile') + { + $cellPhone = $phones[$i]->tel; + } + } + + + $userTableDedails = [ + 'address' => [ + 'latitude' => $userData->latitude, + 'longitude' => $userData->longitude + ], + 'details' => json_decode($userData->details), + ]; + + $timestamp = [ + 'created_at' => $userData->created_at, + 'updated_at' => $userData->updated_at, + 'deleted_at' => $userData->deleted_at + ]; + + $userTableData = [ + 'name' => $userData->brand_fa, + 'business_id' => 3, + 'cell_number' => $cellPhone, + 'detail' => json_encode($userTableDedails), + 'instagram' => json_decode($userData->details)->InstagramID, + 'telegram' => json_decode($userData->details)->TelegramID, + 'email' => json_decode($userData->details)->Email, + 'created_at' => $timestamp['created_at'], + 'updated_at' => $timestamp['updated_at'], + 'deleted_at' => $timestamp['deleted_at'] + ]; + + $userAddress = [ + 'district_id' => $userData->district_id, + 'city_id' => $userData->city_id + ]; + + + $newUserId = $this->insertRowToUsers($userTableData); + $this->categoriesConfig($userData->category_id, $newUserId); + $this->InsertAddress($userAddress, $userData->id,$newUserId); + $this->insertPhone($userData->phones , $timestamp , $newUserId); + $count ++; + $this->info('success transfer user : '. $newUserId . 'count of add :'.$count); + + } + + // $this->deleteTempBusiness(); + + + } + +}