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

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.bean.ShipmentUploadRecordBean;
import cn.net.caoyao.cpzshandset.db.YthPunitShipmentDBHelper;
import cn.net.caoyao.cpzshandset.db.presenter.base.ShipmentUploadStatusDalImpl;

public class YthPunitShipmentUploadStatusDal extends ShipmentUploadStatusDalImpl{
	
	private YthPunitShipmentDBHelper helper;
	
	public YthPunitShipmentUploadStatusDal(Context context) {
		helper = new YthPunitShipmentDBHelper(context);
	}
	
	@Override
	public void insertUploadStatus(ShipmentUploadRecordBean b) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String insertSql = "insert into " + YthPunitShipmentDBHelper.tb_shipment_upload_record +" (option_type,account,driver_id,order_id,driver_name,driver_tel,car_no,order_no,product_id,product_name,label_count,status,error_code) values (?,?,?,?,?,?,?,?,?,?,?,?,?)";
		db.execSQL(insertSql, new Object[]{b.getOption_type(),b.getAccount(),b.getDriver_id(),b.getOrder_id(),b.getDriver_name(),b.getDriver_tel(),b.getCar_no(),b.getOrder_no(),b.getProduct_id(),b.getProduct_name(),b.getLabel_count(),b.getStatus(),b.getError_code()});
		//测试需要自己插入数据时使用
//		String insertSql = "insert into " + YthPunitShipmentDBHelper.tb_shipment_upload_record +" (option_type,label_count,status,error_code,upload_date) values (?,?,?,?,?)";
//		db.execSQL(insertSql, new Object[]{b.getOption_type(),b.getLabel_count(),b.getStatus(),b.getError_code(),b.getUpload_date()});
		db.close();
	}

	@Override
	public void deleteUploadStatusByDate(String date) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String delSql = "delete from " + YthPunitShipmentDBHelper.tb_shipment_upload_record +" where upload_date < ?";
		db.execSQL(delSql, new Object[]{date});
		db.close();
	}

	@Override
	public List<ShipmentUploadRecordBean> queryAllUploadStatus(String date) {
		List<ShipmentUploadRecordBean> list = new ArrayList<>();
		SQLiteDatabase db = helper.getReadableDatabase();
		String rawSql = "select * from " + YthPunitShipmentDBHelper.tb_shipment_upload_record +" where upload_date=?";
		Cursor c = db.rawQuery(rawSql, new String[]{date});
		while (c.moveToNext()) {
			list.add(getShipmentUploadRecordBean(c));
		}
		c.close();
		db.close();
		return list;
	}

	@Override
	public void delete7DaysBefore() {
		SQLiteDatabase db = helper.getWritableDatabase();
		String delSql = "delete from " + YthPunitShipmentDBHelper.tb_shipment_upload_record +" where upload_date<=date('now','localtime','-7 day')";
		db.execSQL(delSql);
		db.close();
	}

	@Override
	public List<ShipmentUploadRecordBean> queryUploadStatusFor7Days() {
		List<ShipmentUploadRecordBean> list = new ArrayList<>();
		SQLiteDatabase db = helper.getReadableDatabase();
		String rawSql = "select * from " + YthPunitShipmentDBHelper.tb_shipment_upload_record +" where upload_date> date('now','localtime','-7 day')";
		Cursor c = db.rawQuery(rawSql, null);
		while (c.moveToNext()) {
			list.add(getShipmentUploadRecordBean(c));
		}
		c.close();
		db.close();
		return list;
	}

	@Override
	public ShipmentUploadRecordBean getShipmentUploadRecordBean(Cursor c) {
		ShipmentUploadRecordBean b = new ShipmentUploadRecordBean();
		b.setId(c.getLong(c.getColumnIndex("id")));
		b.setOption_type(c.getInt(c.getColumnIndex("option_type")));
		b.setAccount(c.getString(c.getColumnIndex("account")));
		b.setDriver_id(c.getLong(c.getColumnIndex("driver_id")));
		b.setOrder_id(c.getLong(c.getColumnIndex("order_id")));
		b.setDriver_name(c.getString(c.getColumnIndex("driver_name")));
		b.setDriver_tel(c.getString(c.getColumnIndex("driver_tel")));
		b.setCar_no(c.getString(c.getColumnIndex("car_no")));
		b.setOrder_no(c.getString(c.getColumnIndex("order_no")));
		b.setProduct_id(c.getLong(c.getColumnIndex("product_id")));
		b.setProduct_name(c.getString(c.getColumnIndex("product_name")));
		b.setLabel_count(c.getInt(c.getColumnIndex("label_count")));
		b.setStatus(c.getInt(c.getColumnIndex("status")));
		b.setError_code(c.getInt(c.getColumnIndex("error_code")));
		b.setUpload_date(c.getString(c.getColumnIndex("upload_date")));
		return b;
	}

	@Override
	public void deleteAllUploadStatus() {
		SQLiteDatabase db = helper.getWritableDatabase();
		String delSql = "delete from " + YthPunitShipmentDBHelper.tb_shipment_upload_record;
		db.execSQL(delSql);
		db.close();
		
	}

	@Override
	public List<String> queryUploadDateFor7Days() {
		List<String> dates = new ArrayList<>();
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "SELECT DISTINCT upload_date FROM " + YthPunitShipmentDBHelper.tb_shipment_upload_record +" WHERE upload_date> date('now','localtime','-7 day') order by upload_date desc";
		Cursor c = db.rawQuery(sql, null);
		while (c.moveToNext()) {
			dates.add(c.getString(0));
		}
		c.close();
		db.close();
		
		
		/*List<ShipmentUploadRecordBean> dates2 = new ArrayList<>();
		String sql2 = "SELECT * FROM " + YthPunitShipmentDBHelper.tb_shipment_upload_record +" order by upload_date desc";
		Cursor c2 = db.rawQuery(sql2, null);
		while (c2.moveToNext()) {
			dates2.add(getShipmentUploadRecordBean(c2));
		}
		c2.close();
		
		LogUtil.e(TConfig.Echo, "所有数据：" + dates2.toString());
		db.close();*/
		return dates;
	}

	@Override
	public int queryLabelCountByOptionAndDate(int option_type, String date) {
		int count = 0;
		SQLiteDatabase db = helper.getReadableDatabase();
		String sql = "SELECT SUM(label_count) FROM " + YthPunitShipmentDBHelper.tb_shipment_upload_record +" WHERE option_type=? AND upload_date=?";
		Cursor c = db.rawQuery(sql, new String[]{String.valueOf(option_type),date});
		if (c.moveToNext()) {
			count = c.getInt(0);
		}
		c.close();
		db.close();
		return count;
	}
	
}
