Browse Source

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

# Conflicts:
#	app/Service/CouponService.php
master
liangyuyan 5 years ago
parent
commit
47c8c6e396
  1. 4
      app/Controller/CouponController.php
  2. 7
      app/Controller/OrderController.php
  3. 6
      app/Model/CouponUserUse.php
  4. 83
      app/Service/CouponService.php
  5. 4
      app/Service/CouponServiceInterface.php
  6. 10
      app/Service/OrderService.php
  7. 2
      app/Service/OrderServiceInterface.php
  8. 11
      app/Service/PurchaseLimitService.php
  9. 2
      app/Service/PurchaseLimitServiceInterface.php

4
app/Controller/CouponController.php

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

7
app/Controller/OrderController.php

@ -102,8 +102,11 @@ class OrderController extends BaseController
* 用户取消订单
*/
public function onlineCancel(){
$OrderId = $this->request->input('order_id');
return $this->success($this->orderService->onlineCancel($OrderId));
$orderId = $this->request->input('order_id');
$orderMain = OrderMain::where('id',$orderId)
->select('global_order_id')
->first();
return $this->success($this->orderService->onlineCancel($orderMain->global_order_id));
}
}

6
app/Model/CouponUserUse.php

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

83
app/Service/CouponService.php

@ -2,6 +2,8 @@
namespace App\Service;
use App\Model\OrderMain;
use App\Model\ShopCar;
use Hyperf\Di\Annotation\Inject;
use Hyperf\DbConnection\Db;
use App\Model\CouponUserRecType;
@ -154,18 +156,17 @@ class CouponService implements CouponServiceInterface
* 获取用户当前订单可用的优惠券列表
* 按分类(1订单 等优惠)分组返回
*/
public function getUserAvailableCoupons($orderAmount,$userId,$marketId,$type,$storetypeId)
public function getUserAvailableCoupons($orderAmount,$userId,$marketId,$type,$storetypeId,$carIds)
{
$storetypeIds = explode(',', str_replace(',', ',', $storetypeId));
$available = [];
$notAvailable = [];
$storetypeIds = explode(',', str_replace(',', ',', $storetypeId));
if ($this->empty($orderAmount) || $this->empty($userId)) {
return $this->success([
if (empty($orderAmount) || empty($userId)) {
return [
'available' => $available,
'not_available' => array_values($notAvailable)
]);
];
}
// 获取用户优惠券
@ -205,12 +206,26 @@ class CouponService implements CouponServiceInterface
foreach ($data as $key => &$item) {
if (in_array($item->id, $couponIds)) {
$item->msg = '今日已使用';
$notAvailable[$item->id] = $item;
} else {
$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 [
'available' => $available,
'not_available' => array_values($notAvailable)
@ -279,50 +294,6 @@ class CouponService implements CouponServiceInterface
return $setRes&&$expireRes;
}
/**
* 取消订单返券
* @param $order_id
* @return bool
*/
public function refundOrderCoupons($order_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 使用记录缓存
$order_main = OrderMain::where('id',$order_id)
->select('global_order_id','user_id')
->first();
$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 $couponId
@ -347,17 +318,20 @@ class CouponService implements CouponServiceInterface
*/
public function orderRefundCoupons($global_order_id)
{
$order_main = OrderMain::where('global_order_id',$global_order_id)
->select('id','user_id')
->first();
$time = time();
Db::beginTransaction();
try {
$couponUses = CouponUserUse::query()
->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)
->select();
if(!empty($couponUse)){
->get();
if(!empty($couponUses)){
foreach($couponUses as $use){
$use->status = CouponUserUse::COUPON_USE_STATE_USED;
$use->status = CouponUserUse::COUPON_USE_STATE_CANCEL;
$use->return_time = $time;
$use->update_time = $time;
$res = $use->save();
@ -383,6 +357,5 @@ class CouponService implements CouponServiceInterface
Db::rollBack();
return false;
}
return true;
}
}

4
app/Service/CouponServiceInterface.php

@ -13,7 +13,7 @@ interface CouponServiceInterface
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
*/
public function getOrderCanUseCoupons($orderAmount, $marketId, $userId, $fields=[], $type=1, $storeTypeIds=[0]);
public function refundOrderCoupons($order_id);
public function clearTodayCouponUsed($userId, $couponId);
public function orderRefundCoupons($global_order_id);
}

10
app/Service/OrderService.php

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

2
app/Service/OrderServiceInterface.php

@ -53,7 +53,7 @@ interface OrderServiceInterface
* @param $global_order_id
* @return mixed
*/
public function onlineCancel($order_id);
public function onlineCancel($global_order_id);
/**
* 线上订单退款

11
app/Service/PurchaseLimitService.php

@ -4,6 +4,7 @@ namespace App\Service;
use App\Model\Order;
use App\Model\OrderGoods;
use App\Model\OrderMain;
use Hyperf\Di\Annotation\Inject;
use Hyperf\DbConnection\Db;
use App\Model\Goods;
@ -46,8 +47,12 @@ class PurchaseLimitService implements PurchaseLimitServiceInterface
return true;
}
public function delSsdbPurchaseRecord($order_id)
public function delSsdbPurchaseRecord($global_order_id)
{
$order_main = OrderMain::where('global_order_id',$global_order_id)
->select('id','user_id')
->first();
$order_id = $order_main->id;
$ssdb = ApplicationContext::getContainer()->get(SSDBTask::class);
$order = Order::query()
->where('order_main_id',$order_id)
@ -65,9 +70,7 @@ class PurchaseLimitService implements PurchaseLimitServiceInterface
->toArray();
foreach ($goods as $k2 => $v2) {
$ssdb->exec('del', SsdbKeysPrefix::PURCHASE_RECORD. date('Ymd') .'_'.$v1['user_id'].'_'.$v2['good_id'], $order->global_order_id);
var_dump($v1['user_id']);
var_dump($v1['good_id']);
$ssdb->exec('del', SsdbKeysPrefix::PURCHASE_RECORD. date('Ymd') .'_'.$v1['user_id'].'_'.$v2['good_id']);
}
}
return true;

2
app/Service/PurchaseLimitServiceInterface.php

@ -10,7 +10,7 @@ interface PurchaseLimitServiceInterface
public function ssdbPurchaseRecord($params,$user_id,$global_order_id);
public function delSsdbPurchaseRecord($order_id);
public function delSsdbPurchaseRecord($global_order_id);
public function test($params);
}
Loading…
Cancel
Save