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

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.UploadBean;
import cn.net.caoyao.cpzshandset.common.config.Config;
import cn.net.caoyao.cpzshandset.db.CounterDBHelper;
import cn.net.caoyao.cpzshandset.db.presenter.base.UploadStatusDalImpl;

public class CounterUploadStatusDal extends UploadStatusDalImpl{
	
	private CounterDBHelper helper;
	
	public CounterUploadStatusDal(Context context) {
		helper = new CounterDBHelper(context);
	}

	@Override
	public void insertUploadStatus(UploadBean b) {
		SQLiteDatabase db = helper.getWritableDatabase();
		String insertSql = "insert into " + CounterDBHelper.tb_upload_status +" (option_type,label_count,status,error_code) values (?,?,?,?)";
		db.execSQL(insertSql, new Object[]{b.getOption_type(),b.getLabel_count(),b.getStatus(),b.getError_code()});
		//测试需要自己插入数据时使用
//		String insertSql = "insert into " + CounterDBHelper.tb_upload_status +" (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 " + CounterDBHelper.tb_upload_status +" where upload_date < ?";
		db.execSQL(delSql, new Object[]{date});
		db.close();
	}

	@Override
	public List<UploadBean> queryAllUploadStatus(String date) {
		List<UploadBean> list = new ArrayList<>();
		SQLiteDatabase db = helper.getReadableDatabase();
		String rawSql = "select * from " + CounterDBHelper.tb_upload_status;
		Cursor c = db.rawQuery(rawSql, null);
		while (c.moveToNext()) {
			list.add(getUploadBean(c));
		}
		c.close();
		db.close();
		return list;
	}

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

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

	@Override
	public UploadBean getUploadBean(Cursor c) {
		UploadBean b = new UploadBean();
		b.setId(c.getInt(c.getColumnIndex("id")));
		b.setOption_type(c.getInt(c.getColumnIndex("option_type")));
		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 " + CounterDBHelper.tb_upload_status;
		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 " + CounterDBHelper.tb_upload_status +" WHERE upload_date> date('now','localtime','-7 day') AND status=0 order by upload_date desc";
		Cursor c = db.rawQuery(sql, null);
		while (c.moveToNext()) {
			dates.add(c.getString(0));
		}
		c.close();
		db.close();
		
		
		/*List<UploadBean> dates2 = new ArrayList<>();
		String sql2 = "SELECT * FROM " + CounterDBHelper.tb_upload_status +" order by upload_date desc";
		Cursor c2 = db.rawQuery(sql2, null);
		while (c2.moveToNext()) {
			dates2.add(getUploadBean(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 " + CounterDBHelper.tb_upload_status +" 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;
	}
	
	@Override
	public void insertErrorUploadStatus(int option_type, int error_code) {
		SQLiteDatabase db = helper.getWritableDatabase();
		//删除之前的错误码
		String delSql = "delete from " + CounterDBHelper.tb_upload_status +" WHERE option_type=? AND status=?";
		db.execSQL(delSql, new Object[]{option_type,Config.SUBMIT_STATUS_SUBMIT_FAIL});
		//插入新的错误码
		String insertSql = "insert into " + CounterDBHelper.tb_upload_status +" (option_type,label_count,status,error_code) values (?,?,?,?)";
		db.execSQL(insertSql, new Object[]{option_type,0,Config.SUBMIT_STATUS_SUBMIT_FAIL,error_code});
		
		db.close();
	}
	@Override
	public UploadBean queryErrorUploadByOptionType(int option_type) {
		SQLiteDatabase db = helper.getReadableDatabase();
		UploadBean bean = null;
		String sql = "select * from " + CounterDBHelper.tb_upload_status +" WHERE option_type=? AND status=?";
		Cursor c = db.rawQuery(sql, new String[]{String.valueOf(option_type),String.valueOf(Config.SUBMIT_STATUS_SUBMIT_FAIL)});
		if (c.moveToNext()) {
			bean = getUploadBean(c);
		}
		c.close();
		db.close();
		return bean;
	}

	@Override
	public void deleteErrorUploadStatusByOptionType(int option_type) {
		SQLiteDatabase db = helper.getWritableDatabase();
		//删除之前的错误码
		String delSql = "delete from " + CounterDBHelper.tb_upload_status +" WHERE option_type=? AND status=?";
		db.execSQL(delSql, new Object[]{option_type,Config.SUBMIT_STATUS_SUBMIT_FAIL});
		db.close();
		
	}
	
}
