AutoJobs.php 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. <?php
  2. namespace App\Jobs\Api\Coupon;
  3. use Illuminate\Bus\Queueable;
  4. use Illuminate\Contracts\Queue\ShouldQueue;
  5. use Illuminate\Foundation\Bus\Dispatchable;
  6. use Illuminate\Queue\InteractsWithQueue;
  7. use Illuminate\Queue\SerializesModels;
  8. use App\Servers\DB\DbService;
  9. use App\Facades\Servers\Logs\Log;
  10. /**
  11. * 自动发放优惠券
  12. */
  13. class AutoJobs implements ShouldQueue
  14. {
  15. use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
  16. protected $autoData;
  17. /**
  18. * Create a new job instance.
  19. *
  20. * @return void
  21. */
  22. public function __construct(array $autoData)
  23. {
  24. $this->autoData = $autoData;
  25. }
  26. /**
  27. * 自动发放优惠券队列运行失败
  28. * @return void
  29. */
  30. public function handle()
  31. {
  32. try {
  33. $companyId = empty($this->autoData['company_id']) ? 0 : $this->autoData['company_id'];
  34. $uid = empty($this->autoData['custom_uid']) ? 0 : $this->autoData['custom_uid'];
  35. // 切换数据库
  36. $result = (new DbService())->getConnectionNameByCompanyId($companyId);
  37. // 如果失败
  38. if( !isset($result['error']) ) $this->autoCoupon($uid);
  39. // 成功处理...
  40. } catch (\Exception $e) {
  41. Log::info('job_error', '自动发放优惠券队列运行失败', ['error' => $e->getMessage()]);
  42. }
  43. }
  44. public function failed(\Throwable $exception)
  45. {
  46. Log::info('job_error', '自动发放优惠券彻底失败', ['error' => $exception]);
  47. }
  48. /**
  49. * 自动发放优惠券
  50. *
  51. */
  52. private function autoCoupon($uid){
  53. // 模型实例
  54. $Rule = new \App\Models\Manager\CouponRewardRule();
  55. // 获取配置列表
  56. $ruleList = $Rule->getList();
  57. // 如果没有信息的话
  58. if( !$ruleList ) return ['success'=>'暂无活动'];
  59. // 其他实例
  60. $RuleProduct = new \App\Models\Manager\CouponRewardProduct();
  61. $OrdersProduct = new \App\Models\Manager\OrdersProduct();
  62. $Custom = new \App\Models\Manager\Custom();
  63. $Coupon = new \App\Models\Manager\Coupon();
  64. $CustomCoupon = new \App\Models\Manager\CustomCoupon();
  65. // 获取客户城市ID
  66. $customCityId = (int) $Custom->getValue($uid,'city_id');
  67. // 循环配置列表
  68. foreach ( $ruleList as $value ) {
  69. // 如果存在城市范围,并且不在城市范围,不参与这个活动
  70. if( $value['city_ids'] && !in_array($customCityId,explode(',',$value['city_ids'])) ) continue;
  71. // 未到开始时间
  72. if( $value['start_time'] > time() ) continue;
  73. // 通过配置ID获取对应的商品范围
  74. $productList = $RuleProduct->getListByRule($value['id']);
  75. // 如果不存在产品范围,跳过
  76. if( !$productList ) continue;
  77. // 获取客户 规定时段内订单的商品ID以及购买数量
  78. $orderList = $OrdersProduct->query()->where([['custom_uid','=',$uid],['insert_time','>=',$value['start_time']],['insert_time','<=',$value['end_time']]])->whereIn('status',[0,2])->get(['product_id','buy_num'])->toArray();
  79. // 如果没有订单总数
  80. if( !$orderList ) continue;
  81. // 计算商品总量
  82. $total = 0;
  83. // 循环商品范围
  84. foreach ($productList as $scope) {
  85. // 循环订单产品
  86. foreach ($orderList as $order) {
  87. // 如果产品不相等
  88. if( $scope['product_id'] != $order['product_id'] ) continue;
  89. // 相等的计算总量
  90. $total += $scope['product_units'] * $order['buy_num'];
  91. }
  92. }
  93. // 判断总数是不是达标
  94. if( $total < $value['std_num'] ) continue;
  95. if ($value['coupon_id']){
  96. $couponIdArray = explode(',',$value['coupon_id']);
  97. foreach ($couponIdArray as $couponId) {
  98. // 达标的是否已经发送过优惠券
  99. $havaCoupon = $CustomCoupon->query()->where([['custom_uid','=',$uid],['coupon_id','=',$couponId]])->first(['status']);
  100. // 已经发过优惠券的,不发
  101. if( $havaCoupon ) continue;
  102. // 获取优惠券的可用时间
  103. $expTime = $Coupon->query()->where([['id','=',$couponId]])->value('exp_time');
  104. // 时间转时间
  105. $expTime = $Coupon->getExpTime($expTime);
  106. // 发送优惠券
  107. $CustomCoupon->add(['coupon_id'=>$couponId,'custom_uid'=>$uid,'exp_time'=>$expTime]);
  108. }
  109. }
  110. }
  111. // 返回成功
  112. return ['success'=>'操作成功'];
  113. }
  114. }