12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- <?php namespace App\Http\Controllers\Api;
- use App\Http\Controllers\Api\Api;
- /**
- * 测试接口
- *
- * @author 刘相欣
- *
- * */
- class Test extends Api{
-
-
- /**
- * 同步 /api/test/sync_user
- *
- * */
- public function sync_user(){
- $uid = 2;
- return $this->autoCoupon($uid);
- }
- /**
- * 自动发放优惠券
- *
- */
- private function autoCoupon($uid){
- // 模型实例
- $Rule = new \App\Models\CouponRewardRule();
- $RuleProduct = new \App\Models\CouponRewardProduct();
- $OrdersProduct = new \App\Models\OrdersProduct();
- $Custom = new \App\Models\Custom();
- $Coupon = new \App\Models\Coupon();
- $CustomCoupon = new \App\Models\CustomCoupon();
- // 获取配置列表
- $ruleList = $Rule->getList();
- // 如果没有信息的话
- if( !$ruleList ) return ['success'=>'暂无活动'];
- // 获取客户城市ID
- $customCityId = (int) $Custom->getValue($uid,'city_id');
- // 循环配置列表
- foreach ( $ruleList as $value ) {
- // 如果存在城市范围,并且不在城市范围,不参与这个活动
- if( $value['city_ids'] && !in_array($customCityId,explode(',',$value['city_ids'])) ) continue;
- // 未到开始时间
- if( $value['start_time'] > time() ) continue;
- // 通过配置ID获取对应的商品范围
- $productList = $RuleProduct->getListByRule($value['id']);
- // 如果不存在产品范围,跳过
- if( !$productList ) continue;
- // 获取客户 规定时段内订单的商品ID以及购买数量
- $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();
- // 如果没有订单总数
- if( !$orderList ) continue;
- // 计算商品总量
- $total = 0;
- // 循环商品范围
- foreach ($productList as $scope) {
- // 循环订单产品
- foreach ($orderList as $order) {
- // 如果产品不相等
- if( $scope['product_id'] != $order['product_id'] ) continue;
- // 相等的计算总量
- $total += $scope['product_units'] * $order['buy_num'];
- }
- }
- // 判断总数是不是达标
- if( $total < $value['std_num'] ) continue;
- // 达标的是否已经发送过优惠券
- $havaCoupon = $CustomCoupon->query()->where([['custom_uid','=',$uid],['coupon_id','=',$value['coupon_id']]])->first(['status']);
- // 已经发过优惠券的,不发
- if( $havaCoupon ) continue;
- // 获取优惠券的可用时间
- $expTime = $Coupon->query()->where([['id','=',$value['coupon_id']]])->value('exp_time');
- // 时间转时间
- $expTime = $Coupon->getExpTime($expTime);
- // 发送优惠券
- $CustomCoupon->add(['coupon_id'=>$value['coupon_id'],'custom_uid'=>$uid,'exp_time'=>$expTime]);
- }
- // 返回成功
- return ['success'=>'操作成功'];
- }
- }
|