<?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.ruoyi.base.mapper.RobotOpsPlayPlanMapper">

    <resultMap type="RobotOpsPlayPlan" id="RobotOpsPlayPlanResult">
        <result property="id"    column="id"    />
        <result property="planName"    column="plan_name"    />
        <result property="loopMode"    column="loop_mode"    />
        <result property="assetCount"    column="asset_count"    />
        <result property="status"    column="status"    />
        <result property="remark"    column="remark"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <resultMap id="RobotOpsPlayPlanRobotOpsPlayPlanItemResult" type="RobotOpsPlayPlan" extends="RobotOpsPlayPlanResult">
        <collection property="robotOpsPlayPlanItemList" ofType="RobotOpsPlayPlanItem" column="id" select="selectRobotOpsPlayPlanItemList" />
    </resultMap>

    <resultMap type="RobotOpsPlayPlanItem" id="RobotOpsPlayPlanItemResult">
        <result property="id"    column="id"    />
        <result property="planId"    column="plan_id"    />
        <result property="assetId"    column="asset_id"    />
        <result property="playOrder"    column="play_order"    />
        <result property="staySeconds"    column="stay_seconds"    />
        <result property="transitionType"    column="transition_type"    />
        <result property="fileUrl"    column="file_url"    />
        <result property="assetType"    column="asset_type"    />
        <result property="assetName"    column="asset_name"    />
        <result property="thumbnailUrl"    column="thumbnail_url"    />
        <result property="resolution"    column="resolution"    />
        <result property="durationSeconds"    column="duration_seconds"/>
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectRobotOpsPlayPlanVo">
        select p.id, p.plan_name, p.loop_mode, p.asset_count, p.status, p.remark, p.create_by, p.create_time, p.update_by, p.update_time, count(i.id) as asset_count
        from robot_ops_play_plan p
                 left join robot_ops_play_plan_item i on p.id = i.plan_id
        group by p.id
    </sql>

    <select id="selectRobotOpsPlayPlanList" parameterType="RobotOpsPlayPlan" resultMap="RobotOpsPlayPlanResult">
        select p.id, p.plan_name, p.loop_mode, p.status, p.remark, p.create_by, p.create_time, p.update_by, p.update_time, count(i.id) as asset_count
        from robot_ops_play_plan p
        left join robot_ops_play_plan_item i on p.id = i.plan_id
        <where>
            <if test="planName != null  and planName != ''"> and p.plan_name like concat('%', #{planName}, '%')</if>
            <if test="loopMode != null  and loopMode != ''"> and p.loop_mode = #{loopMode}</if>
            <if test="status != null  and status != ''"> and p.status = #{status}</if>
        </where>
        group by p.id, p.plan_name, p.loop_mode, p.status, p.remark, p.create_by, p.create_time, p.update_by, p.update_time
    </select>

    <select id="selectRobotOpsPlayPlanById" parameterType="Long" resultMap="RobotOpsPlayPlanRobotOpsPlayPlanItemResult">
        select id, plan_name, loop_mode, asset_count, status, remark, create_by, create_time, update_by, update_time
        from robot_ops_play_plan
        where id = #{id}
    </select>

    <select id="selectRobotOpsPlayPlanItemList" resultMap="RobotOpsPlayPlanItemResult">
        select i.id, i.plan_id, i.asset_id, i.play_order, i.stay_seconds, i.transition_type, a.file_url, a.asset_type, a.asset_name, a.thumbnail_url, a.resolution, a.duration_seconds, i.create_by, i.create_time, i.update_by, i.update_time
        from robot_ops_play_plan_item i
                 left join robot_ops_media_asset a on i.asset_id = a.id
        where i.plan_id = #{plan_id}
    </select>

    <insert id="insertRobotOpsPlayPlan" parameterType="RobotOpsPlayPlan" useGeneratedKeys="true" keyProperty="id">
        insert into robot_ops_play_plan
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="planName != null and planName != ''">plan_name,</if>
            <if test="loopMode != null and loopMode != ''">loop_mode,</if>
            <if test="assetCount != null">asset_count,</if>
            <if test="status != null and status != ''">status,</if>
            <if test="remark != null">remark,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="planName != null and planName != ''">#{planName},</if>
            <if test="loopMode != null and loopMode != ''">#{loopMode},</if>
            <if test="assetCount != null">#{assetCount},</if>
            <if test="status != null and status != ''">#{status},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateRobotOpsPlayPlan" parameterType="RobotOpsPlayPlan">
        update robot_ops_play_plan
        <trim prefix="SET" suffixOverrides=",">
            <if test="planName != null and planName != ''">plan_name = #{planName},</if>
            <if test="loopMode != null and loopMode != ''">loop_mode = #{loopMode},</if>
            <if test="assetCount != null">asset_count = #{assetCount},</if>
            <if test="status != null and status != ''">status = #{status},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteRobotOpsPlayPlanById" parameterType="Long">
        delete from robot_ops_play_plan where id = #{id}
    </delete>

    <delete id="deleteRobotOpsPlayPlanByIds" parameterType="String">
        delete from robot_ops_play_plan where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>

    <delete id="deleteRobotOpsPlayPlanItemByPlanIds" parameterType="String">
        delete from robot_ops_play_plan_item where plan_id in
        <foreach item="planId" collection="array" open="(" separator="," close=")">
            #{planId}
        </foreach>
    </delete>

    <delete id="deleteRobotOpsPlayPlanItemByPlanId" parameterType="Long">
        delete from robot_ops_play_plan_item where plan_id = #{planId}
    </delete>


    <select id="selectCurrentPlayingPlan" resultMap="RobotOpsPlayPlanResult">
        select id, plan_name, loop_mode, asset_count, status, remark, create_by, create_time, update_by, update_time
        from robot_ops_play_plan
        where status = '1'
            limit 1
    </select>

    <select id="selectRobotOpsPlayPlanItemListByPlanId" parameterType="Long" resultMap="RobotOpsPlayPlanItemResult">
        select i.id, i.plan_id, i.asset_id, i.play_order, i.stay_seconds, i.transition_type,
               a.file_url, a.asset_type, a.asset_name, a.thumbnail_url, a.resolution, a.duration_seconds,
               i.create_by, i.create_time, i.update_by, i.update_time
        from robot_ops_play_plan_item i
        left join robot_ops_media_asset a on i.asset_id = a.id
        where i.plan_id = #{planId}
    </select>

    <update id="updatePlayPlanStatus">
        update robot_ops_play_plan
        set status = #{status}, update_time = sysdate()
        where id = #{id}
    </update>

    <insert id="batchRobotOpsPlayPlanItem">
        insert into robot_ops_play_plan_item( id, plan_id, asset_id, play_order, stay_seconds, transition_type, create_by, create_time, update_by, update_time) values
        <foreach item="item" index="index" collection="list" separator=",">
            ( #{item.id}, #{item.planId}, #{item.assetId}, #{item.playOrder}, #{item.staySeconds}, #{item.transitionType}, #{item.createBy}, #{item.createTime}, #{item.updateBy}, #{item.updateTime})
        </foreach>
    </insert>
</mapper>
