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 YthPunitShipmentDBHelper extends SQLiteOpenHelper {
	private static final int VERSION = 1;
	public final static String db_name = "ythScannerShipmentPUnitHandset.db";
	/**
	 * 发运单
	 */
	public static final String tb_invoice = "invoice";
	/**
	 * 线上获取的订单列表
	 */
	public static final String tb_online_truck_invoice ="onlineTruckInvoice";
	
	public static final String tb_driver_info = "driverInfo";
	/**
	 * 责任人表 ResponsiblePersonId
	 * @param context
	 */
	public static final String tb_responsible_person = "responsiblePerson";
	/**
	 * 订单上传记录表
	 */
	public static final String tb_shipment_upload_record="shipmentUploadRecord";
	/**
	 * 发货货物表
	 */
	public static final String tb_shipment_product_info="shipmentProductInfo";
	/**
	 * 仓库表
	 */
	public static final String tb_repertory_info="repertoryInfo";
	public YthPunitShipmentDBHelper(Context context) {
		super(context, db_name, null, VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase db) {
		createInvoice(db);
		createResponsiblePerson(db);
		createOnlineTruckInvoice(db);
		createDriverInfo(db);
		createShipmentUploadRecord(db);
		createShipmentProductInfo(db);
		createRepertoryInfo(db);
	}
	
	/**
	 * id bigint(25) NOT NULL仓库Id 
	 * repertory_no varchar(30) NULL仓库编号
	 * repertory_name varchar(30) NULL仓库名称 
	 * size varchar(50) NULL仓库的大小
	 * create_time datetime NULL创建日期
	 * company_id bigint(10) NULL所属公司Id
	 * status tinyint(4) NULL状态(0:删除; 
	 * fuzeren varchar(30) NULL负责人
	 * 
	 * @param db
	 */
	private void createRepertoryInfo(SQLiteDatabase db) {
		String sql = "CREATE TABLE " + tb_repertory_info +" (id bigint PRIMARY KEY,repertory_no varchar(30),repertory_name varchar(30),size varchar(50),create_time varchar(50),company_id bigint,status integer,fuzeren varchar(30))";
		db.execSQL(sql);
	}

	/**
	 * 
	 * id bigint(25) NOT NULL货物ID 
	 * diver_id bigint(25) NULL司机Id
	 * repertory_id bigint(25) NULL库存货物ID 
	 * item_num varchar(25) NULL件数
	 * huowu_name varchar(25) NULL货物名称 
	 * out_order_no varchar(25) NULL外交货单号
	 * xiangmu_no varchar(20) NULL项目行号 
	 * weight varchar(20) NULL货物重量单位是吨
	 * price varchar(25) NULL货物单价
	 * settle_price varchar(25) NULL结算价格
	 * status tinyint(4) NULL状态:0:删除 1有效 2发货完成 
	 * update_time datetime NULL修改时间
	 * guozhang_time datetime NULL过账时间 
	 * isguozhang tinyint(4) NULL是否过账 0未过账 2过账 1过账失败 
	 * isshenhe tinyint(4) NULL是否审核 0:未审核 2审核成功 1审核失败
	 * @param db
	 */
	private void createShipmentProductInfo(SQLiteDatabase db) {
		String sql = "CREATE TABLE " + tb_shipment_product_info+" (id bigint PRIMARY KEY,order_id bigint,diver_id bigint,repertory_id bigint,item_num varchar(25),huowu_name varchar(25),out_order_no varchar(25),xiangmu_no varchar(20),weight varchar(20),price varchar(25),settle_price varchar(25),online_status integer,update_time varchar(25),guozhang_time varchar(25),isguozhang integer,isshenhe integer,can_shipment_count integer,native_status INTEGER default 0,error_code INTEGER default 0, over_count INTEGER default 0)";
		db.execSQL(sql);
	}

	private void createShipmentUploadRecord(SQLiteDatabase db) {
		String sql = "CREATE TABLE " + tb_shipment_upload_record +" (id INTEGER PRIMARY KEY AUTOINCREMENT,option_type INTEGER,account VARCHAR(20),driver_id bigint,order_id bigint,driver_name VARCHAR(20),driver_tel VARCHAR(20),car_no VARCHAR(20),order_no VARCHAR(20),product_id bigint,product_name VARCHAR(20),label_count INTEGER,status INTEGER,error_code INTEGER,upload_date DATE DEFAULT (date('now','localtime')))";	
		db.execSQL(sql);
	}

	/**
	 * id bigint(20) NOT NULL主键ID
	 * order_id bigint(20) NULL订单ID(关联订单表主键)
	 * driver_name varchar(20) NULL驾驶员姓名
	 * driver_phone varchar(20) NULL驾驶员电话
	 * car_no varchar(20) NULL车牌号
	 * driver_licenseNo varchar(30) NULL驾驶证件号
	 * out_orderId varchar(30) NULL外向交货单号
	 * num varchar(30) NULL数量
	 * over_count：超过数量
	 * can_shipment_count：还可以发货的数量（目前用作已发货包数）
	 * status int(2) NULL状态0:正常1:删除
	 */
	private void createDriverInfo(SQLiteDatabase db) {
		String sql = "CREATE TABLE " + tb_driver_info +" (id bigint PRIMARY KEY,order_id bigint,driver_name VARCHAR(20),driver_phone VARCHAR(20),car_no VARCHAR(20),driver_licenseNo VARCHAR(20),unit_name VARCHAR(20),car_kg VARCHAR(20),huo_kg VARCHAR(20), dest_addr VARCHAR(20), total_money VARCHAR(20), dest_tel VARCHAR(20), sales_name VARCHAR(20),online_status INTEGER,native_status INTEGER default 0,error_code INTEGER default 0,over_count INTEGER,createDate DATE DEFAULT (date('now','localtime')),updateDate DATE DEFAULT (date('now','localtime')))";
		db.execSQL(sql);
	}

	/**
	 * 
	 * id bigint(25) NOT NULL订单ID 
	 * order_no varchar(25) NULL订单号
	 * unit_name varchar(30) NULL单位名称 
	 * create_time datetime NULL创建日期
	 * update_time datetime NULL修改日期 
	 * neiqin_id bigint(10) NULL内勤Id
	 * sales_id bigint(10) NULL业务员ID 
	 * status tinyint(4)NULL状态(1:业务员未提交2:业务员已提交3:审核中4:审核失败5:审核成功) 
	 * submit_time datetime NULL提交时间
	 * audit_time datetime NULL审核时间 
	 * guozhang_time datetime NULL过账时间 
	 * @param db
	 */
	private void createOnlineTruckInvoice(SQLiteDatabase db) {
		String sql = "CREATE TABLE " + tb_online_truck_invoice + " (id bigint PRIMARY KEY,order_no VARCHAR(20),unit_name VARCHAR(20),neiqin_id bigint,sales_id bigint,submit_time VARCHAR(20), audit_time VARCHAR(20),guozhang_time VARCHAR(20), online_status INTEGER,labelMode INTEGER default 1,native_status INTEGER default 0,error_code INTEGER,over_count INTEGER,createDate DATE DEFAULT (date('now','localtime')),updateDate DATE DEFAULT (date('now','localtime')))";
		db.execSQL(sql);
	}

	/**
	 * account：登录账户
	 * pwd：密码
	 * username：责任人名称
	 * tel：电话
	 * @param db
	 */
	private void createResponsiblePerson(SQLiteDatabase db) {
		String sql = "CREATE TABLE " + tb_responsible_person+" (account VARCHAR(20) PRIMARY KEY, pwd VARCHAR(20),username VARCHAR(20), tel VARCHAR(20), userType integer default 0, repertory_id bigint default 0)";
		db.execSQL(sql);
	}

	/**
	 * 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 4,driver_id bigint,product_id bigint,elemtype INTEGER default 2,labelid VARCHAR(20),labelStatus INTEGER default 0 ,timestamp VARCHAR(20),package_id varchar(32),createDate DATE DEFAULT (date('now','localtime')))";
		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;
	}

}
