DeviceMapper.xml 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  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.aijiuyi.admin.mapper.DeviceMapper">
  4. <!--
  5. 分页查询设备列表,LEFT JOIN user_device + user_profile 聚合绑定用户数和姓名
  6. bound_user_count:已绑定且未删除的用户数
  7. bound_user_names:已绑定用户姓名,顿号分隔
  8. 绑定用户姓名过滤使用子查询,避免 LEFT JOIN 过滤导致无用户设备丢失
  9. -->
  10. <select id="pageWithUsers" resultType="com.aijiuyi.admin.entity.Device">
  11. SELECT
  12. d.id,
  13. d.device_code,
  14. d.device_name,
  15. d.device_model,
  16. d.serial_no,
  17. d.firmware_version,
  18. d.online_status,
  19. d.last_online_time,
  20. d.province_code,
  21. d.city_code,
  22. d.district_code,
  23. d.address,
  24. d.remark,
  25. d.deleted,
  26. d.create_time,
  27. d.update_time,
  28. COUNT(DISTINCT ud.user_id) AS bound_user_count,
  29. GROUP_CONCAT(DISTINCT up.name ORDER BY ud.bind_time DESC SEPARATOR '、') AS bound_user_names
  30. FROM device d
  31. LEFT JOIN user_device ud ON ud.device_code = d.device_code
  32. AND ud.deleted = 0
  33. AND ud.status = 1
  34. LEFT JOIN user_profile up ON up.id = ud.user_id
  35. AND up.deleted = 0
  36. WHERE d.deleted = 0
  37. <if test="q.deviceCode != null and q.deviceCode != ''">
  38. AND d.device_code LIKE CONCAT('%', #{q.deviceCode}, '%')
  39. </if>
  40. <if test="q.deviceName != null and q.deviceName != ''">
  41. AND d.device_name LIKE CONCAT('%', #{q.deviceName}, '%')
  42. </if>
  43. <if test="q.onlineStatus != null">
  44. AND d.online_status = #{q.onlineStatus}
  45. </if>
  46. <if test="q.firmwareVersion != null and q.firmwareVersion != ''">
  47. AND d.firmware_version = #{q.firmwareVersion}
  48. </if>
  49. <if test="q.provinceCode != null and q.provinceCode != ''">
  50. AND d.province_code = #{q.provinceCode}
  51. </if>
  52. <if test="q.cityCode != null and q.cityCode != ''">
  53. AND d.city_code = #{q.cityCode}
  54. </if>
  55. <if test="q.districtCode != null and q.districtCode != ''">
  56. AND d.district_code = #{q.districtCode}
  57. </if>
  58. <if test="q.boundUserName != null and q.boundUserName != ''">
  59. AND d.device_code IN (
  60. SELECT ud2.device_code FROM user_device ud2
  61. JOIN user_profile up2 ON up2.id = ud2.user_id
  62. WHERE up2.name LIKE CONCAT('%', #{q.boundUserName}, '%')
  63. AND ud2.status = 1
  64. AND ud2.deleted = 0
  65. AND up2.deleted = 0
  66. )
  67. </if>
  68. GROUP BY d.id
  69. ORDER BY d.create_time DESC
  70. </select>
  71. <!--
  72. 根据设备编号查询绑定的用户列表(联表 user_device + user_profile)
  73. 返回 DeviceUserVO 视图对象:bindId/userId/userName/phone/bindTime/isPrimary
  74. -->
  75. <select id="getUsersByDeviceCode" resultType="com.aijiuyi.admin.entity.DeviceUserVO">
  76. SELECT
  77. ud.id AS bind_id,
  78. ud.user_id,
  79. up.name AS user_name,
  80. up.phone,
  81. ud.bind_time,
  82. ud.is_primary
  83. FROM user_device ud
  84. JOIN user_profile up ON up.id = ud.user_id
  85. AND up.deleted = 0
  86. WHERE ud.device_code = #{deviceCode}
  87. AND ud.deleted = 0
  88. AND ud.status = 1
  89. ORDER BY ud.is_primary DESC, ud.bind_time ASC
  90. </select>
  91. </mapper>