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 WebCommonDBHelper extends SQLiteOpenHelper {
	private static final int VERSION = 1;// 提交状态增加一个生产功能
	public final static String db_name = "webCommonHandset.db";
	/**
	 * 产品信息表
	 */
	public static final String tb_product_info = "productInfo";
	/**
	 * 溯源批次表
	 */
	public final static String tb_sy_batch_info="syBatchInfo";

	/**
	 * 激活信息表
	 */
	public static final String tb_activate_info = "activateInfo";
	/**
	 * 单标操作表
	 */
	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 WebCommonDBHelper(Context context) {
		super(context, db_name, null, VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		createProductInfo(db);
		createSyBatchInfo(db);
		createActivateInfo(db);
		createSingleLabelOption(db);
		createBatchLabelOption(db);
		createDealerInfo(db);
		createWarehouseOption(db);
		createUploadStatus(db);
	}
	
	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	}
	/**
	 * @param db
	 */
	private void createSyBatchInfo(SQLiteDatabase db) {
		String sql = "CREATE TABLE " + tb_sy_batch_info +" (id bigint PRIMARY KEY,batchQyNo VARCHAR(50),batchName VARCHAR(50),productId bigint)";
		db.execSQL(sql);
		
	}
	
	/**
	 * id：表ID,主键自增
	 * describe：描述
	 * option_type：操作类型（其中的插入值对应于HansetOperationConstant类中的值）
	 * option_mode：0：单标激活 1：批量激活
	 * dealer_id:经销商id
	 * status：提交状态  0：未提交  1：提交失败
	 * error_code：服务器返回的错误码
	 * createDate：创建时间
	 * @param db
	 */
	private void createWarehouseOption(SQLiteDatabase db) {
		String sql = "CREATE TABLE " + tb_warehouse_option + " (id INTEGER PRIMARY KEY AUTOINCREMENT,dealer_id varchar(100),describe VARCHAR(20),option_type INTEGER,option_mode INTEGER,account VARCHAR(20),status INTEGER,error_code INTEGER,createDate DATE DEFAULT (date('now','localtime')))";
		db.execSQL(sql);
	}

	/**
	 * 经销商表
	 * @param db
	 */
	private void createDealerInfo(SQLiteDatabase db) {
		String sql = "CREATE TABLE " + tb_dealer_info +" (account varchar(100),pAccount varchar(100),companyId bigint,name varchar(128),namePY varchar(128),salesArea INTEGER,contacter varchar(128),contactPhone varchar,remark varchar(255),operUser varchar(100),level INTEGER)";
		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,account VARCHAR(20),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,account VARCHAR(20))";
		db.execSQL(sql);
	}

	/**
	 * label_id：扫描的标签id   
	 * option_type：操作类型（其中的插入值对应于HansetOperationConstant类中的值）
	 * option_id：关联的表的id
	 * 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,labelStatus INTEGER default 0,account VARCHAR(20))";
		db.execSQL(sql);	
	}

	/**
	 * id：自增主键
	 * product_id：产品id
	 * sy_id：溯源id
	 * option_type：操作类型（其中的插入值对应于HansetOperationConstant类中的值）
	 * option_mode：0：单标激活 1：批量激活
	 * @param db
	 */
	private void createActivateInfo(SQLiteDatabase db) {
		String sql = "CREATE TABLE " + tb_activate_info + " (id INTEGER PRIMARY KEY AUTOINCREMENT,product_id INTEGER,sy_id VARCHAR(20),option_type INTEGER,option_mode INTEGER,status INTEGER,error_code INTEGER,account VARCHAR(20),createDate DATE DEFAULT (date('now','localtime')))";
		db.execSQL(sql);		
	}

	/**
	 * id:产品id，服务器返回的数据直接插入
	 * name：产品名称
	 * namePY：产品名称的拼音
	 * nid：产品编号（即，商品条码）
	 * status
	 * @param db
	 */
	private void createProductInfo(SQLiteDatabase db) {
		String sql = "CREATE TABLE " + tb_product_info + " (proId INTEGER PRIMARY KEY,proName VARCHAR(20), proNamePY VARCHAR(20),proType VARCHAR(20), proCode VARCHAR(20), producer 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;
	}

}
