PartnerCallbackJob.php 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace App\Jobs;
  3. use App\Models\Partner\PartnerBeautyUser;
  4. use App\Models\PartnerModel;
  5. use App\Models\User\UserModel;
  6. use Illuminate\Bus\Queueable;
  7. use Illuminate\Queue\SerializesModels;
  8. use Illuminate\Queue\InteractsWithQueue;
  9. use Illuminate\Contracts\Queue\ShouldQueue;
  10. use Illuminate\Foundation\Bus\Dispatchable;
  11. use Illuminate\Support\Facades\DB;
  12. class PartnerCallbackJob implements ShouldQueue
  13. {
  14. use Dispatchable;
  15. use InteractsWithQueue;
  16. use Queueable;
  17. use SerializesModels;
  18. /**
  19. * 任务最大尝试次数。
  20. *
  21. * @var int
  22. */
  23. public $tries = 1;
  24. /**
  25. * 任务运行的超时时间。
  26. *
  27. * @var int
  28. */
  29. public $timeout = 120;
  30. /**
  31. * Create a new job instance.
  32. *
  33. * @return void
  34. */
  35. public function __construct($id)
  36. {
  37. $this->partner = PartnerBeautyUser::find($id);
  38. }
  39. protected $partner;
  40. /**
  41. * Execute the job.
  42. *
  43. * @return void
  44. */
  45. public function handle()
  46. {
  47. $beauty_partner = $this->partner;
  48. if (
  49. $beauty_partner
  50. && $beauty_partner->original_photo
  51. && $beauty_partner->new_photo
  52. && $beauty_partner->replaced_at > 0
  53. && $beauty_partner->callback_at == 0
  54. ) {
  55. DB::transaction(function () use ($beauty_partner) {
  56. $partner = PartnerModel::find($beauty_partner->partner_id);
  57. if ($partner->{$beauty_partner->field} == $beauty_partner->new_photo) {
  58. $partner->{$beauty_partner->field} = $beauty_partner->original_photo;
  59. $partner->save();
  60. $user = UserModel::find($beauty_partner->uid);
  61. $user->{$beauty_partner->field} = $beauty_partner->original_photo;
  62. $user->save();
  63. $beauty_partner->callback_at = time();
  64. $beauty_partner->save();
  65. }
  66. });
  67. }
  68. }
  69. }