Browse Source

返券

master
parent
commit
7d989e3d6f
  1. 115
      app/Service/CouponRebateService.php

115
app/Service/CouponRebateService.php

@ -312,6 +312,18 @@ class CouponRebateService implements CouponRebateServiceInterface
*/ */
public function couponRebate($order_id) public function couponRebate($order_id)
{ {
//$ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
// $active_id = $ssdb->exec('get',SsdbKeysPrefix::COUPON_REBATE_ACTIVITY);
// $rebate_id = $ssdb->exec('get',SsdbKeysPrefix::COUPON_REBATE_REPAY.$active_id);
$rebate_id = 56;
//判断该优惠券是否有库存
$inventory = Db::table('ims_system_coupon_user')
->where('id',$rebate_id)
->whereRaw('inventory > inventory_use')
->exists();
if(!$inventory){
return '库存不足';
}
/* 判断优惠券类型是否为转发活动优惠券 */ /* 判断优惠券类型是否为转发活动优惠券 */
$coupon = Db::table('ims_system_coupon_user_receive as r') $coupon = Db::table('ims_system_coupon_user_receive as r')
->leftjoin('ims_system_coupon_user_use as u', 'u.user_receive_id', '=', 'r.id') ->leftjoin('ims_system_coupon_user_use as u', 'u.user_receive_id', '=', 'r.id')
@ -322,63 +334,82 @@ class CouponRebateService implements CouponRebateServiceInterface
['r.receive_type', '=', 4], ['r.receive_type', '=', 4],
['u.status', '=', 1], ['u.status', '=', 1],
]) ])
->select('r.id', 'r.send_user_id', 'u.system_coupon_id')
->select('r.id', 'r.user_id', 'r.send_user_id', 'u.system_coupon_id')
->first(); ->first();
/* 如果使用的优惠券为转发活动优惠券 则给赠送者返一张优惠券*/ /* 如果使用的优惠券为转发活动优惠券 则给赠送者返一张优惠券*/
if (isset($coupon->send_user_id) && ($coupon->send_user_id) > 0) { 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 '库存不足';
// }
//return $exist_coupon;
//是否已返过券
$exists_coupon_rebate = Db::table('ims_system_coupon_user_receive')
->where([
['system_coupon_user_id' ,'=', $rebate_id],
['user_id' ,'=', $coupon->send_user_id],
['receive_type' ,'=', 5],
['status' ,'=', 0],
])
->exists();
//开启事务 //开启事务
Db::beginTransaction();
try { try {
/* /*
* 如果已有该优惠券 则领取数量 可用数量 自增1 * 如果已有该优惠券 则领取数量 可用数量 自增1
* 否则新增一条返券记录 * 否则新增一条返券记录
*/ */
$nowTime = time();
$res = Db::table('ims_system_coupon_user_receive')->moreIncrementOrInsert(
[
'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,
'update_time' => $nowTime,
'created_at' => $nowTime,
'updated_at' => $nowTime,
],
[
'number' => 1,
'number_remain' => 1,
]
);
// 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 '事务失败';
// }
if($exists_coupon_rebate){
var_dump(222);
Db::table('ims_system_coupon_user_receive')
->where([
['user_id' ,'=', $coupon->send_user_id],
['receive_type' ,'=', 5],
])
->increment('number');
Db::table('ims_system_coupon_user_receive')
->where([
['user_id' ,'=', $coupon->send_user_id],
['receive_type' ,'=', 5],
])
->increment('number_remain');
}else {
var_dump(111111222);
$nowTime = time();
Db::table('ims_system_coupon_user_receive')->insert([
[
'user_id' => $coupon->send_user_id,
'system_coupon_user_id' => $rebate_id,
'receive_type' => 5,
'status' => 0,
'number' => 1,
'number_remain' => 1,
'order_main_id' => $order_id,
'receive_time' => $nowTime,
'update_time' => $nowTime,
'created_at' => $nowTime,
'updated_at' => $nowTime,
]
]);
}
//首次返券更新rebate_type字段 防止重复返券
Db::table('ims_system_coupon_user_receive')
->where([
['user_id','=',$coupon->user_id],
['receive_type','=',4],
])
->update(['rebate_type' => 2]);
//更新库存操作
Db::table('ims_system_coupon_user')
->where('id', $rebate_id)
->increment('inventory_use');
// 提交 // 提交
Db::commit(); Db::commit();
} catch (\Exception $e) { } catch (\Exception $e) {
// 回滚 // 回滚
Db::rollBack(); Db::rollBack();
return '返券失败';
$errorData['order_main_id'] = $order_id;
$errorData['msg'] = '返券失败';
$this->log->event(
LogLabel::COUPON_LOG,
$errorData
);
} }
} else {
// $str1 = 'order_main_id:'.$this->order_id . ',未查询到用户领取优惠券信息['.$coupon.']';
// $this->couponErrorLog($str1);
} }

Loading…
Cancel
Save