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

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.bean.DealerInfoBean;
import cn.net.caoyao.cpzshandset.db.LiqueurDBHelper;
import cn.net.caoyao.cpzshandset.db.presenter.base.DealerDalImpl;
import cn.net.cpzslibs.prot.handset.bean.res.LiqueurDealerBean;
import cn.net.cpzslibs.prot.utils.StrUtils;

public class LiqueurDealerDal extends DealerDalImpl{
	private LiqueurDBHelper helper;
	public LiqueurDealerDal(Context context) {
		helper = new LiqueurDBHelper(context);
	}

	@Override
	public void insertLiqueurDealer(LiqueurDealerBean b) {
		SQLiteDatabase db = helper.getWritableDatabase();
		Py4j py4j = new Py4j();
		String sql = "replace into " + LiqueurDBHelper.tb_dealer_info+" (id,name,namePY,contacter,contacterPY,contact_phone,remark,sale_area,status) values (?,?,?,?,?,?,?,?,?)";
		String namePY = (StrUtils.isEmpty(b.getName()) ? "" :py4j.getPreLetterPY(b.getName()));
		String contacterPY = (StrUtils.isEmpty(b.getContacter()) ? "":py4j.getPreLetterPY(b.getContacter()));
		db.execSQL(sql, new Object[]{b.getId(),b.getName(),namePY,b.getContacter(),contacterPY,b.getContacter_phone(),b.getRemark(),b.getSales_area(),b.getStatus()});
		py4j = null;
		db.close();
	}

	@Override
	public void insertLiqueurDealerList(List<LiqueurDealerBean> dealers) {
		SQLiteDatabase db = helper.getWritableDatabase();
		Py4j py4j = new Py4j();
		String sql = "replace into " + LiqueurDBHelper.tb_dealer_info+" (id,name,namePY,contacter,contacterPY,contact_phone,remark,sale_area,status) values (?,?,?,?,?,?,?,?,?)";
		
		for (LiqueurDealerBean b : dealers) {
			String namePY = (StrUtils.isEmpty(b.getName()) ? "" :py4j.getPreLetterPY(b.getName()));
			String contacterPY = (StrUtils.isEmpty(b.getContacter()) ? "":py4j.getPreLetterPY(b.getContacter()));
			db.execSQL(sql, new Object[]{b.getId(),b.getName(),namePY,b.getContacter(),contacterPY,b.getContacter_phone(),b.getRemark(),b.getSales_area(),b.getStatus()});
		}
		
		py4j = null;
		db.close();		
	}

	@Override
	public void deleteAllDealer() {
		SQLiteDatabase db = helper.getWritableDatabase();
		String sql = "delete from " + LiqueurDBHelper.tb_dealer_info;
		db.execSQL(sql);
		db.close();
	}


	@Override
	public DealerInfoBean queryDealerById(long id) {
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "select * from " + LiqueurDBHelper.tb_dealer_info +" where id=?";
		Cursor c = db.rawQuery(sql, new String[]{String.valueOf(id)});
		DealerInfoBean b = null;
		if (c.moveToNext()) {
			b = getDealerBean(c);
		}
		c.close();
		db.close();
		return b;
	}

	@Override
	public List<DealerInfoBean> queryAllDealer() {
		SQLiteDatabase db = helper.getReadableDatabase();
		List<DealerInfoBean> list = new ArrayList<>();
		String sql = "select * from " + LiqueurDBHelper.tb_dealer_info;
		Cursor c = db.rawQuery(sql, null);
		while (c.moveToNext()) {
			list.add(getDealerBean(c));
		}
		c.close();
		db.close();
		return list;
	}

	@Override
	public List<DealerInfoBean> searchDealerByCondition(String condition) {
		condition = condition.toUpperCase();
//		LogUtil.e(TConfig.Echo, "查询条件：" + condition);
		SQLiteDatabase db = helper.getReadableDatabase();
		List<DealerInfoBean> list = new ArrayList<>();
//		String sql = "select * from " + LiqueurDBHelper.tb_dealer_info +" where name LIKE '%"+condition+"%' OR namePY LIKE '%"+condition+"%' OR contacter LIKE '%"+condition+"%' OR contacterPY LIKE '%"+condition+"%'";
		String sql = "select * from " + LiqueurDBHelper.tb_dealer_info +" where name LIKE '%"+condition+"%' OR namePY LIKE '%"+condition+"%'";
		Cursor c = db.rawQuery(sql, null);
		while (c.moveToNext()) {
			list.add(getDealerBean(c));
		}
		c.close();
		db.close();
		return list;
	}

	@Override
	public DealerInfoBean getDealerBean(Cursor c) {
		DealerInfoBean b = new DealerInfoBean();
		b.setId(c.getLong(c.getColumnIndex("id")));
		b.setName(c.getString(c.getColumnIndex("name")));
		b.setContacter(c.getString(c.getColumnIndex("contacter")));
		b.setContacter_phone(c.getString(c.getColumnIndex("contact_phone")));
		b.setRemark(c.getString(c.getColumnIndex("remark")));
		b.setSales_area(c.getInt(c.getColumnIndex("sale_area")));
		b.setStatus(c.getInt(c.getColumnIndex("status")));
		return b;
	}

}
