263 lines
9.1 KiB
PHP
263 lines
9.1 KiB
PHP
<?php
|
|
|
|
namespace App\Cron;
|
|
|
|
use App\User;
|
|
use stdClass;
|
|
use App\Models\UserBusinessStructure;
|
|
use App\Services\BusinessPlan\TreeCalcBotOptimized;
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
class BusinessUsersStoreOptimized
|
|
{
|
|
private $month;
|
|
private $year;
|
|
private $user_business_structure;
|
|
private $users_structure = [];
|
|
private $logger;
|
|
|
|
public function __construct($month, $year, ?LoggerInterface $logger = null)
|
|
{
|
|
$this->month = $month;
|
|
$this->year = $year;
|
|
$this->logger = $logger ?? app(LoggerInterface::class);
|
|
}
|
|
|
|
public function getStoreUserBusinessStructure()
|
|
{
|
|
return UserBusinessStructure::where('year', $this->year)
|
|
->where('month', $this->month)
|
|
->first();
|
|
}
|
|
|
|
public function storeUserBusinessStructure()
|
|
{
|
|
if ($this->user_business_structure = $this->getStoreUserBusinessStructure()) {
|
|
$this->logger->info("Found existing business structure for {$this->month}/{$this->year}");
|
|
return $this->user_business_structure;
|
|
}
|
|
|
|
try {
|
|
$this->logger->info("Creating new business structure for {$this->month}/{$this->year}");
|
|
$startTime = microtime(true);
|
|
|
|
// Verwende TreeCalcBotOptimized mit Live-Berechnung für aktuelle Daten
|
|
$treeCalcBot = new TreeCalcBotOptimized($this->month, $this->year, 'admin', true);
|
|
$treeCalcBot->initStructureAdmin(false, true); // forceLiveCalculation = true
|
|
|
|
$this->storeStructure($treeCalcBot);
|
|
|
|
$endTime = microtime(true);
|
|
$duration = round(($endTime - $startTime) * 1000, 2);
|
|
$this->logger->info("Business structure created in {$duration}ms with " . count($this->users_structure) . " users");
|
|
} catch (\Exception $e) {
|
|
$this->logger->error("Error creating business structure: " . $e->getMessage());
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
public function storeBusinessUsersDetail()
|
|
{
|
|
if (!$this->user_business_structure) {
|
|
$this->user_business_structure = $this->getStoreUserBusinessStructure();
|
|
if (!$this->user_business_structure) {
|
|
throw new \Exception('UserBusinessStructure not found');
|
|
}
|
|
}
|
|
|
|
$totalUsers = count($this->user_business_structure->users);
|
|
$processedUsers = 0;
|
|
|
|
$this->logger->info("Processing {$totalUsers} business user details");
|
|
|
|
foreach ($this->user_business_structure->users as $user_id => $completed) {
|
|
if ($completed === 0) {
|
|
try {
|
|
$user = User::find($user_id);
|
|
if ($user) {
|
|
$this->processBusinessUser($user, $user_id);
|
|
$processedUsers++;
|
|
|
|
// Log progress every 50 users
|
|
if ($processedUsers % 50 === 0) {
|
|
$this->logger->info("Processed {$processedUsers}/{$totalUsers} business users");
|
|
}
|
|
} else {
|
|
$this->logger->warning("User {$user_id} not found, skipping");
|
|
$this->markUserCompleted($user_id);
|
|
}
|
|
} catch (\Exception $e) {
|
|
$this->logger->error("Error processing user {$user_id}: " . $e->getMessage());
|
|
// Mark as completed to avoid infinite retry loops
|
|
$this->markUserCompleted($user_id);
|
|
}
|
|
}
|
|
}
|
|
|
|
$this->logger->info("Completed processing {$processedUsers} business user details");
|
|
}
|
|
|
|
private function processBusinessUser(User $user, int $user_id): void
|
|
{
|
|
try {
|
|
$startTime = microtime(true);
|
|
|
|
// Verwende TreeCalcBotOptimized für detaillierte Benutzerberechnung
|
|
$TreeCalcBot = new TreeCalcBotOptimized($this->month, $this->year, 'admin', true);
|
|
$TreeCalcBot->initBusinesslUserDetail($user, true); // forceLiveCalculation = true
|
|
|
|
if (!$TreeCalcBot->business_user) {
|
|
throw new \Exception("business_user not found for user {$user_id}");
|
|
}
|
|
|
|
$this->storeBusinesslUser($TreeCalcBot->business_user);
|
|
$this->markUserCompleted($user_id);
|
|
|
|
$endTime = microtime(true);
|
|
$duration = round(($endTime - $startTime) * 1000, 2);
|
|
$this->logger->debug("Processed user {$user_id} in {$duration}ms");
|
|
} catch (\Exception $e) {
|
|
$this->logger->error("Error in processBusinessUser for {$user_id}: " . $e->getMessage());
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
private function markUserCompleted(int $user_id): void
|
|
{
|
|
$users = $this->user_business_structure->users;
|
|
$users[$user_id] = 1;
|
|
$this->user_business_structure->users = $users;
|
|
$this->user_business_structure->save();
|
|
}
|
|
|
|
public function storeBusinesslUser($business_user)
|
|
{
|
|
try {
|
|
$b_user = $business_user->getBUser();
|
|
$b_user->user_items = $this->storeUserItems($business_user->businessUserItems, 1);
|
|
$b_user->b_structure_id = $this->user_business_structure->id;
|
|
$b_user->save();
|
|
} catch (\Exception $e) {
|
|
$this->logger->error("Error storing business user: " . $e->getMessage());
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
public function storeBusinessCompleted()
|
|
{
|
|
if (!$this->user_business_structure) {
|
|
$this->user_business_structure = $this->getStoreUserBusinessStructure();
|
|
}
|
|
|
|
$incompleteCount = 0;
|
|
foreach ($this->user_business_structure->users as $user_id => $completed) {
|
|
if ($completed === 0) {
|
|
$incompleteCount++;
|
|
}
|
|
}
|
|
|
|
if ($incompleteCount === 0) {
|
|
$this->user_business_structure->completed = 1;
|
|
$this->user_business_structure->save();
|
|
$this->logger->info("Business structure marked as completed");
|
|
return true;
|
|
}
|
|
|
|
$this->logger->info("{$incompleteCount} users still incomplete");
|
|
return false;
|
|
}
|
|
|
|
private function storeUserItems($userItems, $line)
|
|
{
|
|
$ret = [];
|
|
|
|
try {
|
|
foreach ($userItems as $userItem) {
|
|
$temp = null;
|
|
if (count($userItem->businessUserItems) > 0) {
|
|
$temp = $this->storeUserItems($userItem->businessUserItems, $line + 1);
|
|
}
|
|
|
|
$obj = new stdClass();
|
|
$obj->user_id = $userItem->user_id;
|
|
$obj->line = $line;
|
|
$obj->points = $userItem->sales_volume_points_sum ?? 0;
|
|
$obj->parents = $temp;
|
|
$ret[] = $obj;
|
|
}
|
|
} catch (\Exception $e) {
|
|
$this->logger->error("Error storing user items at line {$line}: " . $e->getMessage());
|
|
throw $e;
|
|
}
|
|
|
|
return $ret;
|
|
}
|
|
|
|
private function storeStructure($treeCalcBot)
|
|
{
|
|
try {
|
|
$structure = [];
|
|
$businessUsers = $treeCalcBot->business_users;
|
|
|
|
if (!is_array($businessUsers)) {
|
|
throw new \Exception("business_users is not an array");
|
|
}
|
|
|
|
foreach ($businessUsers as $business_user) {
|
|
$structure[] = $this->storeStructureItem($business_user, 0);
|
|
}
|
|
|
|
$parentless = [];
|
|
$parentlessUsers = $treeCalcBot->parentless;
|
|
|
|
if ($parentlessUsers && is_array($parentlessUsers)) {
|
|
foreach ($parentlessUsers as $pless) {
|
|
$parentless[] = $this->storeStructureItem($pless, 0);
|
|
}
|
|
}
|
|
|
|
$fill = [
|
|
'month' => $this->month,
|
|
'year' => $this->year,
|
|
'structure' => $structure,
|
|
'parentless' => $parentless,
|
|
'users' => $this->users_structure,
|
|
'completed' => false,
|
|
'status' => 0
|
|
];
|
|
|
|
$this->user_business_structure = UserBusinessStructure::create($fill);
|
|
$this->logger->info("Stored structure with " . count($structure) . " root users and " . count($parentless) . " parentless users");
|
|
|
|
return $this->user_business_structure;
|
|
} catch (\Exception $e) {
|
|
$this->logger->error("Error storing structure: " . $e->getMessage());
|
|
throw $e;
|
|
}
|
|
}
|
|
|
|
private function storeStructureItem($item, $deep)
|
|
{
|
|
try {
|
|
$temp = null;
|
|
if (isset($item->businessUserItems) && is_array($item->businessUserItems)) {
|
|
foreach ($item->businessUserItems as $parent) {
|
|
$temp[] = $this->storeStructureItem($parent, $deep + 1);
|
|
}
|
|
}
|
|
|
|
$this->users_structure[$item->user_id] = 0;
|
|
|
|
$obj = new stdClass();
|
|
$obj->user_id = $item->user_id;
|
|
$obj->email = $item->email ?? 'unknown';
|
|
$obj->deep = $deep;
|
|
$obj->parents = $temp;
|
|
|
|
return $obj;
|
|
} catch (\Exception $e) {
|
|
$this->logger->error("Error storing structure item for user {$item->user_id}: " . $e->getMessage());
|
|
throw $e;
|
|
}
|
|
}
|
|
}
|