<?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.procurement.mapper.ResourceProcurementQuoteMapper">

    <resultMap type="ProcurementQuote" id="ProcurementQuoteResult">
        <id     property="id"                  column="id"                   />
        <result property="procurementId"       column="procurement_id"        />
        <result property="supplierId"          column="supplier_id"           />
        <result property="supplierName"        column="supplier_name"          />
        <result property="totalPrice"          column="total_price"            />
        <result property="eta"                 column="eta"                    />
        <result property="note"                column="note"                  />
        <result property="isAwarded"           column="is_awarded"             />
        <result property="createBy"            column="create_by"              />
        <result property="createTime"          column="create_time"             />
        <result property="updateBy"            column="update_by"              />
        <result property="updateTime"         column="update_time"            />
    </resultMap>

    <sql id="selectProcurementQuoteVo">
        select q.id, q.procurement_id, q.supplier_id, q.total_price, q.eta, q.note, q.is_awarded,
               q.create_by, q.create_time, q.update_by, q.update_time,
               s.supplier_name
        from resource_procurement_quote q
        left join resource_supplier s on q.supplier_id = s.id and s.del_flag = '0'
    </sql>

    <select id="selectProcurementQuoteList" parameterType="ProcurementQuote" resultMap="ProcurementQuoteResult">
        <include refid="selectProcurementQuoteVo"/>
        <where>
            <if test="procurementId != null">
                AND q.procurement_id = #{procurementId}
            </if>
            <if test="supplierId != null">
                AND q.supplier_id = #{supplierId}
            </if>
            <if test="isAwarded != null">
                AND q.is_awarded = #{isAwarded}
            </if>
        </where>
        order by q.total_price asc, q.create_time desc
    </select>

    <select id="selectProcurementQuoteListByProcurementId" parameterType="Long" resultMap="ProcurementQuoteResult">
        <include refid="selectProcurementQuoteVo"/>
        where q.procurement_id = #{procurementId}
        order by q.total_price asc, q.create_time desc
    </select>

    <select id="selectProcurementQuoteById" parameterType="Long" resultMap="ProcurementQuoteResult">
        <include refid="selectProcurementQuoteVo"/>
        where q.id = #{id}
    </select>

    <select id="selectProcurementQuoteByProcurementAndSupplier" resultMap="ProcurementQuoteResult">
        <include refid="selectProcurementQuoteVo"/>
        where q.procurement_id = #{procurementId} and q.supplier_id = #{supplierId}
        limit 1
    </select>

    <insert id="insertProcurementQuote" parameterType="ProcurementQuote" useGeneratedKeys="true" keyProperty="id">
        insert into resource_procurement_quote(
            <if test="procurementId != null">procurement_id,</if>
            <if test="supplierId != null">supplier_id,</if>
            <if test="totalPrice != null">total_price,</if>
            <if test="eta != null">eta,</if>
            <if test="note != null">note,</if>
            <if test="isAwarded != null">is_awarded,</if>
            <if test="createBy != null and createBy != ''">create_by,</if>
            create_time
        )values(
            <if test="procurementId != null">#{procurementId},</if>
            <if test="supplierId != null">#{supplierId},</if>
            <if test="totalPrice != null">#{totalPrice},</if>
            <if test="eta != null">#{eta},</if>
            <if test="note != null">#{note},</if>
            <if test="isAwarded != null">#{isAwarded},</if>
            <if test="createBy != null and createBy != ''">#{createBy},</if>
            sysdate()
        )
    </insert>

    <update id="updateProcurementQuote" parameterType="ProcurementQuote">
        update resource_procurement_quote
        <set>
            <if test="totalPrice != null">total_price = #{totalPrice},</if>
            <if test="eta != null">eta = #{eta},</if>
            <if test="note != null">note = #{note},</if>
            <if test="isAwarded != null">is_awarded = #{isAwarded},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            update_time = sysdate()
        </set>
        where id = #{id}
    </update>

    <delete id="deleteProcurementQuoteById" parameterType="Long">
        delete from resource_procurement_quote where id = #{id}
    </delete>

    <delete id="deleteProcurementQuoteByProcurementId" parameterType="Long">
        delete from resource_procurement_quote where procurement_id = #{procurementId}
    </delete>

    <update id="cancelAwardedByProcurementId" parameterType="Long">
        update resource_procurement_quote set is_awarded = 0 where procurement_id = #{procurementId}
    </update>

    <update id="setAwarded" parameterType="Long">
        update resource_procurement_quote set is_awarded = 1 where id = #{id}
    </update>

</mapper>