package com.jiebao.h518.scan;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class SqliteConnect extends SQLiteOpenHelper {
	private static final String DATABASENAME = "h518.db";
	private static final int DATABASEVERSION = 1;

	public static final String TN_Account = "acount";
	public static final String Account_C_Name = "name";
	public static final String Account_C_Pwd = "pwd";

	public static final String TN_Goods = "goods";
	public static final String Goods_C_BarCode = "barcode";
	public static final String Goods_C_Id = "goods_Id";
	public static final String Goods_C_Name = "name";
	public static final String Goods_C_Name_PinYin = "name_pinyin";
	public static final String Goods_C_ProducingArea = "producing_area";
	public static final String Goods_C_Standard = "standard";
	public static final String Goods_C_Unit = "unit";
	public static final String Goods_C_BroughtPrice = "brought_price";
	public static final String Goods_C_SellPrice = "sell_price";

	public static final String TN_Order = "order_msg";
	public static final String Order_C_Id = "order_id";
	public static final String Order_C_Barcode = "barcode";
	public static final String Order_C_per_price = "per_price";
	public static final String Order_C_total_price = "total_price";
	public static final String Order_C_remark = "remark";
	public static final String Order_C_Count = "count";

	public static final String TN_Order_Details = "order_details";
	public static final String OrderD_C_Id = "order_id";
	public static final String OrderD_C_OrderDate = "order_date";
	public static final String OrderD_C_Operator = "operator";
	public static final String OrderD_C_remark = "remark";
	public static final String OrderD_C_total_price = "total_price";

	public static final String TN_PSOrder = "psorder_msg";
	public static final String PSOrder_C_Id = "order_id";
	public static final String PSOrder_C_date = "order_date";
	public static final String PSOrder_C_shop_Id = "shop_id";
	public static final String PSOrder_C_remark = "remark";

	public static final String TN_PSOrderGoods = "psordergoods_msg";
	public static final String PSOrderGoods_c_Id = "order_id";
	public static final String PSOrderGoods_c_Barcode = "barcode";
	public static final String PSOrderGoods_c_count = "count";
	public static final String PSOrderGoods_c_per_price = "per_price";
	public static final String PSOrderGoods_c_total_price = "total_price";
	public static final String PSOrderGoods_c_remark = "remark";

	public static final String TN_GoodsSold = "goods_sold";
	public static final String GoodsSold_C_Order_Id = "order_id";
	public static final String GoodsSold_C_Goods_Id = "goods_id";
	public static final String GoodsSold_C_Barcode = "barcode";
	public static final String GoodsSold_C_Count = "count";
	public static final String GoodsSold_C_totalPrice = "total_price";

	public SqliteConnect(Context context) {
		super(context, DATABASENAME, null, DATABASEVERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
	
		try {
			db.execSQL("CREATE TABLE  if not exists " + TN_Account
					+ "(id integer primary key autoincrement," + ""
					+ Account_C_Name + " text," + Account_C_Pwd + " text)");
			ContentValues values = new ContentValues();
			values.put(Account_C_Name, "5180000010");
			values.put(Account_C_Pwd, "123");
			db.insert(TN_Account, null, values);
			
			db.execSQL("create table if not exists " + TN_Goods
					+ "(id integer primary key autoincrement," + Goods_C_BarCode
					+ " text," + Goods_C_BroughtPrice + " text," + Goods_C_Id
					+ " text," + Goods_C_Name + " text," + Goods_C_Name_PinYin
					+ " text," + Goods_C_ProducingArea + " text,"
					+ Goods_C_SellPrice + " double," + Goods_C_Standard + " text,"
					+ Goods_C_Unit + ")");
			db.execSQL("CREATE TABLE  if not exists " + TN_Order
					+ "(id integer primary key autoincrement," + "" + Order_C_Barcode
					+ " text," + Order_C_Count + " long," + Order_C_Id + " text,"
					+ Order_C_per_price + " double," + Order_C_remark + " text,"
					+ Order_C_total_price + " double)");
			db.execSQL("create table if not exists " + TN_Order_Details
					+ "(id integer primary key autoincrement," + OrderD_C_Id
					+ " text," + OrderD_C_Operator + " text," + OrderD_C_OrderDate
					+ " datetime," + OrderD_C_remark + " text,"
					+ OrderD_C_total_price + " double)");
			db.execSQL("create table if not exists " + TN_PSOrder + "("
					+ PSOrder_C_date + " datetime," + PSOrder_C_Id + " text,"
					+ PSOrder_C_remark + " text," + PSOrder_C_shop_Id + " text)");
			db.execSQL("create table if not exists " + TN_PSOrderGoods + "("
					+ PSOrderGoods_c_count + " long," + PSOrderGoods_c_Barcode
					+ " text," + PSOrderGoods_c_Id + " text,"
					+ PSOrderGoods_c_per_price + " double," + PSOrderGoods_c_remark
					+ " text," + PSOrderGoods_c_total_price + " double)");
			db.execSQL("create table if not exists  " + TN_GoodsSold + "("
					+ GoodsSold_C_Barcode + " text," + GoodsSold_C_Count + " long,"
					+ GoodsSold_C_Goods_Id + " text," + GoodsSold_C_Order_Id
					+ " text," + GoodsSold_C_totalPrice + " double)");
			
			db.execSQL("CREATE TABLE  if not exists CodeType(id integer primary key autoincrement," +
					"type_name text,type_id integer)" );
		} catch (Exception e) {
			// TODO: handle exception
			e.printStackTrace();
		}



	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
		db.execSQL("DROP TABLE IF EXISTS " + TN_Account);
		db.execSQL("DROP TABLE IF EXISTS " + TN_Goods);
		db.execSQL("DROP TABLE IF EXISTS " + TN_Order);
		db.execSQL("drop table if exists " + TN_Order_Details);
		db.execSQL("drop table if exists " + TN_PSOrder);
		db.execSQL("drop table if exists " + TN_PSOrderGoods);
		db.execSQL("drop table if exists " + TN_GoodsSold);
		db.execSQL("DROP TABLE IF EXISTS CodeType");
		onCreate(db);
	}

	public void clearAllMsgButAccount(SQLiteDatabase db) {
		db.execSQL("DROP TABLE IF EXISTS " + TN_Goods);
		db.execSQL("DROP TABLE IF EXISTS " + TN_Order);
		db.execSQL("drop table if exists " + TN_Order_Details);
		db.execSQL("drop table if exists " + TN_PSOrder);
		db.execSQL("drop table if exists " + TN_PSOrderGoods);
		db.execSQL("drop table if exists " + TN_GoodsSold);
		onCreate(db);
	}
	

}
