-- ============================================================
-- KTV服务 定价修复 Part 4
-- 每个模型独立ppid + 按量计费(price_factors=字段名)
-- ============================================================

-- 1. 创建独立ppid（每模型一个）
INSERT INTO pricing_program (id,providerid,name) VALUES 
('pp_ktv_asr','FZoJv7EUEdKL3pUt0BQ5S','KTV-语音识别'),
('pp_ktv_demucs','FZoJv7EUEdKL3pUt0BQ5S','KTV-音频分离'),
('pp_ktv_face_detect','FZoJv7EUEdKL3pUt0BQ5S','KTV-人脸检测'),
('pp_ktv_face_recognize','FZoJv7EUEdKL3pUt0BQ5S','KTV-人脸识别'),
('pp_ktv_face_compare','FZoJv7EUEdKL3pUt0BQ5S','KTV-人脸比对'),
('pp_ktv_subtitle','FZoJv7EUEdKL3pUt0BQ5S','KTV-字幕渲染'),
('pp_ktv_merge','FZoJv7EUEdKL3pUt0BQ5S','KTV-视频合并'),
('pp_ktv_songrate','FZoJv7EUEdKL3pUt0BQ5S','KTV-歌曲评分'),

('pp_ktv_synth_status','FZoJv7EUEdKL3pUt0BQ5S','KTV-合成状态'),
('pp_ktv_realesrgan','FZoJv7EUEdKL3pUt0BQ5S','KTV-超分辨率'),
('pp_ktv_realesrgan_status','FZoJv7EUEdKL3pUt0BQ5S','KTV-超分状态'),
('pp_ktv_rvc','FZoJv7EUEdKL3pUt0BQ5S','KTV-声音转换'),
('pp_ktv_video_eval','FZoJv7EUEdKL3pUt0BQ5S','KTV-视频评估')
ON DUPLICATE KEY UPDATE providerid=VALUES(providerid);

-- 2. 迁移 pricing_program_timing 到新 ppid
UPDATE pricing_program_timing SET ppid='pp_ktv_asr' WHERE id='pt_ktv_asr_transcribe';
UPDATE pricing_program_timing SET ppid='pp_ktv_demucs' WHERE id='pt_ktv_demucs_separate';
UPDATE pricing_program_timing SET ppid='pp_ktv_face_detect' WHERE id='pt_ktv_face_detect';
UPDATE pricing_program_timing SET ppid='pp_ktv_face_recognize' WHERE id='pt_ktv_face_recognize';
UPDATE pricing_program_timing SET ppid='pp_ktv_face_compare' WHERE id='pt_ktv_face_compare';
UPDATE pricing_program_timing SET ppid='pp_ktv_subtitle' WHERE id='pt_ktv_subtitle_render';
UPDATE pricing_program_timing SET ppid='pp_ktv_merge' WHERE id='pt_ktv_merge_video';
UPDATE pricing_program_timing SET ppid='pp_ktv_songrate' WHERE id='pt_ktv_songrate_evaluate';

UPDATE pricing_program_timing SET ppid='pp_ktv_synth_status' WHERE id='pt_ktv_synth_status';
UPDATE pricing_program_timing SET ppid='pp_ktv_realesrgan' WHERE id='pt_ktv_realesrgan_upscale';
UPDATE pricing_program_timing SET ppid='pp_ktv_realesrgan_status' WHERE id='pt_ktv_realesrgan_status';
UPDATE pricing_program_timing SET ppid='pp_ktv_rvc' WHERE id='pt_ktv_rvc_convert';
UPDATE pricing_program_timing SET ppid='pp_ktv_video_eval' WHERE id='pt_ktv_video_eval_evaluate';

-- 3. 更新 pricing_data YAML：price_factors=字段名（按量计费）
-- asr: audio_seconds × 0.01
UPDATE pricing_program_timing SET pricing_data='unit_values:\n  audio_seconds: 1\nfields:\n  price_factors:\n    type: string\n    role: factor\n    label: 计价因子\n  unit_prices:\n    type: float\n    role: factor\n    label: 单位定价\n  unit:\n    type: string\n    role: factor\n    label: 计价单位\npricings:\n- price_factors: audio_seconds\n  unit_prices: 0.01\n  unit: audio_seconds' WHERE id='pt_ktv_asr_transcribe';

