<?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="org.dromara.member.mapper.AppDeviceMapper">

    <resultMap type="org.dromara.member.domain.AppDevice" id="AppDeviceResult">
        <id property="deviceId" column="device_id"/>
        <result property="userId" column="user_id"/>
        <result property="deviceType" column="device_type"/>
        <result property="deviceName" column="device_name"/>
        <result property="deviceModel" column="device_model"/>
        <result property="deviceBrand" column="device_brand"/>
        <result property="osVersion" column="os_version"/>
        <result property="appVersion" column="app_version"/>
        <result property="pushToken" column="push_token"/>
        <result property="isActive" column="is_active"/>
        <result property="lastActiveTime" column="last_active_time"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <resultMap type="org.dromara.member.domain.vo.AppDeviceVo" id="AppDeviceVoResult">
        <id property="deviceId" column="device_id"/>
        <result property="userId" column="user_id"/>
        <result property="deviceType" column="device_type"/>
        <result property="deviceName" column="device_name"/>
        <result property="deviceModel" column="device_model"/>
        <result property="deviceBrand" column="device_brand"/>
        <result property="osVersion" column="os_version"/>
        <result property="appVersion" column="app_version"/>
        <result property="pushToken" column="push_token"/>
        <result property="isActive" column="is_active"/>
        <result property="lastActiveTime" column="last_active_time"/>
        <result property="createTime" column="create_time"/>
        <result property="updateTime" column="update_time"/>
    </resultMap>

    <sql id="selectAppDeviceVo">
        select device_id,
               user_id,
               device_type,
               device_name,
               device_model,
               device_brand,
               os_version,
               app_version,
               push_token,
               is_active,
               last_active_time,
               create_time,
               update_time
        from app_device
    </sql>

    <!-- 查询设备列表 -->
    <select id="selectAppDeviceList" parameterType="AppDevice" resultMap="AppDeviceVoResult">
        <include refid="selectAppDeviceVo"/>
        <where>
            <if test="userId != null">and user_id = #{userId}</if>
            <if test="deviceType != null and deviceType != ''">and device_type = #{deviceType}</if>
            <if test="deviceName != null and deviceName != ''">and device_name like concat('%', #{deviceName}, '%')</if>
            <if test="isActive != null">and is_active = #{isActive}</if>
        </where>
        order by create_time desc
    </select>

    <!-- 根据设备ID查询设备信息 -->
    <select id="selectAppDeviceByDeviceId" parameterType="Long" resultMap="AppDeviceVoResult">
        <include refid="selectAppDeviceVo"/>
        where device_id = #{deviceId}
    </select>

    <!-- 根据用户ID查询设备列表 -->
    <select id="selectAppDeviceByUserId" parameterType="Long" resultMap="AppDeviceVoResult">
        <include refid="selectAppDeviceVo"/>
        where user_id = #{userId} and is_active = '1'
        order by last_active_time desc
    </select>

    <!-- 根据推送令牌查询设备 -->
    <select id="selectAppDeviceByPushToken" parameterType="String" resultMap="AppDeviceResult">
        <include refid="selectAppDeviceVo"/>
        where push_token = #{pushToken} and is_active = '1'
    </select>

    <!-- 新增设备信息 -->
    <insert id="insertAppDevice" parameterType="org.dromara.member.domain.AppDevice" useGeneratedKeys="true"
            keyProperty="deviceId">
        insert into app_device
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="userId != null">user_id,</if>
            <if test="deviceType != null and deviceType != ''">device_type,</if>
            <if test="deviceName != null and deviceName != ''">device_name,</if>
            <if test="deviceModel != null and deviceModel != ''">device_model,</if>
            <if test="deviceBrand != null and deviceBrand != ''">device_brand,</if>
            <if test="osVersion != null and osVersion != ''">os_version,</if>
            <if test="appVersion != null and appVersion != ''">app_version,</if>
            <if test="pushToken != null and pushToken != ''">push_token,</if>
            <if test="isActive != null">is_active,</if>
            <if test="lastActiveTime != null">last_active_time,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateTime != null">update_time,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="userId != null">#{userId},</if>
            <if test="deviceType != null and deviceType != ''">#{deviceType},</if>
            <if test="deviceName != null and deviceName != ''">#{deviceName},</if>
            <if test="deviceModel != null and deviceModel != ''">#{deviceModel},</if>
            <if test="deviceBrand != null and deviceBrand != ''">#{deviceBrand},</if>
            <if test="osVersion != null and osVersion != ''">#{osVersion},</if>
            <if test="appVersion != null and appVersion != ''">#{appVersion},</if>
            <if test="pushToken != null and pushToken != ''">#{pushToken},</if>
            <if test="isActive != null">#{isActive},</if>
            <if test="lastActiveTime != null">#{lastActiveTime},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateTime != null">#{updateTime},</if>
        </trim>
    </insert>

    <!-- 修改设备信息 -->
    <update id="updateAppDevice" parameterType="org.dromara.member.domain.AppDevice">
        update app_device
        <trim prefix="SET" suffixOverrides=",">
            <if test="deviceType != null and deviceType != ''">device_type = #{deviceType},</if>
            <if test="deviceName != null and deviceName != ''">device_name = #{deviceName},</if>
            <if test="deviceModel != null and deviceModel != ''">device_model = #{deviceModel},</if>
            <if test="deviceBrand != null and deviceBrand != ''">device_brand = #{deviceBrand},</if>
            <if test="osVersion != null and osVersion != ''">os_version = #{osVersion},</if>
            <if test="appVersion != null and appVersion != ''">app_version = #{appVersion},</if>
            <if test="pushToken != null and pushToken != ''">push_token = #{pushToken},</if>
            <if test="isActive != null">is_active = #{isActive},</if>
            <if test="lastActiveTime != null">last_active_time = #{lastActiveTime},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where device_id = #{deviceId}
    </update>

    <!-- 删除设备信息 -->
    <delete id="deleteAppDeviceByDeviceId" parameterType="Long">
        delete
        from app_device
        where device_id = #{deviceId}
    </delete>

    <!-- 批量删除设备信息 -->
    <delete id="deleteAppDeviceByDeviceIds" parameterType="String">
        delete from app_device where device_id in
        <foreach item="deviceId" collection="array" open="(" separator="," close=")">
            #{deviceId}
        </foreach>
    </delete>

    <!-- 更新设备活跃状态 -->
    <update id="updateDeviceActiveStatus">
        update app_device
        set is_active        = #{isActive},
            last_active_time = #{lastActiveTime}
        where device_id = #{deviceId}
    </update>

</mapper>