<?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.lz.modules.flow.dao.ResultTagetLibMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.lz.modules.flow.entity.ResultTagetLib">
        <id column="id" property="id"/>
        <result column="is_delete" property="isDelete"/>
        <result column="gmt_create" property="gmtCreate"/>
        <result column="gmt_modified" property="gmtModified"/>
        <result column="name" property="name"/>
        <result column="model_id" property="modelId"/>
        <result column="weight" property="weight"/>
        <result column="key_result" property="keyResult"/>
        <result column="order_by" property="orderBy"/>
    </resultMap>


    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id AS id, is_delete AS isDelete, gmt_create AS gmtCreate, gmt_modified AS gmtModified, name AS name, model_id AS modelId, weight AS weight, key_result AS keyResult, order_by AS orderBy
    </sql>




    <select id="selectResultTagetLibById" resultType="ResultTagetLib" >
        select * from lz_result_taget_lib  where id=#{id} and is_delete = 0 limit 1 
    </select>


    <insert id="insertResultTagetLib" parameterType="ResultTagetLib" useGeneratedKeys="true" keyProperty="id" >
        insert into lz_result_taget_lib(
            <if test="name != null">name, </if>
            <if test="modelId != null">model_id, </if>
            <if test="weight != null">weight, </if>
            <if test="keyResult != null">key_result, </if>
            <if test="orderBy != null">order_by, </if>
            is_delete,
            gmt_create,
            gmt_modified
        )values(
            <if test="name != null">#{ name}, </if>
            <if test="modelId != null">#{ modelId}, </if>
            <if test="weight != null">#{ weight}, </if>
            <if test="keyResult != null">#{ keyResult}, </if>
            <if test="orderBy != null">#{ orderBy}, </if>
            0,
            now(),
            now()
        )
    </insert>


    <update id="updateResultTagetLibById" parameterType="ResultTagetLib" >
        update
            lz_result_taget_lib
        <trim prefix="set" suffixOverrides=",">
            <if test="isDelete != null">is_delete = #{isDelete},</if>
            <if test="gmtCreate != null">gmt_create = #{gmtCreate},</if>
            <if test="name != null">name = #{name},</if>
            <if test="modelId != null">model_id = #{modelId},</if>
            <if test="weight != null">weight = #{weight},</if>
            <if test="keyResult != null">key_result = #{keyResult},</if>
            <if test="orderBy != null">order_by = #{orderBy}</if>                
        </trim>
        ,gmt_modified = now()
        where id = #{id}
    </update>


    <update id="updateCoverResultTagetLibById" parameterType="ResultTagetLib" >
        update
            lz_result_taget_lib    
        set     
            is_delete = #{isDelete},
            gmt_create = #{gmtCreate},
            name = #{name},
            model_id = #{modelId},
            weight = #{weight},
            key_result = #{keyResult},
            order_by = #{orderBy}                
        ,gmt_modified = now()
        where id = #{id}
    </update>


    <update id="deleteResultTagetLibById" parameterType="java.lang.Long">
        update lz_result_taget_lib set is_delete = 1 where id=#{id} limit 1  
    </update>

    <select id="selectResultTagetLibByModelId" resultType="com.lz.modules.flow.model.ResultTagetLibDto" >
        select * from lz_result_taget_lib  where model_id=#{id} and is_delete = 0 order by order_by desc
    </select>

    <select id="selectByCondition" resultType="ResultTagetLib" >
        select * from lz_result_taget_lib  where is_delete = 0
        <if test="req.name != null and req.name != ''">and name like CONCAT('%', req.name, '%')</if>
        <if test="req.modelId != null">and model_id = #{req.modelId} </if>
        <if test="req.weight != null">and weight = #{req.weight} </if>
    </select>

    <update id="deleteResultTagetLibByModelId" parameterType="java.lang.Long">
        update lz_result_taget_lib set is_delete = 1 where model_id=#{id}
    </update>

    <insert id="insertResultTagetLibs" parameterType="FlowChartDetailRecord" useGeneratedKeys="true" keyProperty="id" >

        insert into lz_result_taget_lib(
        name,
        model_id,
        weight,
        key_result,
        order_by,
        is_delete
        )values
        <foreach collection="list" item="item" separator=",">(
            #{ item.name},
            #{ item.modelId},
            #{ item.weight},
            #{ item.keyResult},
            #{ item.orderBy},
            0
            )
        </foreach>
        ;
    </insert>

    <update id="updateResultTagetLibByIds" parameterType="FlowChartDetailRecord" useGeneratedKeys="true" keyProperty="id" >
        <foreach collection="list" item="item" separator=";">
            update
            lz_result_taget_lib
            set
            <if test="item.isDelete != null">is_delete = #{item.isDelete},</if>
            <if test="item.name != null">name = #{item.name},</if>
            <if test="item.modelId != null">model_id = #{item.modelId},</if>
            <if test="item.weight != null"> weight = #{item.weight},</if>
            <if test="item.keyResult != null">key_result = #{item.keyResult},</if>
            <if test="item.orderBy != null">order_by = #{item.orderBy}</if>
            ,gmt_modified = now()
            where id = #{item.id}
        </foreach>

    </update>

    <select id="selectResultTagetLibByModelReqId" resultType="com.lz.modules.flow.req.ResultTagetLibItemReq" >
        select id, is_delete, name, weight, key_result from lz_result_taget_lib  where model_id=#{id} and is_delete = 0 order by order_by asc
    </select>

    <select id="selectResultTagetLibDtoByModelId" resultType="com.lz.modules.flow.model.ResultTagetLibDto" >
        select id, name, model_id, weight, key_result, order_by from lz_result_taget_lib  where model_id=#{id} and is_delete = 0 order by order_by asc
    </select>

</mapper>

