<?php
define('APP_PATH','/workspace/application/');require '/workspace/env.php';require '/workspace/thinkphp/base.php';require_once '/workspace/application/common.php';\think\Config::set(require APP_PATH.'database.php','database');\think\Config::set('meal_type',3);
\think\Db::execute('SET TRANSACTION READ ONLY');\think\Db::startTrans();
try {
 $prefix=\think\Config::get('database.prefix');
 $sql='SELECT COUNT(DISTINCT r.menu_date,r.meal_times,r.dishes_uuid) AS supplies, COUNT(DISTINCT r.menu_date) AS days FROM '.$prefix.'recipes r JOIN '.$prefix.'dishes d ON d.uuid=r.dishes_uuid WHERE r.restaurant_id=1 AND r.menu_date BETWEEN ? AND ? AND d.store_id=10001 AND d.is_del=1 AND d.status=1 AND d.is_delete=0';
 $oracle=\think\Db::query($sql,['2026-10-01','2026-10-12'])[0];
 $times=[];$responses=[];
 foreach(['single','core','full'] as $kind){$t=microtime(true);$logic=new \app\p\logic\MenuNutrition;$responses[$kind]=$kind==='single'?$logic->analyze(1,'2026-10-10','2026-10-10',2):$logic->analyze(1,'2026-10-01','2026-10-12',null,$kind==='full');$times[$kind]=round((microtime(true)-$t)*1000,2);}
 $core=$responses['core'];$full=$responses['full'];unset($core['recommendations_deferred'],$core['recommendations'],$full['recommendations']);
 if($core!==$full)throw new RuntimeException('core/full facts differ');
 if((int)$oracle['supplies']!==$core['supply_count']||(int)$oracle['days']!==count($core['trends']))throw new RuntimeException('independent SQL supply count mismatch');
 $check=[];foreach($responses['full']['recommendations'] as $week){$seen=[];$n=0;foreach($week['groups'] as $g){if(count($g['list'])>3)throw new RuntimeException('more than 3');foreach($g['list'] as $d){if(isset($seen[$d['dish_id']]))throw new RuntimeException('duplicate candidate');$seen[$d['dish_id']]=1;$n++;}}$check[]=['week'=>$week['week_start'],'unique_candidates'=>$n];}
 $records=\think\Db::name('date_menu')->where('restaurant_id',1)->where('meal_type',3)->where('menu_date','<=','2026-10-10')->field('menu_date,COUNT(*) AS records')->group('menu_date')->order('menu_date','desc')->limit(3)->select();
 echo json_encode(['sql_oracle'=>$oracle,'core_full_equal'=>true,'milliseconds_single_sample'=>$times,'week_checks'=>$check,'recent_record_days'=>$records],JSON_PRETTY_PRINT|JSON_UNESCAPED_UNICODE).PHP_EOL;
}finally{\think\Db::rollback();}
