From 3c629bbebb94822923c92b943f4164d73a088851 Mon Sep 17 00:00:00 2001 From: weigang Date: Sat, 18 Jul 2020 11:58:17 +0800 Subject: [PATCH] =?UTF-8?q?1=E3=80=81=E7=94=A8=E6=88=B7=E9=A2=86=E5=8F=96?= =?UTF-8?q?=E4=BC=98=E6=83=A0=E5=88=B8=20=202=E3=80=81=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E6=97=B6=E9=97=B4=E6=A0=BC=E5=BC=8F=203=E3=80=81=E8=8E=B7?= =?UTF-8?q?=E5=8F=96=E7=94=A8=E6=88=B7=E5=8F=AF=E7=94=A8=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 | 117 +++++++++------------------- app/Model/Model.php | 1 + 2 files changed, 38 insertions(+), 80 deletions(-) diff --git a/app/Controller/CouponController.php b/app/Controller/CouponController.php index 78cacc1..7d82af1 100644 --- a/app/Controller/CouponController.php +++ b/app/Controller/CouponController.php @@ -15,6 +15,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 { @@ -63,51 +65,50 @@ class CouponController extends BaseController */ 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(); - + $crt = CouponUserRecType::where( + [ + 'system_coupon_user_id' => $cp->id, + 'receive_type' => $receiveType + ] + )->first(); //TODO 会有超发情况 - $cr = new CouponRec; - $cr->user_id = $userId; + $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) { $fail[] = $cp; - }else{ - if($cr->save()){ + } else { + if ($cr->save()) { $success[] = $cp; - }else{ + } else { $fail[] = $cp; } } - + } return $this->success([ - 'success'=>$success, - 'fail'=>$fail, + 'success' => $success, + 'fail' => $fail, ]); } @@ -167,6 +168,10 @@ class CouponController extends BaseController // 获取用户优惠券 $currentTime = time(); + $container = ApplicationContext::getContainer(); + $redis = $container->get(Redis::class); + $couponDayUsed = $redis->hGetAll('coupon_day_used'); + $couponRecIds = array_values($couponDayUsed); $data = Db::table('ims_system_coupon_user_receive as receive') ->select([ @@ -182,6 +187,7 @@ class CouponController extends BaseController 'coupon.discount_type' ]) ->join('ims_system_coupon_user as coupon', 'coupon.id', '=', 'receive.system_coupon_user_id') + ->whereNotIn('receive.id', $couponRecIds) ->where(['receive.user_id' => $userId]) ->where(['receive.user_id' => $userId]) ->whereIn('coupon.type', [1,$type]) @@ -209,57 +215,8 @@ class CouponController extends BaseController ->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); } } 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'; }