riku
2021-09-24 4f1b7973c53b45f57e451191bfd5a3d2136a004c
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
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
package cn.flightfeather.supervision.lightshare.service.Impl
 
import cn.flightfeather.supervision.common.score.AutoScore
import cn.flightfeather.supervision.domain.entity.Company
import cn.flightfeather.supervision.domain.entity.Evaluation
import cn.flightfeather.supervision.domain.entity.Evaluationrule
import cn.flightfeather.supervision.domain.entity.OverallEvaluation
import cn.flightfeather.supervision.domain.enumeration.AssessmentRuleType
import cn.flightfeather.supervision.domain.enumeration.SceneType
import cn.flightfeather.supervision.domain.mapper.*
import cn.flightfeather.supervision.infrastructure.utils.DateUtil
import cn.flightfeather.supervision.lightshare.service.EvaluationService
import cn.flightfeather.supervision.lightshare.vo.AssessmentGradeVo
import cn.flightfeather.supervision.lightshare.vo.AssessmentSearchCondition
import cn.flightfeather.supervision.lightshare.vo.CreditInfoVo
import com.github.pagehelper.PageHelper
import org.springframework.stereotype.Service
import tk.mybatis.mapper.entity.Example
import java.util.*
import javax.servlet.http.HttpServletResponse
 
