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

import java.util.List;

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

public class YthPunitUserInfoDal extends UserLoginDalImpl{
	
	private YthPunitShipmentDBHelper helper;
	public YthPunitUserInfoDal(Context context) {
		helper = new YthPunitShipmentDBHelper(context);
	}

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

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

	@Override
	public void deleteAllUserInfo() {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "delete from " + YthPunitShipmentDBHelper.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 " + YthPunitShipmentDBHelper.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")));
		bean.setRepertory_id(c.getLong(c.getColumnIndex("repertory_id")));
		return bean;
	}

}
