84 lines
2.2 KiB
PHP
84 lines
2.2 KiB
PHP
<?php
|
|
|
|
namespace App\Services;
|
|
|
|
use App\Model\UserCoupon;
|
|
|
|
/**
|
|
* 用户优惠卷
|
|
*/
|
|
class UserCouponService extends BaseService
|
|
{
|
|
/**
|
|
* 获取患者问诊可用的优惠卷
|
|
* @param string|int $user_id
|
|
* @param string|int $inquiry_type
|
|
* @return array
|
|
*/
|
|
public function getUserInquiryUsableCoupon(string|int $user_id, string|int $inquiry_type): array
|
|
{
|
|
$user_coupons = UserCoupon::getUserInquiryUsableCoupon($user_id, $inquiry_type);
|
|
if (empty($user_coupons)) {
|
|
return array();
|
|
}
|
|
|
|
// 选中的优惠卷
|
|
$selected_coupons = array();
|
|
|
|
// 优惠卷最高金额
|
|
$coupon_high_price = 0;
|
|
|
|
// 是否存在互斥卷
|
|
$is_mutex = 0;
|
|
|
|
// 可选择优惠卷中是否存在互斥卷
|
|
foreach ($user_coupons as $coupon) {
|
|
if ($coupon['is_mutex'] == 1) {
|
|
$is_mutex = 1;
|
|
}
|
|
}
|
|
|
|
foreach ($user_coupons as $coupon) {
|
|
if (empty($selected_coupon)) {
|
|
$selected_coupons[] = $coupon; // 选中的优惠卷数据
|
|
|
|
$coupon_high_price = $coupon['coupon_price'];
|
|
continue;
|
|
}
|
|
|
|
// 处理存在互斥卷情况
|
|
if ($is_mutex == 1) {
|
|
// 选择金额最高的为选中
|
|
if ($coupon['coupon_price'] < $coupon_high_price){
|
|
continue;
|
|
}
|
|
|
|
if ($coupon['coupon_price'] > $coupon_high_price) {
|
|
$coupon_high_price = $coupon['coupon_price'];
|
|
|
|
// 选中的优惠卷数据置空
|
|
$selected_coupons = array();
|
|
$selected_coupons[] = $coupon;
|
|
|
|
continue;
|
|
}
|
|
}
|
|
|
|
$selected_coupons[] = $coupon; // 选中的优惠卷数据
|
|
}
|
|
|
|
return $selected_coupons;
|
|
}
|
|
|
|
// 获取可用优惠卷总金额
|
|
public function getCouponTotalPrice(array $coupons): int
|
|
{
|
|
$coupon_total_price = 0;
|
|
|
|
foreach ($coupons as $coupon){
|
|
$coupon_total_price = bcadd($coupon_total_price,$coupon['coupon_price'],2);
|
|
}
|
|
|
|
return $coupon_total_price;
|
|
}
|
|
} |