<?php
require __DIR__.'/oracle.php';$report=['lane'=>$lane,'passed'=>false,'business_dml'=>0,'ddl'=>0];try{
$staffRows=$q('SELECT ID,UUID,NAME,AI_USER_ID,IS_SHOW,JOB_STATUS FROM '.$p.'staff WHERE NAME LIKE ? ORDER BY ID',['CDXPAY%']);$nameSha=$lane==='B'?'f483ba1c130ac167b850ae5099196f085b2f5b7db2faf5787a201eb5f7dbf321':'8b7f1416ace012169e9ac5c94fceb49115287a7006c1f0712ebac2d30af000b4';$staffs=array_values(array_filter($staffRows,static function($r)use($nameSha){return preg_match('/^CDXPAY[A-F0-9]{8}$/D',$r['name'])===1&&hash('sha256',$r['name'])===$nameSha;}));$check(count($staffs)===1,'Exact known marked Staff');$staff=$staffs[0];$users=$q('SELECT ID,UUID,STAFF_UUID,ACCOUNT,TYPE,IS_DELETE FROM '.$p.'user WHERE STAFF_UUID=? AND IS_DELETE=0',[$staff['uuid']]);$check(count($users)===1&&$users[0]['account']===$staff['name']&&(int)$users[0]['type']===1&&(int)$staff['is_show']===1,'Synthetic user/Staff exact mapping');$check($actor===$actorNow(),'Actor unchanged');$report['reference']=['staff_name_sha256'=>$nameSha,'staff_sha256'=>$hash($staff),'user_sha256'=>$hash($users[0]),'actor_sha256'=>$hash($actor),'actor_uuid_length'=>strlen($actor['uuid'])];$fixtureAuthor='CDXSYE_FIXTURE';$report['fixture_author']=$fixtureAuthor;$report['PC_add_actor_UUID36_vs_create_by32_risk_remains_open']=true;
$natural=[];foreach(['syllabus_evaluate','syllabus_evaluate_ext']as $name){$table=$p.$name;$triggers=$lane==='B'?$q('SELECT TRIGGER_NAME,EVENT_MANIPULATION FROM information_schema.TRIGGERS WHERE TRIGGER_SCHEMA=DATABASE() AND EVENT_OBJECT_TABLE=?',[$table]):$q('SELECT TRIGGER_NAME,STATUS,TRIGGERING_EVENT FROM USER_TRIGGERS WHERE TABLE_NAME=?',[strtoupper($table)]);$fks=$lane==='B'?$q('SELECT TABLE_NAME,CONSTRAINT_NAME,REFERENCED_TABLE_NAME FROM information_schema.KEY_COLUMN_USAGE WHERE TABLE_SCHEMA=DATABASE() AND REFERENCED_TABLE_NAME IS NOT NULL AND (TABLE_NAME=? OR REFERENCED_TABLE_NAME=?)',[$table,$table]):$q("SELECT C.TABLE_NAME,C.CONSTRAINT_NAME,P.TABLE_NAME AS REFERENCED_TABLE_NAME FROM USER_CONSTRAINTS C LEFT JOIN USER_CONSTRAINTS P ON C.R_CONSTRAINT_NAME=P.CONSTRAINT_NAME WHERE C.CONSTRAINT_TYPE='R' AND (C.TABLE_NAME=? OR P.TABLE_NAME=?)",[strtoupper($table),strtoupper($table)]);$check($triggers===[]&&$fks===[],'No unknown trigger/FK');$rows=$q('SELECT * FROM '.$table.' ORDER BY ID');$natural[$name]=$rows;$check((int)$q('SELECT COUNT(*) AS CNT FROM '.$table.' WHERE STAFF_UUID=?',[$staff['uuid']])[0]['cnt']===0,'Marked staff has no prior records');$report['tables'][$name]=['triggers'=>$triggers,'foreign_keys'=>$fks,'natural_count'=>count($rows),'natural_sha256'=>$hash($rows)];if($lane==='C'){$ddl=$db->query("SELECT DBMS_METADATA.GET_DDL('TABLE','".strtoupper($table)."','ZHCT_APP') FROM DUAL")->fetchColumn();if(is_resource($ddl))$ddl=stream_get_contents($ddl);$check(preg_match('/["`]?ID["`]?\s+BIGINT\s+(?:IDENTITY\s*\(|AUTO_INCREMENT\b)/i',$ddl)===1,'Generated ID proven');$report['tables'][$name]['ddl_sha256']=hash('sha256',$ddl);}else{$meta=json_decode(file_get_contents(__DIR__.'/private-preflight.json'),true);$check($meta['tables'][$name]['columns'][0]['extra']==='auto_increment','Generated ID proven');}}
$marker='CDXSYL'.$lane.'20261010_'.strtoupper(bin2hex(random_bytes(3)));$uuid=static function():string{$x=strtoupper(bin2hex(random_bytes(16)));return substr($x,0,8).'-'.substr($x,8,4).'-'.substr($x,12,4).'-'.substr($x,16,4).'-'.substr($x,20);};$write=['syllabus_evaluate'=>[],'syllabus_evaluate_ext'=>[['uuid'=>$uuid(),'staff_uuid'=>$staff['uuid'],'content'=>'数据库兼容测试综合评定 '.$marker."\n第二行'完整中文"]]];
foreach([1,2]as $i)$write['syllabus_evaluate'][]=['uuid'=>$uuid(),'staff_uuid'=>$staff['uuid'],'test_content'=>'数据库兼容测试考核'.$i.' '.$marker."\n内容'完整中文",'test_time'=>'2001-01-0'.$i,'achievement'=>'达标'.$i,'demo'=>'数据库兼容测试备注 '.$marker.' 第'.$i.'条','create_time'=>'2001-01-01 08:00:00','create_by'=>$fixtureAuthor];
$meta=json_decode(file_get_contents(__DIR__.'/private-preflight.json'),true);foreach($write as $name=>$rows)foreach($rows as $row){foreach($meta['tables'][$name]['columns']as $c){$key=strtolower($c['column_name']);if($key==='id')continue;$check(array_key_exists($key,$row),'All non-ID fields explicit');if(($c['is_nullable']??$c['nullable'])==='NO'||($c['nullable']??'')==='N')$check($row[$key]!==null,'Required field');}$check((int)$q('SELECT COUNT(*) AS CNT FROM '.$p.$name.' WHERE UUID=?',[$row['uuid']])[0]['cnt']===0,'Own UUID absent');}foreach(['syllabus_evaluate'=>'test_content','syllabus_evaluate_ext'=>'content']as $name=>$column)$check((int)$q('SELECT COUNT(*) AS CNT FROM '.$p.$name.' WHERE '.$column.' LIKE ?',['%'.$marker.'%'])[0]['cnt']===0,'Marker absent');
$save('private-fixture-lease.json',['staff'=>$staff,'user'=>$users[0],'actor'=>$actor,'marker'=>$marker]);$save('private-fixture-natural.json',$natural);$save('private-fixture-write.json',$write);$report['marker']=$marker;$report['write_shape']=['syllabus_rows'=>2,'ext_rows'=>1,'staff_uuid_sha256'=>hash('sha256',$staff['uuid']),'uuid_format'=>'uppercase GUID36','test_content'=>'数据库兼容测试+marker+换行+单引号完整中文','test_time'=>['2001-01-01','2001-01-02'],'achievement'=>['达标1','达标2'],'demo'=>'数据库兼容测试+marker','create_time'=>'2001-01-01 08:00:00','create_by'=>$fixtureAuthor,'content'=>'数据库兼容测试综合评定+marker+换行+单引号完整中文'];$report['passed']=true;
}catch(Throwable $e){$save('private-fixture-error.json',['class'=>get_class($e),'message'=>$e->getMessage()]);$report['failure']=['class'=>get_class($e),'reason_sha256'=>hash('sha256',$e->getMessage())];}$save('private-fixture-gate.json',$report);echo json_encode($report,JSON_UNESCAPED_UNICODE),"\n";exit($report['passed']?0:2);
