yeg
6 天以前 e38aca8134570513435c003c25dc05f1ae51c9c9
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
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper
PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN"
"http://mybatis.org/dtd/mybatis-3-mapper.dtd">
<mapper namespace="com.battery.business.mapper.TCollectorMapper">
 
    <resultMap type="TCollector" id="TCollectorResult">
        <id property="collectorId" column="collector_id" />
        <result property="adapterId" column="adapter_id" />
        <result property="collectorCode" column="collector_code" />
        <result property="ipAddress" column="ip_address" />
        <result property="macAddress" column="mac_address" />
        <result property="collectorName" column="collector_name" />
        <result property="manufacturer" column="manufacturer" />
        <result property="model" column="model" />
        <result property="channelCount" column="channel_count" />
        <result property="onlineStatus" column="online_status" />
        <result property="testStatus" column="test_status" />
        <result property="lastSeenAtUtc" column="last_seen_at_utc" />
        <result property="status" column="status" />
        <result property="delFlag" column="del_flag" />
        <result property="createBy" column="create_by" />
        <result property="createTime" column="create_time" />
        <result property="updateBy" column="update_by" />
        <result property="updateTime" column="update_time" />
        <result property="remark" column="remark" />
        <collection property="relatedTests" column="collector_id"
                    select="com.battery.business.mapper.DeviceRelatedTestMapper.selectCollectorRelatedTests" />
    </resultMap>
 
    <!-- 在线状态取所链接来源实例的连接结果(ADR-0063:在线只表示来源可达);
         采集器测量由该来源提供,所以它不是独立可达的硬件。SELECT 与 WHERE 各用一次,故抽成片段。 -->
    <sql id="derivedOnlineStatus">
        case when exists (
            select 1 from t_device_adapter adapter_fact
            where adapter_fact.adapter_id = c.adapter_id
              and upper(adapter_fact.connection_status) = 'ONLINE'
        ) then '1' else '0' end
    </sql>
 
    <sql id="selectCollectorVo">
        select c.collector_id, c.collector_code, c.adapter_id, c.ip_address, c.mac_address, c.collector_name,
               c.manufacturer, c.model, c.channel_count,
               <include refid="derivedOnlineStatus" /> as online_status,
               c.last_seen_at_utc,
               case when exists (
                   select 1 from t_test_collector_channel mapping
                   join t_test test_fact on test_fact.test_id = mapping.test_id
                   where mapping.collector_id = c.collector_id
                     and test_fact.del_flag = 0
                     and test_fact.test_status in ('RUNNING', 'PAUSED')
               ) then '1' else '0' end as test_status,
               c.status, c.del_flag, c.create_by,
               c.create_time, c.update_by, c.update_time, c.remark
        from t_collector c
    </sql>
 
    <select id="selectCollectorById" parameterType="Long" resultMap="TCollectorResult">
        <include refid="selectCollectorVo" />
        where collector_id = #{collectorId}
          and del_flag = '0'
    </select>
 
    <select id="selectCollectorList" parameterType="TCollector" resultMap="TCollectorResult">
        <include refid="selectCollectorVo" />
        <where>
            del_flag = '0'
            <if test="device != null and device != ''">
                and (ip_address like concat('%', #{device}, '%')
                    or collector_code like concat('%', #{device}, '%')
                    or mac_address like concat('%', #{device}, '%')
                    or collector_name like concat('%', #{device}, '%'))
            </if>
            <if test="beginCreateTime != null and beginCreateTime != ''">
                and create_time &gt;= #{beginCreateTime}
            </if>
            <if test="endCreateTime != null and endCreateTime != ''">
                and create_time &lt;= #{endCreateTime}
            </if>
            <if test="onlineStatus != null and onlineStatus != ''">
                <!-- WHERE 里不能用 SELECT 别名(MySQL 只在 HAVING 认得),所以按同一表达式再过一遍。 -->
                and (<include refid="derivedOnlineStatus" />) = #{onlineStatus}
            </if>
            <if test="testStatus != null and testStatus != ''">
                and (case when exists (
                    select 1 from t_test_collector_channel mapping
                    join t_test test_fact on test_fact.test_id = mapping.test_id
                    where mapping.collector_id = c.collector_id
                      and test_fact.del_flag = 0
                      and test_fact.test_status in ('RUNNING', 'PAUSED')
                ) then '1' else '0' end) = #{testStatus}
            </if>
            <if test="status != null and status != ''">
                and status = #{status}
            </if>
        </where>
        order by create_time desc, collector_id desc
    </select>
 
    <select id="selectCollectorByCode" parameterType="String" resultMap="TCollectorResult">
        <include refid="selectCollectorVo" />
        where collector_code = #{collectorCode}
          and del_flag = '0'
        limit 1
    </select>
 
    <select id="selectCollectorByMacAddress" parameterType="String" resultMap="TCollectorResult">
        <include refid="selectCollectorVo" />
        where mac_address = #{macAddress}
          and del_flag = '0'
        limit 1
    </select>
 
    <insert id="insertCollector" parameterType="TCollector" useGeneratedKeys="true" keyProperty="collectorId">
        insert into t_collector
        (collector_code, adapter_id, ip_address, mac_address, collector_name, manufacturer, model,
         channel_count, online_status, last_seen_at_utc,
         status, del_flag, create_by, create_time, remark)
        values
        (#{collectorCode}, #{adapterId}, #{ipAddress}, #{macAddress}, #{collectorName}, #{manufacturer}, #{model},
         #{channelCount}, case when #{onlineStatus} in ('1', 'ONLINE') then 'ONLINE' else 'OFFLINE' end,
         #{lastSeenAtUtc},
         #{status}, #{delFlag}, #{createBy}, #{createTime}, #{remark})
    </insert>
 
    <update id="updateCollector" parameterType="TCollector">
        update t_collector
        <set>
            collector_code = #{collectorCode},
            adapter_id = #{adapterId},
            ip_address = #{ipAddress},
            mac_address = #{macAddress},
            collector_name = #{collectorName},
            manufacturer = #{manufacturer},
            model = #{model},
            channel_count = #{channelCount},
            status = #{status},
            remark = #{remark},
            update_by = #{updateBy},
            update_time = #{updateTime}
        </set>
        where collector_id = #{collectorId}
          and del_flag = '0'
    </update>
 
    <update id="updateCollectorStatus" parameterType="TCollector">
        update t_collector
        set status = #{status},
            update_by = #{updateBy},
            update_time = #{updateTime}
        where collector_id = #{collectorId}
          and del_flag = '0'
    </update>
 
    <update id="deleteCollectorById">
        update t_collector
        set del_flag = cast(collector_id as char),
            update_by = #{updateBy},
            update_time = sysdate()
        where collector_id = #{collectorId}
          and del_flag = '0'
    </update>
</mapper>