OnlineMonitorMapper.xml 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "http://mybatis.org/dtd/mybatis-3-mapper.dtd">
  3. <mapper namespace="com.huaxu.dao.OnlineMonitorMapper">
  4. <!-- 结果集 -->
  5. <resultMap type="com.huaxu.dto.MonitorDataCollectDto" id="monitorDataMap">
  6. <result property="sceneId" column="scene_id" jdbcType="INTEGER"/>
  7. <result property="sceneName" column="scene_name" jdbcType="VARCHAR"/>
  8. <result property="pointX" column="point_x" jdbcType="VARCHAR"/>
  9. <result property="pointY" column="point_y" jdbcType="VARCHAR"/>
  10. <result property="address" column="address" jdbcType="VARCHAR"/>
  11. <result property="sceneState" column="scene_state" jdbcType="INTEGER"/>
  12. <collection property="MonitorDataEntities" ofType="com.huaxu.dto.MonitorDataDto" javaType="list">
  13. <result property="deviceId" column="device_id" jdbcType="INTEGER"/>
  14. <result property="attributeId" column="attribute_id" jdbcType="INTEGER"/>
  15. <result property="attributeName" column="attribute_name" jdbcType="VARCHAR"/>
  16. <result property="unit" column="unit" jdbcType="VARCHAR"/>
  17. <result property="isAlarm" column="is_Alarm" jdbcType="VARCHAR"/>
  18. <result property="attributeType" column="attribute_type" jdbcType="VARCHAR"/>
  19. <result property="deviceCode" column="device_code" jdbcType="VARCHAR"/>
  20. <result property="sumValue" column="sum_value" jdbcType="VARCHAR"/>
  21. <result property="latestValue" column="latest_value" jdbcType="VARCHAR"/>
  22. </collection>
  23. </resultMap>
  24. <sql id="Base_Column_List">
  25. t1.id scene_Id,
  26. t1.scene_name ,
  27. t1.point_x ,
  28. t1.point_y,
  29. t1.address ,
  30. if (t7.alarm_count>0, 1, 0) scene_state,
  31. t5.device_id,
  32. t5.attribute_id ,
  33. ifnull(t5.remark, t5.`name`) attribute_name,
  34. t5.unit ,
  35. case when t5.attribute_id is null then null when t6.id is null then 0 else 1 end is_alarm,
  36. t5.attribute_type,
  37. if (t5.device_id is null, null, t4.device_code) device_code
  38. </sql>
  39. <sql id="sceneDeviceJoins">
  40. inner join sms_scene t2 on find_in_set(t1.id, t2.parent_scene_ids) and t2. status = 1
  41. inner join sms_scene_type t3 on t3.id = t1.scene_type_id and t3. status = 1
  42. left join sms_device t4 on t4.scene_id=t2.id and t4. status = 1 and t4.enable_state=1
  43. </sql>
  44. <sql id="attributeJoins">
  45. select a1.device_id,a1.attribute_id,a1.remark,a1.seq,a2.name,a2.unit,a2.attribute_type
  46. from sms_device_parm a1
  47. inner join sms_device_attribute a2 on a1.attribute_id=a2.id
  48. where a1. status = 1 and a2.status = 1
  49. </sql>
  50. <sql id="alarmDetailsJoins">
  51. inner join sms_alarm_details t6 on t6.device_id=t4.id and t6. status = 1
  52. </sql>
  53. <sql id="sceneAlarmJoins">
  54. left join (
  55. select substring_index(substring_index(a3.parent_scene_ids, ',', 2), ',', -1) one_scene_id,count(1) alarm_count
  56. from sms_device a1
  57. inner join sms_alarm_details a2 on a1.id=a2.device_id and a2.`status` = 1 and a2.state = 1
  58. inner join sms_scene a3 on a3.id=a1.scene_id and a3.`status` = 1
  59. where a1.`status` = 1 and a1.enable_state=1
  60. <if test="sceneIds != null and sceneIds.size() > 0">
  61. and a1.scene_id in
  62. <foreach collection="sceneIds" item="item" open="(" close=")" separator=",">
  63. #{item}
  64. </foreach>
  65. </if>
  66. group by one_scene_id
  67. )t7 on t7.one_scene_id=t1.id
  68. </sql>
  69. <!--查询实时报警信息-->
  70. <select id="selectAlarmDetails" resultType="com.huaxu.dto.AlarmDetailsDto">
  71. select
  72. t1.scene_name as "sceneName",
  73. t6.alarm_content as "alarmContent",
  74. t6.alarm_start_time as "alarmStartTime"
  75. from sms_scene t1
  76. <include refid="sceneDeviceJoins"/>
  77. <include refid="alarmDetailsJoins"/>
  78. where t1.parent_scene_id = 0 and t1. status = 1 and t1. enable_state = 1 and t3.scene_type_name = #{sceneTypeName} and t6.state=1
  79. <if test="sceneIds != null and sceneIds.size() > 0">
  80. and t1.id in
  81. <foreach collection="sceneIds" item="item" open="(" close=")" separator=",">
  82. #{item}
  83. </foreach>
  84. </if>
  85. <if test="sceneIds == null or sceneIds.size() == 0">
  86. and t1.id is null
  87. </if>
  88. <if test="sceneName != null and sceneName != ''">
  89. and t1.scene_name like concat('%',#{sceneName},'%')
  90. </if>
  91. <if test="tenantId != null and tenantId != ''">
  92. and t1.tenant_id = #{tenantId}
  93. </if>
  94. order by t1.scene_name
  95. </select>
  96. <!--查询实时报警数量-->
  97. <select id="selectAlarmCount" resultType="java.util.Map">
  98. select
  99. t1.scene_name as "sceneName",
  100. count(1) as "alalmCount"
  101. from sms_scene t1
  102. <include refid="sceneDeviceJoins"/>
  103. <include refid="alarmDetailsJoins"/>
  104. where t1.parent_scene_id = 0 and t1. status = 1 and t1. enable_state = 1 and t3.scene_type_name = #{sceneTypeName} and t6.state=1
  105. <if test="sceneIds != null and sceneIds.size() > 0">
  106. and t1.id in
  107. <foreach collection="sceneIds" item="item" open="(" close=")" separator=",">
  108. #{item}
  109. </foreach>
  110. </if>
  111. <if test="sceneIds == null or sceneIds.size() == 0">
  112. and t1.id is null
  113. </if>
  114. <if test="sceneName != null and sceneName != ''">
  115. and t1.scene_name like concat('%',#{sceneName},'%')
  116. </if>
  117. <if test="tenantId != null and tenantId != ''">
  118. and t1.tenant_id = #{tenantId}
  119. </if>
  120. group by t1.scene_name
  121. order by alalmCount desc
  122. </select>
  123. <!--查询地图悬浮数据-->
  124. <select id="selectMapSuspension" resultMap="monitorDataMap">
  125. select
  126. <include refid="Base_Column_List"/>
  127. from sms_scene t1
  128. <include refid="sceneDeviceJoins"/>
  129. <include refid="sceneAlarmJoins"/>
  130. left join(
  131. <include refid="attributeJoins"/> and a1.is_suspension = 1
  132. )t5 on t5.device_id=t4.id
  133. left join sms_alarm_details t6 on t6.device_id=t4.id and t6. status = 1 and t6.attribute_id = t5.attribute_id and t6.state = 1
  134. where t1.parent_scene_id = 0 and t1. status = 1 and t1. enable_state = 1 and t3.scene_type_name = #{sceneTypeName}
  135. <if test="sceneIds != null and sceneIds.size() > 0">
  136. and t1.id in
  137. <foreach collection="sceneIds" item="item" open="(" close=")" separator=",">
  138. #{item}
  139. </foreach>
  140. </if>
  141. <if test="sceneIds == null or sceneIds.size() == 0">
  142. and t1.id is null
  143. </if>
  144. <if test="tenantId != null and tenantId != ''">
  145. and t1.tenant_id = #{tenantId}
  146. </if>
  147. order by t1.scene_name,t5.seq
  148. </select>
  149. <!--查询地图参数数据-->
  150. <select id="selectMapParam" resultMap="monitorDataMap">
  151. select
  152. <include refid="Base_Column_List"/>
  153. from sms_scene t1
  154. <include refid="sceneDeviceJoins"/>
  155. <include refid="sceneAlarmJoins"/>
  156. left join(
  157. <include refid="attributeJoins"/> and a1.is_map = 1
  158. )t5 on t5.device_id=t4.id
  159. left join sms_alarm_details t6 on t6.device_id=t4.id and t6. status = 1 and t6.attribute_id = t5.attribute_id and t6.state = 1
  160. where t1.parent_scene_id = 0 and t1. status = 1 and t1.id = #{sceneId}
  161. <if test="sceneIds != null and sceneIds.size() > 0">
  162. and t1.id in
  163. <foreach collection="sceneIds" item="item" open="(" close=")" separator=",">
  164. #{item}
  165. </foreach>
  166. </if>
  167. order by t1.scene_name,t5.seq
  168. </select>
  169. <!--查询实时数据列表-->
  170. <select id="selectPage" resultType="com.huaxu.dto.OnlineDataDto">
  171. select t1.id scene_id,t1.scene_name,t1.address
  172. from sms_scene t1
  173. inner join sms_scene_type t3 on t3.id = t1.scene_type_id and t3. status = 1
  174. where t1.parent_scene_id = 0 and t1. status = 1 and t1. enable_state = 1 and t3.scene_type_name = #{onlineDataDto.sceneTypeName}
  175. <if test="onlineDataDto.sceneIds != null and onlineDataDto.sceneIds.size() > 0">
  176. and t1.id in
  177. <foreach collection="onlineDataDto.sceneIds" item="item" open="(" close=")" separator=",">
  178. #{item}
  179. </foreach>
  180. </if>
  181. <if test="onlineDataDto.sceneIds == null or onlineDataDto.sceneIds.size() == 0">
  182. and t1.id is null
  183. </if>
  184. <if test="onlineDataDto.sceneName != null and onlineDataDto.sceneName != ''">
  185. and t1.scene_name like concat('%',#{onlineDataDto.sceneName},'%')
  186. </if>
  187. <if test="onlineDataDto.tenantId != null and onlineDataDto.tenantId != ''">
  188. and t1.tenant_id = #{onlineDataDto.tenantId}
  189. </if>
  190. order by t1.date_create
  191. </select>
  192. <!--查询设备报警分析-->
  193. <select id="selectAlarmStats" resultType="java.util.Map">
  194. select
  195. t4.id,
  196. t4.device_name as "deviceName",
  197. count(1) as "alarmCount",
  198. sum(timestampdiff(minute,t6.alarm_start_time,ifnull(t6.alarm_end_time,now()))) as "alarmDuration"
  199. from sms_scene t1
  200. <include refid="sceneDeviceJoins"/>
  201. <include refid="alarmDetailsJoins"/>
  202. where t1.parent_scene_id = 0 and t1. status = 1 and t1. enable_state = 1 and t1.id = #{id}
  203. <if test="sceneIds != null and sceneIds.size() > 0">
  204. and t1.id in
  205. <foreach collection="sceneIds" item="item" open="(" close=")" separator=",">
  206. #{item}
  207. </foreach>
  208. </if>
  209. <if test="days != null">
  210. and t6.alarm_start_time>DATE(NOW() + INTERVAL-#{days}+1 DAY)
  211. </if>
  212. group by t4.id,t4.device_name
  213. </select>
  214. <!--查询场景对应参数数据-->
  215. <select id="selectSceneParam" resultMap="monitorDataMap">
  216. select
  217. t1.id scene_Id,
  218. t1.scene_name ,
  219. t5.device_id,
  220. t5.attribute_id ,
  221. ifnull(t5.remark, t5.`name`) attribute_name,
  222. if(t6.id is null , 0 , 1 ) is_alarm,
  223. t5.attribute_type,
  224. if (t5.device_id is null, null, t4.device_code) device_code,
  225. t8.latest_value
  226. from sms_scene t1
  227. <include refid="sceneDeviceJoins"/>
  228. inner join(
  229. <include refid="attributeJoins"/>
  230. <if test="type == 1 ">
  231. and a2.attribute_type in(3,4,5,6)
  232. </if>
  233. <if test="type == 2 ">
  234. and a2.attribute_type in(2,3,4,5,6,7,8,9,10,11,12,13,14)
  235. </if>
  236. )t5 on t5.device_id=t4.id
  237. left join sms_alarm_details t6 on t6.device_id=t4.id and t6. status = 1 and t6.attribute_id = t5.attribute_id and t6.state = 1
  238. left join (
  239. select parent_scene_id,max(latest_value) latest_value
  240. from sms_day_report b1
  241. where b1.year = year(now() + interval-1 day) and b1.year = month(now() + interval-1 day) and b1.day = day(now() + interval-1 day)
  242. <if test="sceneIds != null and sceneIds.size() > 0">
  243. and b1.parent_scene_id in
  244. <foreach collection="sceneIds" item="item" open="(" close=")" separator=",">
  245. #{item}
  246. </foreach>
  247. </if>
  248. group by parent_scene_id
  249. )t8 on t8.parent_scene_id = t1.id
  250. where t1.parent_scene_id = 0 and t1. status = 1
  251. <if test="sceneTypeName != null and sceneTypeName != '' ">
  252. and t3.scene_type_name = #{sceneTypeName}
  253. </if>
  254. <if test="sceneId != null and sceneId != '' ">
  255. and t1.id = #{sceneId}
  256. </if>
  257. <if test="sceneIds != null and sceneIds.size() > 0">
  258. and t1.id in
  259. <foreach collection="sceneIds" item="item" open="(" close=")" separator=",">
  260. #{item}
  261. </foreach>
  262. </if>
  263. <if test="tenantId != null and tenantId != ''">
  264. and t1.tenant_id = #{tenantId}
  265. </if>
  266. order by t1.scene_name,t5.seq
  267. </select>
  268. </mapper>