toArray(); $coupons = array(); foreach ($user_coupons as $user_coupon){ $user_coupon['coupon']['user_coupon_id'] = $user_coupon['user_coupon_id']; $coupons[] = $user_coupon['coupon']; } // 选中的优惠卷 $selected_coupons = array(); // 优惠卷最高金额 $coupon_high_price = 0; // 是否存在互斥卷 $is_mutex = 0; // 可选择优惠卷中是否存在互斥卷 foreach ($coupons as $coupon) { if ($coupon['is_mutex'] == 1) { $is_mutex = 1; } } foreach ($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; } /** * 获取可用优惠卷总金额 * @param array $coupons 优惠卷数据 * @return string */ public function getCouponTotalPrice(array $coupons): string { $coupon_total_price = "0"; foreach ($coupons as $coupon){ $coupon_total_price = bcadd($coupon_total_price,$coupon['coupon_price'],2); } return $coupon_total_price; } /** * 获取患者购药可用的优惠卷 * @param string|int $user_id * @param array $coupon_product_datas * @return array */ // public function getUserProductUsableCoupon(string|int $user_id, array $coupon_product_datas): array // { // $user_coupons = UserCoupon::getUserProductUsableCoupon($user_id); // if (empty($user_coupons)) { // return array(); // } // // $user_coupons = $user_coupons->toArray(); // // $coupons = array(); // foreach ($user_coupons as $user_coupon){ // $user_coupon['coupon']['user_coupon_id'] = $user_coupon['user_coupon_id']; // $coupons[] = $user_coupon['coupon']; // } // // // 选中的优惠卷 // $selected_coupons = array(); // // // 优惠卷最高金额 // $coupon_high_price = 0; // // // 是否存在互斥卷 // $is_mutex = 0; // // // 新增字段 // foreach ($coupons as $key => $coupon) { // $coupons[$key]['is_can_use'] = 1;// 是否可使用 // $coupons[$key]['cannot_use_coupon_reason'] = ""; // 不可使用原因 // } // // // 处理优惠卷数量限制问题 // foreach ($coupons as $key => $coupon) { // // 处理数量优惠卷 // if ($coupon['coupon_type'] == 3){ // // 数量是否足够标识字段 // $quantity_quantity = 0; // // foreach ($coupon_product_datas as $coupon_product_data){ // // 存在指定商品的情况,如该优惠卷不包含此商品,跳过 // if (!empty($coupon['product_id'])){ // if (!str_contains($coupon['product_id'], $coupon_product_data['product_id'])){ // continue; // } // } // // // 判断商品数量是否满足 // if($coupon['min_usable_number'] > $coupon_product_data['product_num']){ // continue; // } // // // 标记为数量足够 // $quantity_quantity = 1; // } // // if ($quantity_quantity == 0){ // // 此优惠卷无商品能达到规定最小数量 // $coupons[$key]['is_can_use'] = 0; // $coupons[$key]['cannot_use_coupon_reason'] = "商品不足" . $coupon['min_usable_number'] . "盒,不满足使用优惠卷条件"; // continue; // } // } // // // 处理满减金额/无门槛优惠卷 // if ($coupon['coupon_type'] == 2 || $coupon['coupon_type'] == 1){ // // 可共用一个优惠卷的商品金额问题 // $product_price = 0; // // foreach ($coupon_product_datas as $coupon_product_data) { // // 如该优惠卷不包含此商品,跳过 // if (!empty($coupon['product_id'])){ // if (!str_contains($coupon['product_id'], $coupon_product_data['product_id'])) { // continue; // } // } // // $product_price = bcadd($product_price, // bcmul( // 商品价格*数量 // $coupon_product_data['product_price'], // $coupon_product_data['product_num'], // 2 // ), // 2 // ); // } // // // 满减金额优惠卷 // if ($coupon['coupon_type'] == 2){ // if ($coupon['with_amount'] > $product_price){ // // 此优惠卷因商品金额不足,无法使用 // $coupons[$key]['is_can_use'] = 0; // continue; // } // } // // // 无门槛优惠卷 // if ($coupon['coupon_type'] == 1){ // if ($product_price <= 0){ // // 商品总金额已经为0,不使用优惠卷 // $coupons[$key]['is_can_use'] = 0; // continue; // } // } // } // // // 判断品牌是否符合-暂无品牌 // // // } // // // 可选择优惠卷中是否存在互斥卷 // foreach ($coupons as $coupon) { // if ($coupon['is_can_use'] == 0){ // continue; // } // // if ($coupon['is_mutex'] == 1) { // $is_mutex = 1; // } // } // // // 处理存在互斥卷情况 // foreach ($coupons as $coupon) { // if ($coupon['is_can_use'] == 0){ // continue; // } // // if (empty($selected_coupons)) { // $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; // } /** * 获取患者购药可用的优惠卷 * @param string|int $user_id * @param array $coupon_product_datas * @return array */ public function getUserProductUsableCoupon(string|int $user_id, array $coupon_product_datas): array { $user_coupons = UserCoupon::getUserProductUsableCoupon($user_id); if (empty($user_coupons)) { return array(); } $user_coupons = $user_coupons->toArray(); $coupons = array(); foreach ($user_coupons as $user_coupon){ $user_coupon['coupon']['user_coupon_id'] = $user_coupon['user_coupon_id']; $coupons[] = $user_coupon['coupon']; } // 选中的优惠卷 $selected_coupons = array(); // 优惠卷最高金额 $coupon_high_price = 0; // 是否存在互斥卷 $is_mutex = 0; // 新增字段 foreach ($coupons as $key => $coupon) { $coupons[$key]['is_can_use'] = 1;// 是否可使用 $coupons[$key]['cannot_use_coupon_reason'] = ""; // 不可使用原因 } foreach ($coupons as $key => $coupon) { // 优惠卷类型(1:无门槛 2:满减 3:数量) if ($coupon['coupon_type'] == 3){ // 数量是否足够标识字段 $quantity_quantity = 0; // 品牌 if ($coupon['application_scope'] == 3){ // 获取商品品牌 // 计算相同品牌的商品总数量 // 标记为数量足够 continue; } // 类别 if ($coupon['application_scope'] == 4){ // 获取商品类别 // 计算相同类别的商品总数量 // 标记为数量足够 continue; } // 单品使用 if ($coupon['application_scope'] == 5){ foreach ($coupon_product_datas as $coupon_product_data){ if (!str_contains($coupon['product_id'], $coupon_product_data['product_id'])){ continue; } // 判断商品数量是否满足 if($coupon['min_usable_number'] > $coupon_product_data['actual_quantity']){ continue; } // 标记为数量足够 $quantity_quantity = 1; break; } } // 全场通用/全品类药品 if ($coupon['application_scope'] == 1 || $coupon['application_scope'] == 6){ // 获取总商品数量 $actual_quantity = 0; foreach ($coupon_product_datas as $coupon_product_data){ $actual_quantity = $actual_quantity + $coupon_product_data['actual_quantity']; } // 判断商品数量是否满足 if($coupon['min_usable_number'] > $actual_quantity){ continue; } // 标记为数量足够 $quantity_quantity = 1; } if ($quantity_quantity == 0){ // 此优惠卷无商品能达到规定最小数量 $coupons[$key]['is_can_use'] = 0; $coupons[$key]['cannot_use_coupon_reason'] = "商品不足" . $coupon['min_usable_number'] . "盒,不满足使用优惠卷条件"; continue; } } // 满减金额/无门槛优惠卷 if ($coupon['coupon_type'] == 2 || $coupon['coupon_type'] == 1){ // 获取商品总金额 $product_price = 0; // 品牌 if ($coupon['application_scope'] == 3){ // 获取商品品牌 // 计算相同品牌的商品总金额 continue; } // 类别 if ($coupon['application_scope'] == 4){ // 获取商品类别 // 计算相同类别的商品总金额 continue; } // 单品使用 if ($coupon['application_scope'] == 5){ foreach ($coupon_product_datas as $coupon_product_data){ if (!str_contains($coupon['product_id'], $coupon_product_data['product_id'])){ continue; } $product_price = bcadd( (string)$product_price, bcmul( // 商品价格*实际数量数量 (string)$coupon_product_data['product_price'], (string)$coupon_product_data['actual_quantity'], 2 ), 2 ); } } // 全场通用/全品类药品 if ($coupon['application_scope'] == 1 || $coupon['application_scope'] == 6){ foreach ($coupon_product_datas as $coupon_product_data){ // 计算商品总金额 $product_price = bcadd( (string)$product_price, bcmul( // 商品价格*数量 (string)$coupon_product_data['product_price'], (string)$coupon_product_data['actual_quantity'], 2 ), 2 ); } } // 满减金额优惠卷 if ($coupon['coupon_type'] == 2){ if ($coupon['with_amount'] > $product_price){ // 此优惠卷因商品金额不足,无法使用 $coupons[$key]['is_can_use'] = 0; continue; } } // 无门槛优惠卷 if ($coupon['coupon_type'] == 1){ if ($product_price <= 0){ // 商品总金额已经为0,不使用优惠卷 $coupons[$key]['is_can_use'] = 0; continue; } } } } // 可选择优惠卷中是否存在互斥卷 foreach ($coupons as $coupon) { if ($coupon['is_can_use'] == 0){ continue; } if ($coupon['is_mutex'] == 1) { $is_mutex = 1; } } // 处理存在互斥卷情况 foreach ($coupons as $coupon) { if ($coupon['is_can_use'] == 0){ continue; } if (empty($selected_coupons)) { $selected_coupons[] = $coupon; // 选中的优惠卷数据 $coupon_high_price = $coupon['coupon_price']; continue; } // 处理存在互斥卷情况 if ($is_mutex == 1) { // 选择金额最高的为选中 if ($coupon['coupon_price'] < $coupon_high_price){ continue; } $coupon_high_price = $coupon['coupon_price']; // 选中的优惠卷数据置空 $selected_coupons = array(); $selected_coupons[] = $coupon; continue; } $selected_coupons[] = $coupon; // 选中的优惠卷数据 } return $selected_coupons; } /** * 退还用户优惠卷 * @param string|int $user_coupon_id * @return bool */ public function returnUserCoupon(string|int $user_coupon_id): bool { try { // 获取用户优惠卷数据 $params = array(); $params['user_coupon_id'] = $user_coupon_id; $user_coupon = UserCoupon::getOne($params); if (empty($user_coupon)) { // 无该优惠卷数据,无需处理 return true; } // 恢复优惠卷 $data = array(); // 检测优惠卷过期时间。判断是否需要退还 if (strtotime($user_coupon['valid_end_time']) <= time()) { // 超出过期时间,置为已过期 $data['user_coupon_status'] = 3; } else { $data['user_coupon_status'] = 0; $data['coupon_use_date'] = null; } $params = array(); $params['user_coupon_id'] = $user_coupon_id; UserCoupon::edit($params, $data); }catch (\Throwable $e){ Log::getInstance("UserCouponService-returnUserCoupon")->error($e->getMessage()); return false; } return true; } }