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
package cn.flightfeather.thirdappmodule.repository
 
import cn.flightfeather.thirdappmodule.bean.entity.Evaluation
import cn.flightfeather.thirdappmodule.bean.entity.Evaluationrule
import cn.flightfeather.thirdappmodule.bean.entity.Itemevaluation
import cn.flightfeather.thirdappmodule.bean.vo.EvaluationsubruleVo
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.EvaluationService
import cn.flightfeather.thirdappmodule.repository.dao.EvaluationDao
import io.reactivex.Observable
import io.reactivex.functions.BiFunction
import okhttp3.ResponseBody
import retrofit2.Response
 
/**
 * @author riku
 * Date: 2020/7/22
 * 评分相关数据操作
 */
class EvaluationRepository {
    private val retrofit = RetrofitFactory.instance.retrofit
    private val evaluationDao = EvaluationDao()
 
 
    fun getEvaluationRule(
            provinceCode: String, cityCode: String,
            districtCode: String, sceneTypeId: Byte, resultCallBack: ResultCallBack<List<Evaluationrule>>) {
        val dbService = evaluationDao.getEvaluationRule(provinceCode, cityCode, districtCode, sceneTypeId)
                .map {
                    Response.success(it)
                }
 
        RetrofitFactory.executeResult(dbService, object : ResultObserver<List<Evaluationrule>>() {
            override fun onSuccess(result: List<Evaluationrule>?) {
                resultCallBack.onSuccess(result)
            }
 
            override fun onFailure(e: Throwable, isNetWorkError: Boolean) {
                resultCallBack.onFailure()
            }
        })
    }
 
    fun getEvaluation(inspectionGuid: String, resultCallBack: ResultCallBack<List<Evaluation>>) {
        retrofit.create(EvaluationService::class.java)
                .findByInspectionId(inspectionGuid).enqueue(ResponseBodyCallBack(resultCallBack))
    }
 
    fun getRuleItem(evaluationruleGuid: String, subTaskGuid: String,resultCallBack: ResultCallBack<ArrayList<EvaluationsubruleVo>>) {
        val dbService = evaluationDao.getEvaluationSubRule(evaluationruleGuid).map {
            val list = it.map {
                EvaluationsubruleVo().apply {
                    guid = it.guid
                    erguid = it.erguid
                    ertype = it.ertype
                    fatherid = it.fatherid
                    fathername = it.fathername
                    usedanalyse = it.usedanalyse
                    itemname = it.itemname
                    itemdescription = it.itemdescription
                    minscore = it.minscore
                    maxscore = it.maxscore
                    displayid = it.displayid
                    displaylevel = it.displaylevel
                    problemlist = it.problemlist
                    gitlist = it.gitlist
                    devicelist = it.devicelist
                    createdate = it.createdate
                    updatedate = it.updatedate
                    mutichoice = it.extension1
                    remark = it.remark
                }
            }
            Response.success(list)
        }
 
        val service = retrofit.create(EvaluationService::class.java)
                .getItemEvaluationList(subTaskGuid)
 
        val zipService = Observable.zip(dbService, service,
                BiFunction<Response<List<EvaluationsubruleVo>>, Response<List<Itemevaluation>>, Response<List<EvaluationsubruleVo>>> { t1, t2 ->
                    refreshRuleItem(t1, t2)
                })
 
        RetrofitFactory.executeResult(zipService, object : ResultObserver<List<EvaluationsubruleVo>>() {
            override fun onSuccess(result: List<EvaluationsubruleVo>?) {
                resultCallBack.onSuccess(ArrayList(result ?: emptyList()))
            }
 
            override fun onFailure(e: Throwable, isNetWorkError: Boolean) {
                resultCallBack.onFailure()
            }
        })
    }
 
    private fun refreshRuleItem(
            t1: Response<List<EvaluationsubruleVo>>,
            t2: Response<List<Itemevaluation>>
    ): Response<List<EvaluationsubruleVo>> {
        t1.body()?.forEach { r ->
            if (t2.body()?.isNotEmpty() == true) {
                t2.body()?.forEach t2@{ i ->
                    if (r.guid == i.esrguid) {
                        if (i.extension1.isNullOrEmpty() || i.extension1 == false.toString()) {
                            r.isSelected = false
                        } else {
                            r.grade = "0"
                            try {
                                r.grade = i.value
                            } catch (e: Throwable) {
                                e.printStackTrace()
                            }
                            r.isSelected = true
                        }
                        r.itemEvaluationVo = i
                        return@t2
                    }
                }
            } else {
                // todo: 2020/7/1 目前的评分表格为单选性质,默认每一项初始都不选择
                try {
                    if (r.defaultvalue != "0") {
                        r.isSelected = false
                    }
                } catch (e: Exception) {
                    e.printStackTrace()
                }
            }
        }
        return t1
    }
 
    fun savePoint(
            evaluationVo: Evaluation,
            itemEvaluationVos: ArrayList<Itemevaluation>,
            isUpdate:Boolean = false,
            resultCallBack: ResultCallBack<ResponseBody>
    ) {
        val totalPointService = retrofit.create(EvaluationService::class.java)
                .let {
                    if (isUpdate) {
                        it.uploadEvaluation(evaluationVo)
                    } else {
                        it.putEvaluation(evaluationVo)
                    }
                }
 
        val itemPointService = retrofit.create(EvaluationService::class.java)
                .let {
                    if (isUpdate) {
                        it.uploadItemEvaluationList(itemEvaluationVos)
                    } else {
                        it.putItemEvaluationList(itemEvaluationVos)
                    }
                }
 
        totalPointService.enqueue(ResponseBodyCallBack(resultCallBack))
        itemPointService.enqueue(ResponseBodyCallBack(resultCallBack))
    }
}