diff --git a/app/Controller/CouponController.php b/app/Controller/CouponController.php index 4ad7e7b..9280c58 100644 --- a/app/Controller/CouponController.php +++ b/app/Controller/CouponController.php @@ -16,6 +16,8 @@ use App\Model\CouponUserRecType; use App\Model\Coupon; use App\Model\CouponRec; use Hyperf\DbConnection\Db; +use Hyperf\Redis\Redis; +use Hyperf\Utils\ApplicationContext; class CouponController extends BaseController { @@ -30,20 +32,29 @@ class CouponController extends BaseController $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'); + $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]); + $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('inventory_use <= inventory') + ->whereIn('u.id',$ids) + ->select('u.*','type.one_receive_number') + ->orderBy('u.weigh','desc') + ->limit(4) + ->get(); + return $this->success(['not_reveive'=>$c]); } //统计用户 @@ -181,10 +192,14 @@ class CouponController extends BaseController $type = $this->request->input('type', 1); # 店铺类型id $storetypeId = $this->request->input('storetype_id', 0); + $storetypeIds = explode(',', str_replace(',', ',', $storetypeId)); // 获取用户优惠券 $currentTime = time(); + $container = ApplicationContext::getContainer(); + $redis = $container->get(Redis::class); + $couponIds = $redis->sMembers('coupon_'.date('Ymd').'_used_'.$userId); $data = Db::table('ims_system_coupon_user_receive as receive') ->select([ @@ -201,83 +216,25 @@ class CouponController extends BaseController ]) ->join('ims_system_coupon_user as coupon', 'coupon.id', '=', 'receive.system_coupon_user_id') ->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) + ->whereIn('receive.status', [0,1]) + ->where('receive.number_remain', '>', 0); + + if (is_array($couponIds)&&!empty($couponIds)) { + $data->whereNotIn('coupon.id', $couponIds); + } + + $data = $data->whereIn('coupon.type', [1,$type]) ->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] - ] - ] - ); - }) + ->whereIn('coupon.storetype_id', $storetypeIds) ->orderByRaw('coupon.discounts DESC, coupon.full_amount DESC') ->get(); - //var_dump($d); return $this->success($data); - // $coupons = CouponRec::alias('receive') - // ->field([ - // 'receive.id 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' - // ]) - // ->withAttr('usable_start_time', function ($value){ - // return $value ? date('Y-m-d H:i:s', $value) : ''; - // }) - // ->withAttr('usable_end_time', function ($value){ - // return $value ? date('Y-m-d H:i:s', $value) : ''; - // }) - // ->join(Coupon::getTable() . ' coupon ', 'coupon.id=receive.system_coupon_user_id') - // ->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] - // ] - // ] - // ); - // }) - // ->order('coupon.discounts DESC, coupon.full_amount DESC') - // ->select() - // ->toArray(); - - // var_dump($coupons); } }