PartnerCommitJob.php 2.5 KB

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