package com.mq.xiezaixiaoka.db;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.mq.mylibrary.db.BaseDao;
import com.mq.mylibrary.tool.HappySQL;
import com.mq.mylibrary.tool.StringTool;

import java.util.List;

/**
 * 创建人 mQ
 * 创建时间 2019/8/14 0014 16:51
 * 文件名称 ZhiCangDao
 * 说明:
 **/
public class XieZaiXiaoKaDao extends BaseDao<XieZaiXiaoKaEntity> {

    private XieZaiXiaoKaDao() {
    }

    private static class SingleHelper {
        private static final XieZaiXiaoKaDao INSTANCE = new XieZaiXiaoKaDao();
    }

    public static XieZaiXiaoKaDao get() {
        return SingleHelper.INSTANCE;
    }

    @Override
    public String getTableName() {
        return "xie_zai_xiao_ka";
    }

    public List<XieZaiXiaoKaEntity> getListByCar(SQLiteDatabase db, String car) {
        String sql = null;
        if (StringTool.isEmpty(car)) {
            sql = "select * from " + getTableName() + " order by start_time desc";
        } else {
            sql = "select * from " + getTableName() +
                    " where car = '" + car + "' order by start_time desc";
        }
        Cursor cursor = db.rawQuery(sql, null);
        return HappySQL.cursor2VOList(cursor, XieZaiXiaoKaEntity.class);
    }

    public List<XieZaiXiaoKaEntity> getCarList(SQLiteDatabase db) {
        String sql = "SELECT  * FROM " + getTableName() + " group by car order by start_time desc";
        Cursor cursor = db.rawQuery(sql, null);
        return HappySQL.cursor2VOList(cursor, XieZaiXiaoKaEntity.class);
    }

    public List<XieZaiXiaoKaEntity> getList(SQLiteDatabase db, String start, String end) {
        String sql;
        if (start == null) {
            sql = "select * from " + getTableName() + " order by start_time desc";
        } else {
            String startDate = start + " 00:00:00";
            String endDate = end + " 23:59:59";
            sql = "SELECT  * FROM " + getTableName() +
                    " where strftime('%s',start_time) >= strftime('%s','" + startDate + "')" +
                    " and strftime('%s',end_time) <= strftime('%s','" + endDate + "')" +
                    " order by start_time desc";
        }
        Cursor cursor = db.rawQuery(sql, null);
        return HappySQL.cursor2VOList(cursor, XieZaiXiaoKaEntity.class);
    }

}
