<?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="status"    column="status"    />
        <result property="error"    column="error"    />
        <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"    />
        <result property="algoSubName" column="sub_name"    />
    </resultMap>

    <sql id="selectAlgorithmVo">
        select algo.`id`, algo.`type`, algo.sub_type_id, algo.`name`, algo.`status`, algo.error, algo.start_time, algo.completed_time, algo.cost_second, algo.create_by, algo.create_time, algo.update_by, algo.update_time, algo.remark, sub.name as sub_name from t_algorithm as algo left join t_algorithm_sub_type as sub on algo.sub_type_id = sub.id
    </sql>

    <select id="selectAlgorithmList" parameterType="Algorithm" resultMap="AlgorithmResult">
        <include refid="selectAlgorithmVo"/>
        <where>
            <if test="type != null  and type != ''"> and algo.`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="status != null  and status != ''"> and status = #{status}</if>
            <if test="error != null  and error != ''"> and error = #{error}</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 algo.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="status != null and status != ''">status,</if>
            <if test="error != null">error,</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="status != null and status != ''">#{status},</if>
            <if test="error != null">#{error},</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="status != null and status != ''">status = #{status},</if>
            <if test="error != null">error = #{error},</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="deleteAlgorithmBySubTypeId" parameterType="Long">
        delete from t_algorithm where sub_type_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>

    <select id="selectUrlAndAddressById" parameterType="Long" resultType="Map">
        SELECT
            tast.url,
            tf.path,
            taf.type
        FROM
            t_algorithm ta
                LEFT JOIN t_sub_algorithm tsa ON tsa.algorithm_id = ta.id
                LEFT JOIN t_algorithm_io_field taf ON taf.id = tsa.field_id
                LEFT JOIN t_algorithm_sub_type tast ON tast.id = taf.algorithm_sub_id
                LEFT JOIN t_file tf ON tf.id = tsa.upload_id
        WHERE
            ta.id = #{id}
        ORDER BY
            taf.`index`
    </select>

    <update id="updateStartTimeById" parameterType="map">
        update t_algorithm
            set start_time = #{startTime}, completed_time = null, cost_second = null
        where id = #{id}
    </update>

    <update id="updateCompletedTimeById" parameterType="map">
        update t_algorithm
            set completed_time = #{completedTime}
        where id = #{id}
    </update>

    <update id="updateCostSecondById" parameterType="map">
        update t_algorithm
            set cost_second = #{costSecond}
        where id = #{id}
    </update>

    <update id="updateStatusById" parameterType="map">
        update t_algorithm
            set status = #{statusCode}
        where id = #{id}
    </update>
</mapper>
