ClearPromotionJob.php 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: wuzhenke
  5. * Date: 2019/2/14
  6. * Time: 13:39
  7. */
  8. namespace App\Jobs\Cron;
  9. use App\Models\Jobs;
  10. use App\Repositories\JobsRepository;
  11. use App\Repositories\PromotionRepository;
  12. use Illuminate\Bus\Queueable;
  13. use Illuminate\Contracts\Queue\ShouldQueue;
  14. use Illuminate\Foundation\Bus\Dispatchable;
  15. use Illuminate\Queue\InteractsWithQueue;
  16. use Illuminate\Queue\SerializesModels;
  17. use Illuminate\Support\Facades\DB;
  18. use Illuminate\Support\Facades\Log;
  19. use mysql_xdevapi\Exception;
  20. class ClearPromotionJob implements ShouldQueue
  21. {
  22. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  23. /**
  24. * ClearPromotionJob constructor.
  25. */
  26. public function __construct()
  27. {
  28. }
  29. public function handle(PromotionRepository $promotionRepository, JobsRepository $jobsRepository)
  30. {
  31. //职位推广
  32. $condition[] = ['endtime','<',time()];
  33. $list = $promotionRepository->findWhere($condition);
  34. if ($list->isNotEmpty()) {
  35. $stick_arr = [];
  36. $emergency_arr = [];
  37. foreach ($list as $key => $val) {
  38. if ($val['ptype'] ==1) {
  39. $stick_arr[] = $val['jobid'];
  40. } else {
  41. $emergency_arr[] = $val['jobid'];
  42. }
  43. }
  44. //使用事务原因是,职位的状态没改变,推广的却删掉了,不好排查
  45. DB::beginTransaction();
  46. try{
  47. if ($stick_arr) {
  48. if (!$jobsRepository->modifyData($stick_arr, ['stick'=>0,'stime'=>0])) {
  49. Log::error('清除过期职位置顶失败'.json_encode($stick_arr));
  50. throw new \Exception("error");
  51. }
  52. foreach ($stick_arr as $key => $val) {
  53. event_search_update(Jobs::class, (string)$val, 'update');
  54. }
  55. }
  56. if ($emergency_arr) {
  57. if (!$jobsRepository->modifyData($emergency_arr, ['emergency'=>0])) {
  58. Log::error('清除过期职位紧急失败'.json_encode($emergency_arr));
  59. throw new \Exception("");
  60. }
  61. foreach ($emergency_arr as $key => $val) {
  62. event_search_update(Jobs::class, (string)$val, 'update');
  63. }
  64. }
  65. if (!$promotionRepository->deleteWhere($condition)) {
  66. Log::error('清除职位推广失败'.json_encode($condition));
  67. throw new \Exception("");
  68. }
  69. DB::commit();
  70. }catch (\Exception $exception){
  71. DB::rollback();
  72. }
  73. }
  74. }
  75. }