-- demucs: audio_seconds × 0.02
UPDATE pricing_program_timing SET pricing_data='unit_values:\n  audio_seconds: 1\nfields:\n  price_factors:\n    type: string\n    role: factor\n    label: 计价因子\n  unit_prices:\n    type: float\n    role: factor\n    label: 单位定价\n  unit:\n    type: string\n    role: factor\n    label: 计价单位\npricings:\n- price_factors: audio_seconds\n  unit_prices: 0.02\n  unit: audio_seconds' WHERE id='pt_ktv_demucs_separate';

-- face-detect: detect_count × 0.10
UPDATE pricing_program_timing SET pricing_data='unit_values:\n  detect_count: 1\nfields:\n  price_factors:\n    type: string\n    role: factor\n    label: 计价因子\n  unit_prices:\n    type: float\n    role: factor\n    label: 单位定价\n  unit:\n    type: string\n    role: factor\n    label: 计价单位\npricings:\n- price_factors: detect_count\n  unit_prices: 0.10\n  unit: detect_count' WHERE id='pt_ktv_face_detect';

-- face-recognize: recognize_count × 0.10
UPDATE pricing_program_timing SET pricing_data='unit_values:\n  recognize_count: 1\nfields:\n  price_factors:\n    type: string\n    role: factor\n    label: 计价因子\n  unit_prices:\n    type: float\n    role: factor\n    label: 单位定价\n  unit:\n    type: string\n    role: factor\n    label: 计价单位\npricings:\n- price_factors: recognize_count\n  unit_prices: 0.10\n  unit: recognize_count' WHERE id='pt_ktv_face_recognize';

-- face-compare: compare_count × 0.10
UPDATE pricing_program_timing SET pricing_data='unit_values:\n  compare_count: 1\nfields:\n  price_factors:\n    type: string\n    role: factor\n    label: 计价因子\n  unit_prices:\n    type: float\n    role: factor\n    label: 单位定价\n  unit:\n    type: string\n    role: factor\n    label: 计价单位\npricings:\n- price_factors: compare_count\n  unit_prices: 0.10\n  unit: compare_count' WHERE id='pt_ktv_face_compare';

-- subtitle: subtitle_count × 0.01
UPDATE pricing_program_timing SET pricing_data='unit_values:\n  subtitle_count: 1\nfields:\n  price_factors:\n    type: string\n    role: factor\n    label: 计价因子\n  unit_prices:\n    type: float\n    role: factor\n    label: 单位定价\n  unit:\n    type: string\n    role: factor\n    label: 计价单位\npricings:\n- price_factors: subtitle_count\n  unit_prices: 0.01\n  unit: subtitle_count' WHERE id='pt_ktv_subtitle_render';

-- merge: output_seconds × 0.01
UPDATE pricing_program_timing SET pricing_data='unit_values:\n  output_seconds: 1\nfields:\n  price_factors:\n    type: string\n    role: factor\n    label: 计价因子\n  unit_prices:\n    type: float\n    role: factor\n    label: 单位定价\n  unit:\n    type: string\n    role: factor\n    label: 计价单位\npricings:\n- price_factors: output_seconds\n  unit_prices: 0.01\n  unit: output_seconds' WHERE id='pt_ktv_merge_video';

-- songrate: audio_seconds × 0.005
UPDATE pricing_program_timing SET pricing_data='unit_values:\n  audio_seconds: 1\nfields:\n  price_factors:\n    type: string\n    role: factor\n    label: 计价因子\n  unit_prices:\n    type: float\n    role: factor\n    label: 单位定价\n  unit:\n    type: string\n    role: factor\n    label: 计价单位\npricings:\n- price_factors: audio_seconds\n  unit_prices: 0.005\n  unit: audio_seconds' WHERE id='pt_ktv_songrate_evaluate';

-- synth-generate: audio_seconds × 0.20
UPDATE pricing_program_timing SET pricing_data='unit_values:\n  audio_seconds: 1\nfields:\n  price_factors:\n    type: string\n    role: factor\n    label: 计价因子\n  unit_prices:\n    type: float\n    role: factor\n    label: 单位定价\n  unit:\n    type: string\n    role: factor\n    label: 计价单位\npricings:\n- price_factors: audio_seconds\n  unit_prices: 0.20\n  unit: audio_seconds' WHERE id='pt_ktv_synth_generate';

-- synth-status: flat 0.01
UPDATE pricing_program_timing SET pricing_data='unit_values:\n  次: 1\nfields:\n  price_factors:\n    type: string\n    role: factor\n    label: 计价因子\n  unit_prices:\n    type: float\n    role: factor\n    label: 单位定价\n  unit:\n    type: string\n    role: factor\n    label: 计价单位\npricings:\n- price_factors: flat\n  unit_prices: 0.01\n  unit: 次' WHERE id='pt_ktv_synth_status';

