<?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.bonus.material.task.mapper.TmTaskAgreementMapper">
    <resultMap type="com.bonus.material.task.domain.TmTaskAgreement" id="TmTaskAgreementResult">
        <result property="taskId"    column="task_id"    />
        <result property="agreementId"    column="agreement_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"    />
        <result property="companyId"    column="company_id"    />
    </resultMap>

    <sql id="selectTmTaskAgreementVo">
        select task_id, agreement_id, create_by, create_time, update_by, update_time, remark, company_id from tm_task_agreement
    </sql>

    <select id="selectTmTaskAgreementList" parameterType="com.bonus.material.task.domain.TmTaskAgreement" resultMap="TmTaskAgreementResult">
        <include refid="selectTmTaskAgreementVo"/>
        <where>  
            <if test="agreementId != null "> and agreement_id = #{agreementId}</if>
            <if test="companyId != null "> and company_id = #{companyId}</if>
        </where>
    </select>
    
    <select id="selectTmTaskAgreementByTaskId" parameterType="Long" resultMap="TmTaskAgreementResult">
        <include refid="selectTmTaskAgreementVo"/>
        where task_id = #{taskId}
    </select>
        
    <insert id="insertTmTaskAgreement" parameterType="com.bonus.material.task.domain.TmTaskAgreement" useGeneratedKeys="true" keyProperty="taskId">
        insert into tm_task_agreement
        (
        <if test="taskId != null">
            task_id,
        </if>
        <if test="agreementId != null">
            agreement_id,
        </if>
        <if test="createBy != null and createBy != ''">
            create_by,
        </if>
        <if test="remark != null and remark != ''">
            remark,
        </if>
        <if test="companyId != null">
            company_id,
        </if>
        create_time
        ) values (
        <if test="taskId != null">
            #{taskId},
        </if>
        <if test="agreementId != null">
            #{agreementId},
        </if>
        <if test="createBy != null and createBy != ''">
            #{createBy},
        </if>
        <if test="remark != null and remark != ''">
            #{remark},
        </if>
        <if test="companyId != null">
            #{companyId},
        </if>
        NOW()
        )
    </insert>

    <update id="updateTmTaskAgreement" parameterType="com.bonus.material.task.domain.TmTaskAgreement">
        update tm_task_agreement
        <trim prefix="SET" suffixOverrides=",">
            <if test="agreementId != null">agreement_id = #{agreementId},</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>
            <if test="companyId != null">company_id = #{companyId},</if>
        </trim>
        where task_id = #{taskId}
    </update>

    <delete id="deleteTmTaskAgreementByTaskId" parameterType="Long">
        delete from tm_task_agreement where task_id = #{taskId}
    </delete>

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

    <select id="getAgreementId" resultType="java.lang.String">
        select agreement_id
        from tm_task_agreement
        where task_id = #{taskId}
    </select>

    <select id="selectAgreementIdByTaskId" resultType="java.lang.Long">
        select agreement_id from tm_task_agreement where task_id = #{taskId}
    </select>

    <select id="selectAgreementId" resultType="java.lang.String">
        SELECT DISTINCT bai.agreement_id
        FROM bm_agreement_info bai
            LEFT JOIN lease_publish_details lpd ON bai.unit_id = lpd.unit_id
            AND bai.project_id = lpd.project_id
        WHERE lpd.publish_task = #{publishTask}
    </select>
</mapper>