<?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.SgFeedPlanMapper">
    
    <resultMap type="SgFeedPlan" id="SgFeedPlanResult">
        <result property="createDate"    column="create_date"    />
        <result property="formulaId"    column="formula_id"    />
        <result property="batchId"    column="batch_id"    />
        <result property="sheepHouseId"    column="sheep_house_id"    />
        <result property="sheepCount"    column="sheep_count"    />
        <result property="planDailySize"    column="plan_daily_size"    />
        <result property="ratioMorning"    column="ratio_morning"    />
        <result property="ratioNoon"    column="ratio_noon"    />
        <result property="ratioAfternoon"    column="ratio_afternoon"    />
        <result property="planMorningSize"    column="plan_morning_size"    />
        <result property="planMorningTotal"    column="plan_morning_total"    />
        <result property="actualMorningSize"    column="actual_morning_size"    />
        <result property="planNoonSize"    column="plan_noon_size"    />
        <result property="planNoonTotal"    column="plan_noon_total"    />
        <result property="actualNoonSize"    column="actual_noon_size"    />
        <result property="planAfternoonSize"    column="plan_afternoon_size"    />
        <result property="planAfternoonTotal"    column="plan_afternoon_total"    />
        <result property="actualAfternoonSize"    column="actual_afternoon_size"    />
        <result property="planFeedTotal"    column="plan_feed_total"    />
        <result property="zookeeper"    column="zookeeper"    />
        <result property="planDate"    column="plan_date"    />
        <result property="remark"    column="remark"    />
    </resultMap>

    <sql id="selectSgFeedPlanVo">
        select create_date, formula_id, batch_id, sheep_house_id, sheep_count, plan_daily_size, ratio_morning, ratio_noon, ratio_afternoon, plan_morning_size, plan_morning_total, actual_morning_size, plan_noon_size, plan_noon_total, actual_noon_size, plan_afternoon_size, plan_afternoon_total, actual_afternoon_size, plan_feed_total, zookeeper, plan_date, remark from sg_feed_plan sg_feed_plan_alias
    </sql>

    <select id="selectSgFeedPlanList" parameterType="SgFeedPlan" resultMap="SgFeedPlanResult">
        <include refid="selectSgFeedPlanVo"/>
        <where>
            <if test="formulaId != null  and formulaId != ''">and formula_id LIKE CONCAT('%',#{formulaId},'%')</if>
            <if test="batchId != null  and batchId != ''">and batch_id LIKE CONCAT('%',#{batchId},'%')</if>
            <if test="sheepHouseId != null ">and sheep_house_id LIKE CONCAT('%',#{sheepHouseId},'%')</if>
            <if test="planDate != null ">and plan_date = #{planDate}</if>
            ${params.dataScope}
        </where>
        ORDER BY plan_date ASC, formula_id ASC , batch_id ASC
    </select>
    
    <select id="selectSgFeedPlanByCreateDate" parameterType="Date" resultMap="SgFeedPlanResult">
        <include refid="selectSgFeedPlanVo"/>
        where create_date = #{createDate}
    </select>

    <insert id="insertSgFeedPlan" parameterType="SgFeedPlan">
        insert into sg_feed_plan
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="createDate != null">create_date,</if>
            <if test="formulaId != null and formulaId != ''">formula_id,</if>
            <if test="batchId != null and batchId != ''">batch_id,</if>
            <if test="sheepHouseId != null">sheep_house_id,</if>
            <if test="sheepCount != null">sheep_count,</if>
            <if test="planDailySize != null">plan_daily_size,</if>
            <if test="ratioMorning != null">ratio_morning,</if>
            <if test="ratioNoon != null">ratio_noon,</if>
            <if test="ratioAfternoon != null">ratio_afternoon,</if>
            <if test="planMorningSize != null">plan_morning_size,</if>
            <if test="planMorningTotal != null">plan_morning_total,</if>
            <if test="actualMorningSize != null">actual_morning_size,</if>
            <if test="planNoonSize != null">plan_noon_size,</if>
            <if test="planNoonTotal != null">plan_noon_total,</if>
            <if test="actualNoonSize != null">actual_noon_size,</if>
            <if test="planAfternoonSize != null">plan_afternoon_size,</if>
            <if test="planAfternoonTotal != null">plan_afternoon_total,</if>
            <if test="actualAfternoonSize != null">actual_afternoon_size,</if>
            <if test="planFeedTotal != null">plan_feed_total,</if>
            <if test="zookeeper != null">zookeeper,</if>
            <if test="planDate != null">plan_date,</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="createDate != null">#{createDate},</if>
            <if test="formulaId != null and formulaId != ''">#{formulaId},</if>
            <if test="batchId != null and batchId != ''">#{batchId},</if>
            <if test="sheepHouseId != null">#{sheepHouseId},</if>
            <if test="sheepCount != null">#{sheepCount},</if>
            <if test="planDailySize != null">#{planDailySize},</if>
            <if test="ratioMorning != null">#{ratioMorning},</if>
            <if test="ratioNoon != null">#{ratioNoon},</if>
            <if test="ratioAfternoon != null">#{ratioAfternoon},</if>
            <if test="planMorningSize != null">#{planMorningSize},</if>
            <if test="planMorningTotal != null">#{planMorningTotal},</if>
            <if test="actualMorningSize != null">#{actualMorningSize},</if>
            <if test="planNoonSize != null">#{planNoonSize},</if>
            <if test="planNoonTotal != null">#{planNoonTotal},</if>
            <if test="actualNoonSize != null">#{actualNoonSize},</if>
            <if test="planAfternoonSize != null">#{planAfternoonSize},</if>
            <if test="planAfternoonTotal != null">#{planAfternoonTotal},</if>
            <if test="actualAfternoonSize != null">#{actualAfternoonSize},</if>
            <if test="planFeedTotal != null">#{planFeedTotal},</if>
            <if test="zookeeper != null">#{zookeeper},</if>
            <if test="planDate != null">#{planDate},</if>
            <if test="remark != null">#{remark},</if>
            <if test="userId != null">#{userId},</if>
            <if test="deptId != null">#{deptId},</if>
        </trim>
    </insert>

    <update id="updateSgFeedPlan" parameterType="SgFeedPlan">
        update sg_feed_plan
        <trim prefix="SET" suffixOverrides=",">
            <if test="formulaId != null and formulaId != ''">formula_id = #{formulaId},</if>
            <if test="batchId != null and batchId != ''">batch_id = #{batchId},</if>
            <if test="sheepHouseId != null">sheep_house_id = #{sheepHouseId},</if>
            <if test="sheepCount != null">sheep_count = #{sheepCount},</if>
            <if test="planDailySize != null">plan_daily_size = #{planDailySize},</if>
            <if test="ratioMorning != null">ratio_morning = #{ratioMorning},</if>
            <if test="ratioNoon != null">ratio_noon = #{ratioNoon},</if>
            <if test="ratioAfternoon != null">ratio_afternoon = #{ratioAfternoon},</if>
            <if test="planMorningSize != null">plan_morning_size = #{planMorningSize},</if>
            <if test="planMorningTotal != null">plan_morning_total = #{planMorningTotal},</if>
            <if test="actualMorningSize != null">actual_morning_size = #{actualMorningSize},</if>
            <if test="planNoonSize != null">plan_noon_size = #{planNoonSize},</if>
            <if test="planNoonTotal != null">plan_noon_total = #{planNoonTotal},</if>
            <if test="actualNoonSize != null">actual_noon_size = #{actualNoonSize},</if>
            <if test="planAfternoonSize != null">plan_afternoon_size = #{planAfternoonSize},</if>
            <if test="planAfternoonTotal != null">plan_afternoon_total = #{planAfternoonTotal},</if>
            <if test="actualAfternoonSize != null">actual_afternoon_size = #{actualAfternoonSize},</if>
            <if test="planFeedTotal != null">plan_feed_total = #{planFeedTotal},</if>
            <if test="zookeeper != null">zookeeper = #{zookeeper},</if>
            <if test="planDate != null">plan_date = #{planDate},</if>
            <if test="remark != null">remark = #{remark},</if>
        </trim>
        where create_date = #{createDate}
    </update>

    <delete id="deleteSgFeedPlanByCreateDate" parameterType="Date">
        delete from sg_feed_plan where create_date = #{createDate}
    </delete>

    <delete id="deleteSgFeedPlanByCreateDates" parameterType="String">
        delete from sg_feed_plan where create_date in 
        <foreach item="createDate" collection="array" open="(" separator="," close=")">
            #{createDate}
        </foreach>
    </delete>

    <select id="selectSheepCountByFoldId" parameterType="Integer">
        SELECT COUNT(*)
        FROM bas_sheep
        WHERE sheepfold_id = #{foldId}
    </select>
</mapper>