Test.php 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. <?php namespace App\Http\Controllers\Api;
  2. use App\Http\Controllers\Api\Api;
  3. /**
  4. * 测试接口
  5. *
  6. * @author 刘相欣
  7. *
  8. * */
  9. class Test extends Api{
  10. /**
  11. * 同步 /api/test/sync_user
  12. *
  13. * */
  14. public function sync_user(){
  15. $uid = 2;
  16. return $this->autoCoupon($uid);
  17. }
  18. /**
  19. * 自动发放优惠券
  20. *
  21. */
  22. private function autoCoupon($uid){
  23. // 模型实例
  24. $Rule = new \App\Models\CouponRewardRule();
  25. $RuleProduct = new \App\Models\CouponRewardProduct();
  26. $OrdersProduct = new \App\Models\OrdersProduct();
  27. $Custom = new \App\Models\Custom();
  28. $Coupon = new \App\Models\Coupon();
  29. $CustomCoupon = new \App\Models\CustomCoupon();
  30. // 获取配置列表
  31. $ruleList = $Rule->getList();
  32. // 如果没有信息的话
  33. if( !$ruleList ) return ['success'=>'暂无活动'];
  34. // 获取客户城市ID
  35. $customCityId = (int) $Custom->getValue($uid,'city_id');
  36. // 循环配置列表
  37. foreach ( $ruleList as $value ) {
  38. // 如果存在城市范围,并且不在城市范围,不参与这个活动
  39. if( $value['city_ids'] && !in_array($customCityId,explode(',',$value['city_ids'])) ) continue;
  40. // 未到开始时间
  41. if( $value['start_time'] > time() ) continue;
  42. // 通过配置ID获取对应的商品范围
  43. $productList = $RuleProduct->getListByRule($value['id']);
  44. // 如果不存在产品范围,跳过
  45. if( !$productList ) continue;
  46. // 获取客户 规定时段内订单的商品ID以及购买数量
  47. $orderList = $OrdersProduct->query()->where([['custom_uid','=',$uid],['status','=',1],['insert_time','>=',$value['start_time']],['insert_time','<=',$value['end_time']]])->get(['product_id','buy_num'])->toArray();
  48. // 如果没有订单总数
  49. if( !$orderList ) continue;
  50. // 计算商品总量
  51. $total = 0;
  52. // 循环商品范围
  53. foreach ($productList as $scope) {
  54. // 循环订单产品
  55. foreach ($orderList as $order) {
  56. // 如果产品不相等
  57. if( $scope['product_id'] != $order['product_id'] ) continue;
  58. // 相等的计算总量
  59. $total += $scope['product_units'] * $order['buy_num'];
  60. }
  61. }
  62. // 判断总数是不是达标
  63. if( $total < $value['std_num'] ) continue;
  64. // 达标的是否已经发送过优惠券
  65. $havaCoupon = $CustomCoupon->query()->where([['custom_uid','=',$uid],['coupon_id','=',$value['coupon_id']]])->first(['status']);
  66. // 已经发过优惠券的,不发
  67. if( $havaCoupon ) continue;
  68. // 获取优惠券的可用时间
  69. $expTime = $Coupon->query()->where([['id','=',$value['coupon_id']]])->value('exp_time');
  70. // 时间转时间
  71. $expTime = $Coupon->getExpTime($expTime);
  72. // 发送优惠券
  73. $CustomCoupon->add(['coupon_id'=>$value['coupon_id'],'custom_uid'=>$uid,'exp_time'=>$expTime]);
  74. }
  75. // 返回成功
  76. return ['success'=>'操作成功'];
  77. }
  78. }