Browse Source

购物车 优惠券列表

master
Lemon 5 years ago
parent
commit
5d9abdeb83
  1. 85
      app/Controller/v3/CouponController.php
  2. 9
      app/Controller/v3/ShopCartController.php
  3. 18
      app/Model/v3/Coupon.php
  4. 9
      app/Model/v3/CouponRec.php
  5. 83
      app/Service/v3/Implementations/CouponRecService.php
  6. 2
      app/Service/v3/Implementations/OrderListService.php
  7. 3
      app/Service/v3/Interfaces/CouponRecServiceInterface.php
  8. 2
      config/routes.php

85
app/Controller/v3/CouponController.php

@ -0,0 +1,85 @@
<?php
declare(strict_types=1);
/**
* This file is part of Hyperf.
*
* @link https://www.hyperf.io
* @document https://doc.hyperf.io
* @contact group@hyperf.io
* @license https://github.com/hyperf/hyperf/blob/master/LICENSE
*/
namespace App\Controller\v3;
use Hyperf\Di\Annotation\Inject;
use App\Controller\BaseController;
use App\Service\v3\Interfaces\CouponRecServiceInterface;
class CouponController extends BaseController
{
/**
* @Inject
* @var CouponRecServiceInterface
*/
protected $couponRecService;
/**
* 获取用户可领取优惠卷接口
*/
public function getSystemCouponUserList()
{
}
//统计用户
public function userCouponAccount()
{
}
/**
* 用户领取优惠卷
*/
public function userReceiveCoupon()
{
}
/**
* 获取用户已经领取的优惠卷列表
*/
public function getUserReceiveCouponList()
{
}
/**
* 获取用户当前订单可用的优惠券列表
* 按分类(1订单 等优惠)分组返回
*/
public function getListByUser()
{
$userId = $this->request->input('user_id');
$page = $this->request->input('page');
$pagesize = $this->request->input('pagesize');
$type = $this->request->input('type','1');
/**
* $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;
}
return $this->success($res);
}
}

9
app/Controller/v3/ShopCartController.php

@ -20,7 +20,14 @@ class ShopCartController extends BaseController
//获取购物车失效商品信息
$res['store_lists_invalid'] = $this->shopCartService->undo();
//计算购物车价格
$res['total'] = '820.00';
$res['total'] = $this->shopCartService->getTotal();
return $this->success($res);
}
public function info()
{
$res['shopcart']['count'] = $this->shopCartService->countGoods();
$res['shopcart']['total'] = $this->shopCartService->getTotal();
return $this->success($res);
}
}

18
app/Model/v3/Coupon.php

@ -11,4 +11,22 @@ class Coupon extends Model
protected $table = 'lanzu_coupon';
protected $appends = [
'full_amount_text',
'discounts_text'
];
public function getFullAmountTextAttribute()
{
return '满'.$this->attributes['full_amount'].'可用';
}
public function getDiscountsTextAttribute()
{
if($this->attributes['discount_type'] == 1){
return '¥'.$this->attributes['discounts'];
}elseif($this->attributes['discount_type'] == 2){
return floatval($this->attributes['discounts'])."";
}
}
}

9
app/Model/v3/CouponRec.php

@ -9,6 +9,15 @@ class CouponRec extends Model
protected $table = 'lanzu_coupon_receive';
protected $appends = [
'used_num'
];
public function getUsedNumAttribute()
{
return ($this->attributes['number'] - $this->attributes['number_remain']);
}
public function coupon()
{
return $this->hasOne(Coupon::class,'id','coupon_id');

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

@ -76,4 +76,87 @@ class CouponRecService implements CouponRecServiceInterface
->get()
->toArray();
}
/**
* 用户未使用优惠券列表
*/
public function getUnusedListByUser($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],
['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'])."";
}
}
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)
{
}
}

2
app/Service/v3/Implementations/OrderListService.php

@ -135,7 +135,7 @@ class OrderListService implements OrderListServiceInterface
break;
}
if(!is_null($start_time) && !is_null($end_time)){
if(!empty($start_time) && !empty($end_time)){
$builder->whereBetween('lanzu_order_main.created_at',[strtotime($start_time.' 23:59:59'),strtotime($end_time.' 23:59:59')]);
}

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

@ -8,4 +8,7 @@ interface CouponRecServiceInterface
public function check();
public function undo();
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);
}

2
config/routes.php

@ -128,4 +128,6 @@ Router::addGroup('/v3/', function () {
Router::post('userAddress/setDefault', 'App\Controller\v3\UserAddressController@setDefault');
Router::post('userAddress/get', 'App\Controller\v3\UserAddressController@get');
Router::post('userAddress/delete', 'App\Controller\v3\UserAddressController@delete');
Router::post('coupon/getListByUser', 'App\Controller\v3\CouponController@getListByUser');
Router::post('shopCart/info', 'App\Controller\v3\ShopCartController@info');
},['middleware' => [\App\Middleware\Auth\ApiMiddleware::class, \App\Middleware\Auth\UserMiddleware::class]]);
Loading…
Cancel
Save