<?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.dlink.mapper.JobInstanceMapper">

    <select id="selectForProTable" resultType="com.dlink.model.JobInstance">
        select
        a.*,
        dh.type as type,
        (select dc.alias FROM dlink_cluster dc where dc.id=a.cluster_id) as clusterAlias
        from
        dlink_job_instance a
        <if test='!(param.isHistory!=null and param.isHistory==true)'>
            inner join (
            select max(ji.id) as id from dlink_job_instance ji
            group by ji.task_id
            ) snap on snap.id = a.id
        </if>
        left join dlink_history dh on a.history_id = dh.id
        <where>
            1=1
            <if test='param.status!=null and param.status!=""'>
                and a.status = #{param.status}
            </if>
            <if test='param.type!=null and param.type!=""'>
                and dh.type = #{param.type}
            </if>
            <if test='param.step!=null and param.step!=""'>
                and a.step = #{param.step}
            </if>
            <if test='param.name!=null and param.name!=""'>
                and a.name like "%${param.name}%"
            </if>
            <if test='param.jid!=null and param.jid!=""'>
                and a.jid like "%${param.jid}%"
            </if>
            <if test='param.createTime!=null and param.createTime!=""'>
                and a.create_time <![CDATA[>=]]> str_to_date( #{param.createTime},'%Y-%m-%d %H:%i:%s')
                and a.create_time <![CDATA[<=]]> str_to_date( #{param.createTime},'%Y-%m-%d %H:%i:%s')
            </if>
            <if test='param.updateTime!=null and param.updateTime!=""'>
                and a.update_time <![CDATA[>=]]> str_to_date( #{param.updateTime[0]},'%Y-%m-%d %H:%i:%s')
                and a.update_time <![CDATA[<=]]> str_to_date( #{param.updateTime[1]},'%Y-%m-%d %H:%i:%s')
            </if>
            <if test='ew.sqlSegment!=null and ew.sqlSegment!="" and !ew.sqlSegment.startsWith(" ORDER BY")'>
                and
            </if>
            <if test='ew.sqlSegment!=null and ew.sqlSegment!=""'>
                ${ew.sqlSegment}
            </if>
        </where>
    </select>

    <select id="countStatus" resultType="com.dlink.model.JobInstanceCount">
        select a.status,
               count(1) as counts
        from dlink_job_instance a
                 inner join (
            select max(ji.id) as id
            from dlink_job_instance ji
            group by ji.task_id
        ) snap on snap.id = a.id
        group by status
    </select>

    <select id="countHistoryStatus" resultType="com.dlink.model.JobInstanceCount">
        select status,
               count(1) as counts
        from dlink_job_instance
        group by status
    </select>

    <select id="listJobInstanceActive" resultType="com.dlink.model.JobInstance">
        select *
        from dlink_job_instance
        where status not in ('FAILED', 'CANCELED', 'FINISHED', 'UNKNOWN')
        order by id desc
    </select>

    <select id="getJobInstanceByTaskId" resultType="com.dlink.model.JobInstance">
        select *
        from dlink_job_instance
        where task_id = #{id}
        order by id desc limit 1
    </select>
</mapper>
