diff --git a/app/Controller/CouponController.php b/app/Controller/CouponController.php index e8b26e2..409c2cf 100644 --- a/app/Controller/CouponController.php +++ b/app/Controller/CouponController.php @@ -31,36 +31,60 @@ class CouponController extends BaseController $receive_type = $this->request->input('receive_type', 0); if ($this->empty($user_id) || $this->empty($receive_type)) { - $this->success(['not_reveive' => []]); + return $this->success(['not_reveive' => []]); + } + + $c_ids = []; + // 渠道开启,查询该渠道可以领取的优惠券ID + // 渠道未开启,查询所有优惠券 + if (env('SUB_CHANNEL') == 1) { + $c_ids = CouponUserRecType::where('receive_type', $receive_type)->pluck('system_coupon_user_id'); + } else { + $c_ids = Coupon::pluck('id'); } - $c_ids = CouponUserRecType::where('receive_type', $receive_type)->pluck('system_coupon_user_id'); $nowTime = time(); - $cr_ids = CouponRec::where([ - ['user_id',"=",$user_id], - ['receive_type',"=",$receive_type] - ])->pluck('system_coupon_user_id'); + + $where = [ + ['user_id',"=",$user_id] + ]; + + // 渠道开启,查询该用户在此渠道领过的优惠券ID + if (env('SUB_CHANNEL') == 1) { + array_push($where, ['receive_type', "=", $receive_type]); + } + + $cr_ids = CouponRec::where($where)->pluck('system_coupon_user_id'); //领过券的ID $c_ids = $c_ids->toArray(); $cr_ids = $cr_ids->toArray(); - $c = Db::table('ims_system_coupon_user as u') - ->where([ + // 当前用户可领的优惠券ID + $couponIds = array_diff($c_ids, $cr_ids); + + $whereC = [ ['u.end_time','>',$nowTime], ['u.start_time','<=',$nowTime], - ['type.receive_type','=',$receive_type], ['u.status','=',1], - ]) - ->join('ims_system_coupon_user_receivetype as type', 'u.id', '=', 'type.system_coupon_user_id') - ->whereRaw('u.inventory_use <= u.inventory and u.inventory-u.inventory_use > type.one_receive_number') - ->whereIn('u.id',$c_ids) - ->whereNotIn('u.id',$cr_ids) - ->select('u.*','type.one_receive_number') - ->orderBy('u.weigh','desc') - // ->orderByRaw('FIELD(u.id, '.implode(", " , $ids).')') - ->limit(4) - ->get(); + ]; + + if (env('SUB_CHANNEL') == 1) { + array_push($whereC, ['type.receive_type','=',$receive_type]); + } + + $c = Db::table('ims_system_coupon_user as u') + ->where($whereC) + ->join('ims_system_coupon_user_receivetype as type', 'u.id', '=', 'type.system_coupon_user_id') + ->whereRaw('u.inventory_use <= u.inventory and u.inventory-u.inventory_use > type.one_receive_number') + // ->whereIn('u.id',$c_ids) + // ->whereNotIn('u.id',$cr_ids) + ->whereIn('u.id', $couponIds) + ->select('u.*','type.one_receive_number') + ->orderBy('u.weigh','desc') + // ->orderByRaw('FIELD(u.id, '.implode(", " , $ids).')') + ->limit(4) + ->get(); foreach ($c as $k => &$v){ if($v->discount_type == 2){ $v->discounts = floatval($v->discounts); @@ -117,12 +141,16 @@ class CouponController extends BaseController //$cps = Coupon::whereIn('id', $ids)->sharedLock()->get(); foreach ($cps as $key => $cp) { - $crt = CouponUserRecType::where( - [ - 'system_coupon_user_id' => $cp->id, - 'receive_type' => $receiveType - ] - )->first(); + + $where = [ + 'system_coupon_user_id' => $cp->id, + ]; + + if (env('SUB_CHANNEL') == 1) { + $where['receive_type'] = $receiveType; + } + + $crt = CouponUserRecType::where($where)->first(); $cr = new CouponRec; $cr->user_id = $userId;