<?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.aucma.dms.mapper.DmsRepairMaterialMapper">
    
    <resultMap type="DmsRepairMaterial" id="DmsRepairMaterialResult">
        <result property="materialId"    column="material_id"    />
        <result property="workOrderId"    column="work_order_id"    />
        <result property="materialCode"    column="material_code"    />
        <result property="materialName"    column="material_name"    />
        <result property="materialSpec"    column="material_spec"    />
        <result property="materialType"    column="material_type"    />
        <result property="unit"    column="unit"    />
        <result property="planQuantity"    column="plan_quantity"    />
        <result property="actualQuantity"    column="actual_quantity"    />
        <result property="unitPrice"    column="unit_price"    />
        <result property="totalAmount"    column="total_amount"    />
        <result property="receiveStatus"    column="receive_status"    />
        <result property="receiver"    column="receiver"    />
        <result property="receiveTime"    column="receive_time"    />
        <result property="warehouseId"    column="warehouse_id"    />
        <result property="warehouseName"    column="warehouse_name"    />
        <result property="remark"    column="remark"    />
        <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="selectDmsRepairMaterialVo">
        select material_id, work_order_id, material_code, material_name, material_spec, 
               material_type, unit, plan_quantity, actual_quantity, unit_price, total_amount,
               receive_status, receiver, receive_time, warehouse_id, warehouse_name, remark,
               create_by, create_time, update_by, update_time
        from dms_repair_material
    </sql>

    <select id="selectDmsRepairMaterialList" parameterType="DmsRepairMaterial" resultMap="DmsRepairMaterialResult">
        <include refid="selectDmsRepairMaterialVo"/>
        <where>
            <if test="workOrderId != null">
                and work_order_id = #{workOrderId}
            </if>
            <if test="materialCode != null and materialCode != ''">
                and material_code like '%' || #{materialCode} || '%'
            </if>
            <if test="materialName != null and materialName != ''">
                and material_name like '%' || #{materialName} || '%'
            </if>
            <if test="materialType != null and materialType != ''">
                and material_type = #{materialType}
            </if>
            <if test="receiveStatus != null and receiveStatus != ''">
                and receive_status = #{receiveStatus}
            </if>
        </where>
        order by material_id
    </select>
    
    <select id="selectDmsRepairMaterialByMaterialId" parameterType="Long" resultMap="DmsRepairMaterialResult">
        <include refid="selectDmsRepairMaterialVo"/>
        where material_id = #{materialId}
    </select>
        
    <insert id="insertDmsRepairMaterial" parameterType="DmsRepairMaterial">
        <selectKey keyProperty="materialId" resultType="long" order="BEFORE">
            select HAIWEI.SEQ_DMS_REPAIR_MATERIAL.nextval as materialId from DUAL
        </selectKey>
        insert into dms_repair_material
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="materialId != null">material_id,</if>
            <if test="workOrderId != null">work_order_id,</if>
            <if test="materialCode != null">material_code,</if>
            <if test="materialName != null">material_name,</if>
            <if test="materialSpec != null">material_spec,</if>
            <if test="materialType != null">material_type,</if>
            <if test="unit != null">unit,</if>
            <if test="planQuantity != null">plan_quantity,</if>
            <if test="actualQuantity != null">actual_quantity,</if>
            <if test="unitPrice != null">unit_price,</if>
            <if test="totalAmount != null">total_amount,</if>
            <if test="receiveStatus != null">receive_status,</if>
            <if test="receiver != null">receiver,</if>
            <if test="receiveTime != null">receive_time,</if>
            <if test="warehouseId != null">warehouse_id,</if>
            <if test="warehouseName != null">warehouse_name,</if>
            <if test="remark != null">remark,</if>
            <if test="createBy != null">create_by,</if>
            create_time
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="materialId != null">#{materialId},</if>
            <if test="workOrderId != null">#{workOrderId},</if>
            <if test="materialCode != null">#{materialCode},</if>
            <if test="materialName != null">#{materialName},</if>
            <if test="materialSpec != null">#{materialSpec},</if>
            <if test="materialType != null">#{materialType},</if>
            <if test="unit != null">#{unit},</if>
            <if test="planQuantity != null">#{planQuantity},</if>
            <if test="actualQuantity != null">#{actualQuantity},</if>
            <if test="unitPrice != null">#{unitPrice},</if>
            <if test="totalAmount != null">#{totalAmount},</if>
            <if test="receiveStatus != null">#{receiveStatus},</if>
            <if test="receiver != null">#{receiver},</if>
            <if test="receiveTime != null">#{receiveTime},</if>
            <if test="warehouseId != null">#{warehouseId},</if>
            <if test="warehouseName != null">#{warehouseName},</if>
            <if test="remark != null">#{remark},</if>
            <if test="createBy != null">#{createBy},</if>
            sysdate
        </trim>
    </insert>

    <update id="updateDmsRepairMaterial" parameterType="DmsRepairMaterial">
        update dms_repair_material
        <trim prefix="SET" suffixOverrides=",">
            <if test="workOrderId != null">work_order_id = #{workOrderId},</if>
            <if test="materialCode != null">material_code = #{materialCode},</if>
            <if test="materialName != null">material_name = #{materialName},</if>
            <if test="materialSpec != null">material_spec = #{materialSpec},</if>
            <if test="materialType != null">material_type = #{materialType},</if>
            <if test="unit != null">unit = #{unit},</if>
            <if test="planQuantity != null">plan_quantity = #{planQuantity},</if>
            <if test="actualQuantity != null">actual_quantity = #{actualQuantity},</if>
            <if test="unitPrice != null">unit_price = #{unitPrice},</if>
            <if test="totalAmount != null">total_amount = #{totalAmount},</if>
            <if test="receiveStatus != null">receive_status = #{receiveStatus},</if>
            <if test="receiver != null">receiver = #{receiver},</if>
            <if test="receiveTime != null">receive_time = #{receiveTime},</if>
            <if test="warehouseId != null">warehouse_id = #{warehouseId},</if>
            <if test="warehouseName != null">warehouse_name = #{warehouseName},</if>
            <if test="remark != null">remark = #{remark},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            update_time = sysdate
        </trim>
        where material_id = #{materialId}
    </update>

    <delete id="deleteDmsRepairMaterialByMaterialId" parameterType="Long">
        delete from dms_repair_material where material_id = #{materialId}
    </delete>

    <delete id="deleteDmsRepairMaterialByMaterialIds" parameterType="String">
        delete from dms_repair_material where material_id in 
        <foreach item="materialId" collection="array" open="(" separator="," close=")">
            #{materialId}
        </foreach>
    </delete>

    <delete id="deleteDmsRepairMaterialByWorkOrderId" parameterType="Long">
        delete from dms_repair_material where work_order_id = #{workOrderId}
    </delete>
</mapper>
