<?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.yunfeiyun.agmp.fms.mapper.MppMaterialMapper">
    
    <resultMap type="MppMaterial" id="MppMaterialResult">
        <result property="materialId"    column="materialId"    />
        <result property="taskId"    column="taskId"    />
        <result property="stockId"    column="stockId"    />
        <result property="materialStatus"    column="materialStatus"    />
        <result property="materialCostnum"    column="materialCostnum"    />
    </resultMap>

    <sql id="selectMppMaterialVo">
        select materialId, taskId, stockId, materialStatus, materialCostnum from MppMaterial
    </sql>

    <select id="selectMppMaterialList" parameterType="MppMaterialReqVo" resultType="MppMaterialResVo">
        select m.materialId, m.taskId, m.stockId, m.materialStatus, m.materialCostnum, g.goodsName, fg.finishgoodNo
        from MppMaterial m
        LEFT JOIN WmsStock s on s.stockId = m.stockId
        LEFT JOIN WmsGoods g on g.goodsId = s.goodsId
        LEFT JOIN WmsFinishgoods fg on fg.goodsId = g.goodsId
        <where>  
            <if test="taskId != null  and taskId != ''"> and taskId = #{taskId}</if>
            <if test="stockId != null  and stockId != ''"> and stockId = #{stockId}</if>
            <if test="finishgoodsId != null  and finishgoodsId != ''"> and fg.finishgoodsId = #{finishgoodsId}</if>
            <if test="materialStatus != null  and materialStatus != ''"> and materialStatus = #{materialStatus}</if>
        </where>
    </select>
    
    <select id="selectMppMaterialByMaterialId" parameterType="String" resultMap="MppMaterialResult">
        <include refid="selectMppMaterialVo"/>
        where materialId = #{materialId}
    </select>
        
    <insert id="insertMppMaterial" parameterType="MppMaterial">
        insert into MppMaterial
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="materialId != null">materialId,</if>
            <if test="taskId != null">taskId,</if>
            <if test="stockId != null">stockId,</if>
            <if test="materialStatus != null">materialStatus,</if>
            <if test="materialCostnum != null">materialCostnum,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="materialId != null">#{materialId},</if>
            <if test="taskId != null">#{taskId},</if>
            <if test="stockId != null">#{stockId},</if>
            <if test="materialStatus != null">#{materialStatus},</if>
            <if test="materialCostnum != null">#{materialCostnum},</if>
         </trim>
    </insert>

    <insert id="insertMppMaterialBatch" parameterType="MppMaterial">
        insert into MppMaterial
        <trim prefix="(" suffix=")" suffixOverrides=",">
            materialId,
            taskId,
            stockId,
            materialStatus,
            materialCostnum,
        </trim>
        values
        <foreach item="data" collection="list" separator=",">
            (
            #{data.materialId},
            #{data.taskId},
            #{data.stockId},
            #{data.materialStatus},
            #{data.materialCostnum}
            )
        </foreach>
    </insert>



    <update id="updateMppMaterial" parameterType="MppMaterial">
        update MppMaterial
        <trim prefix="SET" suffixOverrides=",">
            <if test="taskId != null">taskId = #{taskId},</if>
            <if test="stockId != null">stockId = #{stockId},</if>
            <if test="materialStatus != null">materialStatus = #{materialStatus},</if>
            <if test="materialCostnum != null">materialCostnum = #{materialCostnum},</if>
        </trim>
        where materialId = #{materialId}
    </update>

    <delete id="deleteMppMaterialByMaterialId" parameterType="String">
        delete from MppMaterial where materialId = #{materialId}
    </delete>

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