2022-10-28 14:17:10 +08:00
< ? php
namespace App\Jobs ;
use App\Models\Setting ;
use App\Models\User ;
use Illuminate\Bus\Queueable ;
use Illuminate\Contracts\Queue\ShouldBeUnique ;
use Illuminate\Contracts\Queue\ShouldQueue ;
use Illuminate\Foundation\Bus\Dispatchable ;
use Illuminate\Queue\InteractsWithQueue ;
use Illuminate\Queue\SerializesModels ;
use Nexus\Database\NexusDB ;
2024-02-21 21:11:16 +08:00
use Nexus\Nexus ;
2022-10-28 14:17:10 +08:00
2022-11-05 18:43:49 +08:00
class CalculateUserSeedBonus implements ShouldQueue
2022-10-28 14:17:10 +08:00
{
use Dispatchable , InteractsWithQueue , Queueable , SerializesModels ;
private int $beginUid ;
private int $endUid ;
2024-02-21 21:11:16 +08:00
private string $idStr ;
2023-07-15 01:44:48 +08:00
2022-11-05 18:43:49 +08:00
private string $requestId ;
2024-02-21 21:11:16 +08:00
private string $idRedisKey ;
2022-10-28 14:17:10 +08:00
/**
* Create a new job instance .
*
* @ return void
*/
2024-02-21 21:11:16 +08:00
public function __construct ( int $beginUid , int $endUid , string $idStr , string $idRedisKey , string $requestId = '' )
2022-10-28 14:17:10 +08:00
{
$this -> beginUid = $beginUid ;
$this -> endUid = $endUid ;
2024-02-21 21:11:16 +08:00
$this -> idStr = $idStr ;
$this -> idRedisKey = $idRedisKey ;
2022-11-05 18:43:49 +08:00
$this -> requestId = $requestId ;
}
/**
* Determine the time at which the job should timeout .
*
* @ return \DateTime
*/
public function retryUntil ()
{
return now () -> addSeconds ( Setting :: get ( 'main.autoclean_interval_one' ));
2022-10-28 14:17:10 +08:00
}
2023-02-21 01:49:17 +08:00
public $tries = 1 ;
2023-07-17 02:00:20 +08:00
public $timeout = 3600 ;
2023-02-21 01:49:17 +08:00
2022-10-28 14:17:10 +08:00
/**
* Execute the job .
*
* @ return void
*/
public function handle ()
{
2023-07-15 01:44:48 +08:00
$beginTimestamp = time ();
$logPrefix = sprintf ( " [CLEANUP_CLI_CALCULATE_SEED_BONUS_HANDLE_JOB], commonRequestId: %s, beginUid: %s, endUid: %s " , $this -> requestId , $this -> beginUid , $this -> endUid );
$haremAdditionFactor = Setting :: get ( 'bonus.harem_addition' );
$officialAdditionFactor = Setting :: get ( 'bonus.official_addition' );
$donortimes_bonus = Setting :: get ( 'bonus.donortimes' );
$autoclean_interval_one = Setting :: get ( 'main.autoclean_interval_one' );
2024-02-21 21:11:16 +08:00
$idStr = $this -> idStr ;
$delIdRedisKey = false ;
if ( empty ( $idStr ) && ! empty ( $this -> idRedisKey )) {
$delIdRedisKey = true ;
$idStr = NexusDB :: cache_get ( $this -> idRedisKey );
}
if ( empty ( $idStr )) {
do_log ( " $logPrefix , no idStr or idRedisKey " , " error " );
return ;
}
$sql = sprintf ( " select %s from users where id in (%s) " , implode ( ',' , User :: $commonFields ), $idStr );
2023-07-15 01:44:48 +08:00
$results = NexusDB :: select ( $sql );
$logFile = getLogFile ( " seed-bonus-points " );
do_log ( " $logPrefix , [GET_UID_REAL], count: " . count ( $results ) . " , logFile: $logFile " );
$fd = fopen ( $logFile , 'a' );
2024-11-07 03:35:21 +08:00
$seedPointsUpdates = $seedPointsPerHourUpdates = $seedBonusUpdates = [];
$logStr = " " ;
2023-07-15 01:44:48 +08:00
foreach ( $results as $userInfo )
{
$uid = $userInfo [ 'id' ];
$isDonor = is_donor ( $userInfo );
$seedBonusResult = calculate_seed_bonus ( $uid );
$bonusLog = " [CLEANUP_CLI_CALCULATE_SEED_BONUS_HANDLE_USER], user: $uid , seedBonusResult: " . nexus_json_encode ( $seedBonusResult );
$all_bonus = $seedBonusResult [ 'seed_bonus' ];
$bonusLog .= " , all_bonus: $all_bonus " ;
2024-10-29 23:43:38 +08:00
if ( $isDonor && $donortimes_bonus != 0 ) {
2023-07-15 01:44:48 +08:00
$all_bonus = $all_bonus * $donortimes_bonus ;
$bonusLog .= " , isDonor, donortimes_bonus: $donortimes_bonus , all_bonus: $all_bonus " ;
}
if ( $officialAdditionFactor > 0 ) {
$officialAddition = $seedBonusResult [ 'official_bonus' ] * $officialAdditionFactor ;
$all_bonus += $officialAddition ;
$bonusLog .= " , officialAdditionFactor: $officialAdditionFactor , official_bonus: { $seedBonusResult [ 'official_bonus' ] } , officialAddition: $officialAddition , all_bonus: $all_bonus " ;
}
if ( $haremAdditionFactor > 0 ) {
$haremBonus = calculate_harem_addition ( $uid );
$haremAddition = $haremBonus * $haremAdditionFactor ;
$all_bonus += $haremAddition ;
$bonusLog .= " , haremAdditionFactor: $haremAdditionFactor , haremBonus: $haremBonus , haremAddition: $haremAddition , all_bonus: $all_bonus " ;
}
if ( $seedBonusResult [ 'medal_additional_factor' ] > 0 ) {
$medalAddition = $seedBonusResult [ 'medal_bonus' ] * $seedBonusResult [ 'medal_additional_factor' ];
$all_bonus += $medalAddition ;
$bonusLog .= " , medalAdditionFactor: { $seedBonusResult [ 'medal_additional_factor' ] } , medalBonus: { $seedBonusResult [ 'medal_bonus' ] } , medalAddition: $medalAddition , all_bonus: $all_bonus " ;
}
$dividend = 3600 / $autoclean_interval_one ;
$all_bonus = $all_bonus / $dividend ;
$seed_points = $seedBonusResult [ 'seed_points' ] / $dividend ;
2024-11-07 03:35:21 +08:00
// $updatedAt = now()->toDateTimeString();
// $sql = "update users set seed_points = ifnull(seed_points, 0) + $seed_points, seed_points_per_hour = {$seedBonusResult['seed_points']}, seedbonus = seedbonus + $all_bonus, seed_points_updated_at = '$updatedAt' where id = $uid limit 1";
// do_log("$bonusLog, query: $sql");
// NexusDB::statement($sql);
$seedPointsUpdates [] = sprintf ( " case %d then ifnull(seed_points, 0) + %d " , $uid , $seed_points );
$seedPointsPerHourUpdates [] = sprintf ( " case %d then %d " , $uid , $seedBonusResult [ 'seed_points' ]);
$seedBonusUpdates [] = sprintf ( " case %d then %d " , $uid , $all_bonus );
2023-07-15 01:44:48 +08:00
if ( $fd ) {
$log = sprintf (
'%s|%s|%s|%s|%s|%s|%s|%s' ,
date ( 'Y-m-d H:i:s' ), $uid ,
$userInfo [ 'seed_points' ], number_format ( $seed_points , 1 , '.' , '' ), number_format ( $userInfo [ 'seed_points' ] + $seed_points , 1 , '.' , '' ),
$userInfo [ 'seedbonus' ], number_format ( $all_bonus , 1 , '.' , '' ), number_format ( $userInfo [ 'seedbonus' ] + $all_bonus , 1 , '.' , '' )
);
2024-11-07 03:35:21 +08:00
// fwrite($fd, $log . PHP_EOL);
$logStr .= $log . PHP_EOL ;
2023-07-15 01:44:48 +08:00
} else {
do_log ( " logFile: $logFile is not writeable! " , 'error' );
}
}
2024-11-07 03:35:21 +08:00
$nowStr = now () -> toDateTimeString ();
$sql = sprintf (
" update users set seed_points = case id %s end, seed_points_per_hour = case id %s end, seedbonus = case id %s end, seed_points_updated_at = '%s' where id in (%s) " ,
implode ( " " , $seedPointsUpdates ), implode ( " " , $seedPointsPerHourUpdates ), implode ( " " , $seedBonusUpdates ), $nowStr , $idStr
);
$result = sql_query ( $sql );
2024-02-21 21:11:16 +08:00
if ( $delIdRedisKey ) {
NexusDB :: cache_del ( $this -> idRedisKey );
}
2024-11-07 03:35:21 +08:00
fwrite ( $fd , $logStr );
2023-07-15 01:44:48 +08:00
$costTime = time () - $beginTimestamp ;
2024-11-07 03:35:21 +08:00
do_log ( sprintf (
" $logPrefix , [DONE], update user count: %s, result: %s, cost time: %s seconds " ,
count ( $seedPointsUpdates ), var_export ( $result , true ), $costTime
));
2022-10-28 14:17:10 +08:00
}
2023-02-21 01:04:28 +08:00
/**
* Handle a job failure .
*
* @ param \Throwable $exception
* @ return void
*/
public function failed ( \Throwable $exception )
{
do_log ( " failed: " . $exception -> getMessage () . $exception -> getTraceAsString (), 'error' );
}
2022-10-28 14:17:10 +08:00
}