Files
syfadmin/fys-system/src/main/resources/mapper/system/DeviceMapper.xml
2025-06-19 18:09:03 +08:00

169 lines
7.5 KiB
XML

<?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.fuyuanshen.modules.system.mapper.DeviceMapper">
<resultMap id="BaseResultMap" type="com.fuyuanshen.modules.system.domain.Device">
<id column="id" property="id"/>
<result column="device_type" property="deviceType"/>
<result column="customer_id" property="customerId"/>
<!--<result column="device_no" property="deviceNo"/>-->
<result column="device_name" property="deviceName"/>
<result column="device_pic" property="devicePic"/>
<result column="device_mac" property="deviceMac"/>
<result column="device_sn" property="deviceSn"/>
<result column="device_status" property="deviceStatus"/>
<result column="remark" property="remark"/>
<result column="create_by" property="createBy"/>
<result column="update_by" property="updateBy"/>
<result column="create_time" property="createTime"/>
<result column="update_time" property="updateTime"/>
<result column="binding_status" property="bindingStatus"/>
</resultMap>
<sql id="Base_Column_List">
id
, device_type, customer_id, device_name, device_pic, device_mac, device_sn, remark, create_by, update_by, create_time, update_time
</sql>
<sql id="device_Column_List">
d.id,
d.device_type, d.customer_id, d.device_name, d.device_pic, d.device_mac,
d.device_sn, d.remark, d.create_by, d.update_by, d.create_time, d.update_time,
d.binding_status, d.current_owner_id, d.original_owner_id, d.customer_name,
d.device_imei, d.longitude, d.latitude, d.tenant_id
</sql>
<!-- 分页查询设备 -->
<select id="findAll" resultType="com.fuyuanshen.modules.system.domain.Device">
select
<include refid="device_Column_List" />,
t.type_name as typeName
from device d
left join device_type t
on d.device_type = t.id
<where>
<!-- 时间范围等其他条件保持原样 -->
<if test="criteria.deviceName != null and criteria.deviceName.trim() != ''">
and d.device_name like concat('%', TRIM(#{criteria.deviceName}), '%')
</if>
<if test="criteria.deviceMac != null">
and d.device_mac = #{criteria.deviceMac}
</if>
<if test="criteria.deviceSn != null">
and d.device_sn = #{criteria.deviceSn}
</if>
<if test="criteria.deviceType != null">
and d.device_type = #{criteria.deviceType}
</if>
<if test="criteria.deviceStatus != null">
and d.device_status = #{criteria.deviceStatus}
</if>
<if test="criteria.createTime != null and criteria.createTime.size() != 0">
and d.create_time between #{criteria.createTime[0]} and #{criteria.createTime[1]}
</if>
<if test="criteria.tenantId != null">
AND tenant_id = #{criteria.tenantId}
</if>
<!-- 下面这两个条件只需满足一个 -->
<!-- <if test="criteria.customerId != null"> -->
<!-- AND ( -->
<!-- tenant_id = #{criteria.tenantId} -->
<!-- OR d.customer_id = #{criteria.customerId} -->
<!-- ) -->
<!-- </if> -->
<!-- <if test="criteria.customerId == null"> -->
<!-- AND tenant_id = #{criteria.tenantId} -->
<!-- </if> -->
</where>
order by d.id desc
</select>
<select id="findAll1" resultType="com.fuyuanshen.modules.system.domain.Device">
select
d.id, d.customer_id, d.customer_name, d.device_name,
d.device_pic, d.device_mac, d.device_sn, d.create_by, d.update_by,
d.create_time, d.update_time, d.device_type, d.remark, d.device_status, t.type_name
from device d
left join device_type t
on d.device_type = t.id
<where>
<!-- 时间范围等其他条件保持原样 -->
<if test="criteria.deviceName != null and criteria.deviceName.trim() != ''">
and d.device_name like concat('%', TRIM(#{criteria.deviceName}), '%')
</if>
<if test="criteria.deviceMac != null">
and d.device_mac = #{criteria.deviceMac}
</if>
<if test="criteria.deviceSn != null">
and d.device_sn = #{criteria.deviceSn}
</if>
<if test="criteria.deviceType != null">
and d.device_type = #{criteria.deviceType}
</if>
<if test="criteria.createTime != null and criteria.createTime.size() != 0">
and d.create_time between #{criteria.createTime[0]} and #{criteria.createTime[1]}
</if>
<!-- 将两个客户相关条件合并成一个逻辑组 -->
<if test="criteria.customerId != null or (criteria.customerIds != null and !criteria.customerIds.isEmpty())">
AND (
<!-- 条件1: 用户或其下属创建,且 customer_id 为空 -->
<if test="criteria.customerId != null">
d.create_by IN (
SELECT username FROM sys_user
WHERE user_id = #{criteria.customerId} OR pid = #{criteria.customerId}
) AND d.customer_id IS NULL
</if>
<!-- 如果前面的条件不存在,则只保留第二个条件 -->
<choose>
<when test="criteria.customerId != null and (criteria.customerIds != null and !criteria.customerIds.isEmpty())">
OR
</when>
<when test="criteria.customerId == null and (criteria.customerIds != null and !criteria.customerIds.isEmpty())">
<!-- 仅当第一个条件不存在时才开始括号 -->
</when>
</choose>
<!-- 条件2: 客户ID在列表中 -->
<if test="criteria.customerIds != null and !criteria.customerIds.isEmpty()">
(d.customer_id IN
<foreach item="customerId" collection="criteria.customerIds" open="(" separator="," close=")">
#{customerId}
</foreach>
)
</if>
)
</if>
</where>
order by d.id desc
</select>
<select id="findAllDevices" resultType="com.fuyuanshen.modules.system.domain.Device">
select
d.id, d.customer_id, d.device_name,
d.device_pic, d.device_mac, d.device_sn, d.create_by, d.update_by,
d.create_time, d.update_time, d.longitude, d.latitude, d.remark
from device d
<where>
<if test="criteria.deviceName != null and criteria.deviceName.trim() != ''">
and d.device_name like concat('%', TRIM(#{criteria.deviceName}), '%')
</if>
<if test="criteria.deviceMac != null">
and d.device_mac = #{criteria.deviceMac}
</if>
<if test="criteria.deviceType != null">
and d.device_type = #{criteria.deviceType}
</if>
<if test="criteria.createTime != null and criteria.createTime.size() != 0">
and d.create_time between #{criteria.createTime[0]} and #{criteria.createTime[1]}
</if>
</where>
order by d.id desc
</select>
</mapper>