<?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.aegis.resource.requisition.mapper.ResourceRequisitionAgreementMapper">

    <resultMap type="RequisitionAgreement" id="RequisitionAgreementResult">
        <id     property="id"                  column="id"                   />
        <result property="requisitionId"        column="requisition_id"        />
        <result property="serviceScope"         column="service_scope"         />
        <result property="billingType"          column="billing_type"          />
        <result property="unitPrice"            column="unit_price"            />
        <result property="currency"             column="currency"              />
        <result property="effectiveFrom"        column="effective_from"        />
        <result property="effectiveTo"          column="effective_to"          />
        <result property="terms"                column="terms"                 />
        <result property="attachmentUrl"       column="attachment_url"        />
        <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"                />
    </resultMap>

    <sql id="selectRequisitionAgreementVo">
        select id, requisition_id, service_scope, billing_type, unit_price, currency,
               effective_from, effective_to, terms, attachment_url, create_by, create_time,
               update_by, update_time, remark
        from resource_requisition_agreement
    </sql>

    <select id="selectRequisitionAgreementList" parameterType="RequisitionAgreement" resultMap="RequisitionAgreementResult">
        <include refid="selectRequisitionAgreementVo"/>
        <where>
            <if test="requisitionId != null">
                AND requisition_id = #{requisitionId}
            </if>
            <if test="billingType != null and billingType != ''">
                AND billing_type = #{billingType}
            </if>
        </where>
        order by create_time desc
    </select>

    <select id="selectRequisitionAgreementListByRequisitionId" parameterType="Long" resultMap="RequisitionAgreementResult">
        <include refid="selectRequisitionAgreementVo"/>
        where requisition_id = #{requisitionId}
        order by create_time desc
    </select>

    <select id="selectRequisitionAgreementById" parameterType="Long" resultMap="RequisitionAgreementResult">
        <include refid="selectRequisitionAgreementVo"/>
        where id = #{id}
    </select>

    <insert id="insertRequisitionAgreement" parameterType="RequisitionAgreement" useGeneratedKeys="true" keyProperty="id">
        insert into resource_requisition_agreement(
            <if test="requisitionId != null">requisition_id,</if>
            <if test="serviceScope != null">service_scope,</if>
            <if test="billingType != null and billingType != ''">billing_type,</if>
            <if test="unitPrice != null">unit_price,</if>
            <if test="currency != null">currency,</if>
            <if test="effectiveFrom != null">effective_from,</if>
            <if test="effectiveTo != null">effective_to,</if>
            <if test="terms != null">terms,</if>
            <if test="attachmentUrl != null">attachment_url,</if>
            <if test="remark != null">remark,</if>
            <if test="createBy != null and createBy != ''">create_by,</if>
            create_time
        )values(
            <if test="requisitionId != null">#{requisitionId},</if>
            <if test="serviceScope != null">#{serviceScope},</if>
            <if test="billingType != null and billingType != ''">#{billingType},</if>
            <if test="unitPrice != null">#{unitPrice},</if>
            <if test="currency != null">#{currency},</if>
            <if test="effectiveFrom != null">#{effectiveFrom},</if>
            <if test="effectiveTo != null">#{effectiveTo},</if>
            <if test="terms != null">#{terms},</if>
            <if test="attachmentUrl != null">#{attachmentUrl},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createBy != null and createBy != ''">#{createBy},</if>
            sysdate()
        )
    </insert>

    <update id="updateRequisitionAgreement" parameterType="RequisitionAgreement">
        update resource_requisition_agreement
        <set>
            <if test="serviceScope != null">service_scope = #{serviceScope},</if>
            <if test="billingType != null and billingType != ''">billing_type = #{billingType},</if>
            <if test="unitPrice != null">unit_price = #{unitPrice},</if>
            <if test="currency != null">currency = #{currency},</if>
            <if test="effectiveFrom != null">effective_from = #{effectiveFrom},</if>
            <if test="effectiveTo != null">effective_to = #{effectiveTo},</if>
            <if test="terms != null">terms = #{terms},</if>
            <if test="attachmentUrl != null">attachment_url = #{attachmentUrl},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            update_time = sysdate()
        </set>
        where id = #{id}
    </update>

    <delete id="deleteRequisitionAgreementById" parameterType="Long">
        delete from resource_requisition_agreement where id = #{id}
    </delete>

    <delete id="deleteRequisitionAgreementByRequisitionId" parameterType="Long">
        delete from resource_requisition_agreement where requisition_id = #{requisitionId}
    </delete>

</mapper>