Reviewed-on: WillaEngine-Project/WillaEngine#10pull/12/head
commit
bbd54ff756
@ -0,0 +1,250 @@
|
|||||||
|
<?php
|
||||||
|
|
||||||
|
namespace App\Console\Commands;
|
||||||
|
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
use Illuminate\Support\Facades\DB as Database;
|
||||||
|
use Illuminate\Support\Carbon;
|
||||||
|
|
||||||
|
class convertUsers 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::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();
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
Reference in new issue