From f6eb266a86d1c0e1eb9bd20047386624b9ba9f5f Mon Sep 17 00:00:00 2001 From: liangyuyan <1103300295@qq.com> Date: Tue, 4 Aug 2020 11:56:57 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BC=98=E6=83=A0=E5=88=B8=E8=BF=94=E5=88=B8--?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=A2=86=E5=8F=96=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/Controller/CouponController.php | 1 + app/Controller/CouponRebateController.php | 91 ++++++++++++++++++++++ app/Request/couponRebateReceiveRequest.php | 48 ++++++++++++ 3 files changed, 140 insertions(+) create mode 100644 app/Request/couponRebateReceiveRequest.php diff --git a/app/Controller/CouponController.php b/app/Controller/CouponController.php index 409c2cf..5152e63 100644 --- a/app/Controller/CouponController.php +++ b/app/Controller/CouponController.php @@ -67,6 +67,7 @@ class CouponController extends BaseController ['u.end_time','>',$nowTime], ['u.start_time','<=',$nowTime], ['u.status','=',1], + ['u.active_type','=',1], ]; if (env('SUB_CHANNEL') == 1) { diff --git a/app/Controller/CouponRebateController.php b/app/Controller/CouponRebateController.php index 3f90802..12696a6 100644 --- a/app/Controller/CouponRebateController.php +++ b/app/Controller/CouponRebateController.php @@ -17,6 +17,10 @@ use Hyperf\Utils\ApplicationContext; use App\CouponRebate\CouponRebateInterface; use http\Client\Curl\User; use Hyperf\Di\Annotation\Inject; +use App\Model\Coupon; +use App\Model\CouponUserRecType; +use App\Model\CouponRec; +use App\Request\CouponRebateReceiveRequest; class CouponRebateController extends BaseController { /** @@ -30,4 +34,91 @@ class CouponRebateController extends BaseController $res = $this->CouponRebate->isCouponRebate($user_id); return $this->success($res); } + + /** + * 用户领取优惠券 + */ + public function userReceiveCouponA(couponRebateReceiveRequest $validator) + { + $userId = $this->request->input("user_id", 0); + $receiveType = $this->request->input("receive_type", 0); + $ids = $this->request->input("ids", ''); + $sendUserId = $this->request->input("send_user_id", 0); + $phone = $this->request->input("phone", ''); + $ids = explode(',', $ids); + $now = time(); + + // moke 数据 给前端用 + $test = $this->request->input("test", 0); + if($test){ + $cps = Coupon::whereIn('id', $ids)->lockForUpdate()->first(); + return $this->success([ + 'success' => [$cps], + 'fail' => [], + ]); + } + + $success = []; + $fail = []; + + Db::transaction( function () use ($ids,$receiveType,$userId,$sendUserId,$phone,&$success,&$fail,$now) { + //读写锁,完全控制,性能低 + $cps = Coupon::whereIn('id', $ids)->lockForUpdate() + ->select('id','title','status','inventory','inventory_use','start_time','end_time') + ->get(); + + foreach ($cps as $key => $cp) { + + $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; + $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; + $cr->send_user_id = $sendUserId; + $cr->phone = $phone; + //如果优惠卷库存小于等于已领取的数量 或者 未在活动时间内, 则返回领取失败的优惠券 + if ( + $cp->status != 1 + || + $cp->inventory <= $cp->inventory_use + || + $cp->inventory < ($cp->inventory_use+$cr->number) + || + $cp->start_time < $now + || + $cp->end_time > $now + ) + { + $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, + ]); + } } diff --git a/app/Request/couponRebateReceiveRequest.php b/app/Request/couponRebateReceiveRequest.php new file mode 100644 index 0000000..4c4beed --- /dev/null +++ b/app/Request/couponRebateReceiveRequest.php @@ -0,0 +1,48 @@ + 'required|nonempty|integer|exists_enable:ims_cjdc_user,id', + 'receive_type' => 'required|nonempty|integer', + 'ids' => 'required|nonempty', + ]; + } + + public function messages(): array + { + return [ + 'user_id.*' => ':attribute信息不正确', + 'receive_type.*' => ':attribute必须', + 'ids.*' => ':attribute必须', + ]; + } + + public function attributes(): array + { + return [ + 'user_id' => '领取用户ID', + 'receive_type' => '领取方式', + 'ids' => '优惠券', + ]; + } +}