DbManager.java 5.12 KB
package com.ectrip.cyt.db;

import java.util.ArrayList;

import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;

import com.ectrip.cyt.bean.ConfigBean;
import com.ectrip.cyt.config.MyApp;

public class DbManager {
	/**
	 * 获取配置文件
	 */
	public  static ArrayList<ConfigBean> GetConfigs() {
		ArrayList<ConfigBean> products = new ArrayList<ConfigBean>();
		synchronized (MyApp.getInstance().getDb()) {
			try {
				Cursor c = MyApp.getInstance().getDb()
						.rawQuery("SELECT * FROM ec_configs", null);
				for (int i = 0; i < c.getCount(); i++) {
					c.moveToPosition(i);
					products.add(new ConfigBean(c.getString(0), c.getString(1),
							c.getString(2), c.getString(3), c.getString(4), c
							.getString(5), c.getInt(6)));
				}
				c.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			return products;
		}
	}

	public static void InsertConfig(String ec_id, String ec_ip,
									String ec_passwd, String ec_mac, String ec_key, String ec_name,
									int ec_isPrint) {
		synchronized (MyApp.getInstance().getDb()) {
			try {
				MyApp.getInstance()
						.getDb()
						.execSQL(
								"INSERT OR REPLACE INTO ec_configs (ec_cid,ec_ip, ec_passwd, ec_mac,ec_key,ec_name,ec_isPrint) VALUES(?,?,?,?,?,?,?)",
								new Object[] { ec_id, ec_ip, ec_passwd, ec_mac,
										ec_key, ec_name, ec_isPrint });
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	// update ec_contacts set ec_name=?,ec_tel=? where ec_id=?

	/**
	 * 修改配置文件
	 */
	public static void ModifyConfig(ConfigBean bean) {
		synchronized (MyApp.getInstance().getDb()) {
			try {
				MyApp.getInstance()
						.getDb()
						.execSQL(
								"update ec_configs set ec_ip=?, ec_passwd=?, ec_mac=?, ec_key=? ,ec_name=?,ec_isPrint=? where ec_cid=?",
								new Object[] { bean.getEc_ip(),
										bean.getEc_passwd(), bean.getEc_mac(),
										bean.getEc_identity(), bean.getEc_signkey(),
										bean.getEc_isPrint(), bean.getEc_id() });
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 插入配置文件
	 */
	public static void InsertConfig(ConfigBean bean) {
		InsertConfig(bean.getEc_id(), bean.getEc_ip(), bean.getEc_passwd(),
				bean.getEc_mac(), bean.getEc_identity(), bean.getEc_signkey(),
				bean.getEc_isPrint());
	}

	/**
	 */
	public static void ClearConfig() {
		synchronized (MyApp.getInstance().getDb()) {
			try {
				MyApp.getInstance().getDb().execSQL("DELETE FROM ec_configs");
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 插入打印过的票
	 */
	public static void insertPrint(String time, String content,
								   String ec_order_number) {
		synchronized (MyApp.getInstance().getDb()) {
			SQLiteDatabase db = MyApp.getInstance().getDb();
			// 开启事务
			db.beginTransaction();
			try {
				MyApp.getInstance()
						.getDb()
						.execSQL(
								"INSERT OR REPLACE INTO ec_save_print (ec_time, ec_content,ec_order_id) "
										+ "VALUES(?,?,?)",
								new Object[] { time, content, ec_order_number });
				// 设置事务标志为成功,当结束事务时就会提交事务
				db.setTransactionSuccessful();
			} finally {
				// 结束事务
				db.endTransaction();
			}
		}
	}

	/**
	 * 修改打印的票
	 */
	public static void ModifyPrint(String time, String content,
								   String ec_order_number) {
		synchronized (MyApp.getInstance().getDb()) {
			SQLiteDatabase db = MyApp.getInstance().getDb();
			// 开启事务
			db.beginTransaction();
			try {
				MyApp.getInstance()
						.getDb()
						.execSQL(
								"update ec_save_print set ec_time=?, ec_content=? where ec_order_id=?",
								new Object[] { time, content, ec_order_number });
				// 设置事务标志为成功,当结束事务时就会提交事务
				db.setTransactionSuccessful();
			} finally {
				// 结束事务
				db.endTransaction();
			}
		}
	}

	/**
	 * 删除某一时间段内的数据
	 */
	public static void deletePrint(String time) {
		synchronized (MyApp.getInstance().getDb()) {
			try {
				String strsql = "delete from ec_save_print where ec_time like '"
						+ time + "%'";
				MyApp.getInstance().getDb().execSQL(strsql);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}

	/**
	 * 根据订单号查询数据
	 */
	public static String queryOrderNumber(String orderId) {
		String content = null;
		synchronized (MyApp.getInstance().getDb()) {
			try {
				Cursor cursor = MyApp
						.getInstance()
						.getDb()
						.rawQuery(
								"select * from ec_save_print where ec_order_id like ?",
								new String[] { "%" + orderId + "%" });

				if (cursor.moveToFirst()) {
					content = cursor.getString(cursor
							.getColumnIndex("ec_content"));
				}
				cursor.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return content;
	}

	/**
	 * 删除指定时间以内的数据
	 */
	public static void regularDelect(String date) {
		String sql = "delete FROM ec_save_print WHERE ec_time<'" + date + "'";
		synchronized (MyApp.getInstance().getDb()) {
			try {
				MyApp.getInstance().getDb().execSQL(sql);
			} catch (SQLException e) {
				e.printStackTrace();
			}
		}
	}
}