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
package cn.flightfeather.supervision.domain.ds2.repository
 
import cn.flightfeather.supervision.business.autooutput.score.ScoreUtil
import cn.flightfeather.supervision.common.utils.Constant
import cn.flightfeather.supervision.domain.ds2.entity.OverallEvaluation
import cn.flightfeather.supervision.domain.ds2.mapper.OverallEvaluationMapper
import org.springframework.stereotype.Repository
import tk.mybatis.mapper.entity.Example
import java.time.LocalDate
import java.time.ZoneId
import java.util.*
 
@Repository
class OverallEvaluationRep(private val overallEvaluationMapper: OverallEvaluationMapper){
 
    /**
     * 获取最新记录
     */
    fun selectLatest(userId: String?): OverallEvaluation? {
        val res = overallEvaluationMapper.selectByExample(Example(OverallEvaluation::class.java).apply {
            createCriteria().andEqualTo("biGuid", userId)
            orderBy("oeUpdateTime").desc()
        })
        return if (res.isNotEmpty()) res[0] else null
    }
 
    /**
     * 插入一条环信码记录
     * @param userId
     * @param score
     * @param sceneType
     * @param sT
     * @param eT
     * @return
     */
    fun insertOrUpdateOne(
        userId: String?,
        score: Int?,
        sceneType: Constant.SceneType,
        sT: LocalDate,
        eT: LocalDate,
    ): Int {
        val period = "${sT.year}/${sT.monthValue}-${eT.monthValue}"
        val codeLevel = ScoreUtil.scoreToCredit(score)
        val oE = overallEvaluationMapper.selectOne(OverallEvaluation().apply {
            biGuid = userId
            oePeriod = period
        })
        if (oE != null) {
            oE.oeScore = score
            oE.oeCodeLevel = codeLevel.first?.toByte()
            return overallEvaluationMapper.updateByPrimaryKey(oE)
        } else {
            val startTime = sT.withDayOfMonth(1)
            val endTime = eT.plusMonths(1).withDayOfMonth(1).minusDays(1)
            val publishTime = Date.from(endTime.plusDays(1).atStartOfDay().atZone(ZoneId.systemDefault()).toInstant())
            val e = OverallEvaluation().apply {
                biGuid = userId
                oeScore = score
                oePublishTime = publishTime
                oeUpdateTime = publishTime
                oeSceneTypeId = Constant.SceneType.typeMap(sceneType.value.toByte())
                oeSceneType = sceneType.text
                oePeriod = period
                oeCodeLevel = codeLevel.first?.toByte()
                oeStartTime = Date.from(startTime.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant())
                oeEndTime = Date.from(endTime.atStartOfDay().atZone(ZoneId.systemDefault()).toInstant())
            }
            return overallEvaluationMapper.insert(e)
        }
    }
}