Browse Source

修复用户优惠券数量显示

master
lanzu_qsy 5 years ago
parent
commit
73bc391d14
  1. 112
      app/Controller/CouponController.php

112
app/Controller/CouponController.php

@ -9,6 +9,7 @@ declare(strict_types=1);
* @contact group@hyperf.io * @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE * @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/ */
namespace App\Controller; namespace App\Controller;
use App\Model\CouponUserRecType; use App\Model\CouponUserRecType;
@ -22,39 +23,51 @@ class CouponController extends BaseController
/** /**
* 获取用户可领取优惠卷接口 * 获取用户可领取优惠卷接口
*/ */
public function getSystemCouponUserList(){
public function getSystemCouponUserList()
{
$user_id = $this->request->input('user_id'); $user_id = $this->request->input('user_id');
$receive_type = $this->request->input('receive_type'); $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(); $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(); $ids = collect($ids)->unique();
$c = Coupon::where('start_time','<=',$nowTime)
->where('end_time','>',$nowTime)
$c = Coupon::where('start_time', '<=', $nowTime)
->where('end_time', '>', $nowTime)
->whereRaw('inventory_use < inventory') ->whereRaw('inventory_use < inventory')
->whereIn('id',$ids)
->orderBy('weigh','desc')
->whereIn('id', $ids)
->orderBy('weigh', 'desc')
->limit(4) ->limit(4)
->get(); ->get();
return $this->success(['not_reveive'=>$c]);
return $this->success(['not_reveive' => $c]);
} }
//统计用户
public function userCouponAccount() public function userCouponAccount()
{ {
$user_id = $this->request->input('user_id'); $user_id = $this->request->input('user_id');
$nowTime = time(); $nowTime = time();
$userCouponCount = DB::table('ims_system_coupon_user_receive') $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]);
} }
@ -63,51 +76,56 @@ class CouponController extends BaseController
*/ */
public function userReceiveCoupon() public function userReceiveCoupon()
{ {
$userId = $this->request->input("user_id"); $userId = $this->request->input("user_id");
$receiveType = $this->request->input("receive_type"); $receiveType = $this->request->input("receive_type");
$ids = $this->request->input("ids"); $ids = $this->request->input("ids");
$test = $this->request->input("test",0);
$ids = is_array($ids)?implode(',',$ids):$ids;
$test = $this->request->input("test", 0);
$ids = explode(',', $ids);
$cps = Coupon::whereIn('id', $ids)->get(); $cps = Coupon::whereIn('id', $ids)->get();
$now = time(); $now = time();
$success = []; $success = [];
$fail = []; $fail = [];
foreach ($cps as $key => $cp) { foreach ($cps as $key => $cp) {
$crt = CouponUserRecType::where(
[
'system_coupon_user_id' => $cp->id,
'receive_type' => $receiveType
]
)->first();
$crt = CouponUserRecType::where('system_coupon_user_id',$cp->id)->first();
//TODO 会有超发情况
$cr = new CouponRec; $cr = new CouponRec;
$cr->user_id = $userId; $cr->user_id = $userId;
$cr->system_coupon_user_id = $cp->id; $cr->system_coupon_user_id = $cp->id;
$cr->order_main_id = 0; $cr->order_main_id = 0;
$cr->receive_time = $now; $cr->receive_time = $now;
$cr->number = $crt->one_receive_number; $cr->number = $crt->one_receive_number;
$cr->nnumber_remain = $crt->one_receive_number;
$cr->number_remain = $crt->one_receive_number;
$cr->status = 0; $cr->status = 0;
$cr->update_time = $now; $cr->update_time = $now;
$cr->receive_type = $receiveType; $cr->receive_type = $receiveType;
if($test){
if ($test && ($cp->id % 2)) {
$fail[] = $cp;
} else {
//TODO 会有超发情况
//如果优惠卷库存小于等于已领取的数量, 则返回领取失败的优惠券
if ($cp->inventory<=$cp->inventory_use||$cp->inventory<=($cp->inventory_use+$cr->number)){
$fail[] = $cp; $fail[] = $cp;
}else{ }else{
if($cr->save()){
$cp->inventory_use += $cr->number;//记录已领取的数量
if ($cr->save()&&$cp->save()) {
$success[] = $cp; $success[] = $cp;
}else{
} else {
$fail[] = $cp; $fail[] = $cp;
} }
} }
}
} }
return $this->success([ return $this->success([
'success'=>$success,
'fail'=>$fail,
'success' => $success,
'fail' => $fail,
]); ]);
} }
@ -120,9 +138,9 @@ class CouponController extends BaseController
$userId = $this->request->input("user_id"); $userId = $this->request->input("user_id");
$nowTime = time(); $nowTime = time();
$couponIds = CouponRec::where('user_id',$userId)
->whereIn('status',[0,1])
->orderBy('receive_time','desc')
$couponIds = CouponRec::where('user_id', $userId)
->whereIn('status', [0, 1])
->orderBy('receive_time', 'desc')
->get() ->get()
->pluck('system_coupon_user_id'); ->pluck('system_coupon_user_id');
@ -130,17 +148,17 @@ class CouponController extends BaseController
$expired = []; $expired = [];
$couponIds = $couponIds->toArray(); $couponIds = $couponIds->toArray();
$coupons = Coupon::orderByRaw('FIELD(id, '.implode(", " , $couponIds).')')->get();
$coupons = Coupon::orderByRaw('FIELD(id, ' . implode(", ", $couponIds) . ')')->get();
foreach ($coupons as $key => $coupon) { foreach ($coupons as $key => $coupon) {
if($coupon->usable_end_time < $nowTime){
if ($coupon->usable_end_time < $nowTime) {
$expired[] = $coupon; $expired[] = $coupon;
}else{
} else {
$not_expired[] = $coupon; $not_expired[] = $coupon;
} }
} }
$ret = ['not_expired'=>$not_expired,'expired'=>$expired];
$ret = ['not_expired' => $not_expired, 'expired' => $expired];
return $this->success($ret); 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 # 用户id
$userId = $this->request->input('user_id',0);
$userId = $this->request->input('user_id', 0);
# 市场id # 市场id
$marketId = $this->request->input('market_id',0);
$marketId = $this->request->input('market_id', 0);
# 类型,1全平台 2线上 3线下,20200718默认全平台 # 类型,1全平台 2线上 3线下,20200718默认全平台
$type = $this->request->input('type',1);
$type = $this->request->input('type', 1);
# 店铺类型id # 店铺类型id
$storetypeId = $this->request->input('storetype_id',0);
$storetypeId = $this->request->input('storetype_id', 0);
// 获取用户优惠券 // 获取用户优惠券
$currentTime = time(); $currentTime = time();
@ -184,23 +202,23 @@ class CouponController extends BaseController
->join('ims_system_coupon_user as coupon', 'coupon.id', '=', 'receive.system_coupon_user_id') ->join('ims_system_coupon_user as coupon', 'coupon.id', '=', 'receive.system_coupon_user_id')
->where(['receive.user_id' => $userId]) ->where(['receive.user_id' => $userId])
->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('receive.number_remain', '>', 0)
->where('coupon.full_amount', '<=', $orderAmount) ->where('coupon.full_amount', '<=', $orderAmount)
->where('coupon.usable_start_time', '<=', $currentTime) ->where('coupon.usable_start_time', '<=', $currentTime)
->where('coupon.usable_end_time', '>=', $currentTime) ->where('coupon.usable_end_time', '>=', $currentTime)
->where('coupon.usable_number', '<=', Db::raw('receive.number_remain')) ->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) { ->where(function ($query) use ($storetypeId) {
$query->whereOr( $query->whereOr(
[ [
[ [
['coupon.type', 'in', [1,2]],
['coupon.type', 'in', [1, 2]],
['coupon.storetype_id', '=', 0] ['coupon.storetype_id', '=', 0]
], ],
[ [
['coupon.type', 'in', [1,3]],
['coupon.type', 'in', [1, 3]],
['coupon.storetype_id', '=', $storetypeId] ['coupon.storetype_id', '=', $storetypeId]
] ]
] ]

Loading…
Cancel
Save