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

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

public class LiqueurSFYGroupInfoDal extends GroupInfoDalImpl{
	private LiqueurSFYDBHelper helper;
	public LiqueurSFYGroupInfoDal(Context context) {
		helper = new LiqueurSFYDBHelper(context);
	}

	@Override
	public void insertGroupInfoList(List<LiqueurGroupInfoBean> groupInfoBeans) {
		if(groupInfoBeans == null) return;
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "replace into "  + LiqueurSFYDBHelper.tb_group_info +" (id,name,status) values (?,?,?)";
		for (LiqueurGroupInfoBean b : groupInfoBeans) {
			db.execSQL(sql, new Object[]{b.getId(),b.getName(),b.getStatus()});
		}
		db.close();
	}

	@Override
	public List<LiqueurGroupInfoBean> queryAllGroupInfo() {
		SQLiteDatabase db = helper.getReadableDatabase();
		List<LiqueurGroupInfoBean> list = new ArrayList<>();
		String sql = "SELECT * FROM " + LiqueurSFYDBHelper.tb_group_info +" ORDER BY id ASC";
		Cursor c = db.rawQuery(sql, null);
		while (c.moveToNext()) {
			list.add(getGroupInfo(c));
		}
		c.close();
		db.close();
		return list;
	}

	@Override
	public LiqueurGroupInfoBean queryGroupInfoById(long id) {
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "SELECT * FROM " + LiqueurSFYDBHelper.tb_group_info +" WHERE id=?";
		Cursor c = db.rawQuery(sql, new String[]{String.valueOf(id)});
		LiqueurGroupInfoBean b = null;
		if (c.moveToNext()) {
			b = getGroupInfo(c);
		}
		return b;
	}

	@Override
	public LiqueurGroupInfoBean getGroupInfo(Cursor c) {
		LiqueurGroupInfoBean b = new LiqueurGroupInfoBean();
		b.setId(c.getLong(c.getColumnIndex("id")));
		b.setName(c.getString(c.getColumnIndex("name")));
		b.setStatus(c.getInt(c.getColumnIndex("status")));
		return b;
	}

	@Override
	public void deleteAllGroupInfo() {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "delete from " + LiqueurSFYDBHelper.tb_group_info;
		db.execSQL(sql);
		db.close();
	}

	@Override
	public void deleteGroupInfoById(long id) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "delete from " + LiqueurSFYDBHelper.tb_group_info + " WHERE id=?";
		db.execSQL(sql,new Object[]{id});
		db.close();
	}
	

}
