package hf.lskj.com.carstockcount.db;

import java.util.List;
import java.util.ArrayList;
import android.database.Cursor;
import android.database.sqlite.SQLiteStatement;

import org.greenrobot.greendao.AbstractDao;
import org.greenrobot.greendao.Property;
import org.greenrobot.greendao.internal.SqlUtils;
import org.greenrobot.greendao.internal.DaoConfig;
import org.greenrobot.greendao.database.Database;
import org.greenrobot.greendao.database.DatabaseStatement;
import org.greenrobot.greendao.query.Query;
import org.greenrobot.greendao.query.QueryBuilder;

// THIS CODE IS GENERATED BY greenDAO, DO NOT EDIT.
/** 
 * DAO for table "RECORD".
*/
public class RecordDao extends AbstractDao<Record, Long> {

    public static final String TABLENAME = "RECORD";

    /**
     * Properties of entity Record.<br/>
     * Can be used for QueryBuilder and for referencing column names.
     */
    public static class Properties {
        public final static Property Id = new Property(0, Long.class, "id", true, "_id");
        public final static Property Rfid = new Property(1, String.class, "rfid", false, "RFID");
        public final static Property Vin = new Property(2, String.class, "vin", false, "VIN");
        public final static Property Status = new Property(3, String.class, "status", false, "STATUS");
        public final static Property Remark = new Property(4, String.class, "remark", false, "REMARK");
        public final static Property CreateTime = new Property(5, String.class, "createTime", false, "CREATE_TIME");
        public final static Property OperatorId = new Property(6, String.class, "operatorId", false, "OPERATOR_ID");
        public final static Property Type = new Property(7, String.class, "type", false, "TYPE");
        public final static Property StoreId = new Property(8, String.class, "storeId", false, "STORE_ID");
        public final static Property Color = new Property(9, String.class, "color", false, "COLOR");
        public final static Property CarType = new Property(10, String.class, "carType", false, "CAR_TYPE");
    }

    private DaoSession daoSession;

    private Query<Record> carInfo_RecordsQuery;

    public RecordDao(DaoConfig config) {
        super(config);
    }
    
    public RecordDao(DaoConfig config, DaoSession daoSession) {
        super(config, daoSession);
        this.daoSession = daoSession;
    }

    /** Creates the underlying database table. */
    public static void createTable(Database db, boolean ifNotExists) {
        String constraint = ifNotExists? "IF NOT EXISTS ": "";
        db.execSQL("CREATE TABLE " + constraint + "\"RECORD\" (" + //
                "\"_id\" INTEGER PRIMARY KEY ," + // 0: id
                "\"RFID\" TEXT," + // 1: rfid
                "\"VIN\" TEXT," + // 2: vin
                "\"STATUS\" TEXT," + // 3: status
                "\"REMARK\" TEXT," + // 4: remark
                "\"CREATE_TIME\" TEXT," + // 5: createTime
                "\"OPERATOR_ID\" TEXT," + // 6: operatorId
                "\"TYPE\" TEXT," + // 7: type
                "\"STORE_ID\" TEXT," + // 8: storeId
                "\"COLOR\" TEXT," + // 9: color
                "\"CAR_TYPE\" TEXT);"); // 10: carType
    }

    /** Drops the underlying database table. */
    public static void dropTable(Database db, boolean ifExists) {
        String sql = "DROP TABLE " + (ifExists ? "IF EXISTS " : "") + "\"RECORD\"";
        db.execSQL(sql);
    }

