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

import java.util.List;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import cn.net.caoyao.cpzshandset.db.YthLoginShipmentDBHelper;
import cn.net.caoyao.cpzshandset.db.presenter.base.UserLoginDalImpl;
import cn.net.cpzslibs.prot.handset.bean.res.ResponsibleUserInfoBean;

public class YthLoginUserInfoDal extends UserLoginDalImpl{
	
	private YthLoginShipmentDBHelper helper;
	public YthLoginUserInfoDal(Context context) {
		helper = new YthLoginShipmentDBHelper(context);
	}

	@Override
	public void insertUserInfo(ResponsibleUserInfoBean info) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "replace into " + YthLoginShipmentDBHelper.tb_responsible_person +" (account,pwd,username,tel,userType) values (?,?,?,?,?)";
		db.execSQL(sql, new Object[]{info.getAccount(),info.getPwd(),info.getUsername(),info.getTel(),info.getUserType()});
		db.close();
	}

	@Override
	public void insertUserInfoList(List<ResponsibleUserInfoBean> infos) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "replace into " + YthLoginShipmentDBHelper.tb_responsible_person +" (account,pwd,username,tel,userType) values (?,?,?,?,?)";
		db.beginTransaction();
		for (ResponsibleUserInfoBean info : infos) {
			db.execSQL(sql, new Object[]{info.getAccount(),info.getPwd(),info.getUsername(),info.getTel(),info.getUserType()});
		}
		db.setTransactionSuccessful();
		db.endTransaction();
		db.close();
	}

	@Override
	public void deleteAllUserInfo() {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "delete from " + YthLoginShipmentDBHelper.tb_responsible_person;
		db.execSQL(sql);
		db.close();
	}

	@Override
	public ResponsibleUserInfoBean queryUserInfoByAccount(String account) {
		ResponsibleUserInfoBean uBean = null;
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "select * from " + YthLoginShipmentDBHelper.tb_responsible_person +" where account=?";
		Cursor c = db.rawQuery(sql, new String[]{account});
		if (c.moveToNext()) {
			uBean = getUserInfo(c);
		}
		c.close();
		db.close();
		return uBean;
	}

	@Override
	public ResponsibleUserInfoBean getUserInfo(Cursor c) {
		ResponsibleUserInfoBean bean = new ResponsibleUserInfoBean();
		bean.setAccount(c.getString(c.getColumnIndex("account")));
		bean.setPwd(c.getString(c.getColumnIndex("pwd")));
		bean.setUsername(c.getString(c.getColumnIndex("username")));
		bean.setTel(c.getString(c.getColumnIndex("tel")));
		bean.setUserType(c.getInt(c.getColumnIndex("userType")));
		return bean;
	}

}
