<?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 *
        FROM (
        SELECT
        da.id AS id, d.device_name, d.bluetooth_name,
        d.pub_topic, d.sub_topic, d.device_pic,
        d.device_mac, d.device_sn, d.update_by,
        d.device_imei, d.update_time, dg.id AS device_type,
        d.remark, d.binding_status, t.type_name AS typeName,
        da.assignee_id AS customerId, da.assignee_name AS customerName,
        da.active AS deviceStatus, da.create_time AS create_time,
        da.assigner_name AS createByName, da.id AS assignId,
        ROW_NUMBER() OVER (PARTITION BY d.id ORDER BY da.create_time DESC) AS rn
        FROM device d
        LEFT JOIN device_type t ON d.device_type = t.id
        LEFT JOIN device_type_grants dg ON dg.device_type_id = t.id
        LEFT JOIN device_assignments da ON da.device_id = d.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.deviceType != null">
                and d.device_type = #{criteria.deviceType}
            </if>
            <if test="criteria.deviceStatus != null">
                and da.active = #{criteria.deviceStatus}
            </if>
            <if test="criteria.params.beginTime != null and criteria.params.endTime != null">
                and da.create_time between #{criteria.params.beginTime} and #{criteria.params.endTime}
            </if>
            AND da.assignee_id = #{criteria.currentOwnerId}
            AND dg.customer_id = #{criteria.currentOwnerId}
        </where>
        ) AS ranked
        WHERE rn = 1
        ORDER BY create_time DESC
    </select>


    <select id="findAllDevices" resultType="com.fuyuanshen.equipment.domain.Device">
        select
        d.id, d.customer_id, d.device_name,d.bluetooth_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>

    <!-- 根据条件查询 -->
    <select id="findDevices" resultType="com.fuyuanshen.equipment.domain.Device"
            parameterType="com.fuyuanshen.equipment.domain.query.DeviceQueryCriteria">
        SELECT d.*
        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.deviceImei != null">
                and d.device_imei = #{criteria.deviceImei}
            </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.currentOwnerId != null">
                and d.current_owner_id = #{criteria.currentOwnerId}
            </if>
            <if test="criteria.params.beginTime != null and criteria.params.endTime != null">
                and d.create_time between #{criteria.params.beginTime} and #{criteria.params.endTime}
            </if>
        </where>
    </select>

    <!-- 查询APP绑定设备列表 -->
    <select id="queryAppBindDeviceList" resultType="com.fuyuanshen.equipment.domain.vo.AppDeviceVo">
        select d.id, d.device_name, d.device_name,
        d.device_name,
        d.device_mac,
        d.device_sn,
        d.device_imei,
        d.device_pic,
        dt.type_name,
        dt.communication_mode,
        d.bluetooth_name,
        d.binding_time
        from device d
        inner join device_type dt on d.device_type = dt.id
        where d.binding_user_id = #{criteria.bindingUserId}
        <if test="criteria.deviceType != null">
            and d.device_type = #{criteria.deviceType}
        </if>
        <if test="criteria.deviceName != null">
            and d.device_name = #{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>
    </select>


    <select id="queryAppDeviceList" resultType="com.fuyuanshen.equipment.domain.vo.AppDeviceVo">
        select d.id,
        d.device_name,
        d.device_mac,
        d.device_sn,
        d.device_imei,
        d.device_pic,
        dt.type_name,
        dt.communication_mode,
        d.bluetooth_name
        from device d
        inner join device_type dt on d.device_type = dt.id
        where d.binding_user_id = #{criteria.bindingUserId}
        <if test="criteria.deviceType != null">
            and d.device_type = #{criteria.deviceType}
        </if>
        <if test="criteria.deviceName != null">
            and d.device_name = #{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>
    </select>

    <!-- 获取分配设备的客户 -->
    <select id="getAssignCustomer" resultType="com.fuyuanshen.equipment.domain.Device">
        SELECT *
        FROM device
        WHERE original_device_id = #{customerId}
          AND device_status = 1
    </select>

    <!-- 获取设备链 -->
    <select id="findByOriginalDeviceId" resultType="com.fuyuanshen.equipment.domain.Device"
            parameterType="java.lang.Long">
        SELECT id, original_device_id
        FROM device
        WHERE original_device_id = #{originalDeviceId}
    </select>

</mapper>