    @Override
    protected final void bindValues(DatabaseStatement stmt, Record entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        String rfid = entity.getRfid();
        if (rfid != null) {
            stmt.bindString(2, rfid);
        }
 
        String vin = entity.getVin();
        if (vin != null) {
            stmt.bindString(3, vin);
        }
 
        String status = entity.getStatus();
        if (status != null) {
            stmt.bindString(4, status);
        }
 
        String remark = entity.getRemark();
        if (remark != null) {
            stmt.bindString(5, remark);
        }
 
        String createTime = entity.getCreateTime();
        if (createTime != null) {
            stmt.bindString(6, createTime);
        }
 
        String operatorId = entity.getOperatorId();
        if (operatorId != null) {
            stmt.bindString(7, operatorId);
        }
 
        String type = entity.getType();
        if (type != null) {
            stmt.bindString(8, type);
        }
 
        String storeId = entity.getStoreId();
        if (storeId != null) {
            stmt.bindString(9, storeId);
        }
 
        String color = entity.getColor();
        if (color != null) {
            stmt.bindString(10, color);
        }
 
        String carType = entity.getCarType();
        if (carType != null) {
            stmt.bindString(11, carType);
        }
    }

    @Override
    protected final void bindValues(SQLiteStatement stmt, Record entity) {
        stmt.clearBindings();
 
        Long id = entity.getId();
        if (id != null) {
            stmt.bindLong(1, id);
        }
 
        String rfid = entity.getRfid();
        if (rfid != null) {
            stmt.bindString(2, rfid);
        }
 
        String vin = entity.getVin();
        if (vin != null) {
            stmt.bindString(3, vin);
        }
 
        String status = entity.getStatus();
        if (status != null) {
            stmt.bindString(4, status);
        }
 
        String remark = entity.getRemark();
        if (remark != null) {
            stmt.bindString(5, remark);
        }
 
        String createTime = entity.getCreateTime();
        if (createTime != null) {
            stmt.bindString(6, createTime);
        }
 
        String operatorId = entity.getOperatorId();
        if (operatorId != null) {
            stmt.bindString(7, operatorId);
        }
 
        String type = entity.getType();
        if (type != null) {
            stmt.bindString(8, type);
        }
 
        String storeId = entity.getStoreId();
        if (storeId != null) {
            stmt.bindString(9, storeId);
        }
 
        String color = entity.getColor();
        if (color != null) {
            stmt.bindString(10, color);
        }
 
        String carType = entity.getCarType();
        if (carType != null) {
            stmt.bindString(11, carType);
        }
    }

    @Override
    protected final void attachEntity(Record entity) {
        super.attachEntity(entity);
        entity.__setDaoSession(daoSession);
    }

