MerchantSendCouponJob.php 1.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. namespace ln\jobs;
  3. use app\common\repositories\store\coupon\StoreCouponRepository;
  4. use app\common\repositories\store\coupon\StoreCouponSendRepository;
  5. use app\common\repositories\store\coupon\StoreCouponUserRepository;
  6. use ln\interfaces\JobInterface;
  7. use think\facade\Cache;
  8. class MerchantSendCouponJob implements JobInterface
  9. {
  10. public function fire($job, $sendId)
  11. {
  12. $storeCouponSendRepository = app()->make(StoreCouponSendRepository::class);
  13. $send = $storeCouponSendRepository->get((int)$sendId);
  14. if (!$send || $send->status == 1) {
  15. return $job->delete();
  16. }
  17. $cacheKey = '_send_coupon' . $sendId;
  18. $cache = Cache::store('file');
  19. if (!$cache->has($cacheKey)) {
  20. $send->status = -1;
  21. return $job->delete();
  22. }
  23. $storeCouponRepository = app()->make(StoreCouponRepository::class);
  24. $storeCouponUserRepository = app()->make(StoreCouponUserRepository::class);
  25. $coupon = $storeCouponRepository->get($send->coupon_id);
  26. if (!$coupon) {
  27. $send->status = -1;
  28. return $job->delete();
  29. }
  30. $uids = $cache->get($cacheKey);
  31. do {
  32. $install = [];
  33. foreach (array_splice($uids, -30) as $k => $uid) {
  34. $data = $storeCouponRepository->createData($coupon, $uid);
  35. $data['send_id'] = $sendId;
  36. $install[] = $data;
  37. }
  38. try {
  39. $storeCouponUserRepository->insertAll($install);
  40. } catch (\Exception $e) {
  41. }
  42. usleep(100);
  43. } while (count($uids));
  44. $send->status = 1;
  45. $send->save();
  46. return $job->delete();
  47. }
  48. public function failed($data)
  49. {
  50. // TODO: Implement failed() method.
  51. }
  52. }