<?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.xinyilu.system.mapper.motionprescriptionlibrary.MotionPrescriptionLibraryMapper">

    <resultMap type="com.xinyilu.system.domain.vo.motionprescriptionlibrary.MotionPrescriptionLibraryVO"
               id="MotionPrescriptionLibraryResult">
        <result property="id" column="id"/>
        <result property="physicalQualityName" column="physical_quality_name"/>
        <result property="degreeStartFraction" column="degree_start_fraction"/>
        <result property="degreeEndFraction" column="degree_end_fraction"/>
        <result property="motionType" column="motion_type"/>
        <result property="motionFrequency" column="motion_frequency"/>
        <result property="motionStrength" column="motion_strength"/>
        <result property="motionTime" column="motion_time"/>
        <result property="motionCapacity" column="motion_capacity"/>
        <result property="advancedTime" column="advanced_time"/>
        <result property="advancedScheme" column="advanced_scheme"/>
        <result property="prescriptionVideoUrl" column="prescription_video_url"/>
        <result property="createBy" column="create_by"/>
        <result property="createTime" column="create_time"/>
        <result property="updateBy" column="update_by"/>
        <result property="updateTime" column="update_time"/>
        <result property="prescriptionVideoFlag" column="prescriptionVideoFlag"/>
    </resultMap>

    <sql id="selectMotionPrescriptionLibraryVo">
        select id,
               physical_quality_name,
               degree_start_fraction,
               degree_end_fraction,
               motion_type,
               motion_frequency,
               motion_strength,
               motion_time,
               motion_capacity,
               advanced_time,
               advanced_scheme,
               prescription_video_url,
               create_by,
               create_time,
               update_by,
               update_time
        from motion_prescription_library
    </sql>

    <select id="selectMotionPrescriptionLibraryList" parameterType="MotionPrescriptionLibrary"
            resultMap="MotionPrescriptionLibraryResult">
        SELECT
        mpl.id,
        mpl.physical_quality_name,
        mpl.degree_start_fraction,
        mpl.degree_end_fraction,
        mpl.motion_type,
        mpl.motion_frequency,
        mpl.motion_strength,
        mpl.motion_time,
        mpl.motion_capacity,
        mpl.advanced_time,
        mpl.advanced_scheme,
        IF(( SELECT count( 1 ) FROM motion_prescription_video WHERE motion_prescription_id = mpl.id ) > 0, 'YES', 'NO')
        prescriptionVideoFlag
        FROM
        motion_prescription_library mpl
        <where>
            <if test="physicalQualityName != null  and physicalQualityName != ''">
                and mpl.physical_quality_name like concat('%', #{physicalQualityName}, '%')
            </if>
            <if test="degreeStartFraction != null ">
                and mpl.degree_start_fraction = #{degreeStartFraction}
            </if>
            <if test="degreeEndFraction != null ">
                and mpl.degree_end_fraction = #{degreeEndFraction}
            </if>
            <if test="motionType != null  and motionType != ''">
                and mpl.motion_type = #{motionType}
            </if>
            <if test="motionFrequency != null  and motionFrequency != ''">
                and mpl.motion_frequency = #{motionFrequency}
            </if>
            <if test="motionStrength != null  and motionStrength != ''">
                and mpl.motion_strength = #{motionStrength}
            </if>
            <if test="motionTime != null  and motionTime != ''">
                and mpl.motion_time = #{motionTime}
            </if>
            <if test="motionCapacity != null  and motionCapacity != ''">
                and mpl.motion_capacity = #{motionCapacity}
            </if>
            <if test="advancedTime != null  and advancedTime != ''">
                and mpl.advanced_time = #{advancedTime}
            </if>
            <if test="advancedScheme != null  and advancedScheme != ''">
                and mpl.advanced_scheme = #{advancedScheme}
            </if>
        </where>
        order by mpl.create_time desc, mpl.id asc
    </select>

    <select id="selectMotionPrescriptionLibraryById" parameterType="Long"
            resultMap="MotionPrescriptionLibraryResult">
        <include refid="selectMotionPrescriptionLibraryVo"/>
        where id = #{id}
    </select>

    <insert id="insertMotionPrescriptionLibrary" parameterType="MotionPrescriptionLibrary" useGeneratedKeys="true"
            keyProperty="id">
        insert into motion_prescription_library
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="physicalQualityName != null">physical_quality_name,
            </if>
            <if test="degreeStartFraction != null">degree_start_fraction,
            </if>
            <if test="degreeEndFraction != null">degree_end_fraction,
            </if>
            <if test="motionType != null">motion_type,
            </if>
            <if test="motionFrequency != null">motion_frequency,
            </if>
            <if test="motionStrength != null">motion_strength,
            </if>
            <if test="motionTime != null">motion_time,
            </if>
            <if test="motionCapacity != null">motion_capacity,
            </if>
            <if test="advancedTime != null">advanced_time,
            </if>
            <if test="advancedScheme != null">advanced_scheme,
            </if>
            <if test="prescriptionVideoUrl != null">prescription_video_url,
            </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="physicalQualityName != null">#{physicalQualityName},
            </if>
            <if test="degreeStartFraction != null">#{degreeStartFraction},
            </if>
            <if test="degreeEndFraction != null">#{degreeEndFraction},
            </if>
            <if test="motionType != null">#{motionType},
            </if>
            <if test="motionFrequency != null">#{motionFrequency},
            </if>
            <if test="motionStrength != null">#{motionStrength},
            </if>
            <if test="motionTime != null">#{motionTime},
            </if>
            <if test="motionCapacity != null">#{motionCapacity},
            </if>
            <if test="advancedTime != null">#{advancedTime},
            </if>
            <if test="advancedScheme != null">#{advancedScheme},
            </if>
            <if test="prescriptionVideoUrl != null">#{prescriptionVideoUrl},
            </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="updateMotionPrescriptionLibrary" parameterType="MotionPrescriptionLibrary">
        update motion_prescription_library
        <trim prefix="SET" suffixOverrides=",">
            <if test="physicalQualityName != null">physical_quality_name =
                #{physicalQualityName},
            </if>
            <if test="degreeStartFraction != null">degree_start_fraction =
                #{degreeStartFraction},
            </if>
            <if test="degreeEndFraction != null">degree_end_fraction =
                #{degreeEndFraction},
            </if>
            <if test="motionType != null">motion_type =
                #{motionType},
            </if>
            <if test="motionFrequency != null">motion_frequency =
                #{motionFrequency},
            </if>
            <if test="motionStrength != null">motion_strength =
                #{motionStrength},
            </if>
            <if test="motionTime != null">motion_time =
                #{motionTime},
            </if>
            <if test="motionCapacity != null">motion_capacity =
                #{motionCapacity},
            </if>
            <if test="advancedTime != null">advanced_time =
                #{advancedTime},
            </if>
            <if test="advancedScheme != null">advanced_scheme =
                #{advancedScheme},
            </if>
            <if test="prescriptionVideoUrl != null">prescription_video_url =
                #{prescriptionVideoUrl},
            </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="deleteMotionPrescriptionLibraryById" parameterType="Long">
        delete
        from motion_prescription_library
        where id = #{id}
    </delete>

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

    <update id="updateMotionPrescriptionLibraryUrl">
        update motion_prescription_library
        set prescription_video_url = #{prescriptionVideoUrl}, update_time = now()
        where id = #{motionPrescriptionId}
    </update>

    <select id="getMotionPrescriptionLibraryByIds" resultType="motionPrescriptionVideo">
        select id, motion_prescription_id, prescription_video_url from motion_prescription_video
        <where>
            <if test="ids != null and ids.length > 0">
                motion_prescription_id in
                <foreach item="id" collection="ids" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
        </where>
    </select>

    <delete id="deleteMotionPrescriptionLibraryByMotionIds">
        delete from motion_prescription_video where motion_prescription_id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </delete>
</mapper>