203 lines
7 KiB
PHP
203 lines
7 KiB
PHP
<?php
|
|
|
|
namespace App\Console\Commands;
|
|
|
|
use App\Services\UserUtil;
|
|
use App\User;
|
|
use Carbon\Carbon;
|
|
use Illuminate\Console\Command;
|
|
|
|
class UserCleanUp extends Command
|
|
{
|
|
/**
|
|
* The name and signature of the console command.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $signature = 'user:cleanup';
|
|
|
|
/**
|
|
* The console command description.
|
|
*
|
|
* @var string
|
|
*/
|
|
protected $description = 'User Clean Up inactive for Business Structure and UserDetails';
|
|
|
|
private $timeStart;
|
|
|
|
/**
|
|
* Create a new command instance.
|
|
*
|
|
* @return void
|
|
*/
|
|
public function __construct()
|
|
{
|
|
parent::__construct();
|
|
}
|
|
|
|
/**
|
|
* Execute the console command.
|
|
*
|
|
* @return int
|
|
*/
|
|
public function handle()
|
|
{
|
|
$this->info('RUN Command user:cleanup');
|
|
\Log::channel('cleanup')->info('COMMAND [user:cleanup] started.');
|
|
|
|
$this->timeStart = microtime(true);
|
|
|
|
// Schritt 1: User löschen, die länger als 2 Monate inaktiv sind
|
|
$this->deleteInactiveUsers();
|
|
|
|
// Schritt 2: Alle inaktiven User deaktivieren (länger als 2 Wochen inaktiv)
|
|
// Ihre Downline wird dem nächsten aktiven Berater (Sponsor) zugewiesen
|
|
$this->cleanUpInActiveUser();
|
|
|
|
\Log::channel('cleanup')->info('COMMAND [user:cleanup] finished.');
|
|
|
|
return 0;
|
|
}
|
|
|
|
/**
|
|
* Löscht User, die länger als 2 Monate inaktiv sind (payment_account < -2 month)
|
|
* - Weist deren Vertriebspartner-Kinder dem nächsten aktiven Sponsor zu
|
|
* - Überträgt deren Shopping-Kunden zum neuen Sponsor
|
|
* - Konvertiert den User zu einem Shopping-Kunden
|
|
* - Löscht den User (soft delete)
|
|
*/
|
|
private function deleteInactiveUsers()
|
|
{
|
|
$methodStartTime = microtime(true);
|
|
$this->info('START Command deleteInactiveUsers');
|
|
$count = 0;
|
|
|
|
$date = Carbon::now()->modify('-2 month');
|
|
$delete_users = User::where('admin', 0)->where('payment_account', '<', $date)->get();
|
|
|
|
foreach ($delete_users as $delete_user) {
|
|
\DB::beginTransaction();
|
|
|
|
try {
|
|
// Finde nächsten aktiven Sponsor
|
|
$active_sponsor = UserUtil::findNextActiveSponsor($delete_user->id);
|
|
|
|
if (! $active_sponsor) {
|
|
\Log::channel('cleanup')->error('deleteInactiveUsers find no active_sponsor by delete_user_id: '.$delete_user->id);
|
|
\DB::rollBack();
|
|
|
|
continue;
|
|
}
|
|
|
|
// Prüfe ob User Account-Daten hat
|
|
if (! $delete_user->account) {
|
|
\Log::channel('cleanup')->error('deleteInactiveUsers: User has no account data, skipping user_id: '.$delete_user->id);
|
|
\DB::rollBack();
|
|
|
|
continue;
|
|
}
|
|
|
|
// Setze alle Vertriebspartner-Kinder zum neuen Sponsor
|
|
UserUtil::setNewSponsorToChilds($delete_user->id, $active_sponsor->id);
|
|
|
|
// Übertrage Shopping-User zum neuen Sponsor
|
|
UserUtil::setShoppingUserToNewMember($delete_user->id, $active_sponsor->id);
|
|
|
|
// Konvertiere User zu Client beim neuen Sponsor
|
|
UserUtil::setUserToClient($delete_user->id, $active_sponsor->id);
|
|
|
|
$data = [
|
|
'user_id' => $delete_user->id,
|
|
'email' => $delete_user->email,
|
|
'm_account' => $delete_user->account->m_account,
|
|
'm_first_name' => $delete_user->account->m_first_name,
|
|
'm_last_name' => $delete_user->account->m_last_name,
|
|
];
|
|
|
|
// Lösche User (soft delete)
|
|
UserUtil::deleteUser($delete_user);
|
|
|
|
\DB::commit();
|
|
$count++;
|
|
\Log::channel('cleanup')->info('deleteUser: '.json_encode($data));
|
|
} catch (\Exception $e) {
|
|
\DB::rollBack();
|
|
\Log::channel('cleanup')->error('deleteInactiveUsers failed for user_id: '.$delete_user->id.' | Error: '.$e->getMessage());
|
|
|
|
continue;
|
|
}
|
|
}
|
|
|
|
$diff = microtime(true) - $methodStartTime;
|
|
$sec = intval($diff);
|
|
$micro = $diff - $sec;
|
|
|
|
$this->info('END Command deleteInactiveUsers: '.$count.' | Time: '.$sec.'sec :'.round($micro * 1000, 4).' ms');
|
|
}
|
|
|
|
/**
|
|
* Deaktiviert User, die länger als 2 Wochen inaktiv sind
|
|
* - Weist deren Vertriebspartner-Kinder dem nächsten aktiven Sponsor zu
|
|
* - Deaktiviert den User (behält Account, speichert Sponsor in pre_sponsor)
|
|
* - Shopping-Kunden werden NICHT übertragen (bleiben beim deaktivierten User)
|
|
*/
|
|
private function cleanUpInActiveUser()
|
|
{
|
|
$methodStartTime = microtime(true);
|
|
$this->info('START Command cleanUpInActiveUser');
|
|
$count = 0;
|
|
|
|
// Finde User die länger als 2 Wochen inaktiv sind
|
|
$date = Carbon::now()->modify('-2 weeks');
|
|
|
|
$inactive_users = User::where('active', true)
|
|
->where('m_sponsor', '!=', null)
|
|
->where('payment_account', '<', $date)
|
|
->get();
|
|
|
|
foreach ($inactive_users as $inactive_user) {
|
|
\DB::beginTransaction();
|
|
|
|
try {
|
|
// Finde nächsten aktiven Sponsor
|
|
$active_sponsor = UserUtil::findNextActiveSponsor($inactive_user->m_sponsor);
|
|
|
|
if (! $active_sponsor) {
|
|
\Log::channel('cleanup')->error('cleanUpInActiveUser find no active_sponsor by inactive_user: '.$inactive_user->id);
|
|
\DB::rollBack();
|
|
|
|
continue;
|
|
}
|
|
|
|
// Setze alle Vertriebspartner-Kinder zum neuen Sponsor
|
|
UserUtil::setNewSponsorToChilds($inactive_user->id, $active_sponsor->id);
|
|
|
|
$data = [
|
|
'user_id' => $inactive_user->id,
|
|
'email' => $inactive_user->email,
|
|
'm_account' => $inactive_user->account ? $inactive_user->account->m_account : '',
|
|
'm_first_name' => $inactive_user->account ? $inactive_user->account->m_first_name : '',
|
|
'm_last_name' => $inactive_user->account ? $inactive_user->account->m_last_name : '',
|
|
];
|
|
|
|
// Deaktiviere User (setzt pre_sponsor, entfernt m_sponsor, setzt active=false)
|
|
UserUtil::deactiveUser($inactive_user);
|
|
|
|
\DB::commit();
|
|
$count++;
|
|
\Log::channel('cleanup')->info('inactive_user: '.json_encode($data));
|
|
} catch (\Exception $e) {
|
|
\DB::rollBack();
|
|
\Log::channel('cleanup')->error('cleanUpInActiveUser failed for user_id: '.$inactive_user->id.' | Error: '.$e->getMessage());
|
|
|
|
continue;
|
|
}
|
|
}
|
|
|
|
$diff = microtime(true) - $methodStartTime;
|
|
$sec = intval($diff);
|
|
$micro = $diff - $sec;
|
|
|
|
$this->info('END Command cleanUpInActiveUser: '.$count.' | Time: '.$sec.'sec :'.round($micro * 1000, 4).' ms');
|
|
}
|
|
}
|