<?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.app.mapper.equipment.APPDeviceMapper">
    <resultMap id="BaseResultMap" type="com.fuyuanshen.app.domain.APPDevice">
        <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>

    <!-- APP用户设备列表 -->
    <select id="appDeviceList" resultType="com.fuyuanshen.app.domain.APPDevice">
        select d.* from app_device as 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 criteria.deviceMac.trim() != ''">
                and d.device_mac = #{criteria.deviceMac}
            </if>
            <if test="criteria.deviceImei != null and criteria.deviceImei.trim() != ''">
                and d.device_imei = #{criteria.deviceImei}
            </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>
            and d.customer_id = #{criteria.customerId}
        </where>
        order by d.create_time desc
    </select>

    <!-- 分页查询APP/小程序设备 -->
    <select id="queryAll" resultType="com.fuyuanshen.app.domain.APPDevice">
        select d.* from app_device as 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 criteria.deviceMac.trim() != ''">
                and d.device_mac = #{criteria.deviceMac}
            </if>
            <if test="criteria.deviceImei != null and criteria.deviceImei.trim() != ''">
                and d.device_imei = #{criteria.deviceImei}
            </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>
            and d.customer_id = #{criteria.customerId}
        </where>
        order by d.create_time desc
    </select>

    <select id="getTypeList" resultType="com.fuyuanshen.app.domain.vo.APPDeviceTypeVo">
        SELECT dt.id, dt.type_name, dt.communication_mode
        FROM device_type dt
        WHERE EXISTS(select 1
                     from device d
                     where d.device_type = dt.id
                       and d.binding_user_id = #{userId}
                       AND d.binding_status = 1)
    </select>


</mapper>