<?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.hw.mes.mapper.MesMaterialCheckResultMapper">

    <resultMap type="MesMaterialCheckResult" id="MesMaterialCheckResultResult">
        <result property="materialCheckResultId"    column="material_check_result_id"    />
        <result property="planId"    column="plan_id"    />
        <result property="planDetailId"    column="plan_detail_id"    />
        <result property="materialId"    column="material_id"    />
        <result property="standardAmount"    column="standard_amount"    />
        <result property="checkAmount"    column="check_amount"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />

        <result property="checkType"    column="check_type"    />
    </resultMap>

    <sql id="selectMesMaterialCheckResultVo">
        select material_check_result_id, plan_id, plan_detail_id, material_id, standard_amount, check_amount, update_by, update_time from mes_material_check_result
    </sql>

    <select id="selectMesMaterialCheckResultList" parameterType="MesMaterialCheckResult" resultMap="MesMaterialCheckResultResult">
        <include refid="selectMesMaterialCheckResultVo"/>
        <where>
            <if test="planId != null "> and plan_id = #{planId}</if>
            <if test="planDetailId != null "> and plan_detail_id = #{planDetailId}</if>
            <if test="materialId != null "> and material_id = #{materialId}</if>
            <if test="standardAmount != null "> and standard_amount = #{standardAmount}</if>
            <if test="checkAmount != null "> and check_amount = #{checkAmount}</if>
        </where>
    </select>

    <select id="selectMesMaterialCheckResultByMaterialCheckResultId" parameterType="Long" resultMap="MesMaterialCheckResultResult">
        <include refid="selectMesMaterialCheckResultVo"/>
        where material_check_result_id = #{materialCheckResultId}
    </select>

    <insert id="insertMesMaterialCheckResult" parameterType="MesMaterialCheckResult" useGeneratedKeys="true" keyProperty="materialCheckResultId">
        insert into mes_material_check_result
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="planId != null">plan_id,</if>
            <if test="planDetailId != null">plan_detail_id,</if>
            <if test="materialId != null">material_id,</if>
            <if test="standardAmount != null">standard_amount,</if>
            <if test="checkAmount != null">check_amount,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="planId != null">#{planId},</if>
            <if test="planDetailId != null">#{planDetailId},</if>
            <if test="materialId != null">#{materialId},</if>
            <if test="standardAmount != null">#{standardAmount},</if>
            <if test="checkAmount != null">#{checkAmount},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateMesMaterialCheckResult" parameterType="MesMaterialCheckResult">
        update mes_material_check_result
        <trim prefix="SET" suffixOverrides=",">
            <if test="planId != null">plan_id = #{planId},</if>
            <if test="planDetailId != null">plan_detail_id = #{planDetailId},</if>
            <if test="materialId != null">material_id = #{materialId},</if>
            <if test="standardAmount != null">standard_amount = #{standardAmount},</if>
            <if test="checkAmount != null">check_amount = #{checkAmount},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where material_check_result_id = #{materialCheckResultId}
    </update>

    <delete id="deleteMesMaterialCheckResultByMaterialCheckResultId" parameterType="Long">
        delete from mes_material_check_result where material_check_result_id = #{materialCheckResultId}
    </delete>

    <delete id="deleteMesMaterialCheckResultByMaterialCheckResultIds" parameterType="String">
        delete from mes_material_check_result where material_check_result_id in
        <foreach item="materialCheckResultId" collection="array" open="(" separator="," close=")">
            #{materialCheckResultId}
        </foreach>
    </delete>


    <select id="selectMesMaterialCheckResultByUI" resultMap="MesMaterialCheckResultResult">
        <include refid="selectMesMaterialCheckResultVo"/>
        where plan_id = #{planId} and plan_detail_id = #{planDetailId} and material_id = #{materialId} limit 1
    </select>


    <select id="selectMesMaterialCheckResult" resultMap="MesMaterialCheckResultResult">
        SELECT mmb.standard_amount,mmcr.check_amount,mmb.check_type FROM `mes_material_check_result` mmcr
            right join mes_material_bom mmb on mmb.material_id=mmcr.material_id
            where mmb.ancestors like concat(#{ancestors}, '%') and mmcr.plan_id = #{planId} and mmcr.plan_detail_id = #{planDetailId}
    </select>

</mapper>
