Нарисовал что-то такое:
Код:
private PreparedStatement ps;
private final static ServerContext context = ServerContext.get();
private final static String GET_CHECK_EMAIL_QUERY = "SELECT email FROM contract_parameter_type_3 WHERE 1=1 AND pid=183 AND email LIKE '%@%' AND cid=? LIMIT 1";
private final static String DEF_EMAIL = "xxx@xxx.ru";
private ContractDao cDao;
private final static Logger logger = Logger.getLogger( RePrintErrors.class );
@Override
public void execute(Setup setup, ConnectionSet connectionSet) throws Exception {
Connection con = connectionSet.getConnection();
init( con );
doWork( con );
close();
}
private void doWork(Connection con) throws SQLException, BGException {
Printer printer = null;
ResultSet rs = ps.executeQuery();
while( rs.next() ) {
int paymentId = rs.getInt( "payment_id" );
BigDecimal sum = rs.getBigDecimal( "summa" );
if( sum == null ) {
logger.error( "payment sum not found for payment.id: " + paymentId );
continue;
}
//
int contractId = rs.getInt( "cid" );
Contract c = getContract( contractId );
if( c == null ) {
logger.error( "contract not found for contract.id: " + contractId );
continue;
}
//
int posId = rs.getInt( "pos_id" );
printer = CashCheckUtils.getPrinter( posId );
if( printer == null ) {
logger.error( "printer not found for payment.id: " + paymentId + " printer.id: " + posId );
continue;
}
String email = getEmail( contractId );
//
Check check = new Check( Check.Type.PAYMENT );
check.setDocumentId( paymentId );
check.setDocumentDate( rs.getDate( "dt" ) );
check.addPayment( sum, "Оплата услуг по договору " + c.getTitle(), 0 );
check.setTax( -1 );
check.setPaymentType( 1 );
check.setCustomerEmail( email );
//
CashCheckUtils.printCheck( check, printer, 0, con, paymentId, "auto" );
//
logger.info( "reprinted check, payment.id: " + paymentId + " pos: " + posId );
}
PsUtils.closeRS( rs );
}
private Contract getContract(int contractId) {
// ...
}
private String getEmail(int contractId) {
//...
}
private void init(Connection con) throws SQLException, SQLException {
StringBuilder sb = new StringBuilder();
sb.append( "SELECT DISTINCT pl.payment_id, pl.pos_id, (CASE WHEN cp.summa IS NOT NULL THEN cp.summa ELSE cop.summa END) summa, \n" );
sb.append( " (CASE WHEN cp.cid IS NOT NULL THEN cp.cid ELSE cop.cid END) cid, \n" );
sb.append( " (CASE WHEN cp.dt IS NOT NULL THEN cp.dt ELSE cop.dt END) dt, \n" );
sb.append( " (CASE WHEN cp.lm IS NOT NULL THEN cp.lm ELSE cop.lm END) lm FROM cashcheck_payment_log pl \n " );
sb.append( " LEFT JOIN contract_payment cp \n" );
sb.append( " ON pl.payment_id=cp.id \n" );
sb.append( " LEFT JOIN contract_original_payment cop \n" ); // for deleted payments
sb.append( " ON pl.payment_id=cop.id \n" );
sb.append( " WHERE 1=1 \n" );
sb.append( " AND pl.pos_mapping LIKE '%auto%' \n" );
sb.append( " AND pl.check_type LIKE '%PAYMENT%' \n" );
sb.append( " AND pl.last_error IS NOT NULL \n" );
sb.append( " AND pl.fiscal_data IS NULL \n" );
sb.append( " AND pl.last_error LIKE '%error%' " ); // for test
//
logger.info( sb.toString() ); // dbg
//
ps = con.prepareStatement( sb.toString() );
//
cDao = cDao = new ContractDao( con, 0 );
}
private void close() throws SQLException, BGException {
cDao.close();
PsUtils.closePS( ps );
}
Пока честно не запускал, вдруг вы скажете что такой подход всё развалит.
Вроде CashCheckUtils.printCheck() вызывает PaymentQueueManager(con).updatePaymentLog
который в свою очередь делает REPLACE INTO `cashcheck_payment_log` ...
по идее должна перетереться старая запись, но вдруг для корректности нужны дополнительные
манипуляции.