<?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.pdaaphm.biz.mapper.AlgorithmMapper">

    <resultMap type="Algorithm" id="AlgorithmResult">
        <result property="id"    column="id"    />
        <result property="type"    column="type"    />
        <result property="subTypeId"    column="sub_type_id"    />
        <result property="name"    column="name"    />
        <result property="startTime"    column="start_time"    />
        <result property="completedTime"    column="completed_time"    />
        <result property="costSecond"    column="cost_second"    />
        <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="remark"    column="remark"    />
    </resultMap>

    <sql id="selectAlgorithmVo">
        select id, `type`, sub_type_id, `name`, start_time, completed_time, cost_second, create_by, create_time, update_by, update_time, remark from t_algorithm
    </sql>

    <select id="selectAlgorithmList" parameterType="Algorithm" resultMap="AlgorithmResult">
        <include refid="selectAlgorithmVo"/>
        <where>
            <if test="type != null  and type != ''"> and `type` = #{type}</if>
            <if test="subTypeId != null "> and sub_type_id = #{subTypeId}</if>
            <if test="name != null  and name != ''"> and `name` like concat('%', #{name}, '%')</if>
            <if test="startTime != null "> and start_time = #{startTime}</if>
            <if test="completedTime != null "> and completed_time = #{completedTime}</if>
            <if test="costSecond != null "> and cost_second = #{costSecond}</if>
        </where>
    </select>

    <select id="selectAlgorithmById" parameterType="Long" resultMap="AlgorithmResult">
        <include refid="selectAlgorithmVo"/>
        where id = #{id}
    </select>

    <insert id="insertAlgorithm" parameterType="Algorithm" useGeneratedKeys="true" keyProperty="id">
        insert into t_algorithm
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="type != null">`type`,</if>
            <if test="subTypeId != null">sub_type_id,</if>
            <if test="name != null">`name`,</if>
            <if test="startTime != null">start_time,</if>
            <if test="completedTime != null">completed_time,</if>
            <if test="costSecond != null">cost_second,</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>
            <if test="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="type != null">#{type},</if>
            <if test="subTypeId != null">#{subTypeId},</if>
            <if test="name != null">#{name},</if>
            <if test="startTime != null">#{startTime},</if>
            <if test="completedTime != null">#{completedTime},</if>
            <if test="costSecond != null">#{costSecond},</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>
            <if test="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateAlgorithm" parameterType="Algorithm">
        update t_algorithm
        <trim prefix="SET" suffixOverrides=",">
            <if test="type != null">type = #{type},</if>
            <if test="subTypeId != null">sub_type_id = #{subTypeId},</if>
            <if test="name != null">`name` = #{name},</if>
            <if test="startTime != null">start_time = #{startTime},</if>
            <if test="completedTime != null">completed_time = #{completedTime},</if>
            <if test="costSecond != null">cost_second = #{costSecond},</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>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteAlgorithmById" parameterType="Long">
        delete from t_algorithm where id = #{id}
    </delete>

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

    <select id="getOption" resultType="Map">
        select id, `type`, `name` from t_algorithm_sub_type
    </select>
</mapper>
