package cn.net.caoyao.cpzshandset.db;

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

public class JinLiquorDBHelper extends SQLiteOpenHelper {
	private static final int VERSION = 1;// 提交状态增加一个生产功能
	public final static String db_name = "JinLiquorHandset.db";
	/**
	 * 产品信息表
	 */
	public static final String tb_product_info = "productInfo";

	/**
	 * 单标操作表
	 */
	public static final String tb_single_label_option = "singleLabelOption";
	/**
	 * 批量标签操作表
	 */
	public static final String tb_batch_label_option = "batchLabelOption";
	/**
	 * 经销商信息
	 */
	public static final String tb_dealer_info = "dealerInfo";
	/**
	 * 出库、入库、返库表
	 */
//	public static final String tb_warehouse_option = "warehouseOption";
	
	
	/**
	 * 上传记录
	 */
	public static final String tb_upload_status = "uploadStatus";
	/**
	 * 上传详情
	 */
	public static final String tb_upload_detail = "uploadDetails";
	
	/**
	 * 打包的包标签表
	 */
	public static final String tb_replenish_bing_info="replenishBingInfo";//补垛  OPERATION_REPLENISH_BING_CONSTANT
	/**
	 * 打包的子标签表
	 */
	public static final String tb_replenish_bing_label_info="replenishBingLabelInfo";//补垛  新标签


	public JinLiquorDBHelper(Context context) {
		super(context, db_name, null, VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		createProductInfo(db);
		createSingleLabelOption(db);
		createBatchLabelOption(db);
		createDealerInfo(db);
		createUploadStatus(db);
		createReplenishBingInfo(db);
		createReplenishBingLabelInfo(db);
	}
	
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	}
	private void createReplenishBingLabelInfo(SQLiteDatabase db) {
//		String sql = "CREATE TABLE " + tb_replenish_bing_info + " (id INTEGER PRIMARY KEY AUTOINCREMENT,label_id bigint,package_id INTEGER,option_id INTEGER,option_type INTEGER,labelStatus INTEGER default 0)";
//		db.execSQL(sql);
	}
	private void createReplenishBingInfo(SQLiteDatabase db) {
//		String sql = "CREATE TABLE " + tb_replenish_bing_label_info+" ( package_label_id bigint,option_id INTEGER, option_type INTEGER, count INTEGER,status INTEGER,error_code INTEGER,createDate DATE DEFAULT (date('now','localtime')))";
//		db.execSQL(sql);
	}
	

	private void createDealerInfo(SQLiteDatabase db) {
		String sql = "CREATE TABLE " + tb_dealer_info +" (id bigint PRIMARY KEY, company_id bigint,account VARCHAR(20),name VARCHAR(20),namePY VARCHAR(20),pwd VARCHAR(20),contacter VARCHAR(20),contacterPY VARCHAR(20),contact_phone VARCHAR(20),remark VARCHAR(20),sale_area INTEGER,status INTEGER,createDate DATE DEFAULT (date('now','localtime')))";
		db.execSQL(sql);
		
	}


	/**
	 * id：表ID,主键自增
	 * option_type：操作类型（其中的插入值对应于HansetOperationConstant类中的值）
	 * label_count：上传的标签数量
	 * status：提交状态  0：上传成功 1：上传失败
	 * error_code：服务器返回的错误码
	 * upload_date：创建时间
	 * @param db
	 */
	private void createUploadStatus(SQLiteDatabase db) {
		String sql = "CREATE TABLE " + tb_upload_status + " (id INTEGER PRIMARY KEY AUTOINCREMENT,option_type INTEGER,label_count INTEGER,status INTEGER,error_code INTEGER,upload_date DATE DEFAULT (date('now','localtime')))";
		db.execSQL(sql);
	}


	/**
	 * start_label_id：起始标签
	 * end_label_id：结束标签
	 * option_type：操作类型（其中的插入值对应于HansetOperationConstant类中的值）
	 * option_id：关联的表的id
	 * @param db
	 */
	private void createBatchLabelOption(SQLiteDatabase db) {
		String sql = "CREATE TABLE " + tb_batch_label_option + " (id INTEGER PRIMARY KEY AUTOINCREMENT,start_label_id bigint,end_label_id bigint,option_type INTEGER,option_id INTEGER)";
		db.execSQL(sql);
	}

	/**
	 * label_id：扫描的标签id   
	 * option_type：操作类型（其中的插入值对应于HansetOperationConstant类中的值）
	 * option_id：关联的表的id
	 * label_mode:
	 * labelStatus：当前标签的状态   0:默认值    1:不合法标签    2:重复激活标签     3:未激活标签
	 * @param db
	 */
	private void createSingleLabelOption(SQLiteDatabase db) {
		String sql = "CREATE TABLE " + tb_single_label_option + " (id INTEGER PRIMARY KEY AUTOINCREMENT,label_id bigint,option_type INTEGER,option_id INTEGER,label_mode INTEGER default 0,labelStatus INTEGER default 0)";
		db.execSQL(sql);	
	}


	/**
	 * id:产品id，服务器返回的数据直接插入
	 * name：产品名称
	 * namePY：产品名称的拼音
	 * nid：产品编号（即，商品条码）
	 * status
	 * @param db
	 */
	private void createProductInfo(SQLiteDatabase db) {
		String sql = "CREATE TABLE " + tb_product_info + " (id VARCHAR(20),name VARCHAR(20), namePY VARCHAR(20),nid VARCHAR(20), status INTEGER)";
		db.execSQL(sql);		
	}

	
	/**
	 * 检测数据库中是否存在某一列
	 * @param db
	 * @param tableName
	 * @param primaryKeyName
	 * @param columnName
	 * @return true:存在    false:不存在
	 */
	private boolean checkColumnExists(SQLiteDatabase db,String tableName, String primaryKeyName,String columnName){
		boolean b = true;
		String sql = "SELECT * from " + tableName +" where "+primaryKeyName+"=0";
		Cursor cursor = db.rawQuery(sql, null);
		int columnIndex = cursor.getColumnIndex(columnName);
		cursor.close();
		if (columnIndex < 0) {
			b = false;
		}
		return b;
	}

}
