DeviceMapper.xml 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643
  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.zoniot.ccrc.dao.DeviceMapper">
  4. <resultMap id="BaseResultMap" type="com.zoniot.ccrc.entity.Device">
  5. <!--@mbg.generated-->
  6. <!--@Table sc_device-->
  7. <id column="id" property="id" />
  8. <result column="device_no" property="deviceNo" />
  9. <result column="meter_no" property="meterNo" />
  10. <result column="file_no" property="fileNo" />
  11. <result column="seal_no" property="sealNo" />
  12. <result column="site_id" property="siteId" />
  13. <result column="sys_id" property="sysId" />
  14. <result column="device_type_id" property="deviceTypeId" />
  15. <result column="manufacturer_id" property="manufacturerId" />
  16. <result column="building_id" property="buildingId" />
  17. <result column="community_id" property="communityId" />
  18. <result column="loc_desc" property="locDesc" />
  19. <result column="meter_reading" property="meterReading" />
  20. <result column="valve_status" property="valveStatus" />
  21. <result column="device_status" property="deviceStatus" />
  22. <result column="last_receive_time" property="lastReceiveTime" />
  23. <result column="status" property="status" />
  24. <result column="create_by" property="createBy" />
  25. <result column="date_create" property="dateCreate" />
  26. <result column="update_by" property="updateBy" />
  27. <result column="date_update" property="dateUpdate" />
  28. </resultMap>
  29. <sql id="Base_Column_List">
  30. <!--@mbg.generated-->
  31. id, device_no, meter_no, file_no, seal_no, site_id, sys_id, device_type_id, manufacturer_id,
  32. building_id, community_id, loc_desc, meter_reading, valve_status, device_status,
  33. last_receive_time, `status`, create_by, date_create, update_by, date_update
  34. </sql>
  35. <insert id="insertSelective" keyColumn="id" keyProperty="id" parameterType="com.zoniot.ccrc.entity.Device" useGeneratedKeys="true">
  36. <!--@mbg.generated-->
  37. insert into sc_device
  38. <trim prefix="(" suffix=")" suffixOverrides=",">
  39. <if test="deviceNo != null">
  40. device_no,
  41. </if>
  42. <if test="meterNo != null">
  43. meter_no,
  44. </if>
  45. <if test="fileNo != null">
  46. file_no,
  47. </if>
  48. <if test="sealNo != null">
  49. seal_no,
  50. </if>
  51. <if test="siteId != null">
  52. site_id,
  53. </if>
  54. <if test="sysId != null">
  55. sys_id,
  56. </if>
  57. <if test="deviceTypeId != null">
  58. device_type_id,
  59. </if>
  60. <if test="manufacturerId != null">
  61. manufacturer_id,
  62. </if>
  63. <if test="buildingId != null">
  64. building_id,
  65. </if>
  66. <if test="communityId != null">
  67. community_id,
  68. </if>
  69. <if test="locDesc != null">
  70. loc_desc,
  71. </if>
  72. <if test="meterReading != null">
  73. meter_reading,
  74. </if>
  75. <if test="valveStatus != null">
  76. valve_status,
  77. </if>
  78. <if test="deviceStatus != null">
  79. device_status,
  80. </if>
  81. <if test="lastReceiveTime != null">
  82. last_receive_time,
  83. </if>
  84. <if test="status != null">
  85. `status`,
  86. </if>
  87. <if test="createBy != null">
  88. create_by,
  89. </if>
  90. <if test="dateCreate != null">
  91. date_create,
  92. </if>
  93. <if test="updateBy != null">
  94. update_by,
  95. </if>
  96. <if test="dateUpdate != null">
  97. date_update,
  98. </if>
  99. </trim>
  100. <trim prefix="values (" suffix=")" suffixOverrides=",">
  101. <if test="deviceNo != null">
  102. #{deviceNo},
  103. </if>
  104. <if test="meterNo != null">
  105. #{meterNo},
  106. </if>
  107. <if test="fileNo != null">
  108. #{fileNo},
  109. </if>
  110. <if test="sealNo != null">
  111. #{sealNo},
  112. </if>
  113. <if test="siteId != null">
  114. #{siteId},
  115. </if>
  116. <if test="sysId != null">
  117. #{sysId},
  118. </if>
  119. <if test="deviceTypeId != null">
  120. #{deviceTypeId},
  121. </if>
  122. <if test="manufacturerId != null">
  123. #{manufacturerId},
  124. </if>
  125. <if test="buildingId != null">
  126. #{buildingId},
  127. </if>
  128. <if test="communityId != null">
  129. #{communityId},
  130. </if>
  131. <if test="locDesc != null">
  132. #{locDesc},
  133. </if>
  134. <if test="meterReading != null">
  135. #{meterReading},
  136. </if>
  137. <if test="valveStatus != null">
  138. #{valveStatus},
  139. </if>
  140. <if test="deviceStatus != null">
  141. #{deviceStatus},
  142. </if>
  143. <if test="lastReceiveTime != null">
  144. #{lastReceiveTime},
  145. </if>
  146. <if test="status != null">
  147. #{status},
  148. </if>
  149. <if test="createBy != null">
  150. #{createBy},
  151. </if>
  152. <if test="dateCreate != null">
  153. #{dateCreate},
  154. </if>
  155. <if test="updateBy != null">
  156. #{updateBy},
  157. </if>
  158. <if test="dateUpdate != null">
  159. #{dateUpdate},
  160. </if>
  161. </trim>
  162. </insert>
  163. <insert id="replaceSelective" keyColumn="id" keyProperty="id" parameterType="com.zoniot.ccrc.entity.Device" useGeneratedKeys="true">
  164. replace into sc_device
  165. <trim prefix="(" suffix=")" suffixOverrides=",">
  166. <if test="id != null">
  167. id,
  168. </if>
  169. <if test="deviceNo != null">
  170. device_no,
  171. </if>
  172. <if test="meterNo != null">
  173. meter_no,
  174. </if>
  175. <if test="fileNo != null">
  176. file_no,
  177. </if>
  178. <if test="sealNo != null">
  179. seal_no,
  180. </if>
  181. <if test="siteId != null">
  182. site_id,
  183. </if>
  184. <if test="sysId != null">
  185. sys_id,
  186. </if>
  187. <if test="deviceTypeId != null">
  188. device_type_id,
  189. </if>
  190. <if test="manufacturerId != null">
  191. manufacturer_id,
  192. </if>
  193. <if test="buildingId != null">
  194. building_id,
  195. </if>
  196. <if test="communityId != null">
  197. community_id,
  198. </if>
  199. <if test="locDesc != null">
  200. loc_desc,
  201. </if>
  202. <if test="meterReading != null">
  203. meter_reading,
  204. </if>
  205. <if test="valveStatus != null">
  206. valve_status,
  207. </if>
  208. <if test="deviceStatus != null">
  209. device_status,
  210. </if>
  211. <if test="lastReceiveTime != null">
  212. last_receive_time,
  213. </if>
  214. <if test="status != null">
  215. `status`,
  216. </if>
  217. <if test="createBy != null">
  218. create_by,
  219. </if>
  220. <if test="dateCreate != null">
  221. date_create,
  222. </if>
  223. <if test="updateBy != null">
  224. update_by,
  225. </if>
  226. <if test="dateUpdate != null">
  227. date_update,
  228. </if>
  229. </trim>
  230. <trim prefix="values (" suffix=")" suffixOverrides=",">
  231. <if test="id != null">
  232. #{id},
  233. </if>
  234. <if test="deviceNo != null">
  235. #{deviceNo},
  236. </if>
  237. <if test="meterNo != null">
  238. #{meterNo},
  239. </if>
  240. <if test="fileNo != null">
  241. #{fileNo},
  242. </if>
  243. <if test="sealNo != null">
  244. #{sealNo},
  245. </if>
  246. <if test="siteId != null">
  247. #{siteId},
  248. </if>
  249. <if test="sysId != null">
  250. #{sysId},
  251. </if>
  252. <if test="deviceTypeId != null">
  253. #{deviceTypeId},
  254. </if>
  255. <if test="manufacturerId != null">
  256. #{manufacturerId},
  257. </if>
  258. <if test="buildingId != null">
  259. #{buildingId},
  260. </if>
  261. <if test="communityId != null">
  262. #{communityId},
  263. </if>
  264. <if test="locDesc != null">
  265. #{locDesc},
  266. </if>
  267. <if test="meterReading != null">
  268. #{meterReading},
  269. </if>
  270. <if test="valveStatus != null">
  271. #{valveStatus},
  272. </if>
  273. <if test="deviceStatus != null">
  274. #{deviceStatus},
  275. </if>
  276. <if test="lastReceiveTime != null">
  277. #{lastReceiveTime},
  278. </if>
  279. <if test="status != null">
  280. #{status},
  281. </if>
  282. <if test="createBy != null">
  283. #{createBy},
  284. </if>
  285. <if test="dateCreate != null">
  286. #{dateCreate},
  287. </if>
  288. <if test="updateBy != null">
  289. #{updateBy},
  290. </if>
  291. <if test="dateUpdate != null">
  292. #{dateUpdate},
  293. </if>
  294. </trim>
  295. </insert>
  296. <update id="updateByPrimaryKeySelective" parameterType="com.zoniot.ccrc.entity.Device">
  297. <!--@mbg.generated-->
  298. update sc_device
  299. <set>
  300. <if test="deviceNo != null">
  301. device_no = #{deviceNo},
  302. </if>
  303. <if test="meterNo != null">
  304. meter_no = #{meterNo},
  305. </if>
  306. <if test="fileNo != null">
  307. file_no = #{fileNo},
  308. </if>
  309. <if test="sealNo != null">
  310. seal_no = #{sealNo},
  311. </if>
  312. <if test="siteId != null">
  313. site_id = #{siteId},
  314. </if>
  315. <if test="sysId != null">
  316. sys_id = #{sysId},
  317. </if>
  318. <if test="deviceTypeId != null">
  319. device_type_id = #{deviceTypeId},
  320. </if>
  321. <if test="manufacturerId != null">
  322. manufacturer_id = #{manufacturerId},
  323. </if>
  324. <if test="buildingId != null">
  325. building_id = #{buildingId},
  326. </if>
  327. <if test="communityId != null">
  328. community_id = #{communityId},
  329. </if>
  330. <if test="locDesc != null">
  331. loc_desc = #{locDesc},
  332. </if>
  333. <if test="meterReading != null">
  334. meter_reading = #{meterReading},
  335. </if>
  336. <if test="valveStatus != null">
  337. valve_status = #{valveStatus},
  338. </if>
  339. <if test="deviceStatus != null">
  340. device_status = #{deviceStatus},
  341. </if>
  342. <if test="lastReceiveTime != null">
  343. last_receive_time = #{lastReceiveTime},
  344. </if>
  345. <if test="status != null">
  346. `status` = #{status},
  347. </if>
  348. <if test="createBy != null">
  349. create_by = #{createBy},
  350. </if>
  351. <if test="dateCreate != null">
  352. date_create = #{dateCreate},
  353. </if>
  354. <if test="updateBy != null">
  355. update_by = #{updateBy},
  356. </if>
  357. <if test="dateUpdate != null">
  358. date_update = #{dateUpdate},
  359. </if>
  360. </set>
  361. where id = #{id}
  362. </update>
  363. <update id="updateBatch" parameterType="java.util.List">
  364. <!--@mbg.generated-->
  365. update sc_device
  366. <trim prefix="set" suffixOverrides=",">
  367. <trim prefix="device_no = case" suffix="end,">
  368. <foreach collection="list" index="index" item="item">
  369. when id = #{item.id} then #{item.deviceNo}
  370. </foreach>
  371. </trim>
  372. <trim prefix="meter_no = case" suffix="end,">
  373. <foreach collection="list" index="index" item="item">
  374. when id = #{item.id} then #{item.meterNo}
  375. </foreach>
  376. </trim>
  377. <trim prefix="file_no = case" suffix="end,">
  378. <foreach collection="list" index="index" item="item">
  379. when id = #{item.id} then #{item.fileNo}
  380. </foreach>
  381. </trim>
  382. <trim prefix="seal_no = case" suffix="end,">
  383. <foreach collection="list" index="index" item="item">
  384. when id = #{item.id} then #{item.sealNo}
  385. </foreach>
  386. </trim>
  387. <trim prefix="site_id = case" suffix="end,">
  388. <foreach collection="list" index="index" item="item">
  389. when id = #{item.id} then #{item.siteId}
  390. </foreach>
  391. </trim>
  392. <trim prefix="sys_id = case" suffix="end,">
  393. <foreach collection="list" index="index" item="item">
  394. when id = #{item.id} then #{item.sysId}
  395. </foreach>
  396. </trim>
  397. <trim prefix="device_type_id = case" suffix="end,">
  398. <foreach collection="list" index="index" item="item">
  399. when id = #{item.id} then #{item.deviceTypeId}
  400. </foreach>
  401. </trim>
  402. <trim prefix="manufacturer_id = case" suffix="end,">
  403. <foreach collection="list" index="index" item="item">
  404. when id = #{item.id} then #{item.manufacturerId}
  405. </foreach>
  406. </trim>
  407. <trim prefix="building_id = case" suffix="end,">
  408. <foreach collection="list" index="index" item="item">
  409. when id = #{item.id} then #{item.buildingId}
  410. </foreach>
  411. </trim>
  412. <trim prefix="community_id = case" suffix="end,">
  413. <foreach collection="list" index="index" item="item">
  414. when id = #{item.id} then #{item.communityId}
  415. </foreach>
  416. </trim>
  417. <trim prefix="loc_desc = case" suffix="end,">
  418. <foreach collection="list" index="index" item="item">
  419. when id = #{item.id} then #{item.locDesc}
  420. </foreach>
  421. </trim>
  422. <trim prefix="meter_reading = case" suffix="end,">
  423. <foreach collection="list" index="index" item="item">
  424. when id = #{item.id} then #{item.meterReading}
  425. </foreach>
  426. </trim>
  427. <trim prefix="valve_status = case" suffix="end,">
  428. <foreach collection="list" index="index" item="item">
  429. when id = #{item.id} then #{item.valveStatus}
  430. </foreach>
  431. </trim>
  432. <trim prefix="device_status = case" suffix="end,">
  433. <foreach collection="list" index="index" item="item">
  434. when id = #{item.id} then #{item.deviceStatus}
  435. </foreach>
  436. </trim>
  437. <trim prefix="last_receive_time = case" suffix="end,">
  438. <foreach collection="list" index="index" item="item">
  439. when id = #{item.id} then #{item.lastReceiveTime}
  440. </foreach>
  441. </trim>
  442. <trim prefix="`status` = case" suffix="end,">
  443. <foreach collection="list" index="index" item="item">
  444. when id = #{item.id} then #{item.status}
  445. </foreach>
  446. </trim>
  447. <trim prefix="create_by = case" suffix="end,">
  448. <foreach collection="list" index="index" item="item">
  449. when id = #{item.id} then #{item.createBy}
  450. </foreach>
  451. </trim>
  452. <trim prefix="date_create = case" suffix="end,">
  453. <foreach collection="list" index="index" item="item">
  454. when id = #{item.id} then #{item.dateCreate}
  455. </foreach>
  456. </trim>
  457. <trim prefix="update_by = case" suffix="end,">
  458. <foreach collection="list" index="index" item="item">
  459. when id = #{item.id} then #{item.updateBy}
  460. </foreach>
  461. </trim>
  462. <trim prefix="date_update = case" suffix="end,">
  463. <foreach collection="list" index="index" item="item">
  464. when id = #{item.id} then #{item.dateUpdate}
  465. </foreach>
  466. </trim>
  467. </trim>
  468. where id in
  469. <foreach close=")" collection="list" item="item" open="(" separator=", ">
  470. #{item.id}
  471. </foreach>
  472. </update>
  473. <insert id="batchInsert" keyColumn="id" keyProperty="id" parameterType="map" useGeneratedKeys="true">
  474. <!--@mbg.generated-->
  475. insert into sc_device
  476. (device_no, meter_no, file_no, seal_no, site_id, sys_id, device_type_id, manufacturer_id,
  477. building_id, community_id, loc_desc, meter_reading, valve_status, device_status,
  478. last_receive_time, `status`, create_by, date_create, update_by, date_update)
  479. values
  480. <foreach collection="list" item="item" separator=",">
  481. (#{item.deviceNo}, #{item.meterNo}, #{item.fileNo}, #{item.sealNo}, #{item.siteId},
  482. #{item.sysId}, #{item.deviceTypeId}, #{item.manufacturerId}, #{item.buildingId},
  483. #{item.communityId}, #{item.locDesc}, #{item.meterReading}, #{item.valveStatus},
  484. #{item.deviceStatus}, #{item.lastReceiveTime}, #{item.status}, #{item.createBy},
  485. #{item.dateCreate}, #{item.updateBy}, #{item.dateUpdate})
  486. </foreach>
  487. </insert>
  488. <select id="getList" resultType="com.zoniot.ccrc.dto.DeviceDto">
  489. select
  490. sd.*,
  491. sb.name as building_name,
  492. sc.name as community_name,
  493. so.name as org_name,
  494. sdt.equipment_type,
  495. sdt.model,
  496. sdt.is_valve,
  497. sdm.name manufacturer_name,
  498. sgm.customer_no,
  499. sgm.customer_phone,
  500. sgm.customer_name,
  501. sgm.label,
  502. su.username
  503. from sc_device sd
  504. left join sc_building sb on(sb.id = sd.building_id)
  505. left join sc_community sc on(sc.id = sd.community_id)
  506. left join sc_organization so on(so.id = sc.org_id)
  507. left join sc_device_type sdt on(sdt.id = sd.device_type_id)
  508. left join sc_device_manufacturer sdm on(sdm.id = sd.manufacturer_id)
  509. left join sc_grid_management sgm on(sgm.device_id = sd.id)
  510. left join sc_user su on(su.id = sgm.user_id)
  511. where sd.status = 1
  512. <if test="siteId != null"> and sd.site_id = #{siteId}</if>
  513. <if test="userId != null"> and sgm.user_id = #{userId}</if>
  514. <if test="sysId != null"> and sd.sys_id = #{sysId}</if>
  515. <if test="deviceTypeId != null"> and sd.device_type_id = #{deviceTypeId}</if>
  516. <if test="orgId != null"> and sc.org_id = #{orgId}</if>
  517. <if test="province != null"> and sb.province = #{province}</if>
  518. <if test="city != null"> and sb.city = #{city}</if>
  519. <if test="region != null"> and sb.region = #{region}</if>
  520. <if test="communityId != null"> and sd.community_id = #{communityId}</if>
  521. <if test="buildingId != null"> and sd.building_id = #{buildingId}</if>
  522. <if test="deviceNo != null and deviceNo != ''"> AND (sd.device_no LIKE concat('%',#{deviceNo},'%') or sd.meter_no LIKE concat('%',#{deviceNo},'%') or sd.file_no LIKE concat('%',#{deviceNo},'%'))</if>
  523. <if test="clientName != null and clientName != ''"> AND sgm.customer_name LIKE concat('%',#{clientName},'%')</if>
  524. <if test="locDesc != null and locDesc != ''"> AND sd.loc_desc LIKE concat('%',#{locDesc},'%')</if>
  525. <if test="status != null and status != ''"> and sd.device_status = #{status}</if>
  526. <if test="valveStatus != null"> and sd.valve_status = #{valveStatus}</if>
  527. <if test="communityIds != null and communityIds.size() != 0">
  528. and sd.community_id in <foreach collection="communityIds" item="item" open="(" separator="," close=")">#{item}</foreach>
  529. </if>
  530. <if test="sortColumn != null and sortColumn != ''">
  531. <if test="sortOrder != null and sortOrder != ''">
  532. order by ${sortColumn} ${sortOrder}
  533. </if>
  534. </if>
  535. </select>
  536. <select id="deviceAreaList" resultType="com.zoniot.ccrc.dto.BuildingSelectInfoDto">
  537. select
  538. b.id,
  539. b.name,
  540. b.province,
  541. b.city,
  542. b.region,
  543. b.community_id as community,
  544. b.longitude,
  545. b.latitude,
  546. t1.device_count
  547. from sc_building b
  548. right join (
  549. select sd.building_id,count(1) as device_count
  550. from sc_device sd
  551. left join sc_grid_management sgm on(sgm.device_id = sd.id)
  552. <if test="province != null or city != null or region != null or communityId != null">left join sc_building sb on (sb.id = sd.building_id and sb.status = 1)</if>
  553. <if test="orgId != null">left join sc_community sc on(sc.id = sd.community_id)</if>
  554. where sd.status = 1
  555. <if test="siteId != null"> and sd.site_id = #{siteId}</if>
  556. <if test="userId != null"> and sgm.user_id = #{userId}</if>
  557. <if test="sysId != null"> and sd.sys_id = #{sysId}</if>
  558. <if test="deviceTypeId != null"> and sd.device_type_id = #{deviceTypeId}</if>
  559. <if test="orgId != null"> and sc.org_id = #{orgId}</if>
  560. <if test="province != null"> and sb.province = #{province}</if>
  561. <if test="city != null"> and sb.city = #{city}</if>
  562. <if test="region != null"> and sb.region = #{region}</if>
  563. <if test="communityId != null"> and sd.community_id = #{communityId}</if>
  564. <if test="buildingId != null"> and sd.building_id = #{buildingId}</if>
  565. <if test="deviceNo != null and deviceNo != ''"> AND (sd.device_no LIKE concat('%',#{deviceNo},'%') or sd.meter_no LIKE concat('%',#{deviceNo},'%') or sd.file_no LIKE concat('%',#{deviceNo},'%'))</if>
  566. <if test="clientName != null and clientName != ''"> AND sgm.customer_name LIKE concat('%',#{clientName},'%')</if>
  567. <if test="locDesc != null and locDesc != ''"> AND sd.loc_desc LIKE concat('%',#{locDesc},'%')</if>
  568. <if test="status != null and status != ''"> and sd.device_status = #{status}</if>
  569. <if test="valveStatus != null"> and sd.valve_status = #{valveStatus}</if>
  570. <if test="communityIds != null and communityIds.size() != 0">
  571. and sd.community_id in <foreach collection="communityIds" item="item" open="(" separator="," close=")">#{item}</foreach>
  572. </if>
  573. GROUP BY sd.building_id
  574. ) t1 on (t1.building_id = b.id)
  575. where b.status = 1
  576. <if test="siteId != null"> and b.site_id=#{siteId} </if>
  577. </select>
  578. <select id="getDeviceIdByUserNumber" resultType="java.lang.Long">
  579. select b.id
  580. from sc_grid_management a
  581. inner join sc_device b on b.id=a.device_id where a.id=#{id}
  582. </select>
  583. <select id="getBuildingStatistics" resultType="com.zoniot.ccrc.dto.BuildingInfoListDto">
  584. select
  585. sb.id as building_id,
  586. sb.name as building_name,
  587. sb.address ,
  588. sb.province,
  589. sb.city,
  590. sb.region,
  591. sb.community_id as community,
  592. sa1.name as province_name,
  593. sa2.name as city_name,
  594. sa3.name as region_name,
  595. sc.name as community_name,
  596. sb.latitude,
  597. sb.longitude,
  598. temp.device_count,
  599. temp.normal_count,
  600. temp.alarm_count,
  601. temp.fault_count,
  602. temp.offline_count,
  603. temp.device_unused_count
  604. from sc_building sb
  605. LEFT JOIN(
  606. SELECT
  607. sd.building_id,
  608. <if test="sysId != null">sd.sys_id,</if>
  609. count(1) as device_count,
  610. SUM(IF(sd.device_status = 1, 1, 0)) as normal_count,
  611. SUM(IF(sd.device_status = 2, 1, 0)) as alarm_count,
  612. SUM(IF(sd.device_status = 3, 1, 0)) as fault_count,
  613. SUM(IF(sd.device_status = 4, 1, 0)) as offline_count,
  614. SUM(IF(sd.device_status = 5, 1, 0)) as device_unused_count
  615. from sc_device sd
  616. <if test="userId != null"> left join sc_grid_management scm on(scm.device_id = sd.id) </if>
  617. WHERE sd.status = 1
  618. <if test="siteId != null"> and sd.site_id = #{siteId} </if>
  619. <if test="sysId != null"> and sd.sys_id = #{sysId} </if>
  620. <if test="userId != null"> and scm.user_id = #{userId} </if>
  621. <if test="communityIds != null and communityIds.size() != 0"> and sd.community_id in <foreach collection="communityIds" item="item" open="(" separator="," close=")">#{item}</foreach></if>
  622. GROUP BY sd.building_id <if test="sysId != null">,sd.sys_id</if>
  623. ) temp on (sb.id = temp.building_id)
  624. left join sc_area sa1 on sa1.id = sb.province
  625. left join sc_area sa2 on sa2.id = sb.city
  626. left join sc_area sa3 on sa3.id = sb.region
  627. left join sc_community sc on sc.id = sb.community_id
  628. where sb.status = 1
  629. <if test="siteId != null"> and sb.site_id = #{siteId} </if>
  630. <if test="sysId != null"> and temp.sys_id = #{sysId} </if>
  631. <if test="province != null"> AND sb.province = #{province}</if>
  632. <if test="city != null"> AND sb.city = #{city}</if>
  633. <if test="region != null"> AND sb.region = #{region}</if>
  634. <if test="community != null"> AND sb.community_id = #{community}</if>
  635. <if test="type != null and (type == 4 or type == 5) and longitudeMin != 0 and longitudeMax != 0"> and sb.longitude > #{longitudeMin} AND sb.longitude <![CDATA[ < ]]> #{longitudeMax} </if>
  636. <if test="type != null and (type == 4 or type == 5) and latitudeMin != 0 and latitudeMax != 0"> and sb.latitude > #{latitudeMin} AND sb.latitude <![CDATA[ < ]]> #{latitudeMax} </if>
  637. order by sb.update_date desc
  638. </select>
  639. </mapper>