package cn.net.caoyao.cpzshandset.db.dal.yunCar;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import java.util.List;

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

import cn.net.caoyao.cpzshandset.db.YunCarDBHelper;

public class YunCarConfigDal {
	private YunCarDBHelper helper;

	public YunCarConfigDal(Context context) {
		helper = new YunCarDBHelper(context);
	}

	/**
	 * 
	 * @param car_no
	 *            车牌号
	 * @param engine_number
	 * @param vin
	 * @param client_tel
	 * @param label_id
	 *            插入销售信息表
	 */
	public void insertSaleInfo(String car_no, String engine_number, String vin,
			String client_tel, String label_id,String carNodate, int flag) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "insert into "
				+ YunCarDBHelper.tb_sales_info
				+ " (car_no,engine_number,vin,client_tel,label_id,carNodate,error_code,status) values (?,?,?,?,?,?,?,?)";

		db.execSQL(sql, new Object[] { car_no, engine_number, vin, client_tel,
				label_id, carNodate,0, flag });

		db.close();
	}

	public void insertSaleInfoPicture(String label_id, String picture_url) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "insert into " + YunCarDBHelper.tb_sale_picture_info
				+ " (label_id,picture_url) values (?,?)";

		db.execSQL(sql, new Object[] { label_id, picture_url });

		db.close();
	}

	/**
	 * 查询销售表是否有数据
	 * 
	 * @return FALSE 有数据 ture 无数据
	 */
	public boolean querySaleInfoCount() {
		SQLiteDatabase db = helper.getWritableDatabase();
		int count = 0;
		String sql = "select * from " + YunCarDBHelper.tb_sales_info;
		Cursor c = db.rawQuery(sql, null);
		while (c.moveToNext()) {
			count = c.getColumnCount();
		}
		c.close();
		db.close();
		return count > 0 ? false : true;
	}

	/**
	 * 查询图片表是不是有数据
	 * 
	 * @return
	 */
	public boolean querySaleInfoPictrueCount() {
		SQLiteDatabase db = helper.getWritableDatabase();
		int count = 0;
		String sql = "select * from " + YunCarDBHelper.tb_sale_picture_info;
		Cursor c = db.rawQuery(sql, null);
		while (c.moveToNext()) {
			count = c.getColumnCount();
		}
		c.close();
		db.close();
		return count > 0 ? false : true;
	}

	/**
	 * 查询返回图片丽景
	 * 
	 * @return
	 */
	public String querySaleInfoPictrue() {
		SQLiteDatabase db = helper.getWritableDatabase();
		String pic = "";
		String sql = "select * from " + YunCarDBHelper.tb_sale_picture_info;
		Cursor c = db.rawQuery(sql, null);
		while (c.moveToNext()) {
			pic = c.getString(c.getColumnIndex("picture_url"));
		}
		c.close();
		db.close();
		return pic;
	}

	public List<Map<String, String>> querySaleInfo() {
		SQLiteDatabase db = helper.getWritableDatabase();
		List<Map<String, String>> list = new ArrayList<Map<String, String>>();

		String sql = "select * from " + YunCarDBHelper.tb_sales_info;
		Cursor c = db.rawQuery(sql, null);
		Map<String, String> map = null;
		while (c.moveToNext()) {
			map = new HashMap<String, String>();

			String car_no = c.getString(c.getColumnIndex("car_no"));

			String engine_number = c.getString(c
					.getColumnIndex("engine_number"));

			String vin = c.getString(c.getColumnIndex("vin"));
			String label_id = c.getString(c.getColumnIndex("label_id"));
			String client_tel = c.getString(c.getColumnIndex("client_tel"));
			String error_code = c.getString(c.getColumnIndex("error_code"));
			String flag = c.getString(c.getColumnIndex("status"));
			String submit_status = c.getString(c.getColumnIndex("submit_status"));
			String carNodate = c.getString(c.getColumnIndex("carNodate"));
			
			map.put("car_no", car_no);
			map.put("engine_number", engine_number);
			map.put("vin", vin);
			map.put("label_id", label_id);
			map.put("client_tel", client_tel);
			map.put("error_code", error_code);
			map.put("flag", flag);
			map.put("submit_status", submit_status);
			map.put("carNodate", carNodate);
			
			list.add(map);
		}
		c.close();
		db.close();
		return list;
	}

	/**
	 * 查询一个包标签
	 * 
	 * @return
	 */

	public String querySaleInfoLabel() {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "select * from " + YunCarDBHelper.tb_sales_info;
		Cursor c = db.rawQuery(sql, null);
		String label_id = "";
		while (c.moveToNext()) {
			label_id = c.getString(c.getColumnIndex("label_id"));

		}
		c.close();
		db.close();
		return label_id;
	}
	/**
	 * 查询当前日期当前车牌的全部id
	 * @param carNo
	 * @param car_date
	 * @return
	 */
	public List<Integer> querySaleOutId(String carNo,String car_date) {
		SQLiteDatabase db = helper.getWritableDatabase();
		List<Integer> list = new ArrayList<Integer>();

		String sql = "select *  from carNoInfo where car_no ='"+carNo+"' and car_date ='"+car_date+"'";
		Cursor c = db.rawQuery(sql, null);
		while (c.moveToNext()) {
			int car_id = c.getInt(c.getColumnIndex("car_id"));
			list.add(car_id);
		}
		c.close();
		db.close();
		return list;
	}
	
	
	
	
	

	public void deleteAllSaleInfo() {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "delete from " + YunCarDBHelper.tb_sales_info;
		db.execSQL(sql);
		db.close();
	}

	public void deleteAllSaleInfoPicture() {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "delete from " + YunCarDBHelper.tb_sale_picture_info;
		db.execSQL(sql);
		db.close();
	}
	/**
	 * 删除该日期该车牌的所有配件
	 * @param carNo
	 * @param car_date
	 */
	
	public void deleteSaleInfoCarNoID(String carNo,String car_date) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "delete from carNoInfo where car_no='"+carNo+"' and car_date = '"+car_date+"';";
		db.execSQL(sql);
		db.close();
	}


	/**
	 * 根据id更新失败的errCode
	 * 
	 * @return
	 */
	public void upErrCode(int status,String errCode, String car_no) {

		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "update salesInfo set submit_status="+status+",error_code  ='" + errCode
				+ "'where car_no ='" + car_no + "'";

		// errLog(sql);
		if (db.isOpen()) {
			db.execSQL(sql);
			db.close();
		}

	}

}
