<?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.ximai.mes.pro.mapper.productionSolution.ProProductionSolutionBomMapper">

    <resultMap type="ProProductionSolutionBom" id="ProProductionSolutionBomResult">
        <result property="bomItemId" column="bom_item_id"/>
        <result property="productionSolutionId" column="production_solution_id"/>
        <result property="itemId" column="item_id"/>
        <result property="itemName" column="item_name"/>
        <result property="quantity" column="quantity"/>
        <result property="unitOfMeasure" column="unit_of_measure"/>
        <result property="scrapPercentage" column="scrap_percentage"/>
        <result property="alternativeGroup" column="alternative_group"/>
        <result property="alternativePriorities" column="alternative_priorities"/>
        <result property="alternativeStrategy" column="alternative_strategy"/>
        <result property="alternativeProbability" column="alternative_probability"/>
        <result property="bomItemRemark" column="bom_item_remark"/>
        <result property="itemRemark" column="item_remark"/>
    </resultMap>

    <sql id="selectProProductionSolutionBomVo">
        select ppsb.bom_item_id,
               ppsb.production_solution_id,
               ppsb.item_id,
               mi.item_name,
               ppsb.quantity,
               ppsb.unit_of_measure,
               ppsb.scrap_percentage,
               ppsb.alternative_group,
               ppsb.alternative_priorities,
               ppsb.alternative_strategy,
               ppsb.alternative_probability,
               ppsb.bom_item_remark,
               ppsb.item_remark
        from pro_production_solution_bom ppsb
                 left join md_item mi on mi.item_id = ppsb.item_id
    </sql>

    <select id="selectProProductionSolutionBomList" parameterType="ProProductionSolutionBom"
            resultMap="ProProductionSolutionBomResult">
        <include refid="selectProProductionSolutionBomVo"/>
        <where>
            <if test="productionSolutionId != null ">and ppsb.production_solution_id = #{productionSolutionId}</if>
            <if test="itemId != null ">and ppsb.item_id = #{itemId}</if>
            <if test="quantity != null ">and ppsb.quantity = #{quantity}</if>
            <if test="unitOfMeasure != null  and unitOfMeasure != ''">and ppsb.unit_of_measure = #{unitOfMeasure}</if>
            <if test="scrapPercentage != null ">and ppsb.scrap_percentage = #{scrapPercentage}</if>
            <if test="alternativeGroup != null  and alternativeGroup != ''">and ppsb.alternative_group =
                #{alternativeGroup}
            </if>
            <if test="alternativePriorities != null ">and ppsb.alternative_priorities = #{alternativePriorities}</if>
            <if test="alternativeStrategy != null  and alternativeStrategy != ''">and ppsb.alternative_strategy =
                #{alternativeStrategy}
            </if>
            <if test="alternativeProbability != null ">and ppsb.alternative_probability = #{alternativeProbability}</if>
            <if test="bomItemRemark != null  and bomItemRemark != ''">and ppsb.bom_item_remark = #{bomItemRemark}</if>
            <if test="itemRemark != null  and itemRemark != ''">and ppsb.item_remark = #{itemRemark}</if>
        </where>
    </select>

    <select id="selectProProductionSolutionBomByBomItemId" parameterType="Long"
            resultMap="ProProductionSolutionBomResult">
        <include refid="selectProProductionSolutionBomVo"/>
        where ppsb.bom_item_id = #{bomItemId}
    </select>
    <select id="selectProProductionSolutionBomByProductionSolutionId"
            parameterType="Long"
            resultMap="ProProductionSolutionBomResult">
        <include refid="selectProProductionSolutionBomVo"/>
        where ppsb.production_solution_id = #{productionSolutionId}
    </select>
    <select id="selectProProductionSolutionBomByItemId"
            parameterType="Long"
            resultMap="ProProductionSolutionBomResult">
        <include refid="selectProProductionSolutionBomVo"/>
        where ppsb.item_id = #{itemId}
    </select>

    <insert id="insertProProductionSolutionBom" parameterType="ProProductionSolutionBom" useGeneratedKeys="true"
            keyProperty="bomItemId">
        insert into pro_production_solution_bom
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="productionSolutionId != null">production_solution_id,</if>
            <if test="itemId != null">item_id,</if>
            <if test="quantity != null">quantity,</if>
            <if test="unitOfMeasure != null">unit_of_measure,</if>
            <if test="scrapPercentage != null">scrap_percentage,</if>
            <if test="alternativeGroup != null">alternative_group,</if>
            <if test="alternativePriorities != null">alternative_priorities,</if>
            <if test="alternativeStrategy != null">alternative_strategy,</if>
            <if test="alternativeProbability != null">alternative_probability,</if>
            <if test="bomItemRemark != null">bom_item_remark,</if>
            <if test="itemRemark != null">item_remark,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="productionSolutionId != null">#{productionSolutionId},</if>
            <if test="itemId != null">#{itemId},</if>
            <if test="quantity != null">#{quantity},</if>
            <if test="unitOfMeasure != null">#{unitOfMeasure},</if>
            <if test="scrapPercentage != null">#{scrapPercentage},</if>
            <if test="alternativeGroup != null">#{alternativeGroup},</if>
            <if test="alternativePriorities != null">#{alternativePriorities},</if>
            <if test="alternativeStrategy != null">#{alternativeStrategy},</if>
            <if test="alternativeProbability != null">#{alternativeProbability},</if>
            <if test="bomItemRemark != null">#{bomItemRemark},</if>
            <if test="itemRemark != null">#{itemRemark},</if>
        </trim>
    </insert>

    <update id="updateProProductionSolutionBom" parameterType="ProProductionSolutionBom">
        update pro_production_solution_bom
        <trim prefix="SET" suffixOverrides=",">
            <if test="productionSolutionId != null">production_solution_id = #{productionSolutionId},</if>
            <if test="itemId != null">item_id = #{itemId},</if>
            <if test="quantity != null">quantity = #{quantity},</if>
            <if test="unitOfMeasure != null">unit_of_measure = #{unitOfMeasure},</if>
            <if test="scrapPercentage != null">scrap_percentage = #{scrapPercentage},</if>
            <if test="alternativeGroup != null">alternative_group = #{alternativeGroup},</if>
            <if test="alternativePriorities != null">alternative_priorities = #{alternativePriorities},</if>
            <if test="alternativeStrategy != null">alternative_strategy = #{alternativeStrategy},</if>
            <if test="alternativeProbability != null">alternative_probability = #{alternativeProbability},</if>
            <if test="bomItemRemark != null">bom_item_remark = #{bomItemRemark},</if>
            <if test="itemRemark != null">item_remark = #{itemRemark},</if>
        </trim>
        where bom_item_id = #{bomItemId}
    </update>

    <delete id="deleteProProductionSolutionBomByBomItemId" parameterType="Long">
        delete
        from pro_production_solution_bom
        where bom_item_id = #{bomItemId}
    </delete>

    <delete id="deleteProProductionSolutionBomByBomItemIds" parameterType="String">
        delete from pro_production_solution_bom where bom_item_id in
        <foreach item="bomItemId" collection="array" open="(" separator="," close=")">
            #{bomItemId}
        </foreach>
    </delete>
</mapper>