<?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.ruoyi.platform.mapper.WorkflowDao">

    <resultMap type="com.ruoyi.platform.domain.Workflow" id="WorkflowMap">
        <result property="id" column="id" jdbcType="INTEGER"/>
        <result property="name" column="name" jdbcType="VARCHAR"/>
        <result property="description" column="description" jdbcType="VARCHAR"/>
        <result property="dag" column="dag" jdbcType="VARCHAR"/>
        <result property="globalParam" column="global_param" jdbcType="VARCHAR"/>
        <result property="createBy" column="create_by" jdbcType="VARCHAR"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updateBy" column="update_by" jdbcType="VARCHAR"/>
        <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
        <result property="state" column="state" jdbcType="INTEGER"/>
    </resultMap>

    <!--查询单个-->
    <select id="queryById" resultMap="WorkflowMap">
        select
        id, name, description, dag, global_param, create_by, create_time, update_by, update_time, state
        from workflow
        where id = #{id} and state = 1
    </select>

    <!--查询指定行数据-->
    <select id="queryAllByLimit" resultMap="WorkflowMap" >
        select
        id, name, description, dag, global_param, create_by, create_time, update_by, update_time, state
        from workflow
        <where>
            state = 1
            <if test="workflow.id != null">
                and id = #{workflow.id}
            </if>
            <if test="workflow.name != null and workflow.name != ''">
                and name like "%"#{workflow.name}"%"
            </if>
            <if test="workflow.description != null and workflow.description != ''">
                and description = #{workflow.description}
            </if>
            <if test="workflow.dag != null and workflow.dag != ''">
                and dag = #{workflow.dag}
            </if>
            <if test="workflow.globalParam != null and workflow.globalParam != ''">
                and global_param = #{workflow.globalParam}
            </if>
            <if test="workflow.createBy != null and workflow.createBy != ''">
                and create_by = #{workflow.createBy}
            </if>
            <if test="workflow.createTime != null">
                and create_time = #{workflow.createTime}
            </if>
            <if test="workflow.updateBy != null and workflow.updateBy != ''">
                and update_by = #{workflow.updateBy}
            </if>
            <if test="workflow.updateTime != null">
                and update_time = #{workflow.updateTime}
            </if>
        </where>
        order by update_time DESC
        limit #{pageable.offset}, #{pageable.pageSize}
    </select>

    <!--统计总行数-->
    <select id="count" resultType="java.lang.Long">
        select count(1)
        from workflow
        <where>
            state = 1
            <if test="workflow.id != null">
                and id = #{workflow.id}
            </if>
            <if test="workflow.name != null and workflow.name != ''">
                and name like "%"#{workflow.name}"%"
            </if>
            <if test="workflow.description != null and workflow.description != ''">
                and description = #{workflow.description}
            </if>
            <if test="workflow.dag != null and workflow.dag != ''">
                and dag = #{workflow.dag}
            </if>
            <if test="workflow.globalParam != null and workflow.globalParam != ''">
                and global_param = #{workflow.globalParam}
            </if>
            <if test="workflow.createBy != null and workflow.createBy != ''">
                and create_by = #{workflow.createBy}
            </if>
            <if test="workflow.createTime != null">
                and create_time = #{workflow.createTime}
            </if>
            <if test="workflow.updateBy != null and workflow.updateBy != ''">
                and update_by = #{workflow.updateBy}
            </if>
            <if test="workflow.updateTime != null">
                and update_time = #{workflow.updateTime}
            </if>
        </where>
    </select>

    <!--统计总行数-->
    <select id="workflowCount" resultType="java.lang.Long">
        select count(1)
        from workflow
        where state = 1
    </select>

    <!--新增所有列-->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into workflow(name, description, dag, global_param, create_by, create_time, update_by, update_time,state)
        values (#{workflow.name}, #{workflow.description}, #{workflow.dag},#{workflow.globalParam}, #{workflow.createBy}, #{workflow.createTime}, #{workflow.updateBy}, #{workflow.updateTime}, #{workflow.state})
    </insert>



    <insert id="insertBatch" keyProperty="id" useGeneratedKeys="true">
        insert into workflow(name, description, dag, global_param, create_by, create_time, update_by, update_time, state)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.name}, #{entity.description}, #{entity.dag}, #{entity.globalParam}, #{entity.createBy}, #{entity.createTime},
            #{entity.updateBy}, #{entity.updateTime}, #{entity.state})
        </foreach>
    </insert>

    <insert id="insertOrUpdateBatch" keyProperty="id" useGeneratedKeys="true">
        insert into workflow(name, description, dag, global_param, create_by, create_time, update_by, update_time, state)
        values
        <foreach collection="entities" item="entity" separator=",">
            (#{entity.name}, #{entity.description}, #{entity.dag}, #{entity.globalParam},#{entity.createBy}, #{entity.createTime},
            #{entity.updateBy}, #{entity.updateTime}, #{entity.state})
        </foreach>
        on duplicate key update
        name = values(name),
        description = values(description),
        dag = values(dag),
        create_by = values(create_by),
        create_time = values(create_time),
        update_by = values(update_by),
        update_time = values(update_time),
        state = values(state)
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        update workflow
        <set>
            <if test="workflow.name != null and workflow.name != ''">
                name = #{workflow.name},
            </if>
            <if test="workflow.description != null and workflow.description != ''">
                description = #{workflow.description},
            </if>
            <if test="workflow.dag != null and workflow.dag != ''">
                dag = #{workflow.dag},
            </if>
            <if test="workflow.globalParam != null and workflow.globalParam != ''">
                global_param = #{workflow.globalParam},
            </if>
            <if test="workflow.createBy != null and workflow.createBy != ''">
                create_by = #{workflow.createBy},
            </if>
            <if test="workflow.createTime != null">
                create_time = #{workflow.createTime},
            </if>
            <if test="workflow.updateBy != null and workflow.updateBy != ''">
                update_by = #{workflow.updateBy},
            </if>
            <if test="workflow.updateTime != null">
                update_time = #{workflow.updateTime},
            </if>
            <if test="workflow.state != null">
                state = #{workflow.state}
            </if>
        </set>
        where id = #{workflow.id}
    </update>

    <!--通过主键删除-->
    <delete id="deleteById">
        delete from workflow where id = #{id}
    </delete>

    <!--通过流水线名字进行模糊查询-->
    <select id="queryByName" resultMap="WorkflowMap">
        select
        id, name, description, dag, global_param, create_by, create_time, update_by, update_time, state
        from workflow
        <where>
            state = 1
            <if test="workflow.name != null and workflow.name != ''">
                and name like "%"#{workflow.name}"%"
            </if>
        </where>
    </select>

    <!--通过流水线名字进行精准查询-->
    <select id="findByName" resultMap="WorkflowMap">
        select
        id, name, description, dag, global_param, create_by, create_time, update_by, update_time, state
        from workflow
        where name = #{name} and state = 1 limit 1
    </select>
</mapper>

