|
|
<?php
namespace App\Service;
use Hyperf\Di\Annotation\Inject;use Hyperf\DbConnection\Db;use App\Model\CouponUserRecType;use App\Model\Coupon;use App\Model\CouponRec;use Hyperf\Utils\ApplicationContext;use App\TaskWorker\SSDBTask;use App\Constants\SsdbKeysPrefix;use App\Constants\LogLabel;use App\Commons\Log;use Exception;use App\Service\CommonService;
class CouponService implements CouponServiceInterface{ /** * @Inject * @var Log */ protected $log; /** * @Inject * @var CommonService */ protected $commonService;
/** * 获取用户可领取优惠卷接口 */ public function getSystemCouponUserList($userId,$receiveType) { /* 优惠券活动标志 2 */ $ssdb = ApplicationContext::getContainer()->get(SSDBTask::class); $couponActivity = $ssdb->exec('hgetall', SsdbKeysPrefix::COUPON_REBATE_ACTIVITY); $activityType = $couponActivity === false ? 0 : $couponActivity['activity'];
$result = [ 'active_type' => 1, 'not_receive' => [], 'jump_data' => [ 'src' => "/zh_cjdianc/pages/couponrebate/index?activity_type=".$activityType, 'src2' => "/zh_cjdianc/pages/couponrebate/index?activity_type=".$activityType, 'share_bg' => env('OSS_IMG_HOST').'/static/img/coupon_bg.png', 'receive_bg' => env('OSS_IMG_HOST').'/static/img/coupon_share.png', 'coupons' => [] ] ]; $nowTime = time(); $c_ids = []; $whereC = [ ['end_time','>',$nowTime], ['start_time','<=',$nowTime], ['status','=',1] ]; // 渠道开启,查询该渠道可以领取的优惠券ID
// 渠道未开启,查询所有优惠券
if (env('SUB_CHANNEL') == 1) { $c_ids = CouponUserRecType::where('receive_type', $receiveType)->where($whereC)->pluck('system_coupon_user_id'); } else { $c_ids = Coupon::where($whereC)->pluck('id'); }
$couponReceive = CouponRec::where('user_id',$userId);
// 渠道开启,查询该用户在此渠道领过的优惠券ID
if (env('SUB_CHANNEL') == 1) { $couponReceive->where('receive_type', $receiveType); } $cr_ids = $couponReceive->pluck('system_coupon_user_id');
// 可领取的券ID
$c_ids = $c_ids->toArray(); // 已经领取的券ID
$cr_ids = $cr_ids->toArray();
// 当前用户可领的优惠券ID
$couponIds = array_diff($c_ids, $cr_ids);
// 转发型优惠券
$couponReceiveIds = ($couponActivity === false || $this->commonService->empty($couponActivity['forward']) )? [] : explode(',',$couponActivity['forward']); // 所有优惠券
$couponIds = array_merge($couponIds,$couponReceiveIds);
$whereC = [ ['u.end_time','>',$nowTime], ['u.start_time','<=',$nowTime], ['u.status','=',1] ]; // 查询领取型1 和 转发型2
$whereActiveType = [1,2];
if (env('SUB_CHANNEL') == 1) { array_push($whereC, ['type.receive_type','=', $receiveType]); } $coupons = Db::table('ims_system_coupon_user as u') ->join('ims_system_coupon_user_receivetype as type', 'u.id', '=', 'type.system_coupon_user_id') ->whereIn('u.id', $couponIds) ->whereIn('u.active_type', $whereActiveType) ->where($whereC) ->whereRaw('u.inventory_use < u.inventory and u.inventory-u.inventory_use >= type.one_receive_number') ->select('u.*','type.one_receive_number') ->orderBy('u.weigh','desc') ->get();
foreach ($coupons 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)."折"; } //失效时间格式转换
$usable_end_time = date('Y-m-d H:i:s',$coupon->usable_end_time); $coupon->usable_end_time_text = '有效期至:'.$usable_end_time;
if ($coupon->usable_end_time < $nowTime || $coupon->number_remain <= 0) { $expired[] = $coupon; } else { $not_expired[] = $coupon; } }
$result['active_type'] = count($result['jump_data']['coupons']) > 0 ? 2 : $result['active_type'] ;
return $result; }
//统计用户
public function userCouponAccount() {
}
/** * 用户领取优惠卷 */ public function userReceiveCoupon() { }
/** * 获取用户已经领取的优惠卷列表 */ public function getUserReceiveCouponList() {
}
/** * 获取用户当前订单可用的优惠券列表 * 按分类(1订单 等优惠)分组返回 */ public function getUserAvailableCoupons() {
}
}
|