where('status', 'normal')->order('weigh desc,id asc')->select(), null, 'code'); } public static function read(array $product) { return empty($product['pricing_config']) ? null : json_decode($product['pricing_config'], true, 512, JSON_THROW_ON_ERROR); } private static function cents($value) { if (!is_scalar($value) || !preg_match('/^\d{1,8}(?:\.\d{1,2})?$/D', (string)$value) || (float)$value <= 0) { throw new InvalidArgumentException('请输入大于 0 元、最多两位小数的价格'); } return (int)round((float)$value * 100); } private static function validateSet(array $input, array $dictionary) { if (!is_array($input['services'] ?? null)) { throw new InvalidArgumentException('服务与价格配置格式无效'); } $services = []; foreach ($input['services'] as $code => $service) { if (!is_array($service) || !is_array($service['addons'] ?? [])) { throw new InvalidArgumentException('服务或增值服务配置格式无效'); } $type = $dictionary[$code] ?? []; if (empty($type['can_be_main'])) { throw new InvalidArgumentException('存在已停用或不可作为主服务的项目,请重新选择'); } $mode = $service['mode'] ?? ''; if (!in_array($mode, ['fixed', 'quote'], true) || ($mode === 'fixed' && empty($type['support_fixed']))) { throw new InvalidArgumentException($type['name'] . '的计价方式不可用'); } $addons = []; foreach (($service['addons'] ?? []) as $addon => $price) { if ($addon === $code || empty($dictionary[$addon]['can_be_addon'])) { throw new InvalidArgumentException($type['name'] . '包含重复、停用或不可作为增项的服务'); } $addons[$addon] = self::cents($price); } $services[$code] = ['mode' => $mode, 'cents' => $mode === 'fixed' ? self::cents($service['price'] ?? null) : null, 'addons' => $addons]; } $default = $input['default'] ?? ''; if (!is_string($default) || !isset($services[$default])) { throw new InvalidArgumentException('请添加至少一项服务,并指定默认服务'); } return ['default' => $default, 'services' => $services]; } public static function validate(array $input, array $dictionary, array $specIds, array $specNames = []) { $parent = self::validateSet($input, $dictionary); if (array_key_exists('spec_configs', $input)) { if (!is_array($input['spec_configs'])) { throw new InvalidArgumentException('规格配置格式无效'); } $specConfigs = []; foreach ($input['spec_configs'] as $specId => $set) { if (!ctype_digit((string)$specId) || !in_array((int)$specId, $specIds, true) || !is_array($set)) { throw new InvalidArgumentException('规格已变更,请重新打开配置'); } try { $specConfigs[$specId] = self::validateSet($set, $dictionary); } catch (InvalidArgumentException $e) { $label = isset($specNames[$specId]) ? '规格「' . $specNames[$specId] . '」' : '规格独立配置'; throw new InvalidArgumentException($label . ':' . $e->getMessage()); } } return $parent + ['spec_configs' => $specConfigs]; } // Accept the previous representation for compatibility with existing stored configurations. $services = $parent['services']; if (!is_array($input['specs'] ?? [])) { throw new InvalidArgumentException('规格价格格式无效'); } $specs = []; foreach (($input['specs'] ?? []) as $specId => $overrides) { if (!ctype_digit((string)$specId) || !in_array((int)$specId, $specIds, true) || !is_array($overrides)) { throw new InvalidArgumentException('规格已变更,请重新打开配置'); } foreach ($overrides as $code => $override) { if (!isset($services[$code]) || !is_array($override) || !is_array($override['addons'] ?? [])) { throw new InvalidArgumentException('规格价格引用了未配置的服务'); } if (isset($override['price']) && $override['price'] !== '') { if ($services[$code]['mode'] !== 'fixed') { throw new InvalidArgumentException('师傅报价服务无需填写规格单价'); } $specs[$specId][$code]['cents'] = self::cents($override['price']); } foreach (($override['addons'] ?? []) as $addon => $price) { if (!isset($services[$code]['addons'][$addon])) { throw new InvalidArgumentException('规格价格引用了未配置的增值服务'); } if ($price !== '' && $price !== null) { $specs[$specId][$code]['addons'][$addon] = self::cents($price); } } } } return $parent + ['specs' => $specs]; } public static function legacyDraft(array $product, array $dictionary, array $specs) { $draft = ['revision' => 0, 'default' => '', 'services' => [], 'specs' => []]; foreach (json_decode($product['service_types'] ?? '', true) ?: [] as $service) { $code = $service['code'] ?? ''; if (!$code) { continue; } $price = $service['price'] ?? 0; $mode = !empty($dictionary[$code]['support_fixed']) && $price > 0 ? 'fixed' : 'quote'; $draft['services'][$code] = ['mode' => $mode, 'cents' => $mode === 'fixed' ? (int)round($price * 100) : null, 'addons' => []]; if (!empty($service['is_default'])) { $draft['default'] = $code; } } foreach ($specs as $spec) { $prices = json_decode($spec['service_prices'] ?? '', true) ?: []; foreach ($draft['services'] as $code => $service) { if ($service['mode'] !== 'fixed') { continue; } $price = (float)($prices[$code] ?? 0); if (!$price && ($code === $draft['default'] || $code === 'install')) { $price = (float)$spec['base_price']; } if ($price > 0) { $draft['specs'][$spec['id']][$code]['cents'] = (int)round($price * 100); } } } return $draft; } public static function expandLegacySpecs(array $config) { $sets = $config['spec_configs'] ?? []; foreach ($config['specs'] ?? [] as $specId => $overrides) { if (!$overrides || isset($sets[$specId])) { continue; } $services = $config['services']; foreach ($overrides as $code => $override) { if (!isset($services[$code])) { continue; } if ($services[$code]['mode'] === 'fixed' && isset($override['cents'])) { $services[$code]['cents'] = $override['cents']; } $services[$code]['addons'] = array_replace( $services[$code]['addons'], array_intersect_key($override['addons'] ?? [], $services[$code]['addons']) ); } $sets[$specId] = ['default' => $config['default'], 'services' => $services]; } return $sets; } public static function options(array $config, array $dictionary, $specId) { $specConfig = $config['spec_configs'][$specId] ?? null; $set = $specConfig ?? $config; $services = []; foreach ($set['services'] as $code => $service) { $type = $dictionary[$code] ?? []; if (empty($type['can_be_main'])) { continue; } $override = $specConfig === null ? ($config['specs'][$specId][$code] ?? []) : []; $mode = $service['mode'] === 'fixed' && !empty($type['support_fixed']) ? 'fixed' : 'quote'; $addons = []; foreach ($service['addons'] as $addon => $cents) { if ($addon === $code || empty($dictionary[$addon]['can_be_addon'])) { continue; } $addons[$addon] = [ 'code' => $addon, 'name' => $dictionary[$addon]['name'], 'unit' => $dictionary[$addon]['unit'], 'cents' => $override['addons'][$addon] ?? $cents, 'source' => ($specConfig !== null || isset($override['addons'][$addon])) ? 'spec' : 'product', ]; } $services[$code] = [ 'code' => $code, 'name' => $type['name'], 'unit' => $type['unit'], 'mode' => $mode, 'cents' => $mode === 'fixed' ? ($override['cents'] ?? $service['cents']) : null, 'source' => $specConfig !== null || ($mode === 'fixed' && isset($override['cents'])) ? 'spec' : 'product', 'addons' => $addons, ]; } $default = isset($services[$set['default']]) ? $set['default'] : ''; return ['default' => $default, 'services' => $services]; } public static function priceLine(array $config, array $dictionary, $specId, $code, $quantity, array $requestedAddons, $fixed) { $options = self::options($config, $dictionary, $specId); $code = $code === '' ? $options['default'] : $code; $service = $options['services'][$code] ?? null; if (!$service || ($fixed && $service['mode'] !== 'fixed')) { throw new InvalidArgumentException('所选服务不可用或不支持一口价,请重新选择'); } $addons = []; $cents = $fixed ? $service['cents'] : null; $total = ($cents ?? 0) * $quantity; foreach ($requestedAddons as $requested) { if (!is_array($requested)) { throw new InvalidArgumentException('增值服务选项格式无效'); } $num = filter_var($requested['num'] ?? $requested['count'] ?? 1, FILTER_VALIDATE_INT); if ($num === 0) { continue; } $addonCode = $requested['code'] ?? ''; if ($num === false || $num < 0 || !is_string($addonCode) || !isset($service['addons'][$addonCode]) || isset($addons[$addonCode])) { throw new InvalidArgumentException('存在重复或不可选的增值服务,请重新选择'); } $addon = $service['addons'][$addonCode]; $addon['num'] = $num; $addon['price'] = $fixed ? $addon['cents'] / 100 : null; $total += $fixed ? $addon['cents'] * $num : 0; unset($addon['cents']); $addons[$addonCode] = $addon; } return [ 'spec_id' => $specId, 'service_type' => $code, 'service_type_name' => $service['name'], 'base_price' => $fixed ? $cents / 100 : null, 'unit' => $service['unit'], 'extras' => array_values($addons), 'revision' => $config['revision'], 'total_cents' => $total, ]; } public static function publicServices(array $options) { $services = []; foreach ($options['services'] as $code => $service) { $addons = []; foreach ($service['addons'] as $addon) { $addon['price'] = $addon['cents'] / 100; unset($addon['cents']); $addons[] = $addon; } $service['price'] = isset($service['cents']) ? $service['cents'] / 100 : null; $service['is_default'] = (int)($options['default'] === $code); $service['support_fixed'] = (int)($service['mode'] === 'fixed'); $service['addons'] = $addons; unset($service['cents']); $services[] = $service; } return $services; } }