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

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.StationManagerBean;
import cn.net.caoyao.cpzshandset.db.YthDBHelper;
import cn.net.caoyao.cpzshandset.db.presenter.StationManagerDal;

public class YthStationManagerDal implements StationManagerDal{
	private YthDBHelper helper;
	private SQLiteDatabase db;

	public YthStationManagerDal(Context context) {
		helper = new YthDBHelper(context);
	}

	/**
	 * 插入车站信息
	 * 
	 * @param stations
	 */
	public void insertStation(List<StationManagerBean> stations) {
		db = helper.getWritableDatabase();
		Cursor cursor = clearStation(db);
		for (int i = 0; i < stations.size(); i++) {
			StationManagerBean s = stations.get(i);
			String sql = "insert into " + YthDBHelper.tb_station_manager
					+ " (id,stationname,stationdec,line,region,regionid,transmode,status)values(?,?,?,?,?,?,?,?)";
			db.execSQL(sql,
					new Object[] { s.getId(), s.getStationname(), s.getStationdec(), s.getLine(), s.getRegion(),
							s.getRegionid(), s.getTransmode(), s.getStatus() });
		}
		closeDB(db, cursor);
	}
	/**
	 * 插入火车的其实站点
	 * @param s
	 */
	public void insertTrainStartStation(StationManagerBean s){
		db = helper.getWritableDatabase();
		String sql = "select count(*) as count from " + YthDBHelper.tb_station_manager +" where id=?";
		Cursor cursor = db.rawQuery(sql, new String[]{String.valueOf(s.getId())});
		if (cursor.moveToNext()) {
			int count = cursor.getInt(0);
			if (count > 0) {
				String updateSql = "update " + YthDBHelper.tb_station_manager +" set stationname=?,stationdec=?,stationdec=?,line=?,region=?,regionid=?,transmode=?,status=? where id=?";
				db.execSQL(updateSql, new Object[] { s.getStationname(), s.getStationdec(), s.getLine(), s.getRegion(),
							s.getRegionid(), s.getTransmode(), s.getStatus(), s.getId()});
			} else {
				String insertSql = "insert into " + YthDBHelper.tb_station_manager
						+ " (id,stationname,stationdec,line,region,regionid,transmode,status) values (?,?,?,?,?,?,?,?)";
				db.execSQL(insertSql, new Object[] { s.getId(), s.getStationname(), s.getStationdec(), s.getLine(), s.getRegion(),
						s.getRegionid(), s.getTransmode(), s.getStatus() });
			}
		}
		
		closeDB(db, cursor);
	}

	/**
	 * 根据车站ID查询车站信息
	 * @param id
	 * @return
	 */
	public StationManagerBean queryStationById(int id) {
		db = helper.getReadableDatabase();
		String sql = "SELECT * FROM " + YthDBHelper.tb_station_manager +" WHERE id=?";
		Cursor cursor = db.rawQuery(sql, new String[]{String.valueOf(id)});
		StationManagerBean station = null;
		if (cursor.moveToNext()) {
			station = getStation(cursor);
		}
		closeDB(db, cursor);
		return station;
	}
	
	/**
	 * 通过发送方式获取车站列表
	 * @param transmode 1:火车    2:汽车
	 * @return
	 */
	public List<StationManagerBean> searchStationByMode(int transmode){
		List<StationManagerBean> stations = new ArrayList<StationManagerBean>();
		db = helper.getReadableDatabase();
		String sql = "SELECT * FROM " + YthDBHelper.tb_station_manager +" WHERE transmode=?";
		Cursor cursor = db.rawQuery(sql, new String[]{String.valueOf(transmode)});
		while (cursor.moveToNext()) {
			stations.add(getStation(cursor));
		}
		closeDB(db, cursor);
		return stations;
	}
	
	/**
	 * 通过发送方式获取车站列表
	 * @param transmode 1:火车    2:汽车
	 * @return
	 */
	public List<String> queryAllStationNameByMode(int transmode){
		List<String> stations = new ArrayList<String>();
		db = helper.getReadableDatabase();
		String sql = "SELECT DISTINCT stationname FROM " + YthDBHelper.tb_station_manager +" WHERE transmode=? AND stationname<>'' AND stationname is not null";
		Cursor cursor = db.rawQuery(sql, new String[]{String.valueOf(transmode)});
		while (cursor.moveToNext()) {
			stations.add(cursor.getString(0));
		}
		closeDB(db, cursor);
		return stations;
	}
	
