<?php
declare(strict_types=1);require __DIR__.'/native.php';require __DIR__.'/decimal-oracle.php';ck(!file_exists($runtime.'/last-direct-preconditions.json'),'Last direct prerequisites once');$ids=json_decode(file_get_contents($runtime.'/prepared.json'),true)['ids'];$guard=json_decode(file_get_contents($runtime.'/guard.json'),true);$own=ownSnapshot();$old=oldProtection();ck($old===$guard['old_protection'],'Original complete protection before last direct');$scope=$lane==='B'?'DIRECT_SUCCESS_RETRY1':'DIRECT_SUCCESS';$events=array_map(static function($l){return json_decode($l,true);},file($runtime.'/business-native.jsonl',FILE_IGNORE_NEW_LINES));$dml=[];$rollback=[];$commit=[];foreach($events as $e)if($e['scenario']===$scope){if(($e['layer']??'')==='actual PDOStatement.execute'&&in_array($e['operation']??'',['INSERT','UPDATE'],true))$dml[]=$e;if(($e['layer']??'')==='actual PDO.rollBack')$rollback[]=$e;if(($e['layer']??'')==='actual PDO.commit')$commit[]=$e;}ck(count(array_filter($dml,static function($e){return $e['operation']==='INSERT';}))===3&&count(array_filter($dml,static function($e){return $e['operation']==='UPDATE';}))===2,'Exact rolled three INSERT/two UPDATE');ck(count($rollback)===2&&$commit===[],'Native both rollback and zero commit');ck($own['account_settlement']===[]&&$own['user_balance_log']===[]&&$own['user_subsidy']===[]&&$own['user_gift_log']===[],'Fresh connection whole money scopes empty');ck(count($own['meal_order'])===1&&(int)$own['meal_order'][0]['source']===8&&(int)$own['meal_order'][0]['pay_status']===10,'Only committed prepay order remains');$payrow=$lane==='B'?'wire-PREPAY_FIRST_RETRY.json':'wire-PREPAY_FIRST_RETRY1.json';$pre=json_decode(file_get_contents($runtime.'/'.$payrow),true)['own_after'];ck($own['meal_order']===$pre['meal_order']&&$own['STORE_P1']===$pre['STORE_P1'],'Committed prepay exact fullrow unchanged');$direct=readNative('ZHCT','SELECT COUNT(*) n FROM ydy_meal_order WHERE user_id=? AND remark=?',[$ids['STORE_U1'],'CDXCORE11S1_DIRECT']);ck((int)$direct[0]['n']===0,'No committed direct order');$cast=$dm?'VARCHAR(40)':'CHAR';$money=readNative('AI','SELECT CAST(balance AS '.$cast.') balance_text,CAST(gift_balance AS '.$cast.') gift_text,CAST(pay_money AS '.$cast.') pay_text FROM yoshop_user WHERE user_id=?',[$ids['AI_U1']]);ck(dec102text($money[0]['balance_text'])==='1.00'&&dec102text($money[0]['gift_text'])==='0.00'&&dec102text($money[0]['pay_text'])==='0.00','Cash1 gift/pay0 current original physical');$hash=md5('meal_direct_pay_'.$ids['AI_U1'].'_'.$ids['STORE_R1']);$file=$runtime.'/cache/'.substr($hash,0,2).'/'.substr($hash,2).'.php';if(is_file($file)){clearstatcache(true,$file);$raw=file_get_contents($file);ck((int)substr($raw,8,12)===5&&time()>filemtime($file)+5,'Failed prior lock expired naturally no unlink');}saveOnce('last-direct-preconditions.json',['lane'=>$lane,'actual_rolled_back_DML'=>$dml,'actual_rollbacks'=>$rollback,'actual_commits'=>$commit,'own_fullrows'=>$own,'old_fullrows'=>$old,'money_server_text'=>$money,'direct_scope_count'=>$direct,'at'=>date('c'),'HTTP'=>0,'DML'=>0]);echo json_encode(['lane'=>$lane,'rollback2'=>true,'direct_money_scopes0'=>true,'prepay_unchanged'=>true,'cash'=>'1.00','old_protection_equal'=>true]),"\n";
