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

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

import com.bytebeats.py4j.Py4j;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import cn.net.caoyao.cpzshandset.bean.TruckConsigeeBean;
import cn.net.caoyao.cpzshandset.common.config.TConfig;
import cn.net.caoyao.cpzshandset.common.util.LogUtil;
import cn.net.caoyao.cpzshandset.db.YthImageDBHelper;
import cn.net.caoyao.cpzshandset.db.presenter.base.TruckConsigneeDalImpl;

public class YthImageTruckConsigneeDal extends TruckConsigneeDalImpl{
	private YthImageDBHelper helper;
	public YthImageTruckConsigneeDal(Context context) {
		helper = new YthImageDBHelper(context);
	}

	@Override
	public void insertNativeConsignee(TruckConsigeeBean cBean) {
		SQLiteDatabase db = helper.getWritableDatabase();
		Py4j py4j = new Py4j();
		String consigneeNamePY = py4j.getPreLetterPY(cBean.getConsigneeName());
		String sql = "replace into "+YthImageDBHelper.tb_truck_consignee_info +" (consigneeName,consigneeNamePY,originType) values (?,?,?)";
		db.execSQL(sql, new Object[]{cBean.getConsigneeName(),consigneeNamePY,0});
		py4j = null;
		db.close();
	}

	@Override
	public void insertOnlineConsignee(TruckConsigeeBean cBean) {
		SQLiteDatabase db = helper.getWritableDatabase();
		Py4j py4j = new Py4j();
		String consigneeNamePY = py4j.getPreLetterPY(cBean.getConsigneeName());
		String sql = "replace into "+YthImageDBHelper.tb_truck_consignee_info +" (id,consigneeName,consigneeNamePY,originType,createDate) values (?,?,?,?,?)";
		db.execSQL(sql, new Object[]{cBean.getId(),cBean.getConsigneeName(),consigneeNamePY,1,cBean.getCreateDate()});
		py4j = null;
		db.close();		
	}

	@Override
	public void insertConsigneeList(List<TruckConsigeeBean> consigeeBeans) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String delSql = "delete from " + YthImageDBHelper.tb_truck_consignee_info;
		db.execSQL(delSql);
		Py4j py4j = new Py4j();
		String sql = "insert or replace into "+YthImageDBHelper.tb_truck_consignee_info +" (id,consigneeName,consigneeNamePY,originType,createDate) values (?,?,?,?,?)";
		for (TruckConsigeeBean cBean : consigeeBeans) {
			String consigneeNamePY = py4j.getPreLetterPY(cBean.getConsigneeName());
			db.execSQL(sql, new Object[]{cBean.getId(),cBean.getConsigneeName(),consigneeNamePY,1,cBean.getCreateDate()});
		}
		py4j = null;
		db.close();
	}

	@Override
	public void deleteAllTruckConsignee() {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "delete from " + YthImageDBHelper.tb_truck_consignee_info;
		db.execSQL(sql);
		db.close();
		
	}

	@Override
	public List<TruckConsigeeBean> queryAllTruckConsignee() {
		SQLiteDatabase db = helper.getReadableDatabase();
		List<TruckConsigeeBean> cBeans = new ArrayList<>();
		String sql = "select * from " + YthImageDBHelper.tb_truck_consignee_info+" order by id desc";
		Cursor c = db.rawQuery(sql, null);
		while (c.moveToNext()) {
			cBeans.add(getTruckConsigee(c));
		}
		c.close();
		db.close();
		return cBeans;
	}

	@Override
	public TruckConsigeeBean getTruckConsigee(Cursor c) {
		TruckConsigeeBean consigeeBean = new TruckConsigeeBean();
		consigeeBean.setId(c.getInt(c.getColumnIndex("id")));
		consigeeBean.setConsigneeName(c.getString(c.getColumnIndex("consigneeName")));
		consigeeBean.setOriginType(c.getInt(c.getColumnIndex("originType")));
		consigeeBean.setCreateDate(c.getString(c.getColumnIndex("createDate")));
//		LogUtil.d(TConfig.Echo, consigeeBean.getConsigneeName() + "  拼音：" + c.getString(c.getColumnIndex("consigneeNamePY")));
		return consigeeBean;
	}

	@Override
	public List<TruckConsigeeBean> searchConsigneeByCondition(String conditionStr) {
		SQLiteDatabase db = helper.getReadableDatabase();
		List<TruckConsigeeBean> consigeeBeans = new ArrayList<>();
		conditionStr = conditionStr.toUpperCase();
		String sql = "select * from " + YthImageDBHelper.tb_truck_consignee_info+" where consigneeName like '%" + conditionStr+"%' or consigneeNamePY like '%" + conditionStr+"%' order by id desc";
		Cursor c = db.rawQuery(sql, null);
		while (c.moveToNext()) {
			consigeeBeans.add(getTruckConsigee(c));
		}
		c.close();
		db.close();
		return consigeeBeans;
	}

	@Override
	public List<String> queryAllTruckConsigneeName() {
		SQLiteDatabase db = helper.getReadableDatabase();
		List<String> cBeans = new ArrayList<>();
		String sql = "select consigneeName from " + YthImageDBHelper.tb_truck_consignee_info+" order by id desc";
		Cursor c = db.rawQuery(sql, null);
		while (c.moveToNext()) {
			cBeans.add(c.getString(0));
		}
		c.close();
		db.close();
		return cBeans;
	}

	@Override
	public List<String> searchConsigneeNameByCondition(String conditionStr) {
		SQLiteDatabase db = helper.getReadableDatabase();
		List<String> consigeeBeans = new ArrayList<>();
		conditionStr = conditionStr.toUpperCase();
		LogUtil.d(TConfig.Echo, "查询条件：" + conditionStr);
		String sql = "select consigneeName from " + YthImageDBHelper.tb_truck_consignee_info+" where consigneeNamePY like '%" + conditionStr+"%' or consigneeName like '%" + conditionStr+"%' order by id desc";
		Cursor c = db.rawQuery(sql, null);
		while (c.moveToNext()) {
			consigeeBeans.add(c.getString(0));
		}
		LogUtil.i(TConfig.Echo, "查询结果：" + consigeeBeans.toString());
		c.close();
		db.close();
		return consigeeBeans;
	}

}
