From 6d9954c6d23dbebbfd47a1622466ca9d64963ced Mon Sep 17 00:00:00 2001 From: lanzu_qsy <334039090@qq.com> Date: Sat, 18 Jul 2020 11:04:08 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E7=BB=9F=E4=B8=80=E6=97=B6=E9=97=B4?= =?UTF-8?q?=E6=88=B3=E6=A0=BC=E5=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Model/Model.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/Model/Model.php b/app/Model/Model.php index 82ebdb5..f606652 100644 --- a/app/Model/Model.php +++ b/app/Model/Model.php @@ -18,4 +18,5 @@ use Hyperf\ModelCache\CacheableInterface; abstract class Model extends BaseModel implements CacheableInterface { use Cacheable; + protected $dateFormat = 'U'; } From 099f18628fdd8011a81ac73cf8b27563dfe3e0d8 Mon Sep 17 00:00:00 2001 From: "DESKTOP-GG6FIN9\\Administrator" <15040771@qq.com> Date: Sat, 18 Jul 2020 12:05:02 +0800 Subject: [PATCH 2/6] =?UTF-8?q?=E5=A4=B1=E6=95=88=E4=BC=98=E6=83=A0?= =?UTF-8?q?=E5=88=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Controller/CouponController.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/Controller/CouponController.php b/app/Controller/CouponController.php index 78cacc1..ed6efa8 100644 --- a/app/Controller/CouponController.php +++ b/app/Controller/CouponController.php @@ -35,7 +35,10 @@ class CouponController extends BaseController $ids = collect($ids)->unique(); $c = Coupon::where('start_time','<=',$nowTime) - ->where('end_time','>',$nowTime) + ->where([ + ['end_time','>',$nowTime], + ['status','=',1], + ]) ->whereRaw('inventory_use < inventory') ->whereIn('id',$ids) ->orderBy('weigh','desc') From eeb804d427fbfff11600c8a628bb0d90160c32af Mon Sep 17 00:00:00 2001 From: "DESKTOP-GG6FIN9\\Administrator" <15040771@qq.com> Date: Sat, 18 Jul 2020 15:26:45 +0800 Subject: [PATCH 3/6] =?UTF-8?q?=E5=A4=B1=E6=95=88=E4=BC=98=E6=83=A0?= =?UTF-8?q?=E5=88=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Controller/CouponController.php | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/app/Controller/CouponController.php b/app/Controller/CouponController.php index ed6efa8..f470362 100644 --- a/app/Controller/CouponController.php +++ b/app/Controller/CouponController.php @@ -34,14 +34,15 @@ class CouponController extends BaseController $ids = collect($ids)->unique(); - $c = Coupon::where('start_time','<=',$nowTime) + $c = Db::table('ims_system_coupon_user_receive as receive') ->where([ ['end_time','>',$nowTime], ['status','=',1], ]) - ->whereRaw('inventory_use < inventory') + ->join('ims_system_coupon_user as u', 'u.id', '=', 'receive.system_coupon_user_id') + ->whereRaw('inventory_use <= inventory') ->whereIn('id',$ids) - ->orderBy('weigh','desc') + ->orderBy('weigh','desc','id','desc') ->limit(4) ->get(); return $this->success(['not_reveive'=>$c]); From 13d4ea152c1432102cd2d1e7bf9630d79682a46a Mon Sep 17 00:00:00 2001 From: "DESKTOP-GG6FIN9\\Administrator" <15040771@qq.com> Date: Sat, 18 Jul 2020 15:39:57 +0800 Subject: [PATCH 4/6] =?UTF-8?q?=E5=A4=B1=E6=95=88=E4=BC=98=E6=83=A0?= =?UTF-8?q?=E5=88=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Controller/CouponController.php | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) diff --git a/app/Controller/CouponController.php b/app/Controller/CouponController.php index dd6d991..822c23d 100644 --- a/app/Controller/CouponController.php +++ b/app/Controller/CouponController.php @@ -36,15 +36,17 @@ class CouponController extends BaseController $ids = collect($ids)->unique(); - $c = Db::table('ims_system_coupon_user_receive as receive') + $c = Db::table('ims_system_coupon_user as u') ->where([ - ['end_time','>',$nowTime], - ['status','=',1], + ['u.end_time','>',$nowTime], + ['u.start_time','<=',$nowTime], + ['u.status','=',1], ]) - ->join('ims_system_coupon_user as u', 'u.id', '=', 'receive.system_coupon_user_id') + ->join('ims_system_coupon_user_receivetype as type', 'u.id', '=', 'type.system_coupon_user_id') ->whereRaw('inventory_use <= inventory') - ->whereIn('id',$ids) - ->orderBy('weigh','desc','id','desc') + ->whereIn('u.id',$ids) + ->select('u.*','type.one_receive_number') + ->orderBy('u.weigh','desc') ->limit(4) ->get(); return $this->success(['not_reveive'=>$c]); From ff48a3d7c6030b9ca853e8bf0262ddde5b47788d Mon Sep 17 00:00:00 2001 From: "DESKTOP-GG6FIN9\\Administrator" <15040771@qq.com> Date: Sat, 18 Jul 2020 15:58:06 +0800 Subject: [PATCH 5/6] =?UTF-8?q?=E5=A4=B1=E6=95=88=E4=BC=98=E6=83=A0?= =?UTF-8?q?=E5=88=B8?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Controller/CouponController.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/Controller/CouponController.php b/app/Controller/CouponController.php index 822c23d..f1ea847 100644 --- a/app/Controller/CouponController.php +++ b/app/Controller/CouponController.php @@ -30,7 +30,9 @@ 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()); @@ -40,6 +42,7 @@ class CouponController extends BaseController ->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') From 73bc391d14a5af78b598adbc4112a3c7168ee355 Mon Sep 17 00:00:00 2001 From: lanzu_qsy <334039090@qq.com> Date: Sat, 18 Jul 2020 16:26:34 +0800 Subject: [PATCH 6/6] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E7=94=A8=E6=88=B7?= =?UTF-8?q?=E4=BC=98=E6=83=A0=E5=88=B8=E6=95=B0=E9=87=8F=E6=98=BE=E7=A4=BA?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Controller/CouponController.php | 220 +++++++++++++++------------- 1 file changed, 119 insertions(+), 101 deletions(-) diff --git a/app/Controller/CouponController.php b/app/Controller/CouponController.php index 78cacc1..4ad7e7b 100644 --- a/app/Controller/CouponController.php +++ b/app/Controller/CouponController.php @@ -9,6 +9,7 @@ declare(strict_types=1); * @contact group@hyperf.io * @license https://github.com/hyperf/hyperf/blob/master/LICENSE */ + namespace App\Controller; use App\Model\CouponUserRecType; @@ -21,126 +22,143 @@ class CouponController extends BaseController /** * 获取用户可领取优惠卷接口 - */ - public function getSystemCouponUserList(){ - $user_id = $this->request->input('user_id'); + */ + public function getSystemCouponUserList() + { + $user_id = $this->request->input('user_id'); $receive_type = $this->request->input('receive_type'); - $c_ids = CouponUserRecType::where('receive_type',$receive_type)->pluck('system_coupon_user_id'); + $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 = 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 = 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]); + ->select(['ims_system_coupon_user_receive.number_remain']) + ->leftJoin('ims_system_coupon_user', + 'ims_system_coupon_user_receive.system_coupon_user_id', + '=', + 'ims_system_coupon_user.id') + ->where('ims_system_coupon_user.usable_end_time','>',$nowTime) + ->where('ims_system_coupon_user_receive.user_id','=',$user_id) + ->whereIn('ims_system_coupon_user_receive.status',[0,1]) + ->get(); + if (count($userCouponCount)){ + $count = array_sum(array_column($userCouponCount->toArray(),'number_remain')); + }else{ + $count = 0; + } + return $this->success(['total' => $count]); } - /** + /** * 用户领取优惠卷 */ public function userReceiveCoupon() { - - $userId = $this->request->input("user_id"); + $userId = $this->request->input("user_id"); $receiveType = $this->request->input("receive_type"); - $ids = $this->request->input("ids"); - $test = $this->request->input("test",0); - + $ids = $this->request->input("ids"); + $test = $this->request->input("test", 0); - $ids = is_array($ids)?implode(',',$ids):$ids; - - $cps = Coupon::whereIn('id', $ids)->get(); - $now = time(); + $ids = explode(',', $ids); + $cps = Coupon::whereIn('id', $ids)->get(); + $now = time(); $success = []; - $fail = []; + $fail = []; foreach ($cps as $key => $cp) { - - - $crt = CouponUserRecType::where('system_coupon_user_id',$cp->id)->first(); - - //TODO 会有超发情况 - $cr = new CouponRec; - $cr->user_id = $userId; + $crt = CouponUserRecType::where( + [ + 'system_coupon_user_id' => $cp->id, + 'receive_type' => $receiveType + ] + )->first(); + + $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->nnumber_remain = $crt->one_receive_number; - $cr->status = 0; - $cr->update_time = $now; - $cr->receive_type = $receiveType; - - if($test){ + $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 && ($cp->id % 2)) { $fail[] = $cp; - }else{ - if($cr->save()){ - $success[] = $cp; - }else{ + } else { + //TODO 会有超发情况 + //如果优惠卷库存小于等于已领取的数量, 则返回领取失败的优惠券 + if ($cp->inventory<=$cp->inventory_use||$cp->inventory<=($cp->inventory_use+$cr->number)){ $fail[] = $cp; + }else{ + $cp->inventory_use += $cr->number;//记录已领取的数量 + if ($cr->save()&&$cp->save()) { + $success[] = $cp; + } else { + $fail[] = $cp; + } } } - + } return $this->success([ - 'success'=>$success, - 'fail'=>$fail, + 'success' => $success, + 'fail' => $fail, ]); } - /** - * 获取用户已经领取的优惠卷列表 - */ + /** + * 获取用户已经领取的优惠卷列表 + */ public function getUserReceiveCouponList() { - $userId = $this->request->input("user_id"); + $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'); + $couponIds = CouponRec::where('user_id', $userId) + ->whereIn('status', [0, 1]) + ->orderBy('receive_time', 'desc') + ->get() + ->pluck('system_coupon_user_id'); $not_expired = []; - $expired = []; + $expired = []; $couponIds = $couponIds->toArray(); - $coupons = Coupon::orderByRaw('FIELD(id, '.implode(", " , $couponIds).')')->get(); + $coupons = Coupon::orderByRaw('FIELD(id, ' . implode(", ", $couponIds) . ')')->get(); foreach ($coupons as $key => $coupon) { - if($coupon->usable_end_time < $nowTime){ + if ($coupon->usable_end_time < $nowTime) { $expired[] = $coupon; - }else{ + } else { $not_expired[] = $coupon; } } - $ret = ['not_expired'=>$not_expired,'expired'=>$expired]; + $ret = ['not_expired' => $not_expired, 'expired' => $expired]; return $this->success($ret); @@ -154,15 +172,15 @@ class CouponController extends BaseController { // 获取参数 # 订单金额 - $orderAmount = $this->request->input('order_amount',0); + $orderAmount = $this->request->input('order_amount', 0); # 用户id - $userId = $this->request->input('user_id',0); + $userId = $this->request->input('user_id', 0); # 市场id - $marketId = $this->request->input('market_id',0); + $marketId = $this->request->input('market_id', 0); # 类型,1全平台 2线上 3线下,20200718默认全平台 - $type = $this->request->input('type',1); + $type = $this->request->input('type', 1); # 店铺类型id - $storetypeId = $this->request->input('storetype_id',0); + $storetypeId = $this->request->input('storetype_id', 0); // 获取用户优惠券 $currentTime = time(); @@ -184,23 +202,23 @@ 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]) + ->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('coupon.market_id', 'in', [0, $marketId]) ->where(function ($query) use ($storetypeId) { $query->whereOr( [ [ - ['coupon.type', 'in', [1,2]], + ['coupon.type', 'in', [1, 2]], ['coupon.storetype_id', '=', 0] ], [ - ['coupon.type', 'in', [1,3]], + ['coupon.type', 'in', [1, 3]], ['coupon.storetype_id', '=', $storetypeId] ] ] @@ -232,30 +250,30 @@ class CouponController extends BaseController // 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') + // ->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();