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

import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import cn.net.caoyao.cpzshandset.bean.CarNoBean;
import cn.net.caoyao.cpzshandset.db.YunCarDBHelper;
import cn.net.caoyao.cpzshandset.db.presenter.base.CarNoDalImpl;
import cn.net.cpzslibs.prot.handset.bean.res.YunCarNumberBean;


public class YunCarNoDal extends CarNoDalImpl{
	private YunCarDBHelper helper;
	public YunCarNoDal(Context context) {
		helper = new YunCarDBHelper(context);
	}

	@Override
	public void insertOnlineCarNoList(List<YunCarNumberBean> list) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "insert into " + YunCarDBHelper.tb_car_no_info +" (car_id,car_no,car_date) values (?,?,?)";
		for (YunCarNumberBean b : list) {
			db.execSQL(sql, new Object[]{b.getId(),b.getCar_number(),b.getDate()});
		}
		db.close();
	}

	@Override
	public boolean hasExistCarNo(String carNo) {
		SQLiteDatabase db = helper.getReadableDatabase();
		boolean isExist = false;
		String sql = "select count(car_no) from " + YunCarDBHelper.tb_car_no_info + " where car_no = ?";
		Cursor c = db.rawQuery(sql, new String[]{carNo});
		if (c.moveToNext()) {
			int count = c.getInt(0);
			if (count > 0) {
				isExist = true;
			}
		}
		c.close();
		db.close();
		return isExist;
	}

	@Override
	public void deleteAllCarNo() {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "delete from " + YunCarDBHelper.tb_car_no_info;
 		db.execSQL(sql);
 		db.close();
	}

	@Override
	public List<CarNoBean> search(String condition) {
		SQLiteDatabase db = helper.getWritableDatabase();
		List<CarNoBean> list = new ArrayList<>();
		String sql = "select car_id,car_no ,car_date,type,engine_number,label_id,vin,count(*) as num from " + YunCarDBHelper.tb_car_no_info + " where car_no LIKE '%" + condition + "%' GROUP BY car_no,car_date order by car_date desc";
		Cursor c = db.rawQuery(sql, null);
		while (c.moveToNext()) {
			CarNoBean b = getCarNoBean(c);
			b.setNum(c.getInt(c.getColumnIndex("num")));
			list.add(b);
		}
		c.close();
		db.close();
		return list;
	}

	@Override
	public List<CarNoBean> queryAllCarNo() {
		SQLiteDatabase db = helper.getWritableDatabase();
		List<CarNoBean> list = new ArrayList<>();
		String sql = "select car_id,car_no ,car_date,type,engine_number,label_id,vin,count(*) as num from " + YunCarDBHelper.tb_car_no_info + " GROUP BY car_no,car_date order by car_date desc";
		Cursor c = db.rawQuery(sql, null);
		while (c.moveToNext()) {
			CarNoBean b = getCarNoBean(c);
			b.setNum(c.getInt(c.getColumnIndex("num")));
			list.add(b);
		}
		c.close();
		db.close();
		return list;
	}
	@Override
	public CarNoBean getCarNoBean(Cursor c) {
		CarNoBean bean = new CarNoBean();
		bean.setCarId(c.getString(c.getColumnIndex("car_id")));
		bean.setCarNo(c.getString(c.getColumnIndex("car_no")));
		bean.setCarDate(c.getString(c.getColumnIndex("car_date")));
		bean.setType(c.getInt(c.getColumnIndex("type")));
		bean.setEngine_number(c.getString(c.getColumnIndex("engine_number")));
		bean.setLabel_id(c.getString(c.getColumnIndex("label_id")));
		bean.setVin(c.getString(c.getColumnIndex("vin")));
		return bean;
	}



	

	

}
