You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
155 lines
5.7 KiB
155 lines
5.7 KiB
<?php
|
|
|
|
namespace App\Service\v3\Implementations;
|
|
|
|
use App\Model\ShopCar;
|
|
use App\Model\v3\Coupon;
|
|
use App\Model\v3\CouponRec;
|
|
use App\Service\v3\Interfaces\CouponRecServiceInterface;
|
|
use Hyperf\DbConnection\Db;
|
|
use Hyperf\Redis\Redis;
|
|
use Hyperf\Utils\ApplicationContext;
|
|
|
|
class CouponRecService implements CouponRecServiceInterface
|
|
{
|
|
|
|
public function do()
|
|
{
|
|
// TODO: Implement do() method.
|
|
}
|
|
|
|
public function check()
|
|
{
|
|
// TODO: Implement check() method.
|
|
}
|
|
|
|
public function undo()
|
|
{
|
|
// TODO: Implement undo() method.
|
|
}
|
|
|
|
/**
|
|
* 获取当前订单可使用的优惠券
|
|
* @param $totalAmount
|
|
* @param $userId
|
|
* @param $marketId
|
|
* @param $type
|
|
* @param $storeTypeIds
|
|
* @return array
|
|
*/
|
|
public function allForOrderOlAvailable($totalAmount, $userId, $marketId, $type, $storeTypeIds = [])
|
|
{
|
|
// 用户今日使用过的优惠券
|
|
$redis = ApplicationContext::getContainer()->get(Redis::class);
|
|
$couponTodayUsedIds = $redis->sMembers('coupon_'.date('Ymd').'_used_'.$userId);
|
|
$currentTime = time();
|
|
|
|
$builder = Db::table('lanzu_coupon_receive as receive')
|
|
->join('lanzu_coupon as coupon', 'coupon.id', '=', 'receive.coupon_id', 'inner');
|
|
|
|
if (is_array($couponTodayUsedIds)&&!empty($couponTodayUsedIds)) {
|
|
$builder->whereNotIn('coupon.id', $couponTodayUsedIds);
|
|
}
|
|
|
|
foreach ($storeTypeIds as $key => &$item) {
|
|
$item = (string)$item;
|
|
}
|
|
|
|
if (!empty($storeTypeIds)) {
|
|
$builder->whereJsonContains('coupon.category_ids', $storeTypeIds);
|
|
}
|
|
|
|
$builder->where(['receive.user_id' => $userId])
|
|
->whereIn('receive.status', [0,1])
|
|
->where('receive.number_remain', '>', 0)
|
|
->whereIn('coupon.type', [1,$type])
|
|
->where('coupon.full_amount', '<=', $totalAmount)
|
|
->where('coupon.usable_start_time', '<=', $currentTime)
|
|
->where('coupon.usable_end_time', '>=', $currentTime)
|
|
->where('coupon.usable_number', '<=', Db::raw('receive.number_remain'));
|
|
|
|
if ($marketId) {
|
|
$builder->whereJsonContains('coupon.market_ids', [(string)$marketId]);
|
|
}
|
|
|
|
return $builder->orderByRaw('coupon.discounts DESC, coupon.full_amount DESC')
|
|
->get()
|
|
->toArray();
|
|
}
|
|
|
|
/**
|
|
* 用户优惠券列表
|
|
*/
|
|
public function getListByUser($userId,$type,$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],
|
|
]);
|
|
/**
|
|
* $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;
|
|
}
|
|
$builder = $builder->select(
|
|
'lanzu_coupon.title',
|
|
'lanzu_coupon.discounts',
|
|
'lanzu_coupon.full_amount',
|
|
'lanzu_coupon.discount_type',
|
|
'lanzu_coupon.introduce',
|
|
'lanzu_coupon.usable_start_time',
|
|
'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'])."折";
|
|
}
|
|
//拼接时间文字提示
|
|
$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']];
|
|
}
|
|
|
|
/**
|
|
* 获取用户当前线上订单可以使用的优惠券
|
|
* 1、所有未过期额优惠券,并且满足订单的金额要求
|
|
* 2、筛选出其中当日使用过的优惠券
|
|
* 3、筛选出其中活动商品不可用的优惠券(订单中有活动商品且活动商品不可使用优惠券)
|
|
* 4、筛选出其中活动商品可用但商品的活动类型不符合优惠券活动使用类型的要求(订单中有活动商品可以用优惠券,但是活动类型type和优惠券中的available活动类型不可用)
|
|
* @param $cartIds
|
|
* @param $userId
|
|
* @param $marketId
|
|
*/
|
|
public function allForOnlineOrderAvailable($cartIds, $userId, $marketId)
|
|
{
|
|
|
|
}
|
|
|
|
}
|