riku
2022-02-18 d59d55575d913646b7a90fca651904ab889c6723
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
package cn.flightfeather.thirdappmodule.repository
 
import cn.flightfeather.thirdappmodule.bean.entity.*
import cn.flightfeather.thirdappmodule.bean.vo.TaskVo
import cn.flightfeather.thirdappmodule.common.net.ResponseBodyCallBack
import cn.flightfeather.thirdappmodule.common.net.ResultCallBack
import cn.flightfeather.thirdappmodule.common.net.ResultObserver
import cn.flightfeather.thirdappmodule.common.net.RetrofitFactory
import cn.flightfeather.thirdappmodule.httpservice.MonitorObjectVersionService
import cn.flightfeather.thirdappmodule.httpservice.ScenseService
import cn.flightfeather.thirdappmodule.httpservice.SubTaskService
import cn.flightfeather.thirdappmodule.httpservice.TaskService
import cn.flightfeather.thirdappmodule.repository.dao.DomainDao
import cn.flightfeather.thirdappmodule.repository.dao.SceneDao
import io.reactivex.Observable
import okhttp3.ResponseBody
import retrofit2.Response
 
/**
 * @author riku
 * Date: 2019/7/22
 * 任务相关数据获取
 */
class TaskRepository {
    private val retrofit = RetrofitFactory.instance.retrofit
    private val sceneDao = SceneDao()
    private val domainDao = DomainDao()
 
 
    /**
     * 获取总任务
     * @param type 获取方式,0:所有;1:未完成的总任务
     */
    fun getTopClassTaskList(type: Int, resultCallBack: ResultCallBack<ArrayList<TaskVo>>) {
 
        // fixme: 2019/7/22 此处由于原代码的网络请求都使用了默认的回调方式,因此暂不集成RxJava
        retrofit.create(TaskService::class.java).getTopClassTaskList(type)
                .enqueue(ResponseBodyCallBack(resultCallBack))
    }
 
    fun getLastTopClassTask(task: Task, resultCallBack: ResultCallBack<Task>) {
        retrofit.create(TaskService::class.java).getLastTopClassTask(task)
                .enqueue(ResponseBodyCallBack(resultCallBack))
    }
 
    /**
     * 获取监管版本
     * @param tGuid 总任务id
     */
    fun getMOVList(tGuid: String, resultCallBack: ResultCallBack<ArrayList<Monitorobjectversion>>) {
        retrofit.create(MonitorObjectVersionService::class.java).getMOVByTaskGuid(tGuid)
                .enqueue(ResponseBodyCallBack(resultCallBack))
    }
 
    /**
     * 根据监管对象版本获取场景详细信息
     * !!目前此方法只查询本地数据库来获取,有待改进
     */
    fun getLocalScene(taskVo: TaskVo, movList: ArrayList<Monitorobjectversion>, resultCallBack: ResultCallBack<ArrayList<Scense>>) {
        val dbService = Observable.create<Response<ArrayList<Scense>>> { emitter ->
            val resultList = ArrayList<Scense>()
            val tempList = sceneDao.getSceneByArea(taskVo.provincecode, taskVo.citycode, taskVo.districtcode)
            movList.forEach  {m ->
                for (s in tempList) {
                    if (s.guid == m.sguid) {
                        s.monitorNum = m.monitornum
                        s.inspectedNum = m.extension1?.toInt() ?: 0
                        resultList.add(s)
                        break
                    }
                }
            }
            emitter.onNext(Response.success(resultList))
            emitter.onComplete()
        }
 
        RetrofitFactory.executeResult(dbService, object : ResultObserver<ArrayList<Scense>>() {
            override fun onSuccess(result: ArrayList<Scense>?) {
                resultCallBack.onSuccess(result)
            }
 
            override fun onFailure(e: Throwable, isNetWorkError: Boolean) {
                resultCallBack.onFailure()
            }
 
        })
    }
 
