<?php
declare(strict_types=1);
date_default_timezone_set('Asia/Shanghai');require __DIR__.'/contract.php';$db=smPdo();$sha=catSha();catActor($db);$out=[];$errors=[];
$attempt=static function(string $name,callable $fn)use(&$out,&$errors):void{try{$out[$name]=$fn();}catch(Throwable $e){$out[$name]=null;$errors[$name]=['class'=>get_class($e),'message_sha256'=>hash('sha256',$e->getMessage())];smSave('candidate-error-'.$name.'-private.json',['class'=>get_class($e),'message'=>$e->getMessage()]);}};
$attempt('dishes',static function()use($db){$rows=smRows($db,smTop('SELECT id,uuid,name,category FROM ydy_dishes WHERE is_del=1 AND category BETWEEN 1 AND 6 ORDER BY id',5));foreach($rows as $r){if(strpbrk($r['name'],'%_')!==false){continue;}$n=catCount($db,'SELECT COUNT(*) n FROM ydy_dishes WHERE is_del=1 AND category=? AND name LIKE ?',[$r['category'],'%'.$r['name'].'%']);if($n>0&&$n<=100){return $r+['source_count'=>$n];}}return null;});
$attempt('recognize',static function()use($db){$r=smRows($db,smTop('SELECT d.id,d.dishes_uuid FROM ydy_dishes_recognize_img d JOIN ydy_dishes c ON c.uuid=d.dishes_uuid WHERE d.is_delete=0 AND c.is_del=1 ORDER BY d.id DESC',5));return $r?['rows'=>$r,'source_count'=>catCount($db,'SELECT COUNT(*) n FROM ydy_dishes_recognize_img d JOIN ydy_dishes c ON c.uuid=d.dishes_uuid WHERE d.is_delete=0 AND c.is_del=1')]:null;});
$attempt('orders',static function()use($db){$config=smRows($db,"SELECT config_value FROM ydy_config WHERE config_key='consume_order_restaurant' AND status=1");if(count($config)>1){throw new RuntimeException('Ambiguous order restaurant config');}$sameRestaurant=!empty($config)&&(string)$config[0]['config_value']==='1';$r=smRows($db,smTop("SELECT id,bind_time,equipment_code,meal_times,restaurant_id FROM ydy_meal_order WHERE is_delete=0 AND bind_time IS NOT NULL AND equipment_code<>'' ORDER BY id DESC",5));foreach($r as $row){$date=substr((string)$row['bind_time'],0,10);if(!preg_match('/^\d{4}-\d{2}-\d{2}$/D',$date)){continue;}$sql='SELECT COUNT(*) n FROM ydy_meal_order WHERE is_delete=0 AND bind_time>=? AND bind_time<=?';$args=[$date.' 00:00:00',$date.' 23:59:59'];if($sameRestaurant){$equipment=smRows($db,'SELECT id,restaurant_id FROM ydy_equipment WHERE code=? AND del_flag=0',[$row['equipment_code']]);if(count($equipment)!==1){continue;}$sql.=' AND restaurant_id=?';$args[]=$equipment[0]['restaurant_id'];}else{$sql.=' AND equipment_code=?';$args[]=$row['equipment_code'];}$sql.=' AND meal_times=?';$args[]=$row['meal_times'];$n=catCount($db,$sql,$args);if($n>0&&$n<=100){return $row+['date'=>$date,'same_restaurant'=>$sameRestaurant,'source_count'=>$n];}}return null;});
$attempt('card',static function()use($db){$ai=catAiPdo();$r=smRows($db,smTop('SELECT c.card_id,c.staff_uuid,u.id user_id,u.ai_user_id FROM ydy_staff_card c JOIN ydy_staff s ON s.uuid=c.staff_uuid JOIN ydy_user u ON u.staff_uuid=c.staff_uuid WHERE c.card_status=1 AND c.is_delete=0 AND s.is_show=1 AND u.is_delete=0 AND u.ai_user_id>0 ORDER BY c.id',5));foreach($r as $row){$users=catCount($db,'SELECT COUNT(*) n FROM ydy_user WHERE staff_uuid=? AND is_delete=0',[$row['staff_uuid']]);$staff=catCount($db,'SELECT COUNT(*) n FROM ydy_staff WHERE uuid=? AND is_show=1',[$row['staff_uuid']]);$cards=catCount($db,'SELECT COUNT(*) n FROM ydy_staff_card WHERE card_id=? AND card_status=1 AND is_delete=0',[$row['card_id']]);$aiUsers=smRows($ai,'SELECT user_id FROM yoshop_user WHERE user_id=? AND is_delete=0',[$row['ai_user_id']]);if($users===1&&$staff===1&&$cards===1&&count($aiUsers)===1){return $row;}}return null;});
$attempt('face',static function()use($db){$r=smRows($db,smTop('SELECT id,uuid FROM ydy_staff ORDER BY id ASC',20));return $r?['first_page'=>$r,'source_count'=>catCount($db,'SELECT COUNT(*) n FROM ydy_staff')]:null;});
$attempt('rotation',static function()use($db){$r=smRows($db,smTop("SELECT code,uuid,interval_time FROM ydy_nutrition_station WHERE code IS NOT NULL AND code<>'' ORDER BY id",5));foreach($r as $row){$n=catCount($db,'SELECT COUNT(*) n FROM ydy_annex WHERE master_uuid=?',[$row['uuid']]);if($n>0&&$n<=100){return $row+['source_count'=>$n];}}return $r?['empty_source'=>$r[0]]:null;});
smSave('candidates-private.json',['lane'=>getenv('SM7_LANE'),'source_sha256'=>$sha,'candidates'=>$out,'errors'=>$errors,'new_fixture_dml'=>0,'ddl'=>0]);$available=[];foreach($out as $name=>$value){$available[$name]=$value!==null;}echo json_encode(['lane'=>getenv('SM7_LANE'),'source_files'=>count($sha),'candidate_available'=>$available,'error_classes'=>array_map(static function($e){return $e['class'];},$errors),'new_fixture_dml'=>0,'ddl'=>0]),PHP_EOL;
