|
|
|
@ -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; |
|
|
|
@ -22,7 +23,8 @@ class CouponController extends BaseController |
|
|
|
/** |
|
|
|
* 获取用户可领取优惠卷接口 |
|
|
|
*/ |
|
|
|
public function getSystemCouponUserList(){ |
|
|
|
public function getSystemCouponUserList() |
|
|
|
{ |
|
|
|
$user_id = $this->request->input('user_id'); |
|
|
|
$receive_type = $this->request->input('receive_type'); |
|
|
|
|
|
|
|
@ -44,17 +46,28 @@ class CouponController extends BaseController |
|
|
|
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]); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
@ -63,45 +76,50 @@ class CouponController extends BaseController |
|
|
|
*/ |
|
|
|
public function userReceiveCoupon() |
|
|
|
{ |
|
|
|
|
|
|
|
$userId = $this->request->input("user_id"); |
|
|
|
$receiveType = $this->request->input("receive_type"); |
|
|
|
$ids = $this->request->input("ids"); |
|
|
|
$test = $this->request->input("test", 0); |
|
|
|
|
|
|
|
|
|
|
|
$ids = is_array($ids)?implode(',',$ids):$ids; |
|
|
|
|
|
|
|
$ids = explode(',', $ids); |
|
|
|
$cps = Coupon::whereIn('id', $ids)->get(); |
|
|
|
$now = time(); |
|
|
|
$success = []; |
|
|
|
$fail = []; |
|
|
|
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->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->number_remain = $crt->one_receive_number; |
|
|
|
$cr->status = 0; |
|
|
|
$cr->update_time = $now; |
|
|
|
$cr->receive_type = $receiveType; |
|
|
|
|
|
|
|
if($test){ |
|
|
|
if ($test && ($cp->id % 2)) { |
|
|
|
$fail[] = $cp; |
|
|
|
} else { |
|
|
|
if($cr->save()){ |
|
|
|
//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; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|