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

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.LiqueurSFYDBHelper;
import cn.net.caoyao.cpzshandset.db.presenter.base.UserLoginDalImpl;
import cn.net.cpzslibs.prot.handset.bean.res.ResponsibleUserInfoBean;

public class LiqueurSFYUserInfoDal extends UserLoginDalImpl{
	
	private LiqueurSFYDBHelper helper;
	public LiqueurSFYUserInfoDal(Context context) {
		helper = new LiqueurSFYDBHelper(context);
	}

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

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

	@Override
	public void deleteAllUserInfo() {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "delete from " + LiqueurSFYDBHelper.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 " + LiqueurSFYDBHelper.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 List<ResponsibleUserInfoBean> queryAllUser() {
		List<ResponsibleUserInfoBean> l = new ArrayList<>();
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "select * from " + LiqueurSFYDBHelper.tb_responsible_person +" where userType=?";
		Cursor c = db.rawQuery(sql, new String[]{"2"});
		while (c.moveToNext()) {
			l.add(getUserInfo(c));
		}
		c.close();
		db.close();
		return l;
	}

	@Override
	public List<ResponsibleUserInfoBean> searchUserByCondition(String condition) {
		condition = condition.toUpperCase();
		List<ResponsibleUserInfoBean> l = new ArrayList<>();
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "select * from " + LiqueurSFYDBHelper.tb_responsible_person +" where userType=2 AND (username LIKE '%"+condition+"%' OR usernamePY LIKE '%"+condition+"%')";
		Cursor c = db.rawQuery(sql, null);
		while (c.moveToNext()) {
			l.add(getUserInfo(c));
		}
		c.close();
		db.close();
		return l;
	}

	@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;
	}

	

}
