package cn.thewee.gourmetmeal.staff.logic; import java.util.ArrayList; import java.util.HashMap; import org.json.JSONArray; import org.json.JSONException; import android.content.ContentValues; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.graphics.Bitmap; import android.util.Log; import cn.thewee.gourmetmeal.staff.R; import cn.thewee.gourmetmeal.staff.bean.DbHelper; import cn.thewee.gourmetmeal.staff.bean.Dish; import cn.thewee.gourmetmeal.staff.util.JsonDecoder; import cn.thewee.gourmetmeal.staff.util.Tools; public class DishesLogic { private static final String TAG = "DishesLogic"; static SQLiteDatabase db; static Bitmap dishImg = null; static final String img_path = "/sdcard/Meishijia/img/"; /** * 获取菜品种类 * @return */ public ArrayList<String> getDishClass(Context context, boolean is4Spinner){ ArrayList<String> class_list = new ArrayList<String>(); if(is4Spinner){ class_list.add(context.getString(R.string.all)); } db = DbHelper.getInstance(context, null).getReadableDatabase(); Cursor cursor = db.query(DbHelper.TABLE_NAME_DISHES, new String[]{"distinct dish_class"}, null, null, null, null, "dish_class"); if(cursor.moveToFirst()){ do{ class_list.add(cursor.getString(0)); }while(cursor.moveToNext()); } db.close(); cursor.close(); cursor = null; return class_list; } /** * * @return HashMap<String,ArrayList<Dish>> key为种类String,ArrayList为种类下的列表 */ public HashMap<String,ArrayList<Dish>> getDishList(Context context) { HashMap<String,ArrayList<Dish>> dish_list = new HashMap<String,ArrayList<Dish>>(); ArrayList<Dish> dish_list4class = new ArrayList<Dish>(); Dish dish = null; db = DbHelper.getInstance(context, null).getReadableDatabase(); Cursor cursor = db.query(DbHelper.TABLE_NAME_DISHES, new String[]{"dish_id","dish_name","price","introduction","dish_class","img_path"}, null, null, null, null, "dish_class"); //如果查询结果不为空开始创建List if(cursor.moveToFirst()){ StringBuilder curClass = new StringBuilder(cursor.getString(4));//保存当前类别 // Log.i(TAG, "Cursor Count:"+cursor.getCount()); do{ dish = new Dish(); dish.setDish_id(cursor.getString(0)); dish.setDish_name(cursor.getString(1)); dish.setPrice(Double.valueOf(cursor.getString(2))); dish.setIntroduction(cursor.getString(3)); dish.setDish_class(cursor.getString(4)); dish.setImg_path(cursor.getString(5)); // Log.i(TAG, "["+cursor.getPosition()+"]"+dish.toString()); if(!cursor.getString(4).equals(curClass.toString())){ dish_list.put(curClass.toString(),dish_list4class); curClass.replace(0, curClass.length(), cursor.getString(4)); dish_list4class = new ArrayList<Dish>(); } dish_list4class.add(dish); // Log.i(TAG, "dish_list4class.size="+dish_list4class.size()); }while(cursor.moveToNext()); 入 dish_list.put(curClass.toString(),dish_list4class); } //关闭数据库 db.close(); cursor.close(); cursor = null; return dish_list; } /** * * @return HashMap<String,ArrayList<Dish>> key为种类String,ArrayList为种类下的列表 */ public HashMap<String,ArrayList<Dish>> getDishList(Context context, String dishClass) { //总列表 HashMap<String,ArrayList<Dish>> dish_list = new HashMap<String,ArrayList<Dish>>(); //各个种类列表 ArrayList<Dish> dish_list4class = new ArrayList<Dish>(); Dish dish = null; db = DbHelper.getInstance(context, null).getReadableDatabase(); Cursor cursor = db.query(DbHelper.TABLE_NAME_DISHES, new String[]{"dish_id","dish_name","price","introduction","dish_class"}, "dish_class = ?", new String[]{dishClass}, null, null, "dish_class"); if(cursor.moveToFirst()){ do{ //新建菜品Dish实例 dish = new Dish(); dish.setDish_id(cursor.getString(0)); dish.setDish_name(cursor.getString(1)); dish.setPrice(Double.valueOf(cursor.getString(2))); dish.setIntroduction(cursor.getString(3)); dish.setDish_class(cursor.getString(4)); //插入种类列表 dish_list4class.add(dish); if(cursor.isLast()){ //如果当前是最后一行,则将种类的菜品列表加入到菜品总列表中 dish_list.put(cursor.getString(4),dish_list4class); } }while(cursor.moveToNext()); } //关闭数据库 db.close(); cursor.close(); cursor = null; return dish_list; } /** * 通过ID查找 * @param dish */ public Dish getDishById(Context context, String dishId){ Dish dish = new Dish(); SQLiteDatabase db = DbHelper.getInstance(context, null).getReadableDatabase(); Cursor cursor = db.query(DbHelper.TABLE_NAME_DISHES, new String[]{"dish_name", "price", "introduction", "dish_class"}, "dish_id=?", new String[]{dishId}, null, null, null); if(cursor.moveToFirst()){ dish.setDish_id(dishId); dish.setDish_name(cursor.getString(0)); dish.setPrice(Double.valueOf(cursor.getString(1))); dish.setIntroduction(cursor.getString(2)); dish.setDish_class(cursor.getString(3)); } //关闭游标和数据库实例 cursor.close(); db.close(); return dish; } /** * 获取图片 * @param dishId 编号 * @return 图片Bitmap */ public Bitmap getDishImage(Context context, String dish_imgPath){ //若dishImg不为空,则回收之前的图片 if(dishImg!=null){ dishImg.recycle(); } Tools tools = new Tools(); dishImg = tools.getImgThumb(context, dish_imgPath, 300); return dishImg; } /** * 获取图片缩略图 * @param dishId 编号 * @return 图片Bitmap */ public Bitmap getDishThumbImage(Context context, String dishId){ Tools tools = new Tools(); return tools.getImgThumb(context, dishId, 150); } /** * 回收图片内存 */ public void recycleDishImg(){ if(dishImg!=null){ dishImg.recycle(); } } //从网络更新菜单 public boolean updateMenu(Context context){ //配置参数 HashMap<String,String> params = new HashMap<String,String>(); params.put("num", "1"); params.put("update", "menu"); //获取返回Json转成HashMap try { JsonDecoder decoder = new JsonDecoder(); HashMap<String,Object> resultMap = decoder.decode2Map(NetLogic.httpGet("/updateServlet", params)); if(resultMap!=null && resultMap.get("rt").equals("200")){ ArrayList<Dish> dishList = decoder.jsonArray2dishList(context, (JSONArray)resultMap.get("list")); //向SQLite存入数据 saveDishMenu(context, dishList); } } catch (JSONException e) { e.printStackTrace(); return false; } return true; } /** * 将列表保存至本地SQLite数据库 * @param dishList 待保存列表 */ private void saveDishMenu(Context context, ArrayList<Dish> dishList) { // TODO Auto-generated method stub if(dishList != null && dishList.size()>0){ SQLiteDatabase db = DbHelper.getInstance(context, null).getWritableDatabase(); db.delete(DbHelper.TABLE_NAME_DISHES, null, null); ContentValues values = new ContentValues(); for(Dish dish : dishList){ Log.i(TAG+".saveDishMenu", dish.toString()); values.put("dish_name", dish.getDish_name()); values.put("price", String.valueOf(dish.getPrice())); values.put("introduction", dish.getIntroduction()); values.put("dish_class", dish.getDish_class()); values.put("dish_unit", dish.getUnit()); values.put("dish_id", dish.getDish_id()); values.put("img_path", dish.getImg_path()); db.insert(DbHelper.TABLE_NAME_DISHES, "dish_id", values); //清空参数 values.clear(); } values = null; db.close(); } } }