public long addOrUpdatePay(PayOrder order) { if (!enbleLocalStore) { return -1L; } SQLiteDatabase db = this.getWritableDatabase(); ContentValues cv = payOrder2cv(order); long row = -1L; try { row = db.insertOrThrow(TABLE_NAME, null, cv); } catch (SQLException e) { String where = "userId= ? AND orderNumber= ? "; String[] value = {String.valueOf(order.getUserId()), order.getOrderNumber()}; row = db.update(TABLE_NAME, cv, where, value); } return row; }
/** * 将数据库记录转换为PayOrder对象 如果验证失败则返回null * * @param cur * @return */ private PayOrder createPayOrder(Cursor cur) { PayOrder order = null; if (cur != null && !cur.isClosed()) { order = new PayOrder(); order.setOrderNumber(cur.getString(0)); order.setAppId(cur.getString(1)); order.setAmount(cur.getInt(2)); order.setUserId(cur.getLong(3)); order.setPayment(cur.getString(4)); order.setBid(cur.getString(5)); order.setDescr(cur.getString(6)); order.setPayResultEncode(cur.getString(7)); order.setPayStatusCode(cur.getInt(8)); order.setDealTime(new Date(cur.getLong(9))); order.setOrderTime(new Date(cur.getLong(10))); order.setServerState(cur.getInt(11)); // sqlite用long 0 1来区分boolean order.setSandBox(cur.getLong(12) == 1 ? true : false); if (!order.getLocalEncode().equals(cur.getString(13))) { order = null; } } return order; }
/** * 根据payOrder对象生成数据库需要的数据 * * @param order * @return */ private ContentValues payOrder2cv(PayOrder order) { ContentValues cv = new ContentValues(); cv.put("orderNumber", order.getOrderNumber()); cv.put("appId", order.getAppId()); cv.put("amount", order.getAmount()); cv.put("userId", order.getUserId()); cv.put("payment", order.getPayment()); cv.put("bid", order.getBid()); cv.put("descr", order.getDescr()); cv.put("payResultEncode", order.getPayResultEncode()); cv.put("payStatusCode", order.getPayStatusCode()); cv.put("dealTime", order.getDealTime().getTime()); cv.put("orderTime", order.getOrderTime().getTime()); cv.put("serverState", order.getServerState()); cv.put("sandBox", order.isSandBox()); Log.d("dddd", String.valueOf(order.isSandBox())); cv.put("localEncode", order.getLocalEncode()); return cv; }