<?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.ResourceProcurementItemMapper">

    <resultMap type="ProcurementItem" id="ProcurementItemResult">
        <id     property="id"                  column="id"                   />
        <result property="procurementId"        column="procurement_id"         />
        <result property="category"            column="category"               />
        <result property="spec"                column="spec"                   />
        <result property="qty"                 column="qty"                    />
        <result property="unit"                column="unit"                   />
        <result property="isReusable"          column="is_reusable"            />
        <result property="targetMaterialId"     column="target_material_id"      />
        <result property="targetMaterialName" column="target_material_name"    />
        <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="selectProcurementItemVo">
        select i.id, i.procurement_id, i.category, i.spec, i.qty, i.unit, i.is_reusable, i.target_material_id,
               i.create_by, i.create_time, i.update_by, i.update_time, i.remark,
               m.material_name as target_material_name
        from resource_procurement_item i
        left join resource_material m on i.target_material_id = m.id and m.del_flag = '0'
    </sql>

    <select id="selectProcurementItemList" parameterType="ProcurementItem" resultMap="ProcurementItemResult">
        <include refid="selectProcurementItemVo"/>
        <where>
            <if test="procurementId != null">
                AND i.procurement_id = #{procurementId}
            </if>
            <if test="category != null and category != ''">
                AND i.category = #{category}
            </if>
            <if test="targetMaterialId != null">
                AND i.target_material_id = #{targetMaterialId}
            </if>
        </where>
        order by i.create_time desc
    </select>

    <select id="selectProcurementItemListByProcurementId" parameterType="Long" resultMap="ProcurementItemResult">
        <include refid="selectProcurementItemVo"/>
        where i.procurement_id = #{procurementId}
        order by i.create_time desc
    </select>

    <select id="selectProcurementItemById" parameterType="Long" resultMap="ProcurementItemResult">
        <include refid="selectProcurementItemVo"/>
        where i.id = #{id}
    </select>

    <insert id="insertProcurementItem" parameterType="ProcurementItem" useGeneratedKeys="true" keyProperty="id">
        insert into resource_procurement_item(
            <if test="procurementId != null">procurement_id,</if>
            <if test="category != null and category != ''">category,</if>
            <if test="spec != null">spec,</if>
            <if test="qty != null">qty,</if>
            <if test="unit != null">unit,</if>
            <if test="isReusable != null">is_reusable,</if>
            <if test="targetMaterialId != null">target_material_id,</if>
            <if test="remark != null">remark,</if>
            <if test="createBy != null and createBy != ''">create_by,</if>
            create_time
        )values(
            <if test="procurementId != null">#{procurementId},</if>
            <if test="category != null and category != ''">#{category},</if>
            <if test="spec != null">#{spec},</if>
            <if test="qty != null">#{qty},</if>
            <if test="unit != null">#{unit},</if>
            <if test="isReusable != null">#{isReusable},</if>
            <if test="targetMaterialId != null">#{targetMaterialId},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createBy != null and createBy != ''">#{createBy},</if>
            sysdate()
        )
    </insert>

    <update id="updateProcurementItem" parameterType="ProcurementItem">
        update resource_procurement_item
        <set>
            <if test="category != null and category != ''">category = #{category},</if>
            <if test="spec != null">spec = #{spec},</if>
            <if test="qty != null">qty = #{qty},</if>
            <if test="unit != null">unit = #{unit},</if>
            <if test="isReusable != null">is_reusable = #{isReusable},</if>
            <if test="targetMaterialId != null">target_material_id = #{targetMaterialId},</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="deleteProcurementItemById" parameterType="Long">
        delete from resource_procurement_item where id = #{id}
    </delete>

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

</mapper>