<?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.ycwl.basic.mapper.DeviceMapper">
    <insert id="add">
        insert into device(id, scenic_id, name, no, latitude, longitude) values (#{id}, #{scenicId}, #{name}, #{no}, #{latitude}, #{longitude})
    </insert>
    <insert id="addConfig">
        insert into device_config(id, device_id, create_time)
        values (#{id}, #{deviceId}, now())
    </insert>
    <insert id="addEntity">
        insert into device(id, name, no, status, online, ip_addr, keepalive_at, create_at, update_at)
        values (#{id}, #{name}, #{no}, 0, #{online}, #{ipAddr}, #{keepaliveAt}, now(), now())
    </insert>
    <update id="update">
        update device set scenic_id = #{scenicId}, name = #{name}, no = #{no}, longitude = #{longitude}, latitude = #{latitude}, update_at = now() where id = #{id}
    </update>
    <update id="updateStatus">
        update device
        set status = (CASE
            status
                          WHEN 1 THEN
                              0
                          WHEN 0 THEN
                              1
                          ELSE null
            END)
        where id = #{id}
    </update>
    <update id="updateConfig">
        update device_config
        set viid_type = #{viidType},
            store_type = #{storeType},
            store_config_json = #{storeConfigJson},
            store_expire_day = #{storeExpireDay},
            online_check = #{onlineCheck},
            online_max_interval = #{onlineMaxInterval},
            cut_pre = #{cutPre},
            cut_post = #{cutPost},
            enable_pre_book = #{enablePreBook},
            image_free = #{imageFree},
            video_free = #{videoFree},
            pair_device = #{pairDevice}
        where id = #{id}
    </update>
    <update id="updateEntity">
        update device
        set name = #{name},
            no = #{no},
            online = #{online},
            ip_addr = #{ipAddr},
            keepalive_at = #{keepaliveAt},
            update_at = now()
        where id = #{id}
    </update>
    <update id="updateOnlineStatus">
        update device
        set online = #{online},
            <if test="ipAddr != null">ip_addr = #{ipAddr},</if>
            keepalive_at = #{keepaliveAt},
            update_at = now()
        where id = #{id}
    </update>
    <update id="updateSort">
        update device
        set sort = #{sort}
        where id = #{id}
    </update>
    <delete id="deleteById">
        delete from device where id = #{id}
    </delete>
    <select id="list" resultType="com.ycwl.basic.model.pc.device.resp.DeviceRespVO">
        select d.id, scenic_id, d.name, no, d.longitude, d.latitude, d.status, create_at, d.update_at, s.name scenic_name, d.keepalive_at, d.online, p.wvp_device_no as device_no, p.wvp_channel_no channel_no,
        (select s.url from source s where s.device_id=d.id order by id desc limit 1) coverUrl
        from device d
        left join scenic s on d.scenic_id = s.id
        left join device_preview_config p on d.id = p.device_id and p.status = 1
        <where>
            <if test="name!= null and name!= ''">
                and d.`name` like concat('%', #{name}, '%')
            </if>
            <if test="no!= null and no!= ''">
                and `no` like concat('%', #{no}, '%')
            </if>
            <if test="status!= null">
                and d.`status` = #{status}
            </if>
            <if test="scenicId!= null and scenicId!= ''">
                and scenic_id = #{scenicId}
            </if>
            <if test="startTime!=null">
                and d.create_at >= #{startTime}
            </if>
            <if test="endTime!=null">
                and d.create_at &lt;= #{endTime}
            </if>
        </where>
        order by d.scenic_id desc, sort asc
    </select>
    <select id="getById" resultType="com.ycwl.basic.model.pc.device.resp.DeviceRespVO">
        select d.id, scenic_id, d.name, no, d.longitude, d.latitude, d.status, create_at, d.update_at, s.name scenic_name
        from device d
                 left join scenic s on d.scenic_id = s.id
        where d.id = #{id}
    </select>
    <select id="listByScenicIdWithWVP" resultType="com.ycwl.basic.model.pc.device.resp.DeviceRespVO">
        select d.id, d.name, no, d.status, create_at, d.update_at, p.wvp_device_no as device_no, p.wvp_channel_no channel_no
        from device d
        left join device_preview_config p on d.id = p.device_id and p.status = 1
        where d.scenic_id = #{scenicId}
    </select>
    <select id="deviceCountByScenicId" resultType="com.ycwl.basic.model.mobile.scenic.ScenicDeviceCountVO">
        select count(1) totalDeviceCount
        from device
        where scenic_id = #{scenicId} and status = 1
    </select>
    <select id="getConfigByDeviceId" resultType="com.ycwl.basic.model.pc.device.entity.DeviceConfigEntity">
        select *
        from device_config
        where device_id = #{deviceId}
        limit 1
    </select>
    <select id="getByDeviceId" resultType="com.ycwl.basic.model.pc.device.entity.DeviceEntity">
        select *
        from device
        where id = #{deviceId}
    </select>
    <select id="getByDeviceNo" resultType="com.ycwl.basic.model.pc.device.entity.DeviceEntity">
        select *
        from device
        where no = #{deviceNo}
    </select>
    <select id="listAll" resultType="com.ycwl.basic.model.pc.device.entity.DeviceEntity">
        select *
        from device
        where status = 1
        order by sort
    </select>
    <select id="getByDeviceNo2" resultType="com.ycwl.basic.model.pc.device.entity.DeviceEntity">
        select *
        from device
        where no_2 = #{deviceNo}
        limit 1
    </select>
    <select id="listAllByScenicId" resultType="java.lang.Long">
        select id
        from device
        where scenic_id = #{scenicId}
        order by sort
    </select>
</mapper>