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

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.db.YthDBHelper;
import cn.net.caoyao.cpzshandset.db.presenter.base.ConsigneeDalImpl;
import cn.net.cpzslibs.prot.handset.bean.ConsigneeBean;

public class YthConsigneeDal extends ConsigneeDalImpl{
	private YthDBHelper helper;
	private Context context;
	public YthConsigneeDal(Context context) {
		helper = new YthDBHelper(context);
		this.context = context;
	}

	@Override
	public void insertConsignee(ConsigneeBean b) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "insert into " + YthDBHelper.tb_train_client_info+" (id,consignee,regionid,createTime,updateTime) values (?,?,?,?,?)";
		db.execSQL(sql, new Object[]{b.getId(),b.getConsignee(),b.getRegion_id(),b.getCreate_time(),b.getUpdate_time()});
		db.close();
	}

	@Override
	public void insertAllConsignee(List<ConsigneeBean> cBeans) {
		Py4j py4j = new Py4j();
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "insert into " + YthDBHelper.tb_train_client_info+" (id,consignee,regionid,createTime,updateTime,consigneePY) values (?,?,?,?,?,?)";
		db.beginTransaction();
		for (ConsigneeBean b : cBeans) {
//			String consigneePY = StringUtils.getPY(context, b.getConsignee());
			String consigneePY = py4j.getPreLetterPY(b.getConsignee());
			db.execSQL(sql, new Object[]{b.getId(),b.getConsignee(),b.getRegion_id(),b.getCreate_time(),b.getUpdate_time(),consigneePY});
		}
		db.setTransactionSuccessful();
		db.endTransaction();
		py4j = null;
		db.close();
	}

	@Override
	public List<ConsigneeBean> queryAllConsigee() {
		List<ConsigneeBean> cBeans = new ArrayList<>();
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "select * from " + YthDBHelper.tb_train_client_info;
		Cursor c = db.rawQuery(sql, null);
		while (c.moveToNext()) {
			cBeans.add(getConsigee(c));
		}
		c.close();
		db.close();
		return cBeans;
	}

	@Override
	public List<ConsigneeBean> searchConsigeeByCondition(String condition) {
		condition = condition.toUpperCase();
//		LogUtil.e(TConfig.Echo, "查询条件：" + condition);
		List<ConsigneeBean> cBeans = new ArrayList<>();
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "select * from " + YthDBHelper.tb_train_client_info +" where consignee LIKE '%"+condition+"%' OR consigneePY LIKE '%"+condition+"%'";
		Cursor c = db.rawQuery(sql, null);
		while (c.moveToNext()) {
			cBeans.add(getConsigee(c));
		}
		c.close();
		db.close();
		return cBeans;
	}

	@Override
	public ConsigneeBean queryConsigeeById(int id) {
		SQLiteDatabase db = helper.getReadableDatabase();
		ConsigneeBean bean = null;
		String sql = "select * from " + YthDBHelper.tb_train_client_info+" where id=?";
		Cursor c = db.rawQuery(sql, new String[]{String.valueOf(id)});
		if (c.moveToNext()) {
			bean = getConsigee(c);
		}
		c.close();
		db.close();
		return bean;
	}

	@Override
	public void deleteAllConsigee() {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "delete from " + YthDBHelper.tb_train_client_info;
		db.execSQL(sql);
		db.close();
	}

	@Override
	public ConsigneeBean getConsigee(Cursor c) {
		ConsigneeBean bean = new ConsigneeBean();
		bean.setId(c.getInt(c.getColumnIndex("id")));
		bean.setConsignee(c.getString(c.getColumnIndex("consignee")));
		bean.setRegion_id(c.getString(c.getColumnIndex("regionid")));
		bean.setCreate_time(c.getString(c.getColumnIndex("createTime")));
		bean.setUpdate_time(c.getString(c.getColumnIndex("updateTime")));
//		LogUtil.i(TConfig.Echo, bean.getConsignee() +"  拼音：" + c.getString(c.getColumnIndex("consigneePY")));
		return bean;
	}

}