	/**
	 * 通过发送方式获取车站列表
	 * @param transmode 1:火车    2:汽车
	 * @return
	 */
	public List<StationManagerBean> queryAllStationByMode(int transmode){
		List<StationManagerBean> stations = new ArrayList<StationManagerBean>();
		db = helper.getReadableDatabase();
		String sql = "SELECT * FROM " + YthDBHelper.tb_station_manager +" WHERE transmode=?";
		Cursor cursor = db.rawQuery(sql, new String[]{String.valueOf(transmode)});
		while (cursor.moveToNext()) {
			stations.add(getStation(cursor));
		}
		closeDB(db, cursor);
		return stations;
	}
	
	/**
	 * 通过发送方式获取车站列表
	 * @param transmode 1:火车    2:汽车
	 * @return
	 */
	public List<StationManagerBean> queryAllStationByModeAndName(int transmode,String stationName){
		List<StationManagerBean> stations = new ArrayList<StationManagerBean>();
		db = helper.getReadableDatabase();
		String sql;
		if (transmode == 1) {
			sql= "SELECT * FROM " + YthDBHelper.tb_station_manager +" WHERE transmode=? AND stationname=? AND stationdec<>'' AND stationdec is not null";
		} else {
			sql= "SELECT * FROM " + YthDBHelper.tb_station_manager +" WHERE transmode=? AND stationname=?";
		}
		Cursor cursor = db.rawQuery(sql, new String[]{String.valueOf(transmode),stationName});
		while (cursor.moveToNext()) {
			stations.add(getStation(cursor));
		}
		closeDB(db, cursor);
		return stations;
	}
	
	/**
	 * 通过发送方式获取车站列表
	 * @param transmode 1:火车    2:汽车
	 * @return
	 */
	public List<StationManagerBean> searchStationByCondition(int transmode,String stationName,String condition){
		List<StationManagerBean> stations = new ArrayList<StationManagerBean>();
		db = helper.getReadableDatabase();
		Cursor cursor;
		if (transmode == 1) {
			String sql = "SELECT * FROM " + YthDBHelper.tb_station_manager +" WHERE transmode=? AND stationname=? AND stationdec<>'' AND stationdec is not null AND stationdec LIKE '%"+condition+"%'";
			cursor = db.rawQuery(sql,  new String[]{String.valueOf(transmode),stationName});
		} else {
			String sql2 = "SELECT * FROM " + YthDBHelper.tb_station_manager +" WHERE transmode=? AND stationname LIKE '%"+condition+"%' OR stationdec LIKE '%"+condition+"%'";
			cursor = db.rawQuery(sql2, new String[]{String.valueOf(transmode)});
		}
		while (cursor.moveToNext()) {
			stations.add(getStation(cursor));
		}
		closeDB(db, cursor);
		return stations;
	}
	
	/**
	 * 通过发送方式获取车站列表
	 * @param transmode 1:火车    2:汽车
	 * @return
	 */
	public List<String> searchStationNameByCondition(int transmode,String condition){
		List<String> stations = new ArrayList<String>();
		db = helper.getReadableDatabase();
		String sql = "SELECT DISTINCT stationname FROM " + YthDBHelper.tb_station_manager +" WHERE transmode=? AND stationname<>'' AND stationname is not null AND stationname LIKE '%"+condition+"%'";
		Cursor cursor = db.rawQuery(sql, new String[]{String.valueOf(transmode)});
		while (cursor.moveToNext()) {
			stations.add(cursor.getString(0));
		}
		closeDB(db, cursor);
		return stations;
	}

	private void closeDB(SQLiteDatabase db, Cursor cursor) {
		if (cursor != null) {
			cursor.close();
		}
		if (db != null) {
			db.close();
		}
	}

	public StationManagerBean getStation(Cursor cursor) {
		StationManagerBean station = new StationManagerBean();
		station.setId(cursor.getInt(cursor.getColumnIndex("id")));
		station.setStationname(cursor.getString(cursor.getColumnIndex("stationname")));
		station.setStationdec(cursor.getString(cursor.getColumnIndex("stationdec")));
		station.setLine(cursor.getString(cursor.getColumnIndex("line")));
		station.setRegion(cursor.getString(cursor.getColumnIndex("region")));
		station.setRegionid(cursor.getString(cursor.getColumnIndex("regionid")));
		station.setTransmode(cursor.getInt(cursor.getColumnIndex("transmode")));
		station.setStatus(cursor.getInt(cursor.getColumnIndex("status")));
		return station;
	}

	/**
	 * 清除数据库数据
	 * 
	 * @param db
	 */
	private Cursor clearStation(SQLiteDatabase db) {
		String sql = "select count(*) as count from sqlite_master where type ='table' and name ='"
				+ YthDBHelper.tb_station_manager + "';";
		Cursor cursor = db.rawQuery(sql, null);
		if (cursor.moveToNext()) {
			int count = cursor.getInt(0);
			if (count > 0) {
				String delSql = "delete from " + YthDBHelper.tb_station_manager;
				db.execSQL(delSql);
			}
		}
		
		return cursor;
	}

}
