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

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.CommonDBHelper;
import cn.net.caoyao.cpzshandset.db.presenter.base.SheepDealerDalImpl;
import cn.net.cpzslibs.prot.handset.bean.res.DealerBean;
import cn.net.cpzslibs.prot.utils.StrUtils;

public class CommonDealerDal extends SheepDealerDalImpl{
	private CommonDBHelper helper;
	public CommonDealerDal(Context context) {
		helper = new CommonDBHelper(context);
	}

	@Override
	public void insertDealer(DealerBean b) {
		if(b == null) return;
		Py4j py4j = new Py4j();
		SQLiteDatabase db = helper.getWritableDatabase();
		String namePY = (StrUtils.isEmpty(b.getName()) ? "" :py4j.getPreLetterPY(b.getName()));
		String insertSql = "REPLACE INTO " + CommonDBHelper.tb_dealer_info+"(id,province,market,name,namePY,phone,code,remark,createTime,area_code,status) VALUES (?,?,?,?,?,?,?,?,?,?,?)";
		db.execSQL(insertSql,new Object[]{b.getId(),b.getProvince(),b.getMarket(),b.getName(),namePY,b.getPhone(),b.getCode(),b.getRemark(),b.getCreatetime(),b.getArea_code(),b.getStatus()});
		py4j = null;
		db.close();
	}

	@Override
	public void insertDealerList(List<DealerBean> dealers) {
		if(dealers == null) return;
		Py4j py4j = new Py4j();
		SQLiteDatabase db = helper.getWritableDatabase();
		String insertSql = "REPLACE INTO " + CommonDBHelper.tb_dealer_info+"(id,province,market,name,namePY,phone,code,remark,createTime,area_code,status) VALUES (?,?,?,?,?,?,?,?,?,?,?)";
		for (DealerBean b : dealers) {
			String namePY = (StrUtils.isEmpty(b.getName()) ? "" :py4j.getPreLetterPY(b.getName()));
			db.execSQL(insertSql,new Object[]{b.getId(),b.getProvince(),b.getMarket(),b.getName(),namePY,b.getPhone(),b.getCode(),b.getRemark(),b.getCreatetime(),b.getArea_code(),b.getStatus()});
		}
		py4j = null;
		db.close();
	}

	@Override
	public void deleteAllDealer() {
		SQLiteDatabase db = helper.getWritableDatabase();
		String delSql = "DELETE FROM " + CommonDBHelper.tb_dealer_info;
		db.execSQL(delSql);
		db.close();
	}

	@Override
	public DealerBean queryDealerByCode(String code) {
//		LogUtil.e(TConfig.Echo, "查询条件：" + code);
		code = code.toUpperCase();
		SQLiteDatabase db = helper.getReadableDatabase();
		String querySql = "SELECT * FROM " + CommonDBHelper.tb_dealer_info+" WHERE code=?";
		Cursor c = db.rawQuery(querySql, new String[]{code});
		DealerBean b = null;
		if (c.moveToNext()) {
			b = getDealerBean(c);
		}
		c.close();
		db.close();
		return b;
	}
	@Override
	public DealerBean queryDealerById(long id) {
		SQLiteDatabase db = helper.getReadableDatabase();
		String querySql = "SELECT * FROM " + CommonDBHelper.tb_dealer_info+" WHERE id=?";
		Cursor c = db.rawQuery(querySql, new String[]{String.valueOf(id)});
		DealerBean b = null;
		if (c.moveToNext()) {
			b = getDealerBean(c);
		}
		return b;
	}
	@Override
	public List<DealerBean> searchDealerByCondition(String condition) {
		condition = condition.toUpperCase();
		SQLiteDatabase db = helper.getReadableDatabase();
		List<DealerBean> list = new ArrayList<>();
		String sql = "select * from " + CommonDBHelper.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 List<DealerBean> queryAllDealer() {
		SQLiteDatabase db = helper.getReadableDatabase();
		List<DealerBean> list = new ArrayList<>();
		String sql = "select * from " + CommonDBHelper.tb_dealer_info;
		Cursor c = db.rawQuery(sql, null);
		while (c.moveToNext()) {
			list.add(getDealerBean(c));
		}
		c.close();
		db.close();
		return list;
	}


	@Override
	public DealerBean getDealerBean(Cursor c) {
		DealerBean b = new DealerBean();
		b.setId(c.getInt(c.getColumnIndex("id")));
		b.setProvince(c.getString(c.getColumnIndex("province")));
		b.setMarket(c.getString(c.getColumnIndex("market")));
		b.setName(c.getString(c.getColumnIndex("name")));
		b.setPhone(c.getString(c.getColumnIndex("phone")));
		b.setCode(c.getString(c.getColumnIndex("code")));
		b.setRemark(c.getString(c.getColumnIndex("remark")));
		b.setCreatetime(c.getString(c.getColumnIndex("createTime")));
		b.setArea_code(c.getInt(c.getColumnIndex("area_code")));
		b.setStatus(c.getInt(c.getColumnIndex("status")));
		return b;
	}

	
}
