|
|
@ -133,6 +133,9 @@ class CouponRebateService implements CouponRebateServiceInterface |
|
|
return $result; |
|
|
return $result; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
|
* 判断用户是否已领取过优惠券 |
|
|
|
|
|
* */ |
|
|
public function isCouponRebate($user_id) |
|
|
public function isCouponRebate($user_id) |
|
|
{ |
|
|
{ |
|
|
$res = Db::table('ims_system_coupon_user as u') |
|
|
$res = Db::table('ims_system_coupon_user as u') |
|
|
@ -147,6 +150,9 @@ class CouponRebateService implements CouponRebateServiceInterface |
|
|
return $res; |
|
|
return $res; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
|
*获取活动信息 |
|
|
|
|
|
*/ |
|
|
public function getActiveInfo() |
|
|
public function getActiveInfo() |
|
|
{ |
|
|
{ |
|
|
$time = time(); |
|
|
$time = time(); |
|
|
@ -163,4 +169,90 @@ class CouponRebateService implements CouponRebateServiceInterface |
|
|
return $res; |
|
|
return $res; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* |
|
|
|
|
|
* 支付成功 返券 |
|
|
|
|
|
*/ |
|
|
|
|
|
public function couponRebate($order_id) |
|
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
|
|
/* 判断优惠券类型是否为转发活动优惠券 */ |
|
|
|
|
|
$coupon = Db::table('ims_system_coupon_user_receive as r') |
|
|
|
|
|
->leftjoin('ims_system_coupon_user_use as u', 'u.user_receive_id', '=', 'r.id') |
|
|
|
|
|
->where([ |
|
|
|
|
|
['u.order_main_id', '=', $order_id], |
|
|
|
|
|
['r.send_user_id', '>', 0], |
|
|
|
|
|
['r.rebate_type', '=', 1], |
|
|
|
|
|
['r.receive_type', '=', 4], |
|
|
|
|
|
['u.status', '=', 1], |
|
|
|
|
|
]) |
|
|
|
|
|
->select('r.id', 'r.send_user_id', 'u.system_coupon_id') |
|
|
|
|
|
->first(); |
|
|
|
|
|
/* 如果使用的优惠券为转发活动优惠券 则给赠送者返一张优惠券*/ |
|
|
|
|
|
if (isset($coupon->send_user_id) && ($coupon->send_user_id) > 0) { |
|
|
|
|
|
//判断该优惠券是否有库存
|
|
|
|
|
|
// $inventory = Db::table('system_coupon_user_receive as r');
|
|
|
|
|
|
// if($inventory['inventory'] <= 0){
|
|
|
|
|
|
// return '库存不足';
|
|
|
|
|
|
// }
|
|
|
|
|
|
//判断用户是否已有该类型优惠券
|
|
|
|
|
|
$exist_coupon = Db::table('ims_system_coupon_user_receive') |
|
|
|
|
|
->where([ |
|
|
|
|
|
['system_coupon_user_id', '=', $coupon->system_coupon_id], |
|
|
|
|
|
['user_id', '=', $coupon->send_user_id], |
|
|
|
|
|
['receive_type', '=', 5], |
|
|
|
|
|
['status', '=', 0], |
|
|
|
|
|
]) |
|
|
|
|
|
->select('id') |
|
|
|
|
|
->first(); |
|
|
|
|
|
//return $exist_coupon;
|
|
|
|
|
|
//开启事务
|
|
|
|
|
|
try { |
|
|
|
|
|
/* |
|
|
|
|
|
* 如果已有该优惠券 则领取数量 和 可用数量 自增1 |
|
|
|
|
|
* 否则新增一条返券记录 |
|
|
|
|
|
*/ |
|
|
|
|
|
$nowTime = time(); |
|
|
|
|
|
$res = Db::table('ims_system_coupon_user_receive')->updateOrInsert( |
|
|
|
|
|
[ |
|
|
|
|
|
'system_coupon_user_id' => $coupon->system_coupon_id, |
|
|
|
|
|
'user_id' => $coupon->send_user_id, |
|
|
|
|
|
'receive_type' => 5, |
|
|
|
|
|
'status' => 0, |
|
|
|
|
|
], |
|
|
|
|
|
[ |
|
|
|
|
|
'order_main_id' => $order_id, |
|
|
|
|
|
'receive_time' => $nowTime, |
|
|
|
|
|
'number' => 1, |
|
|
|
|
|
'number_remain' => 1, |
|
|
|
|
|
'update_time' => $nowTime, |
|
|
|
|
|
'created_at' => $nowTime, |
|
|
|
|
|
'updated_at' => $nowTime, |
|
|
|
|
|
] |
|
|
|
|
|
); |
|
|
|
|
|
// if ($res) {
|
|
|
|
|
|
// //首次返券更新rebate_type字段 防止重复返券
|
|
|
|
|
|
// Db::table('system_coupon_user_receive')->where('id', $coupon->id)->update(['rebate_type' => 2]);
|
|
|
|
|
|
// //更新库存操作
|
|
|
|
|
|
// Db::table('system_coupon_user')
|
|
|
|
|
|
// ->where('id', $coupon->id)
|
|
|
|
|
|
// ->increment('inventory_use');
|
|
|
|
|
|
// } else {
|
|
|
|
|
|
// Db::rollBack();
|
|
|
|
|
|
// return '事务失败';
|
|
|
|
|
|
// }
|
|
|
|
|
|
// 提交
|
|
|
|
|
|
Db::commit(); |
|
|
|
|
|
} catch (\Exception $e) { |
|
|
|
|
|
// 回滚
|
|
|
|
|
|
Db::rollBack(); |
|
|
|
|
|
return '返券失败'; |
|
|
|
|
|
} |
|
|
|
|
|
} else { |
|
|
|
|
|
// $str1 = 'order_main_id:'.$this->order_id . ',未查询到用户领取优惠券信息['.$coupon.']';
|
|
|
|
|
|
// $this->couponErrorLog($str1);
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return $exist_coupon->id; |
|
|
|
|
|
} |
|
|
} |
|
|
} |