cjl
2023-12-14 6992aaf0587c09f7c511c1afd12e1519d91363d3
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
<?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.moral.api.mapper.AlarmInfoMapper">
 
        <!-- 通用查询映射结果 -->
        <resultMap id="BaseResultMap" type="com.moral.api.entity.AlarmInfo">
                    <id column="id" property="id" />
                    <result column="alarm_time" property="alarmTime" />
                    <result column="index" property="index" />
                    <result column="device_id" property="deviceId" />
                    <result column="alarm_type" property="alarmType" />
                    <result column="alarm_information" property="alarmInformation" />
                    <result column="create_time" property="createTime" />
        </resultMap>
 
    <select id="selectDataByCondition" resultType="java.util.Map">
        select ai.id alarmInfoId,ai.alarm_time,ai.index,ai.alarm_type,ai.alarm_information,d.id deviceId,d.name deviceName,d.longitude,d.latitude
        from alarm_info ai, device d
        where ai.device_id in
        <foreach collection="deviceIds" item="deviceId" index="index" open="(" close=")" separator=",">
            #{deviceId}
        </foreach>
        and d.id = ai.device_id
        <if test="index != null">
            and ai.index = #{index}
        </if>
        <if test="alarmType != null">
            and ai.alarm_type = #{alarmType}
        </if>
        and alarm_time between #{startTime} and #{endTime}
        ORDER by ai.create_time DESC
        limit #{start},#{size}
    </select>
 
    <select id="selectDataByConditionWithoutPage" resultType="java.util.Map">
        select ai.id alarmInfoId,ai.alarm_time,ai.index,ai.alarm_type,ai.alarm_information,d.id deviceId,d.name deviceName,d.longitude,d.latitude
        from alarm_info ai, device d
        where ai.device_id in
        <foreach collection="deviceIds" item="deviceId" index="index" open="(" close=")" separator=",">
            #{deviceId}
        </foreach>
        and d.id = ai.device_id
        <if test="index != null">
            and ai.index = #{index}
        </if>
        <if test="alarmType != null">
            and ai.alarm_type = #{alarmType}
        </if>
        and alarm_time between #{startTime} and #{endTime}
        ORDER by ai.create_time DESC
    </select>
 
    <select id="selectNewestData" resultType="java.util.Map">
        select ai.id alarmInfoId,ai.alarm_time,ai.index,ai.alarm_type,ai.alarm_information,d.id deviceId,d.name deviceName,d.longitude,d.latitude
        from alarm_info ai, device d
        where
        DATE_FORMAT(ai.alarm_time,'%Y-%m-%d') = (SELECT DATE_FORMAT(max( alarm_time),'%Y-%m-%d') from alarm_info limit 1) and   d.id = ai.device_id and ai.device_id in
        (select t.id from device t
                              inner JOIN monitor_point p on p.id = t.monitor_point_id and p.is_delete = 0
 
            and p.organization_id in (   SELECT id FROM (
                                                            SELECT id, parent_id, name FROM organization  WHERE id = #{orgId}
                                                            UNION ALL
                                                            SELECT i.id, i.parent_id, i.name
                                                            FROM organization  i
                                                                     INNER JOIN (
                                                                SELECT * FROM (
                                                                                  SELECT id, parent_id, name FROM organization  WHERE id = #{orgId}
                                                                                  UNION ALL
                                                                                  SELECT id, parent_id, name FROM organization WHERE parent_id = #{orgId}
                                                                              ) t1
                                                            ) t2 ON i.parent_id = t2.id
                                                            where i.is_delete = 0
                                                        ) t3)
         WHERE
             t.is_delete = 0
        )
        ORDER by ai.id DESC
        limit #{size}
    </select>
    <select id="numNews" resultType="java.lang.Integer">
        select count(1) from alarm_info
                        where alarm_time >=  date_format(NOW(),'%Y-%m-%d')
        and device_id in (SELECT
                              t.id
                          FROM
                              device t
                                  INNER JOIN monitor_point p ON p.id = t.monitor_point_id
                                  AND p.is_delete = 0
                                  AND p.organization_id IN (
                                      SELECT
                                          id
                                      FROM
                                          (
                                              SELECT
                                                  id,
                                                  parent_id,
                                                  NAME
                                              FROM
                                                  organization
                                              WHERE
                                                  id = #{organizationId} UNION ALL
                                              SELECT
                                                  i.id,
                                                  i.parent_id,
                                                  i.NAME
                                              FROM
                                                  organization i
                                                      INNER JOIN (
                                                      SELECT
                                                          *
                                                      FROM
                                                          (
                                                              SELECT
                                                                  id,
                                                                  parent_id,
                                                                  NAME
                                                              FROM
                                                                  organization
                                                              WHERE
                                                                  id = #{organizationId} UNION ALL
                                                              SELECT
                                                                  id,
                                                                  parent_id,
                                                                  NAME
                                                              FROM
                                                                  organization
                                                              WHERE
                                                                  parent_id = #{organizationId}
                                                          ) t1
                                                  ) t2 ON i.parent_id = t2.id
                                              WHERE
                                                  i.is_delete = 0
                                          ) t3
                                  )
                          WHERE
                              t.is_delete = 0 )
    </select>
 
</mapper>