<?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.SgFormulaManagementMapper">

    <resultMap type="SgFormulaManagement" id="SgFormulaManagementResult">
        <result property="formulaId" column="formula_id"/>
        <result property="feedStage" column="feed_stage"/>
        <result property="batchId" column="batch_id"/>
        <result property="useStartDate" column="use_start_date"/>
        <result property="useEndDate" column="use_end_date"/>
        <result property="useState" column="use_state"/>
        <result property="remark" column="remark"/>
    </resultMap>

    <sql id="selectSgFormulaManagementVo">
        select formula_id, feed_stage, batch_id, use_start_date, use_end_date, use_state, remark
        from sg_formula_management sg_formula_management_alias
    </sql>

    <select id="selectSgFormulaManagementList" parameterType="SgFormulaManagement"
            resultMap="SgFormulaManagementResult">
        <include refid="selectSgFormulaManagementVo"/>
        <where>
            <if test="formulaId != null  and formulaId != ''">and formula_id LIKE CONCAT('%',#{formulaId},'%')</if>
            <if test="feedStage != null  and feedStage != ''">and feed_stage LIKE CONCAT('%',#{feedStage},'%')</if>
            <if test="batchId != null  and batchId != ''">and batch_id LIKE CONCAT('%',#{batchId},'%')</if>
            ${params.dataScope}
        </where>
    </select>

    <select id="selectSgFormulaManagementByFormulaId" parameterType="String" resultMap="SgFormulaManagementResult">
        <include refid="selectSgFormulaManagementVo"/>
        where formula_id = #{formulaId}
    </select>

    <insert id="insertSgFormulaManagement" parameterType="SgFormulaManagement">
        insert into sg_formula_management
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="formulaId != null and formulaId != ''">formula_id,</if>
            <if test="feedStage != null and feedStage != ''">feed_stage,</if>
            <if test="batchId != null">batch_id,</if>
            <if test="useStartDate != null">use_start_date,</if>
            <if test="useEndDate != null">use_end_date,</if>
            <if test="useState != null">use_state,</if>
            <if test="remark != null">remark,</if>
            <if test="userId != null">user_id,</if>
            <if test="deptId != null">dept_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="formulaId != null and formulaId != ''">#{formulaId},</if>
            <if test="feedStage != null and feedStage != ''">#{feedStage},</if>
            <if test="batchId != null">#{batchId},</if>
            <if test="useStartDate != null">#{useStartDate},</if>
            <if test="useEndDate != null">#{useEndDate},</if>
            <if test="useState != null">#{useState},</if>
            <if test="remark != null">#{remark},</if>
            <if test="userId != null">#{userId},</if>
            <if test="deptId != null">#{deptId},</if>
        </trim>
    </insert>

    <update id="updateSgFormulaManagement" parameterType="SgFormulaManagement">
        update sg_formula_management
        <trim prefix="SET" suffixOverrides=",">
            <if test="feedStage != null and feedStage != ''">feed_stage = #{feedStage},</if>
            <if test="useStartDate != null">use_start_date = #{useStartDate},</if>
            <if test="useEndDate != null">use_end_date = #{useEndDate},</if>
            <if test="useState != null">use_state = #{useState},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where formula_id = #{formulaId} AND batch_id = #{batchId}
    </update>

    <delete id="deleteSgFormulaManagementByFormulaId" parameterType="String">
        delete
        from sg_formula_management
        where formula_id = #{formulaId}
    </delete>

    <delete id="deleteSgFormulaManagementByFormulaIds" parameterType="String">
        delete from sg_formula_management where formula_id in
        <foreach item="formulaId" collection="array" open="(" separator="," close=")">
            #{formulaId}
        </foreach>
    </delete>
    <delete id="deleteSgFormulaManagement" parameterType="SgFormulaManagement">
        delete
        from sg_formula_management
        where formula_id = #{formulaId}
          and batch_id = #{batchId}
    </delete>
</mapper>