try(ResultSetrows=st.executeQuery("select menu_id,parent_id,path,component,perms from sys_menu where menu_id=2600 or (parent_id=2100 and path='collaboration') or component='project/manage/index' for update")){
thrownewIllegalStateException("Collaboration entry conflicts with an existing menu; nothing overwritten");
}
exists=true;
}
}
intmenus=exists?0:st.executeUpdate("insert into sys_menu(menu_id,menu_name,parent_id,order_num,path,component,route_name,is_frame,is_cache,menu_type,visible,status,perms,icon,create_by,create_time,remark) values(2600,'项目协作',2100,2,'collaboration','project/manage/index','',1,0,'C','0','0','project:manage:read','tree','admin',current_timestamp,'保留独立维护的项目协作入口')");
connection.commit();
System.out.println("RESTORED collaboration entry="+menus+" in "+DATABASE+"; role grants, other menus and project business data unchanged");
// Lock the snapshot source too; do not compete with the old application's mutations.
try(ResultSetrs=st.executeQuery("select id from stock_info order by id for update")){while(rs.next()){}}
try(ResultSetrs=st.executeQuery("select id from stock_lot order by id for update")){while(rs.next()){}}
Stringreconciliation="select count(*) from (select s.area_id,s.product_id,s.stock_num,coalesce(sum(case when l.lot_status in (0,1) then l.available_quantity else 0 end),0) lot_num from stock_info s left join stock_lot l on l.area_id=s.area_id and l.product_id=s.product_id group by s.id,s.area_id,s.product_id,s.stock_num having stock_num<>lot_num union all select l.area_id,l.product_id,0,sum(l.available_quantity) from stock_lot l left join stock_info s on s.area_id=l.area_id and s.product_id=l.product_id where s.id is null and l.lot_status in (0,1) group by l.area_id,l.product_id having sum(l.available_quantity)<>0) mismatch";
try(ResultSetrs=st.executeQuery(reconciliation)){
rs.next();if(rs.getLong(1)!=0)thrownewIllegalStateException("Legacy quantity reconciliation failed; no inventory was changed");
}
try(ResultSetrs=st.executeQuery("select (select count(*) from inventory_cost_event)+(select count(*) from project_stock_balance)+(select count(*) from project_item_cost)")){
rs.next();if(rs.getLong(1)!=0)thrownewIllegalStateException("New inventory already populated without cutover marker");
}
st.executeUpdate("insert into inventory_cost_event(project_id,item_id,area_id,event_key,event_type,quantity,receipt_amount,original_cost,effective_cost,create_by) select source_project_id,product_id,area_id,concat('LEGACY-STOCK:',id),'RECEIPT',available_quantity,case when unit_cost>0 then round(available_quantity*unit_cost,6) else null end,case when unit_cost>0 then round(available_quantity*unit_cost,6) else null end,case when unit_cost>0 then round(available_quantity*unit_cost,6) else null end,'inventory-cutover' from stock_lot where lot_status in (0,1) and available_quantity>0 order by id");
st.executeUpdate("insert into project_stock_balance(project_id,item_id,area_id,quantity) select project_id,item_id,area_id,sum(quantity) from inventory_cost_event group by project_id,item_id,area_id");
st.executeUpdate("insert into project_item_cost(project_id,item_id,quantity,amount) select project_id,item_id,sum(quantity),case when count(receipt_amount)=count(*) then sum(receipt_amount) else null end from inventory_cost_event group by project_id,item_id");
st.executeUpdate("insert into inventory_cutover(id,remark) values(1,'Current available stock preserved; historical zero prices treated as unknown; historical documents unchanged')");
connection.commit();
System.out.println("CUTOVER APPLIED TO "+DATABASE+"; original stock and documents preserved");
try(PreparedStatementst=connection.prepareStatement("select table_name, engine from information_schema.tables where table_schema=? and table_type='BASE TABLE' order by table_name")){
// Only additive DDL and scoped DML. Never change server configuration, accounts, catalogs or other schemas.
for(Stringstatement:statements){
Stringnormalized=statement.strip();
if(!normalized.matches("(?is)^(CREATE TABLE(?: IF NOT EXISTS)?|ALTER TABLE|INSERT INTO|UPDATE|SELECT)\\s+.*"))thrownewIllegalArgumentException("Unsupported migration statement");