<?php
/** CLI 默认预览；--apply 只增加查询索引、启用新版入口，不修改盘点数据和事件。 */
if(PHP_SAPI!=='cli')exit;
$root=$argv[1]??'D:/phpstudy_pro/WWW/cqoms.com';if($root==='--apply')$root='D:/phpstudy_pro/WWW/cqoms.com';require $root.'/vendor/autoload.php';$app=new \think\App($root.'/');$app->initialize();
use think\facade\Db;
$p=Db::connect()->getConfig('prefix');if(!preg_match('/^[a-zA-Z0-9_]*$/D',$p))throw new RuntimeException('Invalid prefix');$apply=in_array('--apply',$argv,true);
foreach(['onsite_inventory'=>['idx_inv_line_rack'=>'line,rack'],'onsite_inventory_record'=>['idx_inv_plan_item'=>'plan_id,i_id','idx_inv_plan_line'=>'plan_id,line']] as $table=>$indexes){$existing=array_column(Db::query("SHOW INDEX FROM `{$p}{$table}`"),'Key_name');foreach($indexes as $name=>$cols){if(in_array($name,$existing,true))continue;echo "ADD INDEX $table.$name ($cols)\n";if($apply)Db::execute("ALTER TABLE `{$p}{$table}` ADD INDEX `$name` ($cols)");}}
if($apply)Db::name('admin_menus_wx')->where('action','eInventory')->update(['route'=>'/pages/inventory/index']);
echo $apply?"APPLIED (no inventory records changed)\n":"PREVIEW: use --apply to enable route and indexes\n";
