<?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.SubAlgorithmMapper">

    <resultMap type="SubAlgorithm" id="SubAlgorithmResult">
        <result property="id"    column="id"    />
        <result property="algorithmId"    column="algorithm_id"    />
        <result property="fieldId"    column="field_id"    />
        <result property="uploadId"    column="upload_id"    />
        <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="selectSubAlgorithmVo">
        select id, algorithm_id, field_id, upload_id, create_by, create_time, update_by, update_time, remark from t_sub_algorithm
    </sql>

    <select id="selectSubAlgorithmList" parameterType="SubAlgorithm" resultMap="SubAlgorithmResult">
        <include refid="selectSubAlgorithmVo"/>
        <where>
            <if test="algorithmId != null "> and algorithm_id = #{algorithmId}</if>
            <if test="fieldId != null "> and field_id = #{fieldId}</if>
            <if test="uploadId != null "> and upload_id = #{uploadId}</if>
        </where>
    </select>

    <select id="selectSubAlgorithmById" parameterType="Long" resultMap="SubAlgorithmResult">
        <include refid="selectSubAlgorithmVo"/>
        where id = #{id}
    </select>

    <insert id="insertSubAlgorithm" parameterType="SubAlgorithm" useGeneratedKeys="true" keyProperty="id">
        insert into t_sub_algorithm
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="algorithmId != null">algorithm_id,</if>
            <if test="fieldId != null">field_id,</if>
            <if test="uploadId != null">upload_id,</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="algorithmId != null">#{algorithmId},</if>
            <if test="fieldId != null">#{fieldId},</if>
            <if test="uploadId != null">#{uploadId},</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="updateSubAlgorithm" parameterType="SubAlgorithm">
        update t_sub_algorithm
        <trim prefix="SET" suffixOverrides=",">
            <if test="algorithmId != null">algorithm_id = #{algorithmId},</if>
            <if test="fieldId != null">field_id = #{fieldId},</if>
            <if test="uploadId != null">upload_id = #{uploadId},</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="deleteSubAlgorithmById" parameterType="Long">
        delete from t_sub_algorithm where id = #{id}
    </delete>

    <delete id="deleteSubAlgorithmByUploadId" parameterType="Long">
        delete from t_sub_algorithm where upload_id = #{id}
    </delete>

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

    <select id="selectSubAlgorithmOutputList" parameterType="Long" resultType="Map">
        SELECT
            tsa.id,
            tsa.algorithm_id,
            tsa.field_id
        FROM
            t_algorithm ta
                JOIN t_algorithm_sub_type tast ON tast.id = ta.sub_type_id
                JOIN t_algorithm_io_field taif ON taif.algorithm_sub_id = tast.id
                JOIN t_sub_algorithm tsa ON tsa.field_id = taif.id
                AND tsa.algorithm_id = ta.id
        WHERE
            taif.type = 2
          AND ta.id = #{id}
        ORDER BY
            taif.`index`
    </select>
</mapper>
