feiyu02
2023-11-23 e2392116cd6f875cdc2f46bc04b04d5305f21b56
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
package com.flightfeather.monitor.domain.ds1.repository
 
import com.flightfeather.monitor.domain.ds1.entity.RiskValue
import com.flightfeather.monitor.domain.ds1.mapper.RiskValueMapper
import com.github.pagehelper.PageHelper
import org.springframework.stereotype.Repository
import tk.mybatis.mapper.entity.Example
import java.time.LocalDate
 
@Repository
class RiskValueRep(private val riskValueMapper: RiskValueMapper) {
 
    /**
     * 获取最新一条记录
     */
    fun findLatestData(type: String): RiskValue? {
        val p = PageHelper.startPage<RiskValue>(1, 1)
        riskValueMapper.selectByExample(Example(RiskValue::class.java).apply {
            createCriteria().andEqualTo("type", type)
            orderBy("lst").desc()
        })
        return if (p.isNotEmpty()) {
            p[0]
        } else {
            null
        }
    }
 
    /**
     * 插入列表
     * @param list
     */
    fun insert(list: List<RiskValue>): Int {
        return riskValueMapper.insertList(list)
    }
 
    /**
     * 批量插入或更新,仅限同一日的或同一月的批量数据
     * @param list
     */
    fun insertOrUpdate(list: List<RiskValue>): Int {
        if (list.isEmpty()) return 0
        val newDataList = mutableListOf<RiskValue>()
        val oldDataList = mutableListOf<RiskValue>()
        val dataList = riskValueMapper.selectByExample(Example(RiskValue::class.java).apply {
            createCriteria().andEqualTo("lst", list[0].lst)
                .andEqualTo("type", list[0].type)
        })
        list.forEach { l ->
            val r = dataList.find { d ->
                d?.mnCode == l.mnCode
            }
            if (r != null) {
                l.id = r.id
                oldDataList.add(l)
            } else {
                newDataList.add(l)
            }
        }
        val r1 = riskValueMapper.insertList(newDataList)
        var r2 = 0
        oldDataList.forEach {
            r2 += riskValueMapper.updateByPrimaryKey(it)
        }
        return r1 + r2
    }
 
    /**
     * 查询
     * @param mnCode
     * @param date
     * @param type
     */
    fun select(mnCode: String, date: LocalDate, type: String): List<RiskValue?> {
        val s = date.withDayOfMonth(1).atStartOfDay()
        val e = s.plusMonths(1).minusSeconds(1)
        return riskValueMapper.selectByExample(Example(RiskValue::class.java).apply {
            createCriteria().andEqualTo("mnCode", mnCode)
                .andBetween("lst", s, e)
                .andEqualTo("type", type)
        })
    }
}