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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
package cn.flightfeather.thirdapp.repository
 
import cn.flightfeather.thirdapp.bean.entity.Inspection
import cn.flightfeather.thirdapp.bean.entity.Mediafile
import cn.flightfeather.thirdapp.bean.entity.Scense
import cn.flightfeather.thirdapp.bean.entity.Subtask
import cn.flightfeather.thirdapp.bean.vo.*
import cn.flightfeather.thirdapp.common.net.ResponseBodyCallBack
import cn.flightfeather.thirdapp.common.net.ResultCallBack
import cn.flightfeather.thirdapp.common.net.ResultObserver
import cn.flightfeather.thirdapp.common.net.RetrofitFactory
import cn.flightfeather.thirdapp.httpservice.InspectionService
import cn.flightfeather.thirdapp.httpservice.SubTaskService
import cn.flightfeather.thirdapp.httpservice.TaskService
import cn.flightfeather.thirdapp.repository.dao.MediaFileDao
import cn.flightfeather.thirdapp.repository.dao.SceneDao
import io.reactivex.Observable
import io.reactivex.functions.BiFunction
import okhttp3.ResponseBody
import retrofit2.Call
import retrofit2.Callback
import retrofit2.Response
 
/**
 * @author riku
 * Date: 2019/7/29
 * 巡查相关数据获取
 */
class InspectionRepository {
 
    val retrofit = RetrofitFactory.instance.retrofit
    private val mediaFileDao = MediaFileDao()
    private val sceneDao = SceneDao()
 
    /**
     * 根据用户及用户类型获取月任务信息;
     * 普通用户只能获取自己执行的任务
     * 管理员可获取所有任务
     */
    fun getMonthDayTask(date: String, executorID: String, userType: String, resultCallBack: ResultCallBack<ArrayList<TaskVo>>) {
        retrofit.create(TaskService::class.java).getMonthTask(executorID, date, userType)
                .enqueue(ResponseBodyCallBack(resultCallBack))
    }
 
    /**
     * 根据总任务id获取日任务
     */
    fun getDayTask(taskId: String, executorID: String, userType: String, resultCallBack: ResultCallBack<ArrayList<DayTaskProgressVo>>) {
        retrofit.create(TaskService::class.java).getDayTaskList(taskId, executorID, userType)
                .enqueue(ResponseBodyCallBack(resultCallBack))
    }
 
    /**
     * 根据日任务获取子任务
     */
    fun getSubTask(dayTaskId: String, executorID: String, userType: String, resultCallBack: ResultCallBack<ArrayList<Subtask>>) {
        retrofit.create(SubTaskService::class.java).findByDayTaskID(dayTaskId, executorID, userType)
                .enqueue(ResponseBodyCallBack(resultCallBack))
    }
 
    /**
     * 查询本地是否有图片未上传
     */
    fun checkUnUploadImage(resultCallBack: ResultCallBack<Boolean>) {
        val dbService = mediaFileDao.checkUnUploadImage().map {
            Response.success(it)
        }
 
        RetrofitFactory.executeResult(dbService, object : ResultObserver<Boolean>() {
            override fun onSuccess(result: Boolean?) {
                resultCallBack.onSuccess(result)
            }
 
            override fun onFailure(e: Throwable, isNetWorkError: Boolean) {
                resultCallBack.onFailure()
            }
 
        })
    }
 
    /**
     * 获取巡查信息
     */
    fun getInspectionData(subTaskId: String, resultCallBack: ResultCallBack<InspectionVo>) {
        retrofit.create(InspectionService::class.java).loadInspectionData(subTaskId)
                .enqueue(ResponseBodyCallBack(resultCallBack))
    }
 
    /**
     * 根据巡查id获取问题列表
     */
    fun getProblemList(inspectionId: String, resultCallBack: ResultCallBack<List<ProblemlistVo>>) {
        retrofit.create(InspectionService::class.java).loadProblemList(inspectionId)
                .enqueue(ResponseBodyCallBack(resultCallBack))
    }
 
    /**
     * 根据场景id和对应本次子任务id,查询该场景上一次子任务的所有问题
     * @param dateStr 格式为"yyyy-MM-dd HH:mm"
     */
    fun getLastProblemList(sceneId: String, dateStr: String, resultCallBack: ResultCallBack<LastSubtaskPack>) {
        retrofit.create(InspectionService::class.java).loadLastProblemList(sceneId, dateStr)
                .enqueue(ResponseBodyCallBack(resultCallBack))
    }
 
    /**
     * 更新子任务
     */
    fun updateSubTask(subTask: Subtask, resultCallBack: ResultCallBack<String>) {
        retrofit.create(InspectionService::class.java).updateSubTask(subTask)
                .enqueue(ResponseBodyCallBack(resultCallBack))
    }
 
    /**
     * 更新巡查信息
     */
    fun updateInspection(inspection: Inspection, resultCallBack: ResultCallBack<ResponseBody>) {
        retrofit.create(InspectionService::class.java).updateInspection(inspection)
                .enqueue(ResponseBodyCallBack(resultCallBack))
    }
 
    /**
     * 更新场景信息
     */
    fun updateScene(scene: Scense, resultCallBack: ResultCallBack<ResponseBody>) {
        retrofit.create(InspectionService::class.java).updateScense(scene)
                .enqueue(object : Callback<ResponseBody> {
                    override fun onFailure(p0: Call<ResponseBody>, p1: Throwable) {
                        resultCallBack?.onFailure()
                    }
 
                    override fun onResponse(p0: Call<ResponseBody>, p1: Response<ResponseBody>) {
                        if (p1.isSuccessful) {
                            sceneDao.update(scene)
                            resultCallBack?.onSuccess(p1.body())
                        } else {
                            resultCallBack?.onFailure()
                        }
                    }
                })
    }
 
    /**
     * 根据类型获取巡查中的图片
     */
    fun getMediaFile(inspectionId: String, businessType: Int,resultCallBack: ResultCallBack<ArrayList<Mediafile>>) {
        val dbService = mediaFileDao.getMediaFile(inspectionId, businessType)
 
        val mediaService =  retrofit.create(InspectionService::class.java).getMediaFileList(inspectionId, businessType)
 
        val zipService = Observable.zip(dbService, mediaService,
                BiFunction<List<Mediafile>, Response<List<Mediafile>>, Response<ArrayList<Mediafile>>> { t1, t2 ->
                    Response.success(ArrayList<Mediafile>().apply {
                        addAll(t1)
                        t2.body()?.let { addAll(it) }
                    })
                })
 
        RetrofitFactory.executeResult(zipService, object : ResultObserver<ArrayList<Mediafile>>() {
            override fun onSuccess(result: ArrayList<Mediafile>?) {
                resultCallBack.onSuccess(result)
            }
 
            override fun onFailure(e: Throwable, isNetWorkError: Boolean) {
                resultCallBack.onFailure()
            }
        })
    }
 
    /**
     * 获取技防措施
     */
    fun getGitList(inspectionId: String, resultCallBack: ResultCallBack<List<GitlistVo>>) {
        retrofit.create(InspectionService::class.java).loadGitList(inspectionId)
                .enqueue(ResponseBodyCallBack(resultCallBack))
    }
}