[ 'days' => 30, 'price_field' => 'price', 'backup_price_field' => 'backup_price', ], 'quarterly' => [ 'days' => 90, 'price_field' => 'quarterly_price', 'backup_price_field' => 'backup_quarterly_price', ], 'annual' => [ 'days' => 365, 'price_field' => 'annual_price', 'backup_price_field' => 'backup_annual_price', ], ]; protected $connection = 'default'; protected $table = 'cloud_offers'; protected $casts = [ 'id' => 'int', 'plan_id' => 'int', 'resource_pool_id' => 'int', 'price' => 'decimal:2', 'quarterly_price' => 'decimal:2', 'annual_price' => 'decimal:2', 'backup_price' => 'decimal:2', 'backup_quarterly_price' => 'decimal:2', 'backup_annual_price' => 'decimal:2', 'price_version' => 'int', 'revision' => 'int', 'status' => 'boolean', 'stock' => 'int', 'sort' => 'int', 'created_at' => 'int', 'updated_at' => 'int', ]; public static function generateSystemCode(): string { return 'offer-' . bin2hex(random_bytes(16)); } public static function validateOffer(array $input): array { $poolId = filter_var($input['resource_pool_id'] ?? null, FILTER_VALIDATE_INT); if ($poolId === false || $poolId < 1) { throw new \InvalidArgumentException('请选择有效的销售位置'); } $code = isset($input['code']) && is_string($input['code']) ? trim($input['code']) : ''; if (preg_match('/^[a-z0-9][a-z0-9_-]{0,63}$/D', $code) !== 1) { throw new \InvalidArgumentException('销售方案系统标识需为 1 至 64 位小写字母、数字、下划线或连字符'); } $name = isset($input['name']) && is_string($input['name']) ? trim($input['name']) : ''; if ($name === '' || mb_strlen($name) > 128) { throw new \InvalidArgumentException('销售名称需为 1 至 128 个字符'); } $data = [ 'resource_pool_id' => (int) $poolId, 'code' => $code, 'name' => $name, 'currency' => 'USD', ]; foreach ([ 'price' => '实例月付价格', 'quarterly_price' => '实例季度价格', 'annual_price' => '实例年付价格', 'backup_price' => '自动备份月付价格', 'backup_quarterly_price' => '自动备份季度价格', 'backup_annual_price' => '自动备份年付价格', ] as $field => $label) { $data[$field] = self::normalizePrice( $input[$field] ?? '', $label ); } foreach ([ 'status' => [0, 1, 1, '销售状态'], 'stock' => [-1, 2147483647, -1, '库存'], 'sort' => [-1000000, 1000000, 0, '排序'], ] as $field => [$minimum, $maximum, $default, $label]) { $raw = $input[$field] ?? $default; $value = is_string($raw) || is_int($raw) ? filter_var($raw, FILTER_VALIDATE_INT) : false; if ($value === false || $value < $minimum || $value > $maximum) { throw new \InvalidArgumentException( $label . ' 需为 ' . $minimum . ' 至 ' . $maximum . ' 的整数' ); } $data[$field] = (int) $value; } return $data; } public static function normalizeBillingCycle($value): string { if (!is_string($value) || !isset(self::BILLING_CYCLES[$value])) { throw new \InvalidArgumentException('请选择有效的购买周期'); } return $value; } public static function billingCycleForDays(int $days): string { foreach (self::BILLING_CYCLES as $code => $definition) { if ($definition['days'] === $days) { return $code; } } throw new \InvalidArgumentException('实例服务周期不受销售方案支持'); } /** * Return the exact mutable offer prices selected by a stable cycle code. * Callers must freeze this snapshot into their order or instance ledger. */ public function billingCycle(string $code): array { $code = self::normalizeBillingCycle($code); $definition = self::BILLING_CYCLES[$code]; $priceField = $definition['price_field']; $backupPriceField = $definition['backup_price_field']; return [ 'code' => $code, 'days' => $definition['days'], 'price' => (string) $this->$priceField, 'backup_price' => (string) $this->$backupPriceField, ]; } public function billingCycleForServiceDays(int $days): array { return $this->billingCycle(self::billingCycleForDays($days)); } public function billingCycles(): array { $cycles = []; foreach (array_keys(self::BILLING_CYCLES) as $code) { $cycles[] = $this->billingCycle($code); } return $cycles; } private static function normalizePrice($value, string $label): string { if ((!is_string($value) && !is_int($value)) || preg_match('/^\d{1,10}(?:\.\d{1,2})?$/D', (string) $value) !== 1) { throw new \InvalidArgumentException( $label . '需为非负美元金额,最多两位小数' ); } $parts = explode('.', (string) $value); $whole = ltrim($parts[0], '0'); return ($whole === '' ? '0' : $whole) . '.' . str_pad($parts[1] ?? '', 2, '0'); } }