Files
MBTI_wang/api/app/common/service/TestProductPricing.php
Ghost a411bf7b21 高考版优化
审核模式优化
2026-04-29 18:01:26 +08:00

65 lines
2.5 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
namespace app\common\service;
use app\model\PricingConfig as PricingConfigModel;
use think\facade\Db;
/**
* 与 Payment::calculateAmount 中「测试类产品」分支一致,供非 Controller 复用(如高考定价)
*/
class TestProductPricing
{
/** @var string[] */
public const TEST_PRODUCT_TYPES = ['face', 'mbti', 'sbti', 'disc', 'pdp', 'resume', 'report', 'team_analysis', 'gaokao'];
/**
* @param int|null $enterpriseId 用于查价的企业 IDpersonal 时走 admin_personal+eidenterprise 时走 admin_enterprise+eid
* @param string $pricingTier personal|enterprise|gaokaogaokao=高考版全局/企业 admin_gaokao 覆盖(与 runtime scope=gaokao 一致)
* @return array{0:int,1:string} [amountFen, pricingType personal|enterprise|gaokao]
*/
public static function amountFenForTestProduct(
string $productType,
int $wechatUserId,
?int $enterpriseId = null,
int $quantity = 1,
string $pricingTier = 'personal'
): array {
$pricingType = $pricingTier === 'enterprise' ? 'enterprise' : 'personal';
$quantity = $quantity > 0 ? $quantity : 1;
$pricingEnterpriseId = null;
if ($enterpriseId !== null && (int) $enterpriseId > 0) {
$pricingEnterpriseId = (int) $enterpriseId;
} elseif ($wechatUserId > 0) {
$userEid = (int) Db::name('wechat_users')->where('id', $wechatUserId)->value('enterpriseId');
if ($userEid > 0) {
$pricingEnterpriseId = $userEid;
}
}
$pricingTypeForRead = $pricingType;
$priceKey = $productType;
if ($pricingTier === 'gaokao') {
$pricingTypeForRead = 'gaokao';
$priceKey = $productType === 'gaokao' ? 'gaokao' : $productType;
} elseif ($productType === 'gaokao') {
$pricingTypeForRead = 'gaokao';
$priceKey = 'gaokao';
}
$pricingConfig = PricingConfigModel::getByTypeAndEnterprise($pricingTypeForRead, $pricingEnterpriseId);
$config = [];
if ($pricingConfig && !empty($pricingConfig->config)) {
$raw = $pricingConfig->config;
$config = is_array($raw) ? $raw : (array) $raw;
}
$keyMap = ['team_analysis' => 'teamAnalysis'];
$key = $keyMap[$priceKey] ?? $priceKey;
$unitPriceYuan = isset($config[$key]) ? (float) $config[$key] : 0.0;
$amountFen = (int) round($unitPriceYuan * 100 * $quantity);
return [$amountFen, $pricingTypeForRead];
}
}