PlayerJob.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <?php
  2. declare(strict_types=1);
  3. namespace App\Job;
  4. use App\Utils\LogUtil;
  5. use Hyperf\AsyncQueue\Job;
  6. use Hyperf\DbConnection\Db;
  7. class PlayerJob extends Job
  8. {
  9. //日志板块
  10. private const LOG_MODULE = 'PlayerJob';
  11. public $params;
  12. /**
  13. * 任务执行失败后的重试次数,即最大执行次数为 $maxAttempts+1 次
  14. */
  15. protected int $maxAttempts = 2;
  16. public function __construct($params)
  17. {
  18. // 这里最好是普通数据,不要使用携带 IO 的对象,比如 PDO 对象
  19. $this->params = $params;
  20. }
  21. /**
  22. * Execute the job.
  23. *
  24. * @return void
  25. */
  26. public function handle()
  27. {
  28. //日志统一写入
  29. LogUtil::getInstance('Queues/');//设置日志存入通道
  30. LogUtil::info('开始处理', self::LOG_MODULE, __FUNCTION__, ['params' => $this->params]);
  31. // 根据参数处理具体逻辑
  32. // 通过具体参数获取模型等
  33. // 这里的逻辑会在 ConsumerProcess 进程中执行
  34. // var_dump($this->params);
  35. $res = $this->recode($this->params['player_id'],$this->params['user_id'],$this->params['vote']);
  36. LogUtil::info('处理结果', self::LOG_MODULE, __FUNCTION__,$res);
  37. LogUtil::close();
  38. }
  39. public function recode($player_id,$user_id,$vote){
  40. Db::beginTransaction();
  41. //检查选手
  42. //给选手加票
  43. $update_rs = Db::table('vote_player')->where('id',$player_id)->increment('votes',$vote);
  44. if(!$update_rs){
  45. Db::rollBack();
  46. return false;
  47. }
  48. //日志
  49. $data = [
  50. 'user_id' => $user_id,
  51. 'subject_id' => 1,
  52. 'player_id' => $player_id,
  53. 'vote' => $vote,
  54. 'createdate' => strtotime(date('Y-m-d')),
  55. 'createtime' => time(),
  56. ];
  57. $log_id = Db::table('vote_record')->insert($data);
  58. if(!$log_id){
  59. Db::rollBack();
  60. return false;
  61. }
  62. Db::commit();
  63. return true;
  64. }
  65. }