public class OrderLogic { private static final String TAG = "OrderLogic"; static SQLiteDatabase db; public static String createOrderId(String tableNumber) { StringBuilder orderIdBuilder = new StringBuilder(); orderIdBuilder.append(Tools.getCurDateTime("yyyyMMddHHmm")); orderIdBuilder.append(tableNumber); orderIdBuilder.append("0000"); orderIdBuilder.append(Tools.getRandomNum()); return orderIdBuilder.toString(); } public static String createOrderId(Context context, String tableNumberA, String tableNumberB, int mode) { String orderIdA = findOrderByTable(context, tableNumberA); if(orderIdA!=null){ StringBuilder orderIdBuilder = new StringBuilder(orderIdA); orderIdBuilder.replace(12, 15, tableNumberB); orderIdBuilder.replace(15, 18, tableNumberA); switch(mode){ case BaseActivity.MODE_CHANGE_TABLE: orderIdBuilder.replace(18, 19, "1"); break; case BaseActivity.MODE_MERGE_TABLE: orderIdBuilder.replace(18, 19, "2"); break; } return orderIdBuilder.toString(); } else return null; } public static String findOrderByTable(Context context, String tableNum){ ArrayList<String>uncheckedOrder=getUncheckedOrder(context); "table_num=? AND checkout=0", new String[]{tableNum}, null, null, null); String result = null; for (int i = 0; i < uncheckedOrder.size(); i++) { String str = uncheckedOrder.get(i).toString(); System.out.println(str+" "+i); if(str.substring(12, 12+tableNum.length()).split(tableNum) != null){ result = str; System.out.println(str+" a "+tableNum); return result; } } return result; } public static HashMap<String, Object> submitOrder(String orderId, String memberNum, String username, ArrayList<Dish> dishList) { JSONObject js = new JSONObject(); try { js.put("orders", orderId); js.put("name", username); js.put("cus", memberNum); js.put("sum", String.valueOf(getOrderSum(dishList))); js.put("status", "无"); JSONArray ja = new JSONArray(); for (int i = 0; i < dishList.size(); i++) { JSONObject js1 = new JSONObject(); js1.put("list"+i, dishList.get(i).getDish_id() + "," + dishList.get(i).getCount() + "," + dishList.get(i).getRemark()); ja.put(i, js1.toString()); } js.put("list", ja); Log.i(TAG+".submitOrder", "js:" + js.toString()); } catch (JSONException e1) { e1.printStackTrace(); } List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("list", js.toString())); HashMap<String, Object> resultMap = null; try { resultMap = JsonDecoder.decode2Map(NetLogic.httpPost( "/orderServlet", params)); } catch (JSONException e) { e.printStackTrace(); } return resultMap; } public static HashMap<String, Object> submitOrderAdd(String orderId, String username, ArrayList<Dish> dishList) { JSONObject js = new JSONObject(); try { js.put("orders", orderId); js.put("waiter", username); js.put("sum", String.valueOf(getOrderSum(dishList))); JSONArray ja = new JSONArray(); for (int i = 0; i < dishList.size(); i++) { JSONObject js1 = new JSONObject(); js1.put("list"+i, dishList.get(i).getDish_id() + "," + dishList.get(i).getCount() + "," + dishList.get(i).getRemark()); ja.put(i, js1.toString()); } js.put("list", ja); Log.i(TAG+".submitOrderAdd", "js:"+js.toString()); } catch (JSONException e1) { e1.printStackTrace(); } List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("list", js.toString())); HashMap<String, Object> resultMap = null; try { resultMap = JsonDecoder.decode2Map(NetLogic.httpPost( "/addOrderServlet", params)); } catch (JSONException e) { e.printStackTrace(); } return resultMap; } public static ArrayList<Dish> getTempOrderItems(Context context, String tableNum) { ArrayList<Dish> orderItems = new ArrayList<Dish>(); Dish dish = null; db = DbHelper.getInstance(context, null).getReadableDatabase(); StringBuilder sqlBuilder = new StringBuilder(); sqlBuilder .append("SELECT d.dish_id,d.dish_name,d.price,d.dish_class,t.count,t.remark FROM "); sqlBuilder.append(DbHelper.TABLE_NAME_DISHES); sqlBuilder.append(" d,"); sqlBuilder.append(DbHelper.TABLE_NAME_TEMP); sqlBuilder.append(" t WHERE t.table_num=? AND d.dish_id=t.dish_id"); Cursor cursor = db.rawQuery(sqlBuilder.toString(), new String[] { tableNum }); if (cursor.moveToFirst()) { do { dish = new Dish(); dish.setDish_id(cursor.getString(0)); dish.setDish_name(cursor.getString(1)); dish.setPrice(Double.valueOf(cursor.getString(2))); dish.setDish_class(cursor.getString(3)); dish.setCount(Integer.valueOf(cursor.getString(4))); dish.setRemark(cursor.getString(5)); orderItems.add(dish); } while (cursor.moveToNext()); } return orderItems; } public static int addDishCount(Context context, String dishId, int curCount) { int count = curCount + 1; SQLiteDatabase db = DbHelper.getInstance(context, null) .getWritableDatabase(); StringBuilder sqlBuilder = new StringBuilder(); sqlBuilder.append("UPDATE "); sqlBuilder.append(DbHelper.TABLE_NAME_TEMP); sqlBuilder.append(" SET count='"); sqlBuilder.append(count); sqlBuilder.append("' WHERE dish_id='"); sqlBuilder.append(dishId); sqlBuilder.append("'"); Log.i(TAG, "[addDishCount]" + sqlBuilder.toString()); db.execSQL(sqlBuilder.toString()); db.close(); sqlBuilder = null; return count; } public static int reduceDishCount(Context context, String dishId, int curCount) { int count; if ((count = curCount - 1) == 0) { count = 1; } SQLiteDatabase db = DbHelper.getInstance(context, null) .getWritableDatabase(); StringBuilder sqlBuilder = new StringBuilder(); sqlBuilder.append("UPDATE "); sqlBuilder.append(DbHelper.TABLE_NAME_TEMP); sqlBuilder.append(" SET count='"); sqlBuilder.append(count); sqlBuilder.append("' WHERE dish_id='"); sqlBuilder.append(dishId); sqlBuilder.append("'"); Log.i(TAG, "[reduceDishCount]" + sqlBuilder.toString()); db.execSQL(sqlBuilder.toString()); db.close(); sqlBuilder = null; return count; } public static void saveOrderItemRemark(Context context, String dishId, String remark) { SQLiteDatabase db = DbHelper.getInstance(context, null) .getWritableDatabase(); StringBuilder sqlBuilder = new StringBuilder(); sqlBuilder.append("UPDATE "); sqlBuilder.append(DbHelper.TABLE_NAME_TEMP); sqlBuilder.append(" SET remark='"); sqlBuilder.append(remark); sqlBuilder.append("' WHERE dish_id='"); sqlBuilder.append(dishId); sqlBuilder.append("'"); Log.i(TAG, "[saveOrderItemRemark]" + sqlBuilder.toString()); db.execSQL(sqlBuilder.toString()); db.close(); sqlBuilder = null; } public static ArrayList<String> getUncheckedOrder(Context context) { ArrayList<String> uncheckedOrder = null; try { List<NameValuePair> params = new ArrayList<NameValuePair>(); params.add(new BasicNameValuePair("", "")); JsonDecoder decoder = new JsonDecoder(); HashMap<String,Object> resultMap = JsonDecoder.decode2Map(NetLogic.httpPost("/queryAllOreder", params)); System.out.println(resultMap.get("rt")+" resultMap"); if(resultMap!=null && resultMap.get("rt").equals("200")){ uncheckedOrder = decoder.jsonArray2stringList(context, (JSONArray)resultMap.get("list")); return uncheckedOrder; }else{ SQLiteDatabase db = DbHelper.getInstance(context, null).getReadableDatabase(); Cursor cursor = db.query(DbHelper.TABLE_NAME_ORDERS, new String[]{"order_id"}, "checkout=0", null, null, null, null); if(cursor.moveToFirst()){ uncheckedOrder = new ArrayList<String>(); do{ uncheckedOrder.add(cursor.getString(0)); }while(cursor.moveToNext()); } System.out.println("uncheckedOrder"+uncheckedOrder); return uncheckedOrder; } } catch (JSONException e) { e.printStackTrace(); } return uncheckedOrder; }