|
|
@ -3,6 +3,7 @@ |
|
|
namespace App\Service; |
|
|
namespace App\Service; |
|
|
|
|
|
|
|
|
use App\Model\OrderMain; |
|
|
use App\Model\OrderMain; |
|
|
|
|
|
use App\Model\ShopCar; |
|
|
use Hyperf\Di\Annotation\Inject; |
|
|
use Hyperf\Di\Annotation\Inject; |
|
|
use Hyperf\DbConnection\Db; |
|
|
use Hyperf\DbConnection\Db; |
|
|
use App\Model\CouponUserRecType; |
|
|
use App\Model\CouponUserRecType; |
|
|
@ -155,12 +156,11 @@ class CouponService implements CouponServiceInterface |
|
|
* 获取用户当前订单可用的优惠券列表 |
|
|
* 获取用户当前订单可用的优惠券列表 |
|
|
* 按分类(1订单 等优惠)分组返回 |
|
|
* 按分类(1订单 等优惠)分组返回 |
|
|
*/ |
|
|
*/ |
|
|
public function getUserAvailableCoupons($orderAmount,$userId,$marketId,$type,$storetypeId) |
|
|
|
|
|
|
|
|
public function getUserAvailableCoupons($orderAmount,$userId,$marketId,$type,$storetypeId,$carIds) |
|
|
{ |
|
|
{ |
|
|
$storetypeIds = explode(',', str_replace(',', ',', $storetypeId)); |
|
|
|
|
|
|
|
|
|
|
|
$available = []; |
|
|
$available = []; |
|
|
$notAvailable = []; |
|
|
$notAvailable = []; |
|
|
|
|
|
$storetypeIds = explode(',', str_replace(',', ',', $storetypeId)); |
|
|
|
|
|
|
|
|
if (empty($orderAmount) || empty($userId)) { |
|
|
if (empty($orderAmount) || empty($userId)) { |
|
|
return [ |
|
|
return [ |
|
|
@ -206,12 +206,26 @@ class CouponService implements CouponServiceInterface |
|
|
|
|
|
|
|
|
foreach ($data as $key => &$item) { |
|
|
foreach ($data as $key => &$item) { |
|
|
if (in_array($item->id, $couponIds)) { |
|
|
if (in_array($item->id, $couponIds)) { |
|
|
|
|
|
$item->msg = '今日已使用'; |
|
|
$notAvailable[$item->id] = $item; |
|
|
$notAvailable[$item->id] = $item; |
|
|
} else { |
|
|
} else { |
|
|
$available[] = $item; |
|
|
$available[] = $item; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//如果存在特价商品 不给用券
|
|
|
|
|
|
$carIdsArr = explode(',',$carIds); |
|
|
|
|
|
$shopCarExists = ShopCar::whereIn('id',$carIdsArr) |
|
|
|
|
|
->where('money',0.01) |
|
|
|
|
|
->exists(); |
|
|
|
|
|
if($shopCarExists){ |
|
|
|
|
|
foreach ($available as $k => $v) { |
|
|
|
|
|
$v->msg = '不可使用'; |
|
|
|
|
|
$notAvailable[$v->id] = $v; |
|
|
|
|
|
} |
|
|
|
|
|
$available = []; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
return [ |
|
|
return [ |
|
|
'available' => $available, |
|
|
'available' => $available, |
|
|
'not_available' => array_values($notAvailable) |
|
|
'not_available' => array_values($notAvailable) |
|
|
@ -280,51 +294,6 @@ class CouponService implements CouponServiceInterface |
|
|
return $setRes&&$expireRes; |
|
|
return $setRes&&$expireRes; |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
* 取消订单返券 |
|
|
|
|
|
* @param $order_id |
|
|
|
|
|
* @return bool |
|
|
|
|
|
*/ |
|
|
|
|
|
public function refundOrderCoupons($global_order_id){ |
|
|
|
|
|
$order_main = OrderMain::where('global_order_id',$global_order_id) |
|
|
|
|
|
->select('id','user_id') |
|
|
|
|
|
->first(); |
|
|
|
|
|
$order_id = $order_main->id; |
|
|
|
|
|
$coupon = CouponUserUse::where([ |
|
|
|
|
|
['order_main_id','=',$order_id], |
|
|
|
|
|
['status','=',CouponUserUse::COUPON_USE_STATE_USED], |
|
|
|
|
|
]) |
|
|
|
|
|
->select('id','user_receive_id','number') |
|
|
|
|
|
->first(); |
|
|
|
|
|
|
|
|
|
|
|
if (empty($coupon)) { |
|
|
|
|
|
return ''; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// 返回用户优惠券数量并更新状态
|
|
|
|
|
|
$res = Db::update("UPDATE ims_system_coupon_user_receive SET number_remain=number_remain+{$coupon->number}, status=IF(number=number_remain,0,1), update_time=".time()."" |
|
|
|
|
|
." WHERE id={$coupon->user_receive_id} AND number>=(number_remain+{$coupon->number})"); |
|
|
|
|
|
|
|
|
|
|
|
// 更新使用记录状态为已退回
|
|
|
|
|
|
CouponUserUse::where([ |
|
|
|
|
|
['id','=',$coupon->id], |
|
|
|
|
|
['status','=',CouponUserUse::COUPON_USE_STATE_USED], |
|
|
|
|
|
]) |
|
|
|
|
|
->update([ |
|
|
|
|
|
'status' => CouponUserUse::COUPON_USE_STATE_CANCEL, |
|
|
|
|
|
'return_time' => time(), |
|
|
|
|
|
'update_time' => time(), |
|
|
|
|
|
]); |
|
|
|
|
|
|
|
|
|
|
|
//删除当日 redis 使用记录缓存
|
|
|
|
|
|
$redis = ApplicationContext::getContainer()->get(Redis::class); |
|
|
|
|
|
$remRes = $redis->sRem( |
|
|
|
|
|
'coupon_'.date('Ymd').'_used_'.$order_main->user_id, |
|
|
|
|
|
$coupon->system_coupon_id |
|
|
|
|
|
); |
|
|
|
|
|
return $res; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/* 删除-优惠券今日使用的缓存 |
|
|
/* 删除-优惠券今日使用的缓存 |
|
|
* @param $userId |
|
|
* @param $userId |
|
|
* @param $couponId |
|
|
* @param $couponId |
|
|
@ -347,19 +316,22 @@ class CouponService implements CouponServiceInterface |
|
|
* 先查询是否正常使用优惠券 |
|
|
* 先查询是否正常使用优惠券 |
|
|
* 修改状态,退还领取记录库存,删除ssdb缓存 |
|
|
* 修改状态,退还领取记录库存,删除ssdb缓存 |
|
|
*/ |
|
|
*/ |
|
|
public function orderRefundCoupon($global_order_id) |
|
|
|
|
|
|
|
|
public function orderRefundCoupons($global_order_id) |
|
|
{ |
|
|
{ |
|
|
|
|
|
$order_main = OrderMain::where('global_order_id',$global_order_id) |
|
|
|
|
|
->select('id','user_id') |
|
|
|
|
|
->first(); |
|
|
$time = time(); |
|
|
$time = time(); |
|
|
Db::beginTransaction(); |
|
|
Db::beginTransaction(); |
|
|
try { |
|
|
try { |
|
|
$couponUses = CouponUserUse::query() |
|
|
$couponUses = CouponUserUse::query() |
|
|
->select('id','system_coupon_id','user_id','number','user_receive_id') |
|
|
->select('id','system_coupon_id','user_id','number','user_receive_id') |
|
|
->where('global_order_id',$global_order_id) |
|
|
|
|
|
|
|
|
->where('order_main_id',$order_main->id) |
|
|
->where('status',CouponUserUse::COUPON_USE_STATE_USED) |
|
|
->where('status',CouponUserUse::COUPON_USE_STATE_USED) |
|
|
->select(); |
|
|
|
|
|
if(!empty($couponUse)){ |
|
|
|
|
|
|
|
|
->get(); |
|
|
|
|
|
if(!empty($couponUses)){ |
|
|
foreach($couponUses as $use){ |
|
|
foreach($couponUses as $use){ |
|
|
$use->status = CouponUserUse::COUPON_USE_STATE_USED; |
|
|
|
|
|
|
|
|
$use->status = CouponUserUse::COUPON_USE_STATE_CANCEL; |
|
|
$use->return_time = $time; |
|
|
$use->return_time = $time; |
|
|
$use->update_time = $time; |
|
|
$use->update_time = $time; |
|
|
$res = $use->save(); |
|
|
$res = $use->save(); |
|
|
@ -385,6 +357,5 @@ class CouponService implements CouponServiceInterface |
|
|
Db::rollBack(); |
|
|
Db::rollBack(); |
|
|
return false; |
|
|
return false; |
|
|
} |
|
|
} |
|
|
return true; |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |