123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081 |
- <?php
- declare(strict_types=1);
- namespace App\Job;
- use App\Utils\LogUtil;
- use Hyperf\AsyncQueue\Job;
- use Hyperf\DbConnection\Db;
- class PlayerJob extends Job
- {
- //日志板块
- private const LOG_MODULE = 'PlayerJob';
- public $params;
- /**
- * 任务执行失败后的重试次数,即最大执行次数为 $maxAttempts+1 次
- */
- protected int $maxAttempts = 2;
- public function __construct($params)
- {
- // 这里最好是普通数据,不要使用携带 IO 的对象,比如 PDO 对象
- $this->params = $params;
- }
- /**
- * Execute the job.
- *
- * @return void
- */
- public function handle()
- {
- //日志统一写入
- LogUtil::getInstance('Queues/');//设置日志存入通道
- LogUtil::info('开始处理', self::LOG_MODULE, __FUNCTION__, ['params' => $this->params]);
- // 根据参数处理具体逻辑
- // 通过具体参数获取模型等
- // 这里的逻辑会在 ConsumerProcess 进程中执行
- // var_dump($this->params);
- $res = $this->recode($this->params['player_id'],$this->params['user_id'],$this->params['vote']);
- LogUtil::info('处理结果', self::LOG_MODULE, __FUNCTION__,$res);
- LogUtil::close();
- }
- public function recode($player_id,$user_id,$vote){
- Db::beginTransaction();
- //检查选手
- //给选手加票
- $update_rs = Db::table('vote_player')->where('id',$player_id)->increment('votes',$vote);
- if(!$update_rs){
- Db::rollBack();
- return false;
- }
- //日志
- $data = [
- 'user_id' => $user_id,
- 'subject_id' => 1,
- 'player_id' => $player_id,
- 'vote' => $vote,
- 'createdate' => strtotime(date('Y-m-d')),
- 'createtime' => time(),
- ];
- $log_id = Db::table('vote_record')->insert($data);
- if(!$log_id){
- Db::rollBack();
- return false;
- }
- Db::commit();
- return true;
- }
- }
|