<?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.bonus.canteen.core.cook.mapper.CookRecipeDetailMapper">
    <resultMap type="com.bonus.canteen.core.cook.domain.CookRecipeDetail" id="CookRecipeDetailResult">
        <result property="recipeDetailId"    column="recipe_detail_id"    />
        <result property="saleTypeId"    column="sale_type_id"    />
        <result property="recipeId"    column="recipe_id"    />
        <result property="applyDate"    column="apply_date"    />
        <result property="applyWeek"    column="apply_week"    />
        <result property="applyMonth"    column="apply_month"    />
        <result property="mealtimeType"    column="mealtime_type"    />
        <result property="detailType"    column="detail_type"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
    </resultMap>

    <sql id="selectCookRecipeDetailVo">
        select recipe_detail_id, sale_type_id, recipe_id, apply_date, apply_week, apply_month, mealtime_type,
               detail_type, create_by, create_time, update_by, update_time
        from cook_recipe_detail
    </sql>

    <select id="selectCookRecipeDetailList" parameterType="com.bonus.canteen.core.cook.domain.CookRecipeDetail" resultMap="CookRecipeDetailResult">
        <include refid="selectCookRecipeDetailVo"/>
        <where>
            <if test="saleTypeId != null  and saleTypeId != ''"> and sale_type_id = #{saleTypeId}</if>
            <if test="recipeId != null "> and recipe_id = #{recipeId}</if>
            <if test="applyDate != null "> and apply_date = #{applyDate}</if>
            <if test="applyWeek != null "> and apply_week = #{applyWeek}</if>
            <if test="applyMonth != null "> and apply_month = #{applyMonth}</if>
            <if test="mealtimeType != null "> and mealtime_type = #{mealtimeType}</if>
            <if test="detailType != null "> and detail_type = #{detailType}</if>
        </where>
    </select>

    <select id="selectCookRecipeDetailByRecipeDetailId" parameterType="Long" resultMap="CookRecipeDetailResult">
        <include refid="selectCookRecipeDetailVo"/>
        where recipe_detail_id = #{recipeDetailId}
    </select>

    <insert id="insertCookRecipeDetail" parameterType="com.bonus.canteen.core.cook.domain.CookRecipeDetail" useGeneratedKeys="true" keyProperty="recipeDetailId">
        insert into cook_recipe_detail
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="saleTypeId != null">sale_type_id,</if>
            <if test="recipeId != null">recipe_id,</if>
            <if test="applyDate != null">apply_date,</if>
            <if test="applyWeek != null">apply_week,</if>
            <if test="applyMonth != null">apply_month,</if>
            <if test="mealtimeType != null">mealtime_type,</if>
            <if test="detailType != null">detail_type,</if>
            <if test="createBy != null">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null">update_by,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="saleTypeId != null">#{saleTypeId},</if>
            <if test="recipeId != null">#{recipeId},</if>
            <if test="applyDate != null">#{applyDate},</if>
            <if test="applyWeek != null">#{applyWeek},</if>
            <if test="applyMonth != null">#{applyMonth},</if>
            <if test="mealtimeType != null">#{mealtimeType},</if>
            <if test="detailType != null">#{detailType},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateCookRecipeDetail" parameterType="com.bonus.canteen.core.cook.domain.CookRecipeDetail">
        update cook_recipe_detail
        <trim prefix="SET" suffixOverrides=",">
            <if test="saleTypeId != null">sale_type_id = #{saleTypeId},</if>
            <if test="recipeId != null">recipe_id = #{recipeId},</if>
            <if test="applyDate != null">apply_date = #{applyDate},</if>
            <if test="applyWeek != null">apply_week = #{applyWeek},</if>
            <if test="applyMonth != null">apply_month = #{applyMonth},</if>
            <if test="mealtimeType != null">mealtime_type = #{mealtimeType},</if>
            <if test="detailType != null">detail_type = #{detailType},</if>
            <if test="createBy != null">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where recipe_detail_id = #{recipeDetailId}
    </update>

    <delete id="deleteCookRecipeDetailByRecipeDetailId" parameterType="Long">
        delete from cook_recipe_detail where recipe_detail_id = #{recipeDetailId}
    </delete>

    <delete id="deleteCookRecipeDetailByRecipeDetailIds" parameterType="String">
        delete from cook_recipe_detail where recipe_detail_id in
        <foreach item="recipeDetailId" collection="array" open="(" separator="," close=")">
            #{recipeDetailId}
        </foreach>
    </delete>

    <select id="getAllCookRecipeDetailIds" parameterType="String" resultType="Long">
        SELECT distinct(recipe_detail_id)
        from cook_recipe_detail where recipe_id in
        <foreach item="recipeId" collection="array" open="(" separator="," close=")">
            #{recipeId}
        </foreach>
    </select>

    <select id="getToDeleteCookRecipeDetailIds4PointDays" resultType="Long">
        SELECT distinct(crd.recipe_detail_id)
        FROM cook_recipe_detail crd
        left join cook_recipe cr ON cr.recipe_id = crd.recipe_id
        where cr.recipe_id = #{recipeId} and cr.recipe_type = 1 and crd.apply_date in
        <foreach item="applyDate" collection="dateList" open="(" separator="," close=")">
            #{applyDate}
        </foreach>
    </select>

    <select id="getToDeleteCookRecipeDetailIds4Daily" resultType="Long">
        SELECT distinct(crd.recipe_detail_id)
        FROM cook_recipe_detail crd
                 left join cook_recipe cr ON cr.recipe_id = crd.recipe_id
        where cr.recipe_id = #{recipeId} and cr.recipe_type = 2 and crd.detail_type = 1

        UNION

        SELECT distinct(crd.recipe_detail_id)
        FROM cook_recipe_detail crd
                 left join cook_recipe cr ON cr.recipe_id = crd.recipe_id
        where cr.recipe_id = #{recipeId} and cr.recipe_type = 2 and crd.detail_type = 2 and crd.apply_date >= NOW()-1
    </select>

    <select id="getToDeleteCookRecipeDetailIds4WeeklyTemplate" parameterType="Long" resultType="Long">
        SELECT distinct(crd.recipe_detail_id)
        FROM cook_recipe_detail crd
                 left join cook_recipe cr ON cr.recipe_id = crd.recipe_id
        where cr.recipe_id = #{recipeId} and cr.recipe_type = 3 and crd.detail_type = 1 and crd.apply_week in
        <foreach item="applyWeek" collection="applyWeeks" open="(" separator="," close=")">
            #{applyWeek}
        </foreach>
    </select>

    <select id="getToDeleteCookRecipeDetailIds4WeeklyDetail" resultType="Long">
        SELECT distinct(crd.recipe_detail_id)
        FROM cook_recipe_detail crd
                 left join cook_recipe cr ON cr.recipe_id = crd.recipe_id
        where cr.recipe_id = #{recipeId} and cr.recipe_type = 3 and crd.detail_type = 2 and crd.apply_date in
        <foreach item="applyDate" collection="dateList" open="(" separator="," close=")">
            #{applyDate}
        </foreach>
    </select>

    <select id="getCookRecipeDetailLTemplate" resultMap="CookRecipeDetailResult">
        select * from cook_recipe_detail
        where recipe_id = #{recipeId}
        and detail_type = #{detailType}
        group by mealtime_type
    </select>

    <select id="getByRecipeIdAndDatesAndIntervalId" resultMap="CookRecipeDetailResult">
        select * from cook_recipe_detail
        where recipe_id = #{recipeId}
          and mealtime_type = #{mealtimeType}
          and apply_date in
        <foreach item="applyDate" collection="dateList" open="(" separator="," close=")">
            #{applyDate}
        </foreach>
    </select>

    <select id="getCookRecipeDetailsByApplyDateAndRecipeIds" resultMap="CookRecipeDetailResult">
        <include refid="selectCookRecipeDetailVo"/>
        where apply_date = #{applyDate} and recipe_id in
        <foreach item="recipeId" collection="recipeIdList" open="(" separator="," close=")">
            #{recipeId}
        </foreach>
    </select>

    <select id="getCookRecipeDetailsByDetailTypeAndRecipeIds" resultMap="CookRecipeDetailResult">
        <include refid="selectCookRecipeDetailVo"/>
        where detail_type = 1 and recipe_id in
        <foreach item="recipeId" collection="recipeIdList" open="(" separator="," close=")">
            #{recipeId}
        </foreach>
    </select>
</mapper>
