<?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.zhyc.module.feed.mapper.SgFormulaListMapper">
    
    <resultMap type="SgFormulaList" id="SgFormulaListResult">
        <result property="code"    column="code"    />
        <result property="formulaId"    column="formula_id"    />
        <result property="batchId"    column="batch_id"    />
        <result property="materialId"    column="material_id"    />
        <result property="materialName"    column="material_name"    />
        <result property="ratio"    column="ratio"    />
        <result property="isGranular"    column="is_granular"    />
        <result property="isSupplement"    column="is_supplement"    />
    </resultMap>

    <sql id="selectSgFormulaListVo">
        select code, formula_id, material_id, material_name, ratio, is_granular, is_supplement from sg_formula_list
    </sql>

    <select id="selectSgFormulaListList" parameterType="SgFormulaList" resultMap="SgFormulaListResult">
        <include refid="selectSgFormulaListVo"/>
        <where>
            <if test="materialId != null  and materialId != ''">material_id = #{materialId}</if>
            <if test="formulaId != null  and formulaId != ''">and formula_id = #{formulaId}</if>
            <if test="batchId != null  and batchId != ''"> and batch_id = #{batchId}</if>
        </where>
    </select>
    
    <select id="selectSgFormulaListByCode" parameterType="Long" resultMap="SgFormulaListResult">
        <include refid="selectSgFormulaListVo"/>
        where code = #{code}
    </select>

    <insert id="insertSgFormulaList" parameterType="SgFormulaList">
        insert into sg_formula_list
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="code != null">code,</if>
            <if test="formulaId != null">formula_id,</if>
            <if test="batchId != null">batch_id,</if>
            <if test="materialId != null">material_id,</if>
            <if test="materialName != null">material_name,</if>
            <if test="ratio != null">ratio,</if>
            <if test="isGranular != null">is_granular,</if>
            <if test="isSupplement != null">is_supplement,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="code != null">#{code},</if>
            <if test="formulaId != null">#{formulaId},</if>
            <if test="batchId != null">#{batchId},</if>
            <if test="materialId != null">#{materialId},</if>
            <if test="materialName != null">#{materialName},</if>
            <if test="ratio != null">#{ratio},</if>
            <if test="isGranular != null">#{isGranular},</if>
            <if test="isSupplement != null">#{isSupplement},</if>
         </trim>
    </insert>

    <update id="updateSgFormulaList" parameterType="SgFormulaList">
        update sg_formula_list
        <trim prefix="SET" suffixOverrides=",">
            <if test="formulaId != null">formula_id = #{formulaId},</if>
            <if test="materialId != null">material_id = #{materialId},</if>
            <if test="materialName != null">material_name = #{materialName},</if>
            <if test="ratio != null">ratio = #{ratio},</if>
            <if test="isGranular != null">is_granular = #{isGranular},</if>
            <if test="isSupplement != null">is_supplement = #{isSupplement},</if>
        </trim>
        where code = #{code}
    </update>

    <delete id="deleteSgFormulaListByCode" parameterType="Long">
        delete from sg_formula_list where code = #{code}
    </delete>

    <delete id="deleteSgFormulaListByCodes" parameterType="String">
        delete from sg_formula_list where code in 
        <foreach item="code" collection="array" open="(" separator="," close=")">
            #{code}
        </foreach>
    </delete>

    <delete id="deleteSgFormulaListByFormulaIdAndBatchId" parameterType="SgFormulaList">
        DELETE FROM sg_formula_list WHERE formula_id = #{formulaId} AND batch_id = #{batchId}
    </delete>
</mapper>