package com.mq.zhicang.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 ZhiCangDao extends BaseDao<ZhiCangEntity> {

    private ZhiCangDao() {
    }

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

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

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

    public List<ZhiCangEntity> 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, ZhiCangEntity.class);
    }

    public List<ZhiCangEntity> 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, ZhiCangEntity.class);
    }
}
