package com.mq.mylibrary.db.car;

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.LogTool;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.List;


/**
 * 创建人 mQ
 * 创建时间 2019/8/16 0016 10:30
 * 文件名称 CarDao
 * 说明:
 **/
public class CarDao extends BaseDao<CarEntity> {

    private CarDao() {
    }

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

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


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


    public boolean insertByJSONArray(SQLiteDatabase db, JSONArray array) {
        if (array == null || array.length() == 0) {
            LogTool.i("初始化: 没有车辆信息");
            return true;
        }
        try {
            for (int i = 0; i < array.length(); i++) {
                long flg = insertByJSONObject(db, array.getJSONObject(i));
                if (flg == -1)
                    return false;
            }
            return true;
        } catch (JSONException e) {
            e.printStackTrace();
            return false;
        }
    }

    public long insertByJSONObject(SQLiteDatabase db, JSONObject object) {
        CarEntity entity = new CarEntity();
        entity.setCar_id(object.optString("carId"));
        entity.setCar_number(object.optString("clbh"));
        entity.setWork_time(object.optString("zyrq"));
        return insert(db, entity);
    }

    public List<CarEntity> getList(SQLiteDatabase db) {
        Cursor cursor = getAll(db);

        return HappySQL.cursor2VOList(cursor, CarEntity.class);
    }

}
