Пример копирования тарифного плана
Материал из BiTel WiKi
Версия от 09:18, 14 апреля 2010; Cromeshnic (Обсуждение | вклад)
Пример копирования глобального тарифного плана в персональный.
import java.sql.*; import java.util.*; import bitel.billing.server.tariff.*; import bitel.billing.server.contract.bean.*; private CopyNode(node_id,new_node_id,new_mtree_id) { query = "SELECT id, type, data, pos FROM mtree_node WHERE parent_node='"+node_id+"'"; ps = con.prepareStatement(query); rs = ps.executeQuery(); while (rs.next()) { node_id_ = rs.getInt(1); type = rs.getString(2); data = rs.getString(3); pos = rs.getInt(4); new_node_ps = con.prepareStatement( "INSERT INTO mtree_node (parent_node,mtree_id,type,data,pos) VALUES (?, ?, ?, ?, ?)" ); new_node_ps.setInt( 1, new_node_id ); new_node_ps.setInt( 2, new_mtree_id ); new_node_ps.setString( 3, type ); new_node_ps.setString( 4, data ); new_node_ps.setInt( 5, pos ); new_node_ps.executeUpdate(); new_node_rs = con.prepareStatement( "SELECT LAST_INSERT_ID()" ).executeQuery(); new_node_rs.next(); new_node_id_ = new_node_rs.getInt(1); CopyNode(node_id_,new_node_id_,new_mtree_id); } } private CopyMTree(tree_id,new_tree_id) { // сгенерировать lm lm = new Date().getTime(); query = "SELECT module_tariff_tree.id, module_tariff_tree.mid, mtree_node.id "+ "FROM module_tariff_tree "+ " INNER JOIN mtree_node "+ " ON (module_tariff_tree.id=mtree_node.mtree_id) "+ "WHERE module_tariff_tree.tree_id='"+tree_id+"' AND mtree_node.type='root'"; ps = con.prepareStatement(query); rs = ps.executeQuery(); while (rs.next()) { mtree_id = rs.getInt(1); mid = rs.getInt(2); node_id = rs.getInt(3); new_mtree_ps = con.prepareStatement( "INSERT INTO module_tariff_tree (mid, tree_id, lm) VALUES (?, ?, ?)" ); new_mtree_ps.setInt( 1, mid ); new_mtree_ps.setInt( 2, new_tree_id ); new_mtree_ps.setLong( 3, lm ); new_mtree_ps.executeUpdate(); new_mtree_rs = con.prepareStatement( "SELECT LAST_INSERT_ID()" ).executeQuery(); new_mtree_rs.next(); new_mtree_id = new_mtree_rs.getInt(1); max_pos_ps = con.prepareStatement( "SELECT MAX(pos) FROM mtree_node" ); max_pos_rs = max_pos_ps.executeQuery(); max_pos_rs.next(); new_pos = max_pos_rs.getInt(1) + 1; new_node_ps = con.prepareStatement( "INSERT INTO mtree_node (parent_node,mtree_id,type,pos) VALUES ('0', ?, 'root', ?)" ); new_node_ps.setInt( 1, new_mtree_id ); new_node_ps.setInt( 2, new_pos ); new_node_ps.executeUpdate(); new_node_rs = con.prepareStatement( "SELECT LAST_INSERT_ID()" ).executeQuery(); new_node_rs.next(); new_node_id = new_node_rs.getInt(1); CopyNode(node_id,new_node_id,new_mtree_id); }; } private CopyTarif(tpid) { query = "SELECT tariff_tree_link.tree_id, tariff_plan.title "+ "FROM tariff_tree_link "+ " INNER JOIN tariff_plan "+ " ON (tariff_tree_link.tpid=tariff_plan.id) "+ "WHERE tariff_tree_link.tpid='"+tpid+"'"; ps = con.prepareStatement(query); rs = ps.executeQuery(); if ( !rs.next() ) { return; } tree_id = rs.getInt(1); tarif_title = rs.getString(2); // создать ПТ personal_tariff_manager = new PersonalTariffManager( con ); personal_tariff = new PersonalTariff(); personal_tariff.setContractId(cid); personal_tariff.setTitle(tarif_title); personal_tariff_manager.updatePersonalTariff(personal_tariff); // получить treeID tid = personal_tariff.getId(); personal_tariff = personal_tariff_manager.getPersonalTariff(tid); new_tree_id = personal_tariff.getTreeId(); CopyMTree(tree_id,new_tree_id); } cid = event.getContractID(); //Идентификатор глобального тарифного плана TPID = 62; CopyTarif(TPID);
Для копирования глобальных тарифных планов (пустой новый тп нужно создать руками): --Cromeshnic 07:07, 23 марта 2010 (UTC)
public void CopyTarif(tpid, new_tpid, con) { query = "SELECT tariff_tree_link.tree_id, tariff_plan.title "+ "FROM tariff_tree_link "+ " INNER JOIN tariff_plan "+ " ON (tariff_tree_link.tpid=tariff_plan.id) "+ "WHERE tariff_tree_link.tpid='"+tpid+"'"; ps = con.prepareStatement(query); rs = ps.executeQuery(); if ( !rs.next() ) { return; } tree_id = rs.getInt(1); tarif_title = rs.getString(2); query = "SELECT tariff_tree_link.tree_id, tariff_plan.title "+ "FROM tariff_tree_link "+ " INNER JOIN tariff_plan "+ " ON (tariff_tree_link.tpid=tariff_plan.id) "+ "WHERE tariff_tree_link.tpid='"+new_tpid+"'"; ps = con.prepareStatement(query); rs = ps.executeQuery(); if ( !rs.next() ) { return; } new_tree_id = rs.getInt(1); CopyMTree(tree_id,new_tree_id,con); }
Олсо, исправленная версия CopyNode: добавлен параметр mtree_id и условие and mtree_id='"+mtree_id+"'" в запросе. Необходимо при копировании наследованных веток тарифов:
public void CopyNode(node_id,new_node_id,mtree_id,new_mtree_id,con) { query = "SELECT id, type, data, pos FROM mtree_node WHERE parent_node='"+node_id+"' and mtree_id='"+mtree_id+"'"; ps = con.prepareStatement(query); rs = ps.executeQuery(); while (rs.next()) { node_id_ = rs.getInt(1); type = rs.getString(2); data = rs.getString(3); pos = rs.getInt(4); new_node_ps = con.prepareStatement( "INSERT INTO mtree_node (parent_node,mtree_id,type,data,pos) VALUES (?, ?, ?, ?, ?)" ); new_node_ps.setInt( 1, new_node_id ); new_node_ps.setInt( 2, new_mtree_id ); new_node_ps.setString( 3, type ); new_node_ps.setString( 4, data ); new_node_ps.setInt( 5, pos ); new_node_ps.executeUpdate(); new_node_rs = con.prepareStatement( "SELECT LAST_INSERT_ID()" ).executeQuery(); new_node_rs.next(); new_node_id_ = new_node_rs.getInt(1); CopyNode(node_id_,new_node_id_,mtree_id,new_mtree_id,con); } }