Merge branch 'dev' of https://git.willaspace.com/WillaEngine-Project/WillaEngine into dev
commit
10037f6384
@ -0,0 +1,327 @@
|
||||
<?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)
|
||||
{
|
||||
|
||||
foreach($phones as $phone)
|
||||
{
|
||||
|
||||
$number_type ;
|
||||
$regex = '/^09[0,1,2,3,9]{1}[0-9]{8}$/';
|
||||
|
||||
if (preg_match($regex,$phone->tel) == 1)
|
||||
{
|
||||
$number_type = 'cellphone';
|
||||
}else{
|
||||
$number_type = 'telephone';
|
||||
}
|
||||
|
||||
|
||||
Database::table('wmuser_common.phones')->insertOrIgnore([
|
||||
'phonable_id' => $userId,
|
||||
'number' => $phone->tel,
|
||||
'label' => $phone->label,
|
||||
'number_type' => $number_type,
|
||||
'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)
|
||||
{
|
||||
|
||||
$check_client_status = Database::table('wmuser_crm.client_statuses')
|
||||
->where('name' , $clientStatus)
|
||||
->first();
|
||||
|
||||
|
||||
$client_status_id;
|
||||
|
||||
if ($check_client_status === null) {
|
||||
|
||||
Database::table('wmuser_crm.client_statuses')->insertOrIgnore([
|
||||
'name' => $clientStatus,
|
||||
'user_id' => 52,
|
||||
'business_id' => 3,
|
||||
'created_at' => Carbon::now(),
|
||||
'updated_at' => Carbon::now()
|
||||
]);
|
||||
|
||||
$client_status_id = Database::table('wmuser_crm.client_statuses')
|
||||
->orderBy('id', 'DESC')
|
||||
->first();
|
||||
|
||||
} else {
|
||||
|
||||
$client_status_id = $check_client_status;
|
||||
|
||||
}
|
||||
|
||||
|
||||
return $client_status_id->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)
|
||||
->first();
|
||||
|
||||
|
||||
|
||||
$check_category_exist = Database::table('wmuser_crm.client_categories')
|
||||
->where('name_en' , $category->name_en)
|
||||
->first();
|
||||
|
||||
$client_category_id ;
|
||||
|
||||
if ($check_category_exist === null)
|
||||
{
|
||||
Database::table('wmuser_crm.client_categories')->insertOrIgnore([
|
||||
'name_en' => $category->name_en,
|
||||
'name' => $category->name_fa,
|
||||
'parent_id' => null,
|
||||
'business_id' => 3,
|
||||
'user_id' => $client_id,
|
||||
'level' => 1,
|
||||
'created_at' => Carbon::now(),
|
||||
'updated_at' => Carbon::now()
|
||||
]);
|
||||
|
||||
$client_category_id = Database::table('wmuser_crm.client_categories')
|
||||
->orderBy('id', 'DESC')
|
||||
->first();
|
||||
|
||||
|
||||
$client_category_id = $client_category_id->id;
|
||||
|
||||
} else {
|
||||
|
||||
$client_category_id = $check_category_exist->id;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Database::table('wmuser_crm.client_client_category')->insertOrIgnore([
|
||||
'client_category_id' => $client_category_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;
|
||||
|
||||
foreach ($phones as $phone)
|
||||
{
|
||||
|
||||
$phone->tel = str_replace(' ','',$phone->tel);
|
||||
|
||||
}
|
||||
|
||||
for( $i=0; $i <= count($phones)-1 ; $i++ )
|
||||
{
|
||||
|
||||
$regex = '/^09[0,1,2,3,9]{1}[0-9]{8}$/';
|
||||
|
||||
$match = preg_match($regex,$phones[$i]->tel);
|
||||
|
||||
if ($match == 1)
|
||||
{
|
||||
$cellPhone = $phones[$i]->tel;
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
$userTableDedails = [
|
||||
'address' => [
|
||||
'latitude' => $userData->latitude,
|
||||
'longitude' => $userData->longitude
|
||||
],
|
||||
'details' => json_decode($userData->details),
|
||||
];
|
||||
|
||||
|
||||
$userTableData = [
|
||||
'name' => $userData->brand_fa,
|
||||
'name_en' => $userData->brand_en,
|
||||
'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(),
|
||||
'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($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