love_php/app/Jobs/SendInviteCoin.php

75 lines
2.2 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\CoinLog;
use App\Http\Response\ResponseJson;
class SendInviteCoin implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
use ResponseJson;
public $tries = 1;
protected $user_id, $from_user_id;
/**
* Create a new job instance.
*
* @return void
*/
public function __construct($user_id, $from_user_id)
{
$this->user_id = $user_id;
$this->from_user_id = $from_user_id;
}
/**
* Execute the job.
*
* @return void
*/
public function handle()
{
try {
$start_time = env('APP_ENV') == 'producation'?'2020-12-24':'2020-12-22';
if (date('Y-m-d') < $start_time) return true;
$user = User::find($this->from_user_id);
if (empty($user)) throw new \Exception("用户不存在", 1);
$log = $user->inviteLogs()->where('user_id',$this->user_id)->first();
if (empty($log)) throw new \Exception("分享记录不存在", 1);
$coin = $log->type=='FIRST'?10:1;
$this->sendCoin($user, $this->user_id, $coin);
return true;
} catch (\Exception $e) {
$this->getError($e);
return false;
}
}
public function sendCoin($user, $login_user_id, $coin)
{
try {
$remark='分享用户';
//判断记录是否存在
$count = $user->coinLogs()->where(['type'=>CoinLog::RECSYSTEM, 'coin'=>$coin, 'remark'=>$remark,'type_id'=>$login_user_id])->count();
if ($count) return false;
\DB::beginTransaction();
//增加福币记录
$user->addCoinLog(CoinLog::RECSYSTEM, $login_user_id, $coin, $remark);
//修改福币钱包
$user->updateCoinInfo('add', $coin, 'other');
\DB::commit();
return true;
} catch (\Exception $e) {
\DB::rollback();
$this->getError($e);
return false;
}
}
}