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

import java.util.List;

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

public class YthScannerUserInfoDal extends UserLoginDalImpl{
	
	private YthScannerShipmentDBHelper helper;
	public YthScannerUserInfoDal(Context context) {
		helper = new YthScannerShipmentDBHelper(context);
	}

	@Override
	public void insertUserInfo(ResponsibleUserInfoBean info) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "replace into " + YthScannerShipmentDBHelper.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 " + YthScannerShipmentDBHelper.tb_responsible_person +" (account,pwd,username,tel,userType) values (?,?,?,?,?)";
		for (ResponsibleUserInfoBean info : infos) {
			db.execSQL(sql, new Object[]{info.getAccount(),info.getPwd(),info.getUsername(),info.getTel(),info.getUserType()});
		}
		db.close();
	}

	@Override
	public void deleteAllUserInfo() {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "delete from " + YthScannerShipmentDBHelper.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 " + YthScannerShipmentDBHelper.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;
	}

}
