@Override public Pay mapRow(ResultSet rs, int rowNum) throws SQLException { Pay pay = new Pay(); pay.setId(rs.getInt("id")); Timestamp timestamp = rs.getTimestamp("time"); if (timestamp != null) { pay.setTime(new java.util.Date(timestamp.getTime())); } pay.setDemand_id(rs.getString("demand_id")); Manufacturer manufacturer = new Manufacturer(rs.getInt("manufacturer_id"), rs.getString("manufacturer_name"), null); pay.setManufacturer(manufacturer); User user = new User( rs.getInt("user_id"), rs.getString("user_name"), rs.getString("user_email"), rs.getString("user_login")); pay.setUser(user); pay.setStorno((rs.getInt("storno") == 0 ? false : true)); pay.setSumm(rs.getDouble("summ")); pay.setNumDoc(rs.getString("numDoc")); pay.setClient( new Client( rs.getInt("client_id"), rs.getString("client_name"), rs.getString("client_email"), rs.getString("client_address"), null)); return pay; }
@Override public Pay createPay(Pay pay) { Pay result = new Pay(); Pay currentPay = pay; MapSqlParameterSource params = new MapSqlParameterSource(); String sqlUpdate = "insert into pays (time, demand_id, manufacturer, storno, summ, user, numDoc, client)" + " Values (:time, :demand_id, :manufacturer, :storno, :summ, :user, :NumDoc, :client)"; if (currentPay.getId() > 0) { // В БД есть такой элемент sqlUpdate = "update pays set time=:time, demand_id=:demand_id, manufacturer=:manufacturer, storno=:storno" + ", summ=:summ, user=:user, numDoc=:NumDoc, client=:client where id=:id"; params.addValue("id", currentPay.getId()); } params.addValue("time", pay.getTime()); params.addValue("demand_id", pay.getDemand_id()); params.addValue( "user", (((User) pay.getUser()).getId() == 0 ? Service.ID_CUSTOMER : ((User) pay.getUser()).getId())); params.addValue("manufacturer", ((Manufacturer) pay.getManufacturer()).getId()); params.addValue("storno", (pay.isStorno() ? 1 : 0)); params.addValue("summ", (pay.isStorno() ? -1 : 1) * pay.getSumm()); params.addValue("NumDoc", pay.getNumDoc()); params.addValue("client", ((Client) pay.getClient()).getId()); KeyHolder keyHolder = new GeneratedKeyHolder(); jdbcTemplate.update(sqlUpdate, params, keyHolder); try { if (keyHolder.getKey() != null) { result = getPayById(keyHolder.getKey().intValue()); } } catch (EmptyResultDataAccessException e) { result = new Pay(); } return result; }