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

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.TruckAddressBean;
import cn.net.caoyao.cpzshandset.db.YthLoginShipmentDBHelper;
import cn.net.caoyao.cpzshandset.db.presenter.base.TruckAddressDalImpl;

public class YthLoginTruckAddressDal extends TruckAddressDalImpl{
	private YthLoginShipmentDBHelper helper;
	public YthLoginTruckAddressDal(Context context) {
		helper = new YthLoginShipmentDBHelper(context);
	}
	@Override
	public void insertNativeTruckAddress(TruckAddressBean addressBean) {
		SQLiteDatabase db = helper.getWritableDatabase();
		/*String querySql = "select id from "+YthLoginShipmentDBHelper.tb_truck_shipment_address_info+" where cityCode=? and detailAddress=?";
		Cursor c = db.rawQuery(querySql, new String[]{String.valueOf(addressBean.getCityCode()),addressBean.getDetailAddress()});
		int id = 0;
		if (c.moveToNext()) {
			id = c.getInt(0);
		}
		c.close();
		if (id > 0) {
			String updateSql = "update " + YthLoginShipmentDBHelper.tb_truck_shipment_address_info +" set updateDate=? where id=?";
			db.execSQL(updateSql, new Object[]{addressBean.getUpdateDate(),id});
		} else {
			Py4j py4j = new Py4j();
			String detailAddressPY = py4j.getPreLetterPY(addressBean.getDetailAddress()).toUpperCase();
			String sql = "replace into "+YthLoginShipmentDBHelper.tb_truck_shipment_address_info + " (provinceCode,cityCode,detailAddress,detailAddressPY,originType,updateDate) values (?,?,?,?,?,?)";
			db.execSQL(sql, new Object[]{addressBean.getCityCode()/1000*1000,addressBean.getCityCode(),addressBean.getDetailAddress(),detailAddressPY,0,addressBean.getUpdateDate()});
			py4j = null;
		}*/
		
		Py4j py4j = new Py4j();
		String detailAddressPY = py4j.getPreLetterPY(addressBean.getDetailAddress()).toUpperCase();
		String sql = "insert or replace into "+YthLoginShipmentDBHelper.tb_truck_shipment_address_info + " (provinceCode,cityCode,detailAddress,detailAddressPY,originType,updateDate) values (?,?,?,?,?,?)";
		db.execSQL(sql, new Object[]{addressBean.getCityCode(),addressBean.getCityCode(),addressBean.getDetailAddress(),detailAddressPY,0,addressBean.getUpdateDate()});
		py4j = null;
		db.close();
	}
	@Override
	public void insertOnlineTruckAddress(TruckAddressBean addressBean) {
		Py4j py4j = new Py4j();
		SQLiteDatabase db = helper.getWritableDatabase();
		String detailAddressPY = py4j.getPreLetterPY(addressBean.getDetailAddress()).toUpperCase();
		String sql = "replace into "+YthLoginShipmentDBHelper.tb_truck_shipment_address_info + " (id,provinceCode,cityCode,detailAddress,detailAddressPY,originType,createDate) values (?,?,?,?,?,?,?)";
		db.execSQL(sql, new Object[]{addressBean.getId(),addressBean.getCityCode(),addressBean.getCityCode(),addressBean.getDetailAddress(),detailAddressPY,1,addressBean.getCreateDate()});
		db.close();	
		py4j = null;
	}
	@Override
	public void insertTruckAddressList(List<TruckAddressBean> addressBeans) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "replace into "+YthLoginShipmentDBHelper.tb_truck_shipment_address_info + " (id,provinceCode,cityCode,detailAddress,detailAddressPY,originType,createDate) values (?,?,?,?,?,?,?)";
		Py4j py4j = new Py4j();
		db.beginTransaction();
		for (TruckAddressBean addressBean : addressBeans) {
			String detailAddressPY = py4j.getPreLetterPY(addressBean.getDetailAddress().toUpperCase());
			db.execSQL(sql, new Object[]{addressBean.getId(),addressBean.getCityCode(),addressBean.getCityCode(),addressBean.getDetailAddress(),detailAddressPY,1,addressBean.getCreateDate()});
		}
		db.setTransactionSuccessful();
		db.endTransaction();
		db.close();	
		py4j = null;
	}
	@Override
	public void deleteAllTruckTruckAddress() {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "delete from " + YthLoginShipmentDBHelper.tb_truck_shipment_address_info;
		db.execSQL(sql);
		db.close();
	}
	@Override
	public List<TruckAddressBean> queryAllTruckTruckAddress() {
		SQLiteDatabase db = helper.getReadableDatabase();
		List<TruckAddressBean> addressBeans = new ArrayList<>();
		String sql = "select * from " + YthLoginShipmentDBHelper.tb_truck_shipment_address_info +" order by id desc";
		Cursor c = db.rawQuery(sql, null);
		while (c.moveToNext()) {
			addressBeans.add(getTruckAddress(c));
		}
		c.close();
		db.close();
		return addressBeans;
	}
	@Override
	public List<TruckAddressBean> searchTruckAddressByCondition(int cityCode, String conditionStr) {
		conditionStr = conditionStr.toUpperCase();
		SQLiteDatabase db = helper.getReadableDatabase();
		List<TruckAddressBean> addressBeans = new ArrayList<>();
		String sql = "select * from " + YthLoginShipmentDBHelper.tb_truck_shipment_address_info +" where cityCode=? and detailAddress like '%"+conditionStr+"%' or detailAddressPY like '%"+conditionStr+"%' order by id desc";
		Cursor c = db.rawQuery(sql, new String[]{String.valueOf(cityCode)});
		while (c.moveToNext()) {
			addressBeans.add(getTruckAddress(c));
		}
		c.close();
		db.close();
		return addressBeans;
	}
	@Override
	public TruckAddressBean getTruckAddress(Cursor c) {
		TruckAddressBean aBean = new TruckAddressBean();
		aBean.setId(c.getInt(c.getColumnIndex("id")));
		aBean.setCityCode(c.getInt(c.getColumnIndex("cityCode")));
		aBean.setDetailAddress(c.getString(c.getColumnIndex("detailAddress")));
		aBean.setOriginType(c.getInt(c.getColumnIndex("originType")));
		aBean.setCreateDate(c.getString(c.getColumnIndex("createDate")));
		aBean.setUpdateDate(c.getString(c.getColumnIndex("updateDate")));
//		LogUtil.d(TConfig.Echo, aBean.getDetailAddress() +"  拼音：" + c.getString(c.getColumnIndex("detailAddressPY")));
		return aBean;
	}
	@Override
	public List<TruckAddressBean> queryTruckAddressByCityCode(int cityCode) {
		SQLiteDatabase db = helper.getReadableDatabase();
		List<TruckAddressBean> addressBeans = new ArrayList<>();
		if (cityCode <= 0) {
			String sql = "select * from " + YthLoginShipmentDBHelper.tb_truck_shipment_address_info +" order by id desc";
			Cursor c = db.rawQuery(sql, null);
			while (c.moveToNext()) {
				addressBeans.add(getTruckAddress(c));
			}
			c.close();
		} else {
			String sql = "select * from " + YthLoginShipmentDBHelper.tb_truck_shipment_address_info +"  where provinceCode=? order by id desc";
			Cursor c = db.rawQuery(sql, new String[]{String.valueOf(cityCode)});
			while (c.moveToNext()) {
				addressBeans.add(getTruckAddress(c));
			}
			c.close();
		}
		
		db.close();
		return addressBeans;
	}
	@Override
	public List<String> queryTruckAddressNameByCityCode(int cityCode) {
		SQLiteDatabase db = helper.getReadableDatabase();
		List<String> addressNames = new ArrayList<>();
		if (cityCode <= 0) {
			String sql = "select detailAddress from " + YthLoginShipmentDBHelper.tb_truck_shipment_address_info +" order by id desc";
			Cursor c = db.rawQuery(sql, null);
			while (c.moveToNext()) {
				addressNames.add(c.getString(0));
			}
			c.close();
		} else {
			String sql = "select detailAddress from " + YthLoginShipmentDBHelper.tb_truck_shipment_address_info +"  where provinceCode=? order by id desc";
//			Cursor c = db.rawQuery(sql, new String[]{String.valueOf(cityCode/1000*1000)});
			Cursor c = db.rawQuery(sql, new String[]{String.valueOf(cityCode)});
			while (c.moveToNext()) {
				addressNames.add(c.getString(0));
			}
			c.close();
		}
		
		db.close();
		return addressNames;
	}
	@Override
	public List<String> searchTruckAddressNameByCondition(int cityCode, String conditionStr) {
		SQLiteDatabase db = helper.getReadableDatabase();
		conditionStr = conditionStr.toUpperCase();
		List<String> addressBeans = new ArrayList<>();
		String sql = "select detailAddress from " + YthLoginShipmentDBHelper.tb_truck_shipment_address_info +" where detailAddress like '%"+conditionStr+"%' or detailAddressPY like '%"+conditionStr+"%' and provinceCode=? order by id desc";
//		Cursor c = db.rawQuery(sql, new String[]{String.valueOf(cityCode/1000*1000)});
		Cursor c = db.rawQuery(sql, new String[]{String.valueOf(cityCode)});
		while (c.moveToNext()) {
			addressBeans.add(c.getString(0));
		}
		c.close();
		db.close();
		return addressBeans;
	}
	

}
