Browse Source

优惠券列表

master
Lemon 6 years ago
parent
commit
b15b6b85d0
  1. 14
      app/Controller/v3/CouponController.php
  2. 110
      app/Service/v3/Implementations/CouponRecService.php
  3. 4
      app/Service/v3/Interfaces/CouponRecServiceInterface.php

14
app/Controller/v3/CouponController.php

@ -67,17 +67,9 @@ class CouponController extends BaseController
/** /**
* $type unused 未使用 used 已使用 expired已失效 * $type unused 未使用 used 已使用 expired已失效
*/ */
switch ($type){
case 'unused':
$res = $this->couponRecService->getUnusedListByUser($userId,$page,$pagesize);
break;
case 'used':
$res = $this->couponRecService->getUsedListByUser($userId,$page,$pagesize);
break;
case 'expired':
$res = $this->couponRecService->getExpiredListByUser($userId,$page,$pagesize);
break;
}
$res = $this->couponRecService->getListByUser($userId,$type,$page,$pagesize);
return $this->success($res); return $this->success($res);
} }

110
app/Service/v3/Implementations/CouponRecService.php

@ -78,102 +78,48 @@ class CouponRecService implements CouponRecServiceInterface
} }
/** /**
* 用户未使用优惠券列表
* 用户优惠券列表
*/ */
public function getUnusedListByUser($userId,$page = 1,$pagesize = 5)
public function getListByUser($userId,$type,$page = 1,$pagesize = 5)
{ {
//查询可用优惠券
//查询优惠券
$builder = CouponRec::query()->join('lanzu_coupon', 'lanzu_coupon.id', '=', 'lanzu_coupon_receive.coupon_id') $builder = CouponRec::query()->join('lanzu_coupon', 'lanzu_coupon.id', '=', 'lanzu_coupon_receive.coupon_id')
->where([ ->where([
['lanzu_coupon_receive.user_id' ,'=', $userId], ['lanzu_coupon_receive.user_id' ,'=', $userId],
['lanzu_coupon.usable_end_time' ,'>', time()],
['lanzu_coupon_receive.number_remain' ,'>', 0]
]); ]);
$builder = $builder->whereIn('lanzu_coupon_receive.status',[0,1])
->select(
'lanzu_coupon.title',
'lanzu_coupon.discounts',
'lanzu_coupon.full_amount',
'lanzu_coupon.discount_type',
'lanzu_coupon.introduce',
'lanzu_coupon.usable_end_time',
'lanzu_coupon_receive.number',
'lanzu_coupon_receive.number_remain'
)
->orderBy('lanzu_coupon.weigh','desc');
$paginate = $builder->paginate($pagesize);
$couponList = $paginate->toArray();
foreach ($couponList['data'] as $key => &$coupon) {
//拼接满减文字提示
$coupon['full_amount_text'] = '满' . $coupon['full_amount'] . "可用";
//判断是折扣优惠券还是满减优惠券
if($coupon['discount_type'] == 1){
$coupon['discounts_text'] = '¥'.$coupon['discounts'];
}elseif($coupon['discount_type'] == 2){
$coupon['discounts_text'] = floatval($coupon['discounts'])."";
}
/**
* $type unused 未使用 used 已使用 expired 已失效
*/
switch ($type){
case 'unused':
$builder = $builder->where([
['lanzu_coupon.usable_end_time' ,'>', time()],
['lanzu_coupon_receive.number_remain' ,'>', 0]
])
->whereIn('lanzu_coupon_receive.status',[0,1]);
break;
case 'used':
$builder = $builder->whereIn('lanzu_coupon_receive.status',[1,2]);
break;
case 'expired':
$builder = $builder->where(function ($query) {
$query->where('lanzu_coupon.usable_end_time', '<', time())
->orWhere('lanzu_coupon.status', '<>', 1);
});
break;
} }
return ['has_more_pages' => $paginate->hasMorePages(), 'coupon_list' => $couponList['data']];
}
/**
* 用户已使用
*/
public function getUsedListByUser($userId,$page = 1,$pagesize = 5)
{
//查询已使用优惠券
$builder = CouponRec::query()->join('lanzu_coupon', 'lanzu_coupon.id', '=', 'lanzu_coupon_receive.coupon_id')
->where('lanzu_coupon_receive.user_id' ,$userId)
->whereIn('lanzu_coupon_receive.status',[1,2])
->select(
'lanzu_coupon.title',
'lanzu_coupon.discounts',
'lanzu_coupon.full_amount',
'lanzu_coupon.discount_type',
'lanzu_coupon.introduce',
'lanzu_coupon.usable_end_time',
'lanzu_coupon_receive.number',
'lanzu_coupon_receive.number_remain'
)
->orderBy('lanzu_coupon_receive.updated_at','desc');
$paginate = $builder->paginate($pagesize);
$couponList = $paginate->toArray();
foreach ($couponList['data'] as $key => &$coupon) {
//拼接满减文字提示
$coupon['full_amount_text'] = '满' . $coupon['full_amount'] . "可用";
//判断是折扣优惠券还是满减优惠券
if($coupon['discount_type'] == 1){
$coupon['discounts_text'] = '¥'.$coupon['discounts'];
}elseif($coupon['discount_type'] == 2){
$coupon['discounts_text'] = floatval($coupon['discounts'])."";
}
}
return ['has_more_pages' => $paginate->hasMorePages(), 'coupon_list' => $couponList['data']];
}
/**
* 用户已失效
*/
public function getExpiredListByUser($userId,$page = 1,$pagesize = 5)
{
//查询失效优惠券
$builder = CouponRec::query()->join('lanzu_coupon', 'lanzu_coupon.id', '=', 'lanzu_coupon_receive.coupon_id')
->where('lanzu_coupon_receive.user_id',$userId)
->where(function ($query) {
$query->where('lanzu_coupon.usable_end_time', '<', time())
->orWhere('lanzu_coupon.status', '<>', 1);
})
->select(
$builder = $builder->select(
'lanzu_coupon.title', 'lanzu_coupon.title',
'lanzu_coupon.discounts', 'lanzu_coupon.discounts',
'lanzu_coupon.full_amount', 'lanzu_coupon.full_amount',
'lanzu_coupon.discount_type', 'lanzu_coupon.discount_type',
'lanzu_coupon.introduce', 'lanzu_coupon.introduce',
'lanzu_coupon.usable_start_time',
'lanzu_coupon.usable_end_time', 'lanzu_coupon.usable_end_time',
'lanzu_coupon_receive.number', 'lanzu_coupon_receive.number',
'lanzu_coupon_receive.number_remain' 'lanzu_coupon_receive.number_remain'
)
->orderBy('lanzu_coupon.weigh','desc');
)
->orderBy('lanzu_coupon.weigh','desc');
$paginate = $builder->paginate($pagesize); $paginate = $builder->paginate($pagesize);
$couponList = $paginate->toArray(); $couponList = $paginate->toArray();
foreach ($couponList['data'] as $key => &$coupon) { foreach ($couponList['data'] as $key => &$coupon) {
@ -185,6 +131,8 @@ class CouponRecService implements CouponRecServiceInterface
}elseif($coupon['discount_type'] == 2){ }elseif($coupon['discount_type'] == 2){
$coupon['discounts_text'] = floatval($coupon['discounts']).""; $coupon['discounts_text'] = floatval($coupon['discounts'])."";
} }
//拼接时间文字提示
$coupon['time_text'] = date("Y-m-d H:i:s",$coupon['usable_start_time']). ' - ' .date("Y-m-d H:i:s",$coupon['usable_end_time']);
} }
return ['has_more_pages' => $paginate->hasMorePages(), 'coupon_list' => $couponList['data']]; return ['has_more_pages' => $paginate->hasMorePages(), 'coupon_list' => $couponList['data']];
} }

4
app/Service/v3/Interfaces/CouponRecServiceInterface.php

@ -8,7 +8,5 @@ interface CouponRecServiceInterface
public function check(); public function check();
public function undo(); public function undo();
public function allForOrderOlAvailable($totalAmount,$userId,$marketId,$type,$storeTypeId); public function allForOrderOlAvailable($totalAmount,$userId,$marketId,$type,$storeTypeId);
public function getUnusedListByUser($userId,$page = 1,$pagesize = 5);
public function getUsedListByUser($userId,$page = 1,$pagesize = 5);
public function getExpiredListByUser($userId,$page = 1,$pagesize = 5);
public function getListByUser($userId,$type,$page = 1,$pagesize = 5);
} }
Loading…
Cancel
Save