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

	/**
	 * 激活信息表
	 */
	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_product_choice_count = "productChoiceCount";
	/**
	 * 汽车客户信息表
	 */
	public static final String tb_truck_consignee_info = "truckConsigneeInfo";
	/**
	 * 汽车客户信息表 中的索引
	 */
//	public static final String tb_truck_consignee_index_name = "truckConsigneeIndex";
	/**
	 * 汽车收货地址的详细信息表
	 */
	public static final String tb_truck_shipment_address_info = "truckShipmentAddress";
	/**
	 * 汽车收货地址的详细信息表 索引
	 */
//	public static final String tb_truck_shipment_addres_index_name = "truckShipmentAddressIndex";
	/**
	 * 汽车发运单
	 */
	public static final String tb_truck_invoice ="truckInvoice";
	/**
	 * 发运单
	 */
	public static final String tb_invoice = "invoice";
	public YthImageDBHelper(Context context) {
		super(context, db_name, null, VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		createProductInfo(db);
		createProductChoiceCount(db);
		createActivateInfo(db);
		createSingleLabelOption(db);
		createBatchLabelOption(db);
		createTruckConsigneeInfo(db);
		createTruckShipmentAddressInfo(db);
		createTruckInvoice(db);
		createInvoice(db);
	}
	
	/**
	 * id:发运单ID 
	 * transmode:发运方式： 1：火车发运(车皮) 2:汽车发运(货车) 
	 * transid:发运单id(火车发运单/汽车发运单)
	 * elemtype:类型 1：托盘包含的标签 2：单个产品的标签 
	 * labelid:标签ID 
	 * timestamp:时间戳
	 * expand1:目前用作判定invoice是否有效
	 * @param db
	 */
	private void createInvoice(SQLiteDatabase db) {
		String sql = "CREATE TABLE " + tb_invoice
				+ " (id INTEGER PRIMARY KEY AUTOINCREMENT, transmode INTEGER default 2,transid INTEGER,elemtype INTEGER default 2,labelid VARCHAR(20),timestamp VARCHAR(20),package_id varchar(32),createDate DATE DEFAULT (date('now','localtime')))";
		db.execSQL(sql);
	}

	/**
	 *id：主键，自增
	 * truckno：车牌号
	 * drivername：司机名称
	 * driverPhone：司机电话
	 * orderNo：订单号
	 * gruopInfo：班组信息
	 * startStation：起始站
	 * terminalStationCode：结束站的code码
	 * detailAddress：到达站的详细地址信息
	 * consignee：收货人信息
	 * timestamp：订单创建时间
	 * labelMode：
	 * status：提交状态
	 * error_code：错误码
	 * createDate：创建时间
	 * updateDate：更新时间
	 * @param db
	 */
	private void createTruckInvoice(SQLiteDatabase db) {
		String sql = "CREATE TABLE " + tb_truck_invoice + " (id INTEGER PRIMARY KEY,truckNo VARCHAR(20),driverName VARCHAR(20), driverPhone VARCHAR(20),orderNo VARCHAR(20),gruopInfo VARCHAR(20), startStation INTEGER default 0,terminalStationCode INTEGER,detailAddress VARCHAR(20),consignee VARCHAR(20),timestamp VARCHAR(20),labelMode INTEGER default 1,status INTEGER default 0,error_code INTEGER,createDate DATE DEFAULT (date('now','localtime')),updateDate DATE DEFAULT (date('now','localtime')))";
		db.execSQL(sql);		
	}

	/**
	 * id：主键，自增
	 * cityCode：三级地址信息的Code码
	 * detailAddress：详细地址信息的文字信息
	 * detailAddressPY：详细地址信息的拼音
	 * originType：来源   0：本地生成    1：服务器获取
	 * createDate：创建时间
	 * updateDate：更新日期
	 * @param db
	 */
	private void createTruckShipmentAddressInfo(SQLiteDatabase db) {
		String sql = "CREATE TABLE " + tb_truck_shipment_address_info
				+ " (id INTEGER PRIMARY KEY AUTOINCREMENT,provinceCode INTEGER,cityCode INTEGER,detailAddress VARCHAR(20),detailAddressPY VARCHAR(20),originType INTEGER default 0,createDate DATE DEFAULT (date('now','localtime')),updateDate DATE DEFAULT (date('now','localtime')))";
		db.execSQL(sql);
		// 给detailAddress列建立索引 CREATE UNIQUE INDEX unique_index_url ON http_cache
		// (url);
//		String createIndexSql = "CREATE UNIQUE INDEX truckShipmentAddressIndex ON " + tb_truck_shipment_address_info
//				+ " (detailAddress)";
//		db.execSQL(createIndexSql);
		String createIndexSql = "CREATE UNIQUE INDEX truckShipmentAddressIndex ON " + tb_truck_shipment_address_info + "(cityCode, detailAddress)";
		db.execSQL(createIndexSql);
	}

	/**
	 * id：自增主键 
	 * consigneeName：客户名称 
	 * consigneeNamePY：客户名称的首字母拼音 
	 * originType：来源   0：本地生成    1：服务器获取
	 * createDate：创建时间
	 * updateDate：更新日期
	 * @param db
	 */
	private void createTruckConsigneeInfo(SQLiteDatabase db) {
		String sql = "CREATE TABLE " + tb_truck_consignee_info
				+ " (id INTEGER PRIMARY KEY AUTOINCREMENT,consigneeName VARCHAR(20),consigneeNamePY VARCHAR(20),originType INTEGER default 0,createDate DATE DEFAULT (date('now','localtime')),updateDate DATE DEFAULT (date('now','localtime')))";
		db.execSQL(sql);
		// 给consigneeName列建立索引 CREATE UNIQUE INDEX unique_index_url ON http_cache
		// (url);
		String createIndexSql = "CREATE UNIQUE INDEX truckConsigneeIndex ON " + tb_truck_consignee_info
				+ " (consigneeName)";
		db.execSQL(createIndexSql);
	}

	/**
	 * 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
	 * 
	 * @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)";
		db.execSQL(sql);
	}

	/**
	 * id:激活表ID,主键自增 product_id：对应的产品表中的产品ID product_date：生产日期
	 * product_batch_number：生产批号 group_number:班组号 factory_code:厂区 producer：生产者
	 * option_mode：操作方式 0：单标激活 1：批量激活 status：提交状态 0：未提交 1：提交失败
	 * error_code：服务器返回的错误码 createDate：创建时间
	 * 
	 * @param db
	 */
	private void createActivateInfo(SQLiteDatabase db) {
		String sql = "CREATE TABLE " + tb_activate_info
				+ " (id INTEGER PRIMARY KEY AUTOINCREMENT,product_id VARCHAR(20),product_date VARCHAR(20),product_batch_number VARCHAR(20),group_number VARCHAR(20),factory_code VARCHAR(20) DEFAULT '358000000_6',producer VARCHAR(20),option_mode INTEGER DEFAULT 0 ,status INTEGER DEFAULT 0 ,error_code INTEGER,createDate DATE DEFAULT (date('now','localtime')))";
		db.execSQL(sql);
	}

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

	/**
	 * id:产品id，服务器返回的数据直接插入 name：产品名称 nid：产品编号（即，商品条码） p_image_url:图片的URL
	 * choiceCount:选择次数
	 * 
	 * @param db
	 */
	private void createProductChoiceCount(SQLiteDatabase db) {
		String sql = "CREATE TABLE " + tb_product_choice_count + " (id INTEGER PRIMARY KEY,choiceCount INTEGER)";
		db.execSQL(sql);
	}

	@Override
	public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
	}

	/**
	 * 检测数据库中是否存在某一列
	 * 
	 * @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;
	}

}
