alias('CL') ->field('C.*,CL.id as cl_id,CL.create_time as receive_time') ->join('coupon C', 'C.id = CL.coupon_id') ->where([ ['CL.id', '=', intval($coupon_list_id)], ['CL.user_id', '=', intval($user_id)], ['CL.status', '=', CouponEnum::USE_STATUS_NOT], ['CL.invalid_time', '>', time()], ])->findOrEmpty()->toArray(); if ($coupon) { $result = self::isUseWhere($coupon, $orderStatus['goods']['id'], $orderStatus['total_amount']); if ($result) { return [ 'isUsable' => true, 'money' => $coupon['money'] ]; } } return [ 'isUsable' => false, 'money' => 0 ]; } /** * @notes 验证是否满足基本使用条件 * @param $coupon * @param $goods_id * @param $order_total_amount * @return bool * @author 张无忌 * @date 2021/8/4 9:47 */ public static function isUseWhere($coupon, $goods_id, $order_total_amount) { switch ($coupon['use_time_type']) { case CouponEnum::USE_TIME_TYPE_FIXED: if ($coupon['use_time_start'] >= time() and $coupon['use_time_end'] <= time()) { return false; } break; case CouponEnum::USE_TIME_TYPE_TODAY: $use_time = intval($coupon['use_time'] * 86400); if ($coupon['receive_time'] >= time() and $use_time <= time()) { return false; } break; case CouponEnum::USE_TIME_TYPE_TOMORROW: $receive_time = date('Y-m-d', $coupon['receive_time']); $receive_time = strtotime($receive_time) + 86400; $use_time = intval($coupon['use_time'] * 86400); if (time() <= $receive_time and $use_time <= time()) { return false; } break; } switch ($coupon['use_goods_type']) { case CouponEnum::USE_GOODS_TYPE_NOT: break; case CouponEnum::USE_GOODS_TYPE_ALLOW: if (!$coupon['use_goods_ids']) { return false; } $use_goods_ids = $coupon['use_goods_ids']; if (!in_array($goods_id, $use_goods_ids)) { return false; } break; case CouponEnum::USE_GOODS_TYPE_BAN: if ($coupon['use_goods_ids']) { $use_goods_ids = $coupon['use_goods_ids']; if (in_array($goods_id, $use_goods_ids)) { return false; } } break; } switch ($coupon['condition_type']) { case CouponEnum::CONDITION_TYPE_NOT: break; case CouponEnum::CONDITION_TYPE_FULL: if ($order_total_amount < $coupon['condition_money']) { return false; } break; } return true; } }