| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394 |
- <?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.aijiuyi.admin.mapper.DeviceMapper">
- <!--
- 分页查询设备列表,LEFT JOIN user_device + user_profile 聚合绑定用户数和姓名
- bound_user_count:已绑定且未删除的用户数
- bound_user_names:已绑定用户姓名,顿号分隔
- 绑定用户姓名过滤使用子查询,避免 LEFT JOIN 过滤导致无用户设备丢失
- -->
- <select id="pageWithUsers" resultType="com.aijiuyi.admin.entity.Device">
- SELECT
- d.id,
- d.device_code,
- d.device_name,
- d.device_model,
- d.serial_no,
- d.firmware_version,
- d.online_status,
- d.last_online_time,
- d.province_code,
- d.city_code,
- d.district_code,
- d.address,
- d.remark,
- d.deleted,
- d.create_time,
- d.update_time,
- COUNT(DISTINCT ud.user_id) AS bound_user_count,
- GROUP_CONCAT(DISTINCT up.name ORDER BY ud.bind_time DESC SEPARATOR '、') AS bound_user_names
- FROM device d
- LEFT JOIN user_device ud ON ud.device_code = d.device_code
- AND ud.deleted = 0
- AND ud.status = 1
- LEFT JOIN user_profile up ON up.id = ud.user_id
- AND up.deleted = 0
- WHERE d.deleted = 0
- <if test="q.deviceCode != null and q.deviceCode != ''">
- AND d.device_code LIKE CONCAT('%', #{q.deviceCode}, '%')
- </if>
- <if test="q.deviceName != null and q.deviceName != ''">
- AND d.device_name LIKE CONCAT('%', #{q.deviceName}, '%')
- </if>
- <if test="q.onlineStatus != null">
- AND d.online_status = #{q.onlineStatus}
- </if>
- <if test="q.firmwareVersion != null and q.firmwareVersion != ''">
- AND d.firmware_version = #{q.firmwareVersion}
- </if>
- <if test="q.provinceCode != null and q.provinceCode != ''">
- AND d.province_code = #{q.provinceCode}
- </if>
- <if test="q.cityCode != null and q.cityCode != ''">
- AND d.city_code = #{q.cityCode}
- </if>
- <if test="q.districtCode != null and q.districtCode != ''">
- AND d.district_code = #{q.districtCode}
- </if>
- <if test="q.boundUserName != null and q.boundUserName != ''">
- AND d.device_code IN (
- SELECT ud2.device_code FROM user_device ud2
- JOIN user_profile up2 ON up2.id = ud2.user_id
- WHERE up2.name LIKE CONCAT('%', #{q.boundUserName}, '%')
- AND ud2.status = 1
- AND ud2.deleted = 0
- AND up2.deleted = 0
- )
- </if>
- GROUP BY d.id
- ORDER BY d.create_time DESC
- </select>
- <!--
- 根据设备编号查询绑定的用户列表(联表 user_device + user_profile)
- 返回 DeviceUserVO 视图对象:bindId/userId/userName/phone/bindTime/isPrimary
- -->
- <select id="getUsersByDeviceCode" resultType="com.aijiuyi.admin.entity.DeviceUserVO">
- SELECT
- ud.id AS bind_id,
- ud.user_id,
- up.name AS user_name,
- up.phone,
- ud.bind_time,
- ud.is_primary
- FROM user_device ud
- JOIN user_profile up ON up.id = ud.user_id
- AND up.deleted = 0
- WHERE ud.device_code = #{deviceCode}
- AND ud.deleted = 0
- AND ud.status = 1
- ORDER BY ud.is_primary DESC, ud.bind_time ASC
- </select>
- </mapper>
|