-- realesrgan-upscale: image_count × 0.50
UPDATE pricing_program_timing SET pricing_data='unit_values:\n  image_count: 1\nfields:\n  price_factors:\n    type: string\n    role: factor\n    label: 计价因子\n  unit_prices:\n    type: float\n    role: factor\n    label: 单位定价\n  unit:\n    type: string\n    role: factor\n    label: 计价单位\npricings:\n- price_factors: image_count\n  unit_prices: 0.50\n  unit: image_count' WHERE id='pt_ktv_realesrgan_upscale';

-- realesrgan-status: flat 0.01
UPDATE pricing_program_timing SET pricing_data='unit_values:\n  次: 1\nfields:\n  price_factors:\n    type: string\n    role: factor\n    label: 计价因子\n  unit_prices:\n    type: float\n    role: factor\n    label: 单位定价\n  unit:\n    type: string\n    role: factor\n    label: 计价单位\npricings:\n- price_factors: flat\n  unit_prices: 0.01\n  unit: 次' WHERE id='pt_ktv_realesrgan_status';

-- rvc: audio_seconds × 0.15
UPDATE pricing_program_timing SET pricing_data='unit_values:\n  audio_seconds: 1\nfields:\n  price_factors:\n    type: string\n    role: factor\n    label: 计价因子\n  unit_prices:\n    type: float\n    role: factor\n    label: 单位定价\n  unit:\n    type: string\n    role: factor\n    label: 计价单位\npricings:\n- price_factors: audio_seconds\n  unit_prices: 0.15\n  unit: audio_seconds' WHERE id='pt_ktv_rvc_convert';

-- video-eval: video_seconds × 0.05
UPDATE pricing_program_timing SET pricing_data='unit_values:\n  video_seconds: 1\nfields:\n  price_factors:\n    type: string\n    role: factor\n    label: 计价因子\n  unit_prices:\n    type: float\n    role: factor\n    label: 单位定价\n  unit:\n    type: string\n    role: factor\n    label: 计价单位\npricings:\n- price_factors: video_seconds\n  unit_prices: 0.05\n  unit: video_seconds' WHERE id='pt_ktv_video_eval_evaluate';

-- 4. 更新 llm_api_map 指向独立 ppid
UPDATE llm_api_map SET ppid='pp_ktv_asr' WHERE llmid='llm_ktv_asr_transcribe';
UPDATE llm_api_map SET ppid='pp_ktv_demucs' WHERE llmid='llm_ktv_demucs_separate';
UPDATE llm_api_map SET ppid='pp_ktv_face_detect' WHERE llmid='llm_ktv_face_detect';
UPDATE llm_api_map SET ppid='pp_ktv_face_recognize' WHERE llmid='llm_ktv_face_recognize';
UPDATE llm_api_map SET ppid='pp_ktv_face_compare' WHERE llmid='llm_ktv_face_compare';
UPDATE llm_api_map SET ppid='pp_ktv_subtitle' WHERE llmid='llm_ktv_subtitle_render';
UPDATE llm_api_map SET ppid='pp_ktv_merge' WHERE llmid='llm_ktv_merge_video';
UPDATE llm_api_map SET ppid='pp_ktv_songrate' WHERE llmid='llm_ktv_songrate_evaluate';

UPDATE llm_api_map SET ppid='pp_ktv_synth_status' WHERE llmid='llm_ktv_synth_status';
UPDATE llm_api_map SET ppid='pp_ktv_realesrgan' WHERE llmid='llm_ktv_realesrgan_upscale';
UPDATE llm_api_map SET ppid='pp_ktv_realesrgan_status' WHERE llmid='llm_ktv_realesrgan_status';
UPDATE llm_api_map SET ppid='pp_ktv_rvc' WHERE llmid='llm_ktv_rvc_convert';
UPDATE llm_api_map SET ppid='pp_ktv_video_eval' WHERE llmid='llm_ktv_video_eval_evaluate';

-- 修复 pricing_program 字符集不一致（生产环境 utf8mb4_general_ci vs organization utf8mb4_unicode_ci）
ALTER TABLE pricing_program MODIFY ownerid VARCHAR(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
ALTER TABLE pricing_program MODIFY providerid VARCHAR(32) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
