diff --git a/app/Controller/CouponController.php b/app/Controller/CouponController.php index e8b26e2..4565025 100644 --- a/app/Controller/CouponController.php +++ b/app/Controller/CouponController.php @@ -34,33 +34,52 @@ class CouponController extends BaseController $this->success(['not_reveive' => []]); } - $c_ids = CouponUserRecType::where('receive_type', $receive_type)->pluck('system_coupon_user_id'); + $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'); + } + $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(); + // 当前用户可领的优惠券ID + $couponIds = array_diff($c_ids, $cr_ids); + $c = Db::table('ims_system_coupon_user as u') - ->where([ - ['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(); + ->where([ + ['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) + ->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);