Browse Source

Merge branch 'purchase_limit' of ssh://120.24.33.109:11022/hyzjshwo/lanzu_api_hyperf into purchase_limit

master
weigang 5 years ago
parent
commit
ee076dd076
  1. 6
      app/Controller/CouponController.php
  2. 6
      app/Model/CouponUserUse.php
  3. 79
      app/Service/CouponService.php
  4. 6
      app/Service/CouponServiceInterface.php
  5. 6
      app/Service/OrderService.php
  6. 2
      app/Service/PurchaseLimitService.php

6
app/Controller/CouponController.php

@ -199,8 +199,10 @@ class CouponController extends BaseController
$type = $this->request->input('type', 1); $type = $this->request->input('type', 1);
# 店铺类型id # 店铺类型id
$storetypeId = $this->request->input('storetype_id', 0); $storetypeId = $this->request->input('storetype_id', 0);
$res = $this->couponService->getUserAvailableCoupons($orderAmount,$userId,$marketId,$type,$storetypeId);
# 购物车商品id
$carIds = $this->request->input('car_ids', 0);
$res = $this->couponService->getUserAvailableCoupons($orderAmount,$userId,$marketId,$type,$storetypeId,$carIds);
return $this->success($res); return $this->success($res);
} }

6
app/Model/CouponUserUse.php

@ -13,5 +13,11 @@ class CouponUserUse extends Model
public $timestamps = false; public $timestamps = false;
protected $fillable = [
'status',
'return_time',
'update_time'
];
protected $table = 'ims_system_coupon_user_use'; protected $table = 'ims_system_coupon_user_use';
} }

79
app/Service/CouponService.php

@ -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;
} }
} }

6
app/Service/CouponServiceInterface.php

@ -13,7 +13,7 @@ interface CouponServiceInterface
public function getUserReceiveCouponList(); public function getUserReceiveCouponList();
public function getUserAvailableCoupons($orderAmount,$userId,$marketId,$type,$storetypeId);
public function getUserAvailableCoupons($orderAmount,$userId,$marketId,$type,$storetypeId,$carIds);
/** /**
* 当前订单可用优惠券列表 * 当前订单可用优惠券列表
@ -26,8 +26,6 @@ interface CouponServiceInterface
* @return mixed * @return mixed
*/ */
public function getOrderCanUseCoupons($orderAmount, $marketId, $userId, $fields=[], $type=1, $storeTypeIds=[0]); public function getOrderCanUseCoupons($orderAmount, $marketId, $userId, $fields=[], $type=1, $storeTypeIds=[0]);
public function refundOrderCoupons($global_order_id);
public function clearTodayCouponUsed($userId, $couponId); public function clearTodayCouponUsed($userId, $couponId);
public function orderRefundCoupon($global_order_id);
public function orderRefundCoupons($global_order_id);
} }

6
app/Service/OrderService.php

@ -734,10 +734,10 @@ class OrderService implements OrderServiceInterface
OrderMain::where('global_order_id',$global_order_id) OrderMain::where('global_order_id',$global_order_id)
->update(['state' => OrderMain::ORDER_STATE_CANCEL]); ->update(['state' => OrderMain::ORDER_STATE_CANCEL]);
//撤销redis 用券记录 //撤销redis 用券记录
$res = $this->couponService->refundOrderCoupons($global_order_id);
$redisRes = $this->couponService->orderRefundCoupons($global_order_id);
//撤销特价商品购买记录 //撤销特价商品购买记录
$res = $this->purchaseLimitService->delSsdbPurchaseRecord($global_order_id);
return $res;
$ssdbRes = $this->purchaseLimitService->delSsdbPurchaseRecord($global_order_id);
return $redisRes && $ssdbRes;
} }
/** /**
* @inheritDoc * @inheritDoc

2
app/Service/PurchaseLimitService.php

@ -71,8 +71,6 @@ class PurchaseLimitService implements PurchaseLimitServiceInterface
foreach ($goods as $k2 => $v2) { foreach ($goods as $k2 => $v2) {
$ssdb->exec('del', SsdbKeysPrefix::PURCHASE_RECORD. date('Ymd') .'_'.$v1['user_id'].'_'.$v2['good_id']); $ssdb->exec('del', SsdbKeysPrefix::PURCHASE_RECORD. date('Ymd') .'_'.$v1['user_id'].'_'.$v2['good_id']);
var_dump($v1['user_id']);
var_dump($v1['good_id']);
} }
} }
return true; return true;

Loading…
Cancel
Save