Merge pull request 'solve rename' (#18) from changeUsers into dev
Reviewed-on: WillaEngine-Project/WillaEngine#18pull/23/head
commit
7d26e38cd5
@ -0,0 +1,254 @@
|
||||
<?php
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use Illuminate\Console\Command;
|
||||
use Illuminate\Support\Facades\DB as Database;
|
||||
use Illuminate\Support\Carbon;
|
||||
|
||||
|
||||
class ConvertUser extends Command
|
||||
{
|
||||
/**
|
||||
* The name and signature of the console command.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $signature = 'db:convert';
|
||||
|
||||
/**
|
||||
* The console command description.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $description = 'This command when work you need transfer a table rows to another table row';
|
||||
|
||||
/**
|
||||
* Create a new command instance.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function __construct()
|
||||
{
|
||||
parent::__construct();
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* Return data from Temp-business
|
||||
*
|
||||
* @return rows
|
||||
*
|
||||
*/
|
||||
private function getBusinessRows()
|
||||
{
|
||||
$data = Database::table('temp_businesses')->get();
|
||||
return $data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a row in table users
|
||||
*/
|
||||
private function insertRowToUsers($data)
|
||||
{
|
||||
Database::table('users')->insertOrIgnore($data);
|
||||
$lastUser = Database::table('users')->orderBy('id', 'DESC')->first();
|
||||
return $lastUser->id;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Insert to wmuser_common phone
|
||||
*/
|
||||
private function insertPhone($phones,$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' => Carbon::now(),
|
||||
'updated_at' => Carbon::now(),
|
||||
]);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 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()
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
/**
|
||||
* 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()
|
||||
]);
|
||||
|
||||
$lastStatusid = Database::table('wmuser_crm.client_statuses')->orderBy('id', 'DESC')->first();
|
||||
|
||||
return $lastStatusid->id;
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* update user status
|
||||
**/
|
||||
|
||||
private function updateToUserStatus($userId,$userStatusId)
|
||||
{
|
||||
Database::table('users')
|
||||
->where('id', $userId)
|
||||
->update(['user_status_id' => $userStatusId]);
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert categories with Id
|
||||
*/
|
||||
private function categoriesConfig($categoryId, $client_id)
|
||||
{
|
||||
|
||||
$category = Database::table('categories')->where('id' , $categoryId)->get();
|
||||
|
||||
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),
|
||||
];
|
||||
|
||||
|
||||
$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' => Carbon::now(),
|
||||
'updated_at' => Carbon::now(),
|
||||
'deleted_at' => Carbon::now(),
|
||||
'user_status_id' => null,
|
||||
];
|
||||
|
||||
$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 , $newUserId);
|
||||
$lastStatusId = $this->insertToClientStatuses($userData->status);
|
||||
$this->updateToUserStatus($newUserId,$lastStatusId);
|
||||
$count ++;
|
||||
|
||||
$this->info('success transfer user : '. $newUserId . ' count of add : '.$count);
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in new issue