package cn.flightfeather.thirdapp.common.database;
|
|
import android.content.Context;
|
import android.database.sqlite.SQLiteDatabase;
|
import android.os.Environment;
|
|
import com.ping.greendao.gen.ChangeAdviceDao;
|
import com.ping.greendao.gen.DaoMaster;
|
import com.ping.greendao.gen.DaoSession;
|
|
import org.greenrobot.greendao.query.QueryBuilder;
|
|
import java.io.File;
|
import java.util.List;
|
|
import cn.flightfeather.thirdapp.CommonApplication;
|
import cn.flightfeather.thirdapp.bean.ChangeAdvice;
|
import io.reactivex.Observable;
|
import io.reactivex.ObservableEmitter;
|
import io.reactivex.ObservableOnSubscribe;
|
import io.reactivex.schedulers.Schedulers;
|
|
public class GreenDaoDb implements DbSource {
|
|
private DaoSession daoSession;
|
private static GreenDaoDb greenDaoDb;
|
|
//<editor-fold desc="Constructor">
|
// FIXME: 2019/3/27 by riku 原来的代码将greendao数据库初始化放在了自定义Application类中,暂时直接调用之前的初始化
|
private GreenDaoDb(CommonApplication commonApplication) {
|
daoSession = commonApplication.getDaoSession();
|
}
|
|
private GreenDaoDb(Context context) {
|
File path = new File(Environment.getExternalStorageDirectory(), "FlightFeather/third_new.db");
|
path.getParentFile().mkdirs();
|
//创建数据库shop.db"
|
DaoMaster.DevOpenHelper helper = new DaoMaster.DevOpenHelper(context, path.getAbsolutePath(), null);
|
//获取可写数据库
|
SQLiteDatabase db = helper.getWritableDatabase();
|
//获取数据库对象
|
DaoMaster daoMaster = new DaoMaster(db);
|
|
QueryBuilder.LOG_SQL = true;
|
QueryBuilder.LOG_VALUES = true;
|
|
daoSession = daoMaster.newSession();
|
}
|
|
public static synchronized GreenDaoDb getInstance(CommonApplication commonApplication) {
|
if (greenDaoDb == null) {
|
greenDaoDb = new GreenDaoDb(commonApplication);
|
}
|
return greenDaoDb;
|
}
|
|
public static synchronized GreenDaoDb getInstance(Context context) {
|
if (greenDaoDb == null) {
|
greenDaoDb = new GreenDaoDb(context);
|
}
|
return greenDaoDb;
|
}
|
//</editor-fold>t
|
|
@Override
|
public Observable<List<ChangeAdvice>> getAdviceByProblemType(final String ptGuid) {
|
final ChangeAdviceDao adviceDao = daoSession.getChangeAdviceDao();
|
return Observable.create(new ObservableOnSubscribe<List<ChangeAdvice>>() {
|
@Override
|
public void subscribe(ObservableEmitter<List<ChangeAdvice>> emitter) throws Exception {
|
List<ChangeAdvice> adviceList = adviceDao.queryBuilder()
|
.where(
|
ChangeAdviceDao.Properties.AdProblemtypeguid
|
.eq(ptGuid)
|
).list();
|
emitter.onNext(adviceList);
|
emitter.onComplete();
|
}
|
}).subscribeOn(Schedulers.io());
|
}
|
}
|