    /**
     * 根据上次月任务获取场景详细信息
     * !!目前此方法只查询本地数据库来获取,有待改进
     */
    fun getLocalScene2(taskVo: TaskVo, movList: ArrayList<Monitorobjectversion>, resultCallBack: ResultCallBack<ArrayList<Scense>>) {
        val dbService = Observable.create<Response<ArrayList<Scense>>> { emitter ->
            val resultList = ArrayList<Scense>()
            val tempList = sceneDao.getSceneByArea(taskVo.provincecode, taskVo.citycode, taskVo.districtcode)
            movList.forEach  {m ->
                tempList.forEach s@{s ->
                    if (s.guid == m.sguid) {
                        s.monitorNum = 1
                        s.inspectedNum = 0
                        resultList.add(s)
                        return@s
                    }
                }
            }
 
            tempList.forEach {
                //获取新的场景。extension1:是否完工;monitorNum==0:表示不在上次监管版本中,即新场景
                if (it.extension1 != "0" && it.monitorNum == 0) {
                    resultList.add(it)
                }
            }
            emitter.onNext(Response.success(resultList))
            emitter.onComplete()
        }
 
        RetrofitFactory.executeResult(dbService, object : ResultObserver<ArrayList<Scense>>() {
            override fun onSuccess(result: ArrayList<Scense>?) {
                resultCallBack.onSuccess(result)
            }
 
            override fun onFailure(e: Throwable, isNetWorkError: Boolean) {
                resultCallBack.onFailure()
            }
 
        })
    }
 
    /**
     *
     * @param task 总任务
     * @param mode 0:只会获取总任务对应的监管版本中存在的场景;1:除了监管版本中存在的场景,还会获取剩余的可用场景
     */
    fun getSceneByTask(task: Task, mode: Int, resultCallBack: ResultCallBack<ArrayList<Scense>>) {
        val sceneService = retrofit.create(ScenseService::class.java).getByTaskId(task, mode)
 
        RetrofitFactory.executeResult(sceneService, object : ResultObserver<ArrayList<Scense>>() {
            override fun onSuccess(result: ArrayList<Scense>?) {
                resultCallBack.onSuccess(result)
            }
 
            override fun onFailure(e: Throwable, isNetWorkError: Boolean) {
                resultCallBack.onFailure()
            }
        })
    }
 
    /**
     * 新增任务(总任务或日任务)
     */
    fun putTask(task: Task, resultCallBack: ResultCallBack<ResponseBody>) {
        retrofit.create(TaskService::class.java).putTask(task)
                .enqueue(ResponseBodyCallBack(resultCallBack))
    }
 
    /**
     * 上传子任务
     */
    fun putSubTask(subTaskList: ArrayList<Subtask>, resultCallBack: ResultCallBack<ResponseBody>) {
        retrofit.create(SubTaskService::class.java).putSubTaskList(subTaskList)
                .enqueue(ResponseBodyCallBack(resultCallBack))
    }
 
    /**
     * 更新监管版本
     */
    fun updateMOV(movList: ArrayList<Monitorobjectversion>, resultCallBack: ResultCallBack<ResponseBody>) {
        retrofit.create(MonitorObjectVersionService::class.java).postMOVList(movList)
                .enqueue(ResponseBodyCallBack(resultCallBack))
    }
 
    /**
     * 新增监管版本信息
     */
    fun putMOV(movList: ArrayList<Monitorobjectversion>, resultCallBack: ResultCallBack<ResponseBody>) {
        retrofit.create(MonitorObjectVersionService::class.java).putMOVList(movList)
                .enqueue(ResponseBodyCallBack(resultCallBack))
    }
 
    /**
     * 删除监管版本信息
     */
    fun deleteMOV(movList: ArrayList<Monitorobjectversion>, resultCallBack: ResultCallBack<ResponseBody>) {
        retrofit.create(MonitorObjectVersionService::class.java).deleteMOVList(movList)
                .enqueue(ResponseBodyCallBack(resultCallBack))
    }
 
    /**
     * 获取总任务类型
     */
    fun getTaskType(resultCallBack: ResultCallBack<List<Domainitem>>) {
        val dbService = domainDao.getTaskType().map {
            Response.success(ArrayList(it).toList())
        }
 
        RetrofitFactory.executeResult(dbService, object : ResultObserver<List<Domainitem>>() {
            override fun onSuccess(result: List<Domainitem>?) {
                resultCallBack.onSuccess(result)
            }
 
            override fun onFailure(e: Throwable, isNetWorkError: Boolean) {
                resultCallBack.onFailure()
            }
 
        })
    }
}