MerchantSendCouponJob.php 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. <?php
  2. // +----------------------------------------------------------------------
  3. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  4. // +----------------------------------------------------------------------
  5. // | Copyright (c) 2016~2020 https://www.crmeb.com All rights reserved.
  6. // +----------------------------------------------------------------------
  7. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  8. // +----------------------------------------------------------------------
  9. // | Author: CRMEB Team <admin@crmeb.com>
  10. // +----------------------------------------------------------------------
  11. namespace crmeb\jobs;
  12. use app\common\repositories\store\coupon\StoreCouponRepository;
  13. use app\common\repositories\store\coupon\StoreCouponSendRepository;
  14. use app\common\repositories\store\coupon\StoreCouponUserRepository;
  15. use crmeb\interfaces\JobInterface;
  16. use think\facade\Cache;
  17. class MerchantSendCouponJob implements JobInterface
  18. {
  19. public function fire($job, $sendId)
  20. {
  21. $storeCouponSendRepository = app()->make(StoreCouponSendRepository::class);
  22. $send = $storeCouponSendRepository->get((int)$sendId);
  23. if (!$send || $send->status == 1) {
  24. return $job->delete();
  25. }
  26. $cacheKey = '_send_coupon' . $sendId;
  27. $cache = Cache::store('file');
  28. if (!$cache->has($cacheKey)) {
  29. $send->status = -1;
  30. return $job->delete();
  31. }
  32. $storeCouponRepository = app()->make(StoreCouponRepository::class);
  33. $storeCouponUserRepository = app()->make(StoreCouponUserRepository::class);
  34. $coupon = $storeCouponRepository->get($send->coupon_id);
  35. if (!$coupon) {
  36. $send->status = -1;
  37. return $job->delete();
  38. }
  39. $uids = $cache->get($cacheKey);
  40. do {
  41. $install = [];
  42. foreach (array_splice($uids, -30) as $k => $uid) {
  43. $data = $storeCouponRepository->createData($coupon, $uid);
  44. $data['send_id'] = $sendId;
  45. $install[] = $data;
  46. }
  47. try {
  48. $storeCouponUserRepository->insertAll($install);
  49. } catch (\Exception $e) {
  50. }
  51. usleep(100);
  52. } while (count($uids));
  53. $send->status = 1;
  54. $send->save();
  55. return $job->delete();
  56. }
  57. public function failed($data)
  58. {
  59. // TODO: Implement failed() method.
  60. }
  61. }