love_php/app/Jobs/SendSignCoin.php

99 lines
2.9 KiB
PHP
Raw Permalink Normal View History

2026-04-02 09:20:51 +08:00
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use App\Models\User;
use App\Models\SignLog;
use App\Models\Coin;
use App\Models\CoinLog;
use App\Models\RankHistory;
use App\Http\Response\ResponseJson;
class SendSignCoin implements ShouldQueue
{
use ResponseJson;
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
//签到成功赠送对应的福币
public $tries = 1;
protected $user_id;
protected $rule;//数字代表福币字符表示会员
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($user_id)
{
$this->user_id = $user_id;
$this->rule = [2,3,5,7,10,15,'rank'];
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
try {
$user = User::find($this->user_id);
//获取当前已经签到次数
$count = $user->signLogs()->signType(SignLog::NEWYEARTYPE)->count();
if (empty($count)) return true;
if ($count < 7 && is_numeric(($this->rule)[$count-1])) {
//赠送对应的福币
$coin = ($this->rule)[$count-1];
$this->sendCoin($user, $coin, $count);
}elseif ($count == 7 && ($this->rule)[$count-1] == 'rank') {
$this->sendSuperRank($user);
}else{
$this->sendCoin($user, 2, $count);
}
return true;
} catch (\Exception $e) {
$this->getError($e);
return false;
}
}
public function sendSuperRank($user)
{
try {
//是否已经有记录
$count = $user->rankHistories()->type(RankHistory::SIGNNEWYEAR)->count();
if ($count) return false;
//赠送会员
$user->addSuperRank($day=15, $month=0, $type=RankHistory::SIGNNEWYEAR);
} catch (\Exception $e) {
$this->getError($e);
return false;
}
}
public function sendCoin($user, $coin, $count)
{
try {
$remark='累计'.$count.'天元旦签到';
//判断记录是否存在
$count = $user->coinLogs()->where(['type'=>CoinLog::RECSYSTEM, 'coin'=>$coin, 'remark'=>$remark])->count();
if ($count) return false;
\DB::beginTransaction();
//增加福币记录
$user->addCoinLog(CoinLog::RECSYSTEM, 0, $coin, $remark);
//修改福币钱包
$user->updateCoinInfo('add', $coin, 'other');
\DB::commit();
return true;
} catch (\Exception $e) {
\DB::rollback();
$this->getError($e);
return false;
}
}
}