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

    <resultMap type="SgFeedStatistic" id="SgFeedStatisticResult">
        <result property="id" column="id"/>
        <result property="formulaId" column="formula_id"/>
        <result property="formulaBatchId" column="formula_batch_id"/>
        <result property="sheepFoldCount" column="sheep_fold_count"/>
        <result property="silageLossRate" column="silage_loss_rate"/>
        <result property="feedTotalSize" column="feed_total_size"/>
        <result property="feedDailySize" column="feed_daily_size"/>
        <!--
        适配泛型TypeHandler
        弃用: XML中无法使用"<>"
         -->
        <!--        <result property="materialList"    column="material_list"-->
        <!--        typeHandler="com.zhyc.module.feed.mapper.TypeHandler.JsonTypeHandler<java.util.List<com.zhyc.module.feed.domain.SgFormulaList>>" />-->
        <!-- 分别使用两个TypeHandler完成实体类List<T> 到 数据库 JSON串的映射转换 -->
        <result property="materialList" column="material_list"
                typeHandler="com.zhyc.module.feed.mapper.TypeHandler.SgFormulaListHandler"/>
        <result property="sheepFoldList" column="sheep_fold_list"
                typeHandler="com.zhyc.module.feed.mapper.TypeHandler.DaSheepfoldHandler"/>
        <result property="feedDate" column="feed_date"/>
    </resultMap>

    <sql id="selectSgFeedStatisticVo">
        select id,
               formula_id,
               formula_batch_id,
               sheep_fold_count,
               silage_loss_rate,
               feed_total_size,
               feed_daily_size,
               material_list,
               sheep_fold_list,
               feed_date
        from sg_feed_statistic sg_feed_statistic_alias
    </sql>

    <select id="selectSgFeedStatisticList" parameterType="SgFeedStatistic" resultMap="SgFeedStatisticResult">
        <include refid="selectSgFeedStatisticVo"/>
        <where>
            <if test="formulaId != null  and formulaId != ''">and formula_id LIKE CONCAT('%',#{formulaId},'%')</if>
            <if test="formulaBatchId != null  and formulaBatchId != ''">and formula_batch_id = LIKE
                CONCAT('%',#{formulaBatchId},'%')
            </if>
            <if test="silageLossRate != null  and silageLossRate != ''">and silage_loss_rate = #{silageLossRate}</if>
            ${params.dataScope}
        </where>
    </select>

    <select id="selectSgFeedStatisticById" parameterType="String" resultMap="SgFeedStatisticResult">
        <include refid="selectSgFeedStatisticVo"/>
        where id = #{id}
    </select>

    <insert id="insertSgFeedStatistic" parameterType="SgFeedStatistic">
        insert into sg_feed_statistic
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="id != null">id,</if>
            <if test="formulaId != null and formulaId != ''">formula_id,</if>
            <if test="formulaBatchId != null and formulaBatchId != ''">formula_batch_id,</if>
            <if test="sheepFoldCount != null">sheep_fold_count,</if>
            <if test="silageLossRate != null">silage_loss_rate,</if>
            <if test="feedTotalSize != null">feed_total_size,</if>
            <if test="feedDailySize != null">feed_daily_size,</if>
            <if test="materialList != null">material_list,</if>
            <if test="sheepFoldList != null">sheep_fold_list,</if>
            <if test="feedDate != null">feed_date,</if>
            <if test="userId != null">user_id,</if>
            <if test="deptId != null">dept_id,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="id != null">#{id},</if>
            <if test="formulaId != null and formulaId != ''">#{formulaId},</if>
            <if test="formulaBatchId != null and formulaBatchId != ''">#{formulaBatchId},</if>
            <if test="sheepFoldCount != null">#{sheepFoldCount},</if>
            <if test="silageLossRate != null">#{silageLossRate},</if>
            <if test="feedTotalSize != null">#{feedTotalSize},</if>
            <if test="feedDailySize != null">#{feedDailySize},</if>
            # 写入操作需要手动指定 TypeHandler 参数
            <if test="materialList != null">
                #{materialList, typeHandler=com.zhyc.module.feed.mapper.TypeHandler.SgFormulaListHandler},
            </if>
            <if test="sheepFoldList != null">
                #{sheepFoldList, typeHandler=com.zhyc.module.feed.mapper.TypeHandler.DaSheepfoldHandler},
            </if>
            <if test="feedDate != null">#{feedDate},</if>
            <if test="userId != null">#{userId},</if>
            <if test="deptId != null">#{deptId},</if>
        </trim>
    </insert>

    <update id="updateSgFeedStatistic" parameterType="SgFeedStatistic">
        update sg_feed_statistic
        <trim prefix="SET" suffixOverrides=",">
            <if test="formulaId != null and formulaId != ''">formula_id = #{formulaId},</if>
            <if test="formulaBatchId != null and formulaBatchId != ''">formula_batch_id = #{formulaBatchId},</if>
            <if test="sheepFoldCount != null">sheep_fold_count = #{sheepFoldCount},</if>
            <if test="silageLossRate != null">silage_loss_rate = #{silageLossRate},</if>
            <if test="feedTotalSize != null">feed_total_size = #{feedTotalSize},</if>
            <if test="feedDailySize != null">feed_daily_size = #{feedDailySize},</if>
            <if test="materialList != null">material_list =
                #{materialList, typeHandler=com.zhyc.module.feed.mapper.TypeHandler.SgFormulaListHandler},
            </if>
            <if test="sheepFoldList != null">sheep_fold_list =
                #{sheepFoldList, typeHandler=com.zhyc.module.feed.mapper.TypeHandler.DaSheepfoldHandler},
            </if>
            <if test="feedDate != null">feed_date = #{feedDate},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteSgFeedStatisticById" parameterType="String">
        delete
        from sg_feed_statistic
        where id = #{id}
    </delete>

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