@Service
class EvaluationServiceImpl(
        val evaluationMapper: EvaluationMapper,
        val evaluationruleMapper: EvaluationruleMapper,
        val userinfoMapper: UserinfoMapper,
        val baseInfoMapper: BaseInfoMapper,
        val companyMapper: CompanyMapper,
        val overallEvaluationMapper: OverallEvaluationMapper,
        val autoScore: AutoScore
) : EvaluationService {
 
    override fun findOne(id: String): Evaluation = evaluationMapper.selectByPrimaryKey(id)
 
    override fun findAll(): MutableList<Evaluation> = evaluationMapper.selectAll()
 
    override fun save(evaluation: Evaluation): Int {
        updateRank(evaluation, true)
        return  evaluationMapper.insert(evaluation)
    }
 
    override fun update(evaluation: Evaluation): Int {
        updateRank(evaluation, false)
        return evaluationMapper.updateByPrimaryKey(evaluation)
    }
 
    override fun delete(id: String): Int = evaluationMapper.deleteByPrimaryKey(id)
 
    override fun getTotalPoints(userId: String, evaluatorType: Int, startTime: String, endTime: String, sceneTypeId: Int?, erGuid: String?, eId: String?): List<Evaluation> {
        val example = Example(Evaluation::class.java)
        val criteria = example.createCriteria()
        val startDate = DateUtil().StringToDate(startTime)
        val endDate = DateUtil().StringToDate(endTime)
        criteria.andEqualTo("iguid", userId)
                .andBetween("createdate", startDate, endDate)
        example.and(example.createCriteria().apply {
            if (evaluatorType == 0) {
                orEqualTo("evaluatorrealname", evaluatorType.toString())
                        .orIsNull("evaluatorrealname")
            } else {
                andEqualTo("evaluatorrealname", evaluatorType.toString())
            }
        })
        erGuid?.let { criteria.andEqualTo("stguid", erGuid) }
        eId?.let { criteria.andEqualTo("guid", eId) }
        sceneTypeId?.let {
            example.and(example.createCriteria()
                    .orEqualTo("scensetypeid", sceneTypeId.toByte())
                    .orIsNull("scensetypeid")
            )
        }
 
        return evaluationMapper.selectByExample(example)
    }
 
    override fun getHistoryPoint(userId: String, page: Int, per_page: Int, response: HttpServletResponse): List<AssessmentGradeVo> {
        val userInfo = userinfoMapper.selectByPrimaryKey(userId) ?: return emptyList()
        val example = Example(Evaluation::class.java).apply {
            createCriteria().andEqualTo("iguid", userId)
                    .andEqualTo("ertype", AssessmentRuleType.Total.value.toByte())
            //根据评估人的类型进行筛选,自评和官方评分分开排名
            and(createCriteria().orIsNull("evaluatorrealname")
                    .orEqualTo("evaluatorrealname", 0))
            orderBy("updatedate").desc()
        }
        val counts = evaluationMapper.selectCountByExample(example)
        val totalPage = Math.ceil(counts.toDouble() / per_page.toDouble()).toInt()
        response.setIntHeader("totalPage", totalPage)
        response.setIntHeader("currentPage", page)
 
        val offset = per_page.times(page - 1)
        PageHelper.offsetPage<Evaluation>(offset, per_page)
 
        val resultList = mutableListOf<AssessmentGradeVo>()
        evaluationMapper.selectByExample(example).forEach {
            resultList.add(AssessmentGradeVo().apply {
                this.userId = userId
                userRealName = userInfo.realname
                sceneType = userInfo.extension2?.toIntOrNull() ?: SceneType.NoType.value
                tPGuid = it.guid
                tPRuleGuid = it.stguid
                totalPoint = it.resultscorebef?.toInt() ?: 0
                level = getEvaluationLevel(tPRuleGuid, totalPoint)
                rank = it.promissednum ?: 1
                updateDate = it.updatedate
            })
        }
 
        return resultList
    }
 
    override fun getCreditInfo(userId: String): CreditInfoVo {
        val userinfo = userinfoMapper.selectByPrimaryKey(userId)
        val baseInfo = baseInfoMapper.selectByPrimaryKey(userId)
        val company = companyMapper.selectByPrimaryKey(baseInfo.ciGuid)
 
 
        val result = CreditInfoVo(
                userId,
                baseInfo.biName,
                userinfo.extension2?.toIntOrNull() ?: SceneType.NoType.value,
                baseInfo.ciName,
                baseInfo.biManagementCompany,
                baseInfo.biContact,
                baseInfo.biTelephone,
                baseInfo.biAddress,
                district = company.ciDistrictName,
                town = company.ciTownName
        )
 
        val rule = evaluationruleMapper.selectByExample(Example(Evaluationrule::class.java).apply {
            val c = createCriteria()
            userinfo.extension2?.toByteOrNull()?.let {
                c.andEqualTo("scensetypeid", it)
                        .andEqualTo("ruletype", "0")
            }
        }).takeIf { it.isNotEmpty() }?.get(0) ?: return result
 
        val overallEvaluation = overallEvaluationMapper.selectByExample(Example(OverallEvaluation::class.java).apply {
            createCriteria().andEqualTo("biGuid", baseInfo.biGuid)
            orderBy("oePublishTime").desc()
        }).takeIf { it.isNotEmpty() }?.get(0) ?: return result
 
        //评分记录对应 的打分周期 ,格式为 YYYY/M-M,某年某月至某月
        val period = overallEvaluation.oePeriod
        //评分等级分数界限,格式为  0,59#60,89#90,100,表示有三个分数段
        val levels = rule.extension1?.split("#")?.asReversed()
        //评分等级对应的描述,格式为 较差#一般#优秀,对应三个分数段的描述
        val levelDes = rule.extension2?.split("#")?.asReversed()
 
        var creditLevel: Int? = null
        var creditLevelDes: String? = null
 
        if (levels != null && levelDes != null && levels.size == levelDes.size) {
            for (i in levels.indices) {
                val list = levels[i].split(",")
                if (list.size == 2) {
                    val min = list[0].toInt()
                    val max = list[1].toInt()
                    val score = overallEvaluation.oeScore
                    if (score in min..max) {
                        creditLevel = i
                        creditLevelDes = levelDes[i]
                        break
                    }
                }
            }
        }
        val codeLevel =
            when (overallEvaluation.oeCodeLevel) {
                0.toByte() -> "绿码"
                1.toByte() -> "黄码"
                2.toByte() -> "红码"
                else -> null
            }
 
        result.publishTime = DateUtil().DateToString(overallEvaluation.oePublishTime, "YYYY年MM月")
        result.updateTime = DateUtil().DateToString(overallEvaluation.oeUpdateTime, "YYYY年MM月")
        result.creditLevel = creditLevel
        result.creditLevelDes = creditLevelDes
        result.codeLevel = codeLevel
        result.score = overallEvaluation.oeScore?.toString()
 
        return result
    }
 
 
    override fun getAssessments(userId: String, condition: AssessmentSearchCondition, page: Int, perPage: Int, response: HttpServletResponse): List<AssessmentGradeVo> {
        val user = userinfoMapper.selectByPrimaryKey(userId)
        val districtName = user.extension1
 
        val p = PageHelper.startPage<Evaluation>(page, perPage)
 
        val result = mutableListOf<AssessmentGradeVo>()
        evaluationMapper.getAssessments(districtName?:"", condition.searchText, "", condition.sceneTypes).forEach {
            //此处由于 PageHelper 的分页操作,会导致结果的数量强制为 perPage的值,包括null
            if (it != null) {
                result.add(AssessmentGradeVo().apply {
                    this.userId = it["userId"] as String?
                    userRealName = it["userRealName"] as String?
                    sceneType = (it["sceneType"] as String?)?.toIntOrNull() ?: SceneType.Restaurant.value
                    tPGuid = it["tPGuid"] as String?
                    tPRuleGuid = it["tPRuleGuid"] as String?
                    totalPoint = (it["totalPoint"] as String?)?.toIntOrNull() ?: 0
                    level = getEvaluationLevel(tPRuleGuid, totalPoint)
                    rank = (it["rank"] as Int?) ?: -1
                    updateDate = it["updateDate"] as Date?
                })
            }
        }
 
        response.setIntHeader("totalPage", p.pages)
        response.setIntHeader("currentPage", p.pageNum)
 
        return result
    }
 
    override fun autoScore(year: Int, month: Int, sceneType: Int, response: HttpServletResponse): HttpServletResponse {
        autoScore.go()
        return response
    }
 
    /**
     * 根据分数获取等级
     */
    private fun getEvaluationLevel(ruleId:String?, score: Int): String {
        return when (score) {
            in 0..40 -> "极差"
            in 41..64 -> "较差"
            in 65..79 -> "一般"
            in 80..94 -> "良好"
            in 95..Int.MAX_VALUE -> "优秀"
            else -> "极差"
        }
    }
 
    private fun updateRank(evaluation: Evaluation, isInsert: Boolean) {
        if (evaluation.ertype == AssessmentRuleType.Total.value.toByte()) {
            val eList = evaluationMapper.selectByExample(Example(Evaluation::class.java).apply {
                createCriteria().andEqualTo("scensename", evaluation.scensename)
                        .andIsNotNull("scensename")
                //根据评估人的类型进行筛选,自评和官方评分封面开排名
                if (evaluation.evaluatorrealname == null || evaluation.evaluatorrealname == "0") {
                    and(createCriteria().orIsNull("evaluatorrealname")
                            .orEqualTo("evaluatorrealname", evaluation.evaluatorrealname))
                } else {
                    and(createCriteria().andEqualTo("evaluatorrealname", evaluation.evaluatorrealname))
                }
            })
            if (isInsert) {
                eList.add(evaluation)
            } else {
                for (i in eList.indices) {
                    if (evaluation.guid == eList[i].guid) {
                        eList[i].resultscorebef = evaluation.resultscorebef
                        break
                    }
                }
            }
            eList.sortByDescending {
                it.resultscorebef?.toInt() ?: 0
            }
            val updateList = mutableListOf<Evaluation>()
            for (i in eList.indices) {
                if (eList[i].guid == evaluation.guid){
                    evaluation.promissednum = i + 1
                }
                else if (eList[i].promissednum != i + 1) {
                    eList[i].promissednum = i + 1
                    updateList.add(eList[i])
                }
            }
            updateList.forEach {
                evaluationMapper.updateByPrimaryKey(it)
            }
        }
    }
}