|
|
|
@ -379,6 +379,133 @@ class CouponRebateService implements CouponRebateServiceInterface |
|
|
|
} |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
/* |
|
|
|
* 支付成功 返券 |
|
|
|
*/ |
|
|
|
public function couponRebateInTask($order_id) |
|
|
|
{ |
|
|
|
//获取SSDB上的活动信息
|
|
|
|
$ssdb = ApplicationContext::getContainer()->get(SSDBTask::class); |
|
|
|
$active = $ssdb->exec('hgetall',SsdbKeysPrefix::COUPON_REBATE_ACTIVITY); |
|
|
|
//判断返券优惠券是否有库存
|
|
|
|
$inventory = Db::table('ims_system_coupon_user') |
|
|
|
->where('id',$active['repay']) |
|
|
|
->whereRaw('inventory > inventory_use') |
|
|
|
->exists(); |
|
|
|
if(!$inventory){ |
|
|
|
//库存不足
|
|
|
|
return false; |
|
|
|
} |
|
|
|
|
|
|
|
//获取活动发放优惠券id
|
|
|
|
$coupon_ids = explode(',',$active['forward']); |
|
|
|
|
|
|
|
/* 判断被使用的优惠券类型是否为转发活动优惠券 */ |
|
|
|
$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], |
|
|
|
]) |
|
|
|
->whereIn('r.system_coupon_user_id',$coupon_ids) |
|
|
|
->select('r.user_id', 'r.send_user_id') |
|
|
|
->first(); |
|
|
|
/* 如果使用的优惠券为转发活动优惠券 |
|
|
|
**则给赠送者返一张优惠券 |
|
|
|
* **自己给自己转发的券不给返券 |
|
|
|
*/ |
|
|
|
if ($coupon && ($coupon->user_id != $coupon->send_user_id)) { |
|
|
|
//是否已返过券
|
|
|
|
$exists_coupon_rebate = Db::table('ims_system_coupon_user_receive') |
|
|
|
->where([ |
|
|
|
['system_coupon_user_id' ,'=', $active['repay']], |
|
|
|
['user_id' ,'=', $coupon->send_user_id], |
|
|
|
['receive_type' ,'=', 5], |
|
|
|
]) |
|
|
|
->select('id','status') |
|
|
|
->first(); |
|
|
|
//开启事务
|
|
|
|
Db::beginTransaction(); |
|
|
|
try { |
|
|
|
//返券
|
|
|
|
if($exists_coupon_rebate){ |
|
|
|
//如果已有该优惠券 则领取数量 和 可用数量 自增1
|
|
|
|
Db::table('ims_system_coupon_user_receive') |
|
|
|
->where([ |
|
|
|
['id' ,'=', $exists_coupon_rebate->id], |
|
|
|
]) |
|
|
|
->increment('number'); |
|
|
|
Db::table('ims_system_coupon_user_receive') |
|
|
|
->where([ |
|
|
|
['id' ,'=', $exists_coupon_rebate->id], |
|
|
|
]) |
|
|
|
->increment('number_remain'); |
|
|
|
//如果该用户在领取表中 status为已用完状态 2 则改为已用部分 1
|
|
|
|
if($exists_coupon_rebate->status == 2){ |
|
|
|
Db::table('ims_system_coupon_user_receive') |
|
|
|
->where([ |
|
|
|
['id' ,'=', $exists_coupon_rebate->id], |
|
|
|
]) |
|
|
|
->update(['status' => 1]);; |
|
|
|
} |
|
|
|
}else { |
|
|
|
//否则新增一条返券记录
|
|
|
|
$nowTime = time(); |
|
|
|
Db::table('ims_system_coupon_user_receive')->insert([ |
|
|
|
[ |
|
|
|
'user_id' => $coupon->send_user_id, |
|
|
|
'system_coupon_user_id' => $active['repay'], |
|
|
|
'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], |
|
|
|
]) |
|
|
|
->whereIn('system_coupon_user_id',$coupon_ids) |
|
|
|
->update(['rebate_type' => 2]); |
|
|
|
//更新库存操作
|
|
|
|
Db::table('ims_system_coupon_user') |
|
|
|
->where('id', $active['repay']) |
|
|
|
->increment('inventory_use'); |
|
|
|
|
|
|
|
//添加领取记录到ssdb
|
|
|
|
$data = [ |
|
|
|
$order_id,$coupon->user_id, |
|
|
|
]; |
|
|
|
$ssdb->exec('multi_hset', SsdbKeysPrefix::COUPON_REBATE_LIST.$coupon->send_user_id, $data); |
|
|
|
// 提交
|
|
|
|
Db::commit(); |
|
|
|
} catch (\Exception $e) { |
|
|
|
//写入日志文件
|
|
|
|
$log_Data = array(); |
|
|
|
$log_Data['name'] = '返券'; |
|
|
|
$log_Data['order_id'] = $order_id; |
|
|
|
$log_Data['msg'] = '返券失败'; |
|
|
|
$this->log->event( |
|
|
|
LogLabel::COUPON_LOG, |
|
|
|
$log_Data |
|
|
|
); |
|
|
|
// 回滚
|
|
|
|
Db::rollBack(); |
|
|
|
} |
|
|
|
} |
|
|
|
return true; |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* 清优惠券领取记录(SSDB) |
|
|
|
|