request->input('user_id'); $receive_type = $this->request->input('receive_type'); $c_ids = CouponUserRecType::where('receive_type',$receive_type)->pluck('system_coupon_user_id'); $nowTime = time(); $cr_ids = CouponRec::where('user_id',$user_id)->pluck('system_coupon_user_id'); $ids = array_merge($c_ids->toArray(),$cr_ids->toArray()); $ids = collect($ids)->unique(); $c = Coupon::where('start_time','<=',$nowTime) ->where('end_time','>',$nowTime) ->whereRaw('inventory_use < inventory') ->whereIn('id',$ids) ->orderBy('weigh','desc') ->limit(4) ->get(); return $this->success(['not_reveive'=>$c]); } public function userCouponAccount() { $user_id = $this->request->input('user_id'); $nowTime = time(); $userCouponCount = DB::table('ims_system_coupon_user_receive') ->leftJoin('ims_system_coupon_user', 'ims_system_coupon_user_receive.system_coupon_user_id', '=', 'ims_system_coupon_user.id') ->count(); // $userCouponCount = CouponRec::with('coupon')->where('user_id',$user_id)->where('usable_start_time','<=',$nowTime) // ->where('usable_end_time','>',$nowTime)->count(); return $this->success(['total'=>$userCouponCount]); } /** * 用户领取优惠卷 */ public function userReceiveCoupon() { $userId = $this->request->input("user_id"); $receiveType = $this->request->input("receive_type"); $ids = $this->request->input("ids"); $test = $this->request->input("test", 0); $ids = explode(',', $ids); $cps = Coupon::whereIn('id', $ids)->get(); $now = time(); $success = []; $fail = []; foreach ($cps as $key => $cp) { $crt = CouponUserRecType::where( [ 'system_coupon_user_id' => $cp->id, 'receive_type' => $receiveType ] )->first(); //TODO 会有超发情况 $cr = new CouponRec; $cr->user_id = $userId; $cr->system_coupon_user_id = $cp->id; $cr->order_main_id = 0; $cr->receive_time = $now; $cr->number = $crt->one_receive_number; $cr->number_remain = $crt->one_receive_number; $cr->status = 0; $cr->update_time = $now; $cr->receive_type = $receiveType; if ($test) { $fail[] = $cp; } else { if ($cr->save()) { $success[] = $cp; } else { $fail[] = $cp; } } } return $this->success([ 'success' => $success, 'fail' => $fail, ]); } /** * 获取用户已经领取的优惠卷列表 */ public function getUserReceiveCouponList() { $userId = $this->request->input("user_id"); $nowTime = time(); $couponIds = CouponRec::where('user_id',$userId) ->whereIn('status',[0,1]) ->orderBy('receive_time','desc') ->get() ->pluck('system_coupon_user_id'); $not_expired = []; $expired = []; $couponIds = $couponIds->toArray(); $coupons = Coupon::orderByRaw('FIELD(id, '.implode(", " , $couponIds).')')->get(); foreach ($coupons as $key => $coupon) { if($coupon->usable_end_time < $nowTime){ $expired[] = $coupon; }else{ $not_expired[] = $coupon; } } $ret = ['not_expired'=>$not_expired,'expired'=>$expired]; return $this->success($ret); } /** * 获取用户当前订单可用的优惠券列表 * 按分类(1订单 等优惠)分组返回 */ public function getUserAvailableCoupons() { // 获取参数 # 订单金额 $orderAmount = $this->request->input('order_amount',0); # 用户id $userId = $this->request->input('user_id',0); # 市场id $marketId = $this->request->input('market_id',0); # 类型,1全平台 2线上 3线下,20200718默认全平台 $type = $this->request->input('type',1); # 店铺类型id $storetypeId = $this->request->input('storetype_id',0); // 获取用户优惠券 $currentTime = time(); $container = ApplicationContext::getContainer(); $redis = $container->get(Redis::class); $couponDayUsed = $redis->hGetAll('coupon_day_used'); $couponRecIds = array_values($couponDayUsed); $data = Db::table('ims_system_coupon_user_receive as receive') ->select([ 'receive.id as receive_id', 'receive.user_id', 'receive.number_remain', 'coupon.id', 'coupon.title', 'coupon.full_amount', 'coupon.discounts', 'coupon.usable_start_time', 'coupon.usable_end_time', 'coupon.discount_type' ]) ->join('ims_system_coupon_user as coupon', 'coupon.id', '=', 'receive.system_coupon_user_id') ->whereNotIn('receive.id', $couponRecIds) ->where(['receive.user_id' => $userId]) ->where(['receive.user_id' => $userId]) ->whereIn('coupon.type', [1,$type]) ->whereIn('receive.status', [0,1]) ->where('receive.number_remain', '>', 0) ->where('coupon.full_amount', '<=', $orderAmount) ->where('coupon.usable_start_time', '<=', $currentTime) ->where('coupon.usable_end_time', '>=', $currentTime) ->where('coupon.usable_number', '<=', Db::raw('receive.number_remain')) ->where('coupon.market_id', 'in', [0,$marketId]) ->where(function ($query) use ($storetypeId) { $query->whereOr( [ [ ['coupon.type', 'in', [1,2]], ['coupon.storetype_id', '=', 0] ], [ ['coupon.type', 'in', [1,3]], ['coupon.storetype_id', '=', $storetypeId] ] ] ); }) ->orderByRaw('coupon.discounts DESC, coupon.full_amount DESC') ->get(); return $this->success($data); } }