1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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());
    }
}