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.TrainStationDalImpl;
import cn.net.cpzslibs.prot.handset.bean.res.TrainStationBean;

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

	@Override
	public void insertAllTrainStaion(List<TrainStationBean> stationBeans) {
		Py4j py4j = new Py4j();
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "insert into " + YthDBHelper.tb_train_station_manager +" (id,province,city,county,station,provinceCode,createTime,updateTime,stationPY,provinceCodePY) values (?,?,?,?,?,?,?,?,?,?)";
		db.beginTransaction();
//		long startTime = 0;s
		for (TrainStationBean b : stationBeans) {
//			String stationPY = py4j.getPreLetterPY(b.getStation());
//			String provinceCodePY = py4j.getPreLetterPY(b.getProvince_code());
			String stationPY = "";
			String provinceCodePY="";
			db.execSQL(sql, new Object[]{b.getId(),b.getProvince(),b.getCity(),b.getCounty(),b.getStation(),b.getProvince_code(),b.getCreate_time(),b.getUpdate_time(),stationPY,provinceCodePY});
		}
		
		
		db.setTransactionSuccessful();
		db.endTransaction();
//		long endTime = System.currentTimeMillis();
//		Log.e(TConfig.Echo, "间隔时间：" + (endTime - startTime));
		py4j = null;
		db.close();
	}
	/*private String getPY(String contentStr) {
		if (StringUtils.isEmpty(contentStr)) return "";
		char[] c = contentStr.toCharArray();
		if (c == null) return "";
		StringBuilder sb = new StringBuilder();
		for (int i = 0; i < c.length; i++) {
			String s = PinyinUtil.toPinyin(context, c[i]);//.substring(0, 1).toUpperCase()
			if (StringUtils.isEmpty(s)) {
				sb.append(c[i]);
			}else {
				sb.append(s.substring(0, 1).toUpperCase());	
			}
			
		}
		return sb.toString();
	}*/

	@Override
	public void insertTrainStaion(TrainStationBean b) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "insert into " + YthDBHelper.tb_train_station_manager +" (id,province,city,county,station,provinceCode,createTime,updateTime) values (?,?,?,?,?,?,?,?)";
		db.execSQL(sql, new Object[]{b.getId(),b.getProvince(),b.getCity(),b.getCounty(),b.getStation(),b.getProvince_code(),b.getCreate_time(),b.getUpdate_time()});
		db.close();
	}

	@Override
	public List<TrainStationBean> queryAllStationInfo() {
		SQLiteDatabase db = helper.getReadableDatabase();
		List<TrainStationBean> beans = new ArrayList<>();
		String sql = "select * from " + YthDBHelper.tb_train_station_manager;
		Cursor c = db.rawQuery(sql, null);
		while (c.moveToNext()) {
			beans.add(getTrainStation(c));
		}
		c.close();
		db.close();
		return beans;
	}

	@Override
	public List<TrainStationBean> queryStationByProvinceCode(String provinceCode) {
		SQLiteDatabase db = helper.getReadableDatabase();
		List<TrainStationBean> beans = new ArrayList<>();
		Cursor c;
		if ("全国".equals(provinceCode)) {
			String sql = "select * from " + YthDBHelper.tb_train_station_manager;
			c = db.rawQuery(sql, null);
		} else {
			String sql = "select * from " + YthDBHelper.tb_train_station_manager +" where provinceCode=?";
			c = db.rawQuery(sql, new String[]{provinceCode});
		}
		while (c.moveToNext()) {
			beans.add(getTrainStation(c));
		}
		c.close();
		db.close();
		return beans;
	}

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

	@Override
	public List<String> queryAllProvinceCode() {
		SQLiteDatabase db = helper.getReadableDatabase();
		List<String> provinces = new ArrayList<>();
		provinces.add("全国");
		String sql = "SELECT DISTINCT provinceCode FROM "+YthDBHelper.tb_train_station_manager +" where provinceCode<>'' AND provinceCode is not null";
		Cursor c = db.rawQuery(sql, null);
		while (c.moveToNext()) {
			provinces.add(c.getString(0));
		}
		c.close();
		db.close();
		return provinces;
	}
	
	@Override
	public List<TrainStationBean> searchTrainStationByCondition(String provinceCode, String condition) {
		condition = condition.toUpperCase();
		SQLiteDatabase db = helper.getReadableDatabase();
		List<TrainStationBean> beans = new ArrayList<>();
		Cursor c;
		if ("全国".equals(provinceCode)) {
			String sql = "select * from " + YthDBHelper.tb_train_station_manager+" where station LIKE '%"+condition+"%' OR stationPY LIKE '%"+condition+"%'";
			c = db.rawQuery(sql, null);
		} else {
			String sql = "select * from " + YthDBHelper.tb_train_station_manager+" where station LIKE '%"+condition+"%' OR stationPY LIKE '%"+condition+"%' AND provinceCode=?";
			c = db.rawQuery(sql, new String[]{provinceCode});
		}
		while (c.moveToNext()) {
			beans.add(getTrainStation(c));
		}
		c.close();
		db.close();
		return beans;
	}


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

	@Override
	public TrainStationBean getTrainStation(Cursor c) {
		TrainStationBean bean = new TrainStationBean();
		bean.setId(c.getInt(c.getColumnIndex("id")));
		bean.setProvince(c.getString(c.getColumnIndex("province")));
		bean.setCity(c.getString(c.getColumnIndex("city")));
		bean.setCounty(c.getString(c.getColumnIndex("county")));
		bean.setStation(c.getString(c.getColumnIndex("station")));
		bean.setProvince_code(c.getString(c.getColumnIndex("provinceCode")));
		bean.setCreate_time(c.getString(c.getColumnIndex("createTime")));
		bean.setUpdate_time(c.getString(c.getColumnIndex("updateTime")));
		return bean;
	}


}
