<?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.equipment.mapper.DeviceMapper">
    <resultMap id="BaseResultMap" type="com.fuyuanshen.equipment.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.device_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.equipment.domain.Device">
        select d.* ,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.deviceImei != null">
                and d.device_imei = #{criteria.deviceImei}
            </if>
            <if test="criteria.deviceTypeId != null">
                and d.device_type = #{criteria.deviceTypeId}
            </if>
            <if test="criteria.deviceStatus != null">
                -- and da.active = #{criteria.deviceStatus}
            </if>
            <if test="criteria.currentOwnerId != null">
                d.current_owner_id = #{criteria.currentOwnerId}
            </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>
        </where>
    </select>

    <select id="findAllDevices" resultType="com.fuyuanshen.equipment.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.deviceTypeId != null">
                and d.device_type = #{criteria.deviceTypeId}
            </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>