    @Override
    public Long readKey(Cursor cursor, int offset) {
        return cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0);
    }    

    @Override
    public Record readEntity(Cursor cursor, int offset) {
        Record entity = new Record( //
            cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0), // id
            cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1), // rfid
            cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2), // vin
            cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3), // status
            cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4), // remark
            cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5), // createTime
            cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6), // operatorId
            cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7), // type
            cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8), // storeId
            cursor.isNull(offset + 9) ? null : cursor.getString(offset + 9), // color
            cursor.isNull(offset + 10) ? null : cursor.getString(offset + 10) // carType
        );
        return entity;
    }
     
    @Override
    public void readEntity(Cursor cursor, Record entity, int offset) {
        entity.setId(cursor.isNull(offset + 0) ? null : cursor.getLong(offset + 0));
        entity.setRfid(cursor.isNull(offset + 1) ? null : cursor.getString(offset + 1));
        entity.setVin(cursor.isNull(offset + 2) ? null : cursor.getString(offset + 2));
        entity.setStatus(cursor.isNull(offset + 3) ? null : cursor.getString(offset + 3));
        entity.setRemark(cursor.isNull(offset + 4) ? null : cursor.getString(offset + 4));
        entity.setCreateTime(cursor.isNull(offset + 5) ? null : cursor.getString(offset + 5));
        entity.setOperatorId(cursor.isNull(offset + 6) ? null : cursor.getString(offset + 6));
        entity.setType(cursor.isNull(offset + 7) ? null : cursor.getString(offset + 7));
        entity.setStoreId(cursor.isNull(offset + 8) ? null : cursor.getString(offset + 8));
        entity.setColor(cursor.isNull(offset + 9) ? null : cursor.getString(offset + 9));
        entity.setCarType(cursor.isNull(offset + 10) ? null : cursor.getString(offset + 10));
     }
    
    @Override
    protected final Long updateKeyAfterInsert(Record entity, long rowId) {
        entity.setId(rowId);
        return rowId;
    }
    
    @Override
    public Long getKey(Record entity) {
        if(entity != null) {
            return entity.getId();
        } else {
            return null;
        }
    }

    @Override
    public boolean hasKey(Record entity) {
        return entity.getId() != null;
    }

    @Override
    protected final boolean isEntityUpdateable() {
        return true;
    }
    
    /** Internal query to resolve the "records" to-many relationship of CarInfo. */
    public List<Record> _queryCarInfo_Records(String vin) {
        synchronized (this) {
            if (carInfo_RecordsQuery == null) {
                QueryBuilder<Record> queryBuilder = queryBuilder();
                queryBuilder.where(Properties.Vin.eq(null));
                queryBuilder.orderRaw("T.'CREATE_TIME' DESC");
                carInfo_RecordsQuery = queryBuilder.build();
            }
        }
        Query<Record> query = carInfo_RecordsQuery.forCurrentThread();
        query.setParameter(0, vin);
        return query.list();
    }

    private String selectDeep;

    protected String getSelectDeep() {
        if (selectDeep == null) {
            StringBuilder builder = new StringBuilder("SELECT ");
            SqlUtils.appendColumns(builder, "T", getAllColumns());
            builder.append(',');
            SqlUtils.appendColumns(builder, "T0", daoSession.getCarInfoDao().getAllColumns());
            builder.append(" FROM RECORD T");
            builder.append(" LEFT JOIN CAR_INFO T0 ON T.\"VIN\"=T0.\"VIN\"");
            builder.append(' ');
            selectDeep = builder.toString();
        }
        return selectDeep;
    }
    
    protected Record loadCurrentDeep(Cursor cursor, boolean lock) {
        Record entity = loadCurrent(cursor, 0, lock);
        int offset = getAllColumns().length;

        CarInfo carInfo = loadCurrentOther(daoSession.getCarInfoDao(), cursor, offset);
        entity.setCarInfo(carInfo);

        return entity;    
    }

    public Record loadDeep(Long key) {
        assertSinglePk();
        if (key == null) {
            return null;
        }

        StringBuilder builder = new StringBuilder(getSelectDeep());
        builder.append("WHERE ");
        SqlUtils.appendColumnsEqValue(builder, "T", getPkColumns());
        String sql = builder.toString();
        
        String[] keyArray = new String[] { key.toString() };
        Cursor cursor = db.rawQuery(sql, keyArray);
        
        try {
            boolean available = cursor.moveToFirst();
            if (!available) {
                return null;
            } else if (!cursor.isLast()) {
                throw new IllegalStateException("Expected unique result, but count was " + cursor.getCount());
            }
            return loadCurrentDeep(cursor, true);
        } finally {
            cursor.close();
        }
    }
    
    /** Reads all available rows from the given cursor and returns a list of new ImageTO objects. */
    public List<Record> loadAllDeepFromCursor(Cursor cursor) {
        int count = cursor.getCount();
        List<Record> list = new ArrayList<Record>(count);
        
        if (cursor.moveToFirst()) {
            if (identityScope != null) {
                identityScope.lock();
                identityScope.reserveRoom(count);
            }
            try {
                do {
                    list.add(loadCurrentDeep(cursor, false));
                } while (cursor.moveToNext());
            } finally {
                if (identityScope != null) {
                    identityScope.unlock();
                }
            }
        }
        return list;
    }
    
    protected List<Record> loadDeepAllAndCloseCursor(Cursor cursor) {
        try {
            return loadAllDeepFromCursor(cursor);
        } finally {
            cursor.close();
        }
    }
    

    /** A raw-style query where you can pass any WHERE clause and arguments. */
    public List<Record> queryDeep(String where, String... selectionArg) {
        Cursor cursor = db.rawQuery(getSelectDeep() + where, selectionArg);
        return loadDeepAllAndCloseCursor(cursor);
    }
 
}
