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 YunCarDBHelper extends SQLiteOpenHelper {
	private static final int VERSION = 1;// 提交状态增加一个生产功能
	public final static String db_name = "yunCarHandset.db";
	/**
	 * 单标操作表
	 */
	public static final String tb_single_label_option = "singleLabelOption";
	/**
	 * 批量标签操作表
	 */
	public static final String tb_batch_label_option = "batchLabelOption";
	
	/**
	 * 销售
	 */
	public static final String tb_sales_info = "salesInfo";
	
	/**
	 * 上传记录
	 */
	public static final String tb_upload_status = "uploadStatus";
	/**
	 * 车牌号表
	 */
	public static final String tb_car_no_info = "carNoInfo";
	/**
	 * 销售图片表
	 */
	public static final String tb_sale_picture_info = "salePictureInfo";
	
	/**
	 * 领料出库表
	 */
	public static final String tb_LingLiao_info = "lingliaoInfo";
	
	


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

	@Override
	public void onCreate(SQLiteDatabase db) {
		createSingleLabelOption(db);
		createBatchLabelOption(db);
		createCarNoInfo(db);
		createSalesInfo(db);
		createSalePictureInfo(db);
		createUploadStatus(db);
		createLingLiaoInfo(db);
	}
	

	private void createSalePictureInfo(SQLiteDatabase db) {
		String sql = "CREATE TABLE " + tb_sale_picture_info +" (label_id bigint,picture_url VARCHAR(64))";
		db.execSQL(sql);
		
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	}
	
	private void createCarNoInfo(SQLiteDatabase db) {
		String sql = "CREATE TABLE " + tb_car_no_info +" (car_id VARCHAR(20) ,car_no VARCHAR(20),car_date VARCHAR(32),engine_number VARCHAR(32), vin VARCHAR(32), label_id VARCHAR(32), type INTEGER DEFAULT 0)";
		db.execSQL(sql);
		
	}
	
	/**
	 * id：表ID,主键自增
	 * describe：描述
	 * sales_area：销售区域
	 * option_mode：0：单标激活 1：批量激活
	 * status：提交状态  0：未提交  1：提交失败
	 * error_code：服务器返回的错误码
	 * createDate：创建时间
	 * @param db
	 */
	private void createSalesInfo(SQLiteDatabase db) {
		String sql = "CREATE TABLE " + tb_sales_info + " (id INTEGER PRIMARY KEY AUTOINCREMENT,car_no VARCHAR(20),engine_number VARCHAR(20),vin VARCHAR(20),client_tel VARCHAR(20),label_id bigint,carNodate varchar(32),option_mode INTEGER,status INTEGER,submit_status INTEGER default 0,error_code 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
	 * 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)";
		db.execSQL(sql);	
	}

	private void createLingLiaoInfo(SQLiteDatabase db) {
		String sql = "CREATE TABLE " + tb_LingLiao_info + " (id INTEGER PRIMARY KEY AUTOINCREMENT,car_no VARCHAR(20),lingliaodate VARCHAR(20),picture_url VARCHAR(64),option_mode INTEGER,submit_status INTEGER default 0,error_code INTEGER default 0,createDate DATE DEFAULT (date('now','localtime')))";
		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;
	}

}
