update 20.10.2025
This commit is contained in:
parent
8c11130b5d
commit
a939cd51ef
616 changed files with 84821 additions and 4121 deletions
|
|
@ -1,10 +1,11 @@
|
|||
<?php
|
||||
|
||||
namespace App\Cron;
|
||||
|
||||
use App\User;
|
||||
use stdClass;
|
||||
use App\Models\UserBusinessStructure;
|
||||
use App\Services\BusinessPlan\TreeCalcBot;
|
||||
use App\Services\BusinessPlan\TreeCalcBotOptimized;
|
||||
|
||||
class BusinessUsersStore
|
||||
{
|
||||
|
|
@ -14,7 +15,7 @@ class BusinessUsersStore
|
|||
private $user_business_structure;
|
||||
private $users_structure = [];
|
||||
|
||||
|
||||
|
||||
public function __construct($month, $year)
|
||||
{
|
||||
$this->month = $month;
|
||||
|
|
@ -22,16 +23,17 @@ class BusinessUsersStore
|
|||
}
|
||||
|
||||
|
||||
public function getStoreUserBusinessStructure(){
|
||||
public function getStoreUserBusinessStructure()
|
||||
{
|
||||
return UserBusinessStructure::where('year', $this->year)->where('month', $this->month)->first();
|
||||
}
|
||||
|
||||
public function storeUserBusinessStructure()
|
||||
{
|
||||
if($this->user_business_structure = $this->getStoreUserBusinessStructure()){
|
||||
if ($this->user_business_structure = $this->getStoreUserBusinessStructure()) {
|
||||
return $this->user_business_structure;
|
||||
}
|
||||
$treeCalcBot = new TreeCalcBot($this->month, $this->year, 'admin');
|
||||
$treeCalcBot = new TreeCalcBotOptimized($this->month, $this->year, 'admin');
|
||||
//only load, when no structur is save
|
||||
$treeCalcBot->initStructureAdmin(false);
|
||||
$this->storeStructure($treeCalcBot);
|
||||
|
|
@ -39,19 +41,19 @@ class BusinessUsersStore
|
|||
|
||||
public function storeBusinessUsersDetail()
|
||||
{
|
||||
if(!$this->user_business_structure){
|
||||
if (!$this->user_business_structure) {
|
||||
$this->user_business_structure = $this->getStoreUserBusinessStructure();
|
||||
if(!$this->user_business_structure){
|
||||
if (!$this->user_business_structure) {
|
||||
abort(403, 'not found UserBusinessStructure');
|
||||
}
|
||||
}
|
||||
foreach($this->user_business_structure->users as $user_id=>$completed){
|
||||
if($completed === 0){
|
||||
$user = User::find($user_id);
|
||||
if($user){
|
||||
$TreeCalcBot = new TreeCalcBot($this->month, $this->year, 'admin');
|
||||
foreach ($this->user_business_structure->users as $user_id => $completed) {
|
||||
if ($completed === 0) {
|
||||
$user = User::find($user_id);
|
||||
if ($user) {
|
||||
$TreeCalcBot = new TreeCalcBotOptimized($this->month, $this->year, 'admin');
|
||||
$TreeCalcBot->initBusinesslUserDetail($user);
|
||||
if(!$TreeCalcBot->business_user){
|
||||
if (!$TreeCalcBot->business_user) {
|
||||
abort(403, 'not found TreeCalcBot->business_user');
|
||||
}
|
||||
$this->storeBusinesslUser($TreeCalcBot->business_user);
|
||||
|
|
@ -60,13 +62,13 @@ class BusinessUsersStore
|
|||
$this->user_business_structure->users = $users;
|
||||
$this->user_business_structure->save();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public function storeBusinesslUser($business_user){
|
||||
public function storeBusinesslUser($business_user)
|
||||
{
|
||||
$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;
|
||||
|
|
@ -74,12 +76,13 @@ class BusinessUsersStore
|
|||
}
|
||||
|
||||
|
||||
public function storeBusinessCompleted(){
|
||||
if(!$this->user_business_structure){
|
||||
public function storeBusinessCompleted()
|
||||
{
|
||||
if (!$this->user_business_structure) {
|
||||
$this->user_business_structure = $this->getStoreUserBusinessStructure();
|
||||
}
|
||||
foreach($this->user_business_structure->users as $user_id=>$completed){
|
||||
if($completed === 0){
|
||||
foreach ($this->user_business_structure->users as $user_id => $completed) {
|
||||
if ($completed === 0) {
|
||||
return false;
|
||||
}
|
||||
$this->user_business_structure->completed = 1;
|
||||
|
|
@ -87,21 +90,22 @@ class BusinessUsersStore
|
|||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
private function storeUserItems($userItems, $line){
|
||||
|
||||
private function storeUserItems($userItems, $line)
|
||||
{
|
||||
$ret = [];
|
||||
foreach($userItems as $userItem){
|
||||
foreach ($userItems as $userItem) {
|
||||
$temp = null;
|
||||
if(count($userItem->businessUserItems) > 0){
|
||||
$temp = $this->storeUserItems($userItem->businessUserItems, $line+1);
|
||||
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;
|
||||
$obj->parents = $temp;
|
||||
$ret[] = $obj;
|
||||
$ret[] = $obj;
|
||||
}
|
||||
return $ret;
|
||||
}
|
||||
|
|
@ -114,13 +118,13 @@ class BusinessUsersStore
|
|||
}*/
|
||||
|
||||
$structure = [];
|
||||
foreach($treeCalcBot->business_users as $business_user){
|
||||
foreach ($treeCalcBot->business_users as $business_user) {
|
||||
$structure[] = $this->storeStructureItem($business_user, 0);
|
||||
}
|
||||
|
||||
$parentless = [];
|
||||
if($treeCalcBot->parentless){
|
||||
foreach($treeCalcBot->parentless as $pless){
|
||||
if ($treeCalcBot->parentless) {
|
||||
foreach ($treeCalcBot->parentless as $pless) {
|
||||
$parentless[] = $this->storeStructureItem($pless, 0);
|
||||
}
|
||||
}
|
||||
|
|
@ -137,14 +141,15 @@ class BusinessUsersStore
|
|||
return $this->user_business_structure;
|
||||
}
|
||||
|
||||
|
||||
|
||||
private function storeStructureItem($item, $deep){
|
||||
|
||||
private function storeStructureItem($item, $deep)
|
||||
{
|
||||
$temp = null;
|
||||
if($item->businessUserItems){
|
||||
foreach($item->businessUserItems as $parent){
|
||||
$temp[] = $this->storeStructureItem($parent, $deep+1);
|
||||
}
|
||||
if ($item->businessUserItems) {
|
||||
foreach ($item->businessUserItems as $parent) {
|
||||
$temp[] = $this->storeStructureItem($parent, $deep + 1);
|
||||
}
|
||||
}
|
||||
$this->users_structure[$item->user_id] = 0;
|
||||
$obj = new stdClass();
|
||||
|
|
@ -156,7 +161,4 @@ class BusinessUsersStore
|
|||
$obj->parents = $temp;
|
||||
return $obj;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
|
|
|||
263
app/Cron/BusinessUsersStoreOptimized.php
Normal file
263
app/Cron/BusinessUsersStoreOptimized.php
Normal file
|
|
@ -0,0 +1,263 @@
|
|||
<?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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue