<?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.CookRecipeDishesMapper">
    <resultMap type="com.bonus.canteen.core.cook.domain.CookRecipeDishes" id="CookRecipeDishesResult">
        <result property="id"    column="id"    />
        <result property="recipeDetailId"    column="recipe_detail_id"    />
        <result property="dishesId"    column="dishes_id"    />
        <result property="price"    column="price"    />
        <result property="salePrice"    column="sale_price"    />
        <result property="sizeType"    column="size_type"    />
        <result property="supplyNum"    column="supply_num"    />
        <result property="saleNum"    column="sale_num"    />
        <result property="remanentNum"    column="remanent_num"    />
        <result property="limitNum"    column="limit_num"    />
        <result property="chefId"    column="chef_id"    />
        <result property="recommendFlag"    column="recommend_flag"    />
        <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="selectCookRecipeDishesVo">
        select id, recipe_detail_id, dishes_id, price, sale_price, size_type, supply_num, sale_num, remanent_num, limit_num, chef_id, recommend_flag, create_by, create_time, update_by, update_time from cook_recipe_dishes
    </sql>

    <select id="selectCookRecipeDishesList" parameterType="com.bonus.canteen.core.cook.domain.CookRecipeDishes" resultMap="CookRecipeDishesResult">
        <include refid="selectCookRecipeDishesVo"/>
        <where>  
            <if test="recipeDetailId != null "> and recipe_detail_id = #{recipeDetailId}</if>
            <if test="dishesId != null "> and dishes_id = #{dishesId}</if>
            <if test="price != null "> and price = #{price}</if>
            <if test="salePrice != null "> and sale_price = #{salePrice}</if>
            <if test="sizeType != null "> and size_type = #{sizeType}</if>
            <if test="supplyNum != null "> and supply_num = #{supplyNum}</if>
            <if test="saleNum != null "> and sale_num = #{saleNum}</if>
            <if test="remanentNum != null "> and remanent_num = #{remanentNum}</if>
            <if test="limitNum != null "> and limit_num = #{limitNum}</if>
            <if test="chefId != null "> and chef_id = #{chefId}</if>
            <if test="recommendFlag != null "> and recommend_flag = #{recommendFlag}</if>
        </where>
    </select>
    
    <select id="selectCookRecipeDishesById" parameterType="Long" resultMap="CookRecipeDishesResult">
        <include refid="selectCookRecipeDishesVo"/>
        where id = #{id}
    </select>
        
    <insert id="insertCookRecipeDishes" parameterType="com.bonus.canteen.core.cook.domain.CookRecipeDishes" useGeneratedKeys="true" keyProperty="id">
        insert into cook_recipe_dishes
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="recipeDetailId != null">recipe_detail_id,</if>
            <if test="dishesId != null">dishes_id,</if>
            <if test="price != null">price,</if>
            <if test="salePrice != null">sale_price,</if>
            <if test="sizeType != null">size_type,</if>
            <if test="supplyNum != null">supply_num,</if>
            <if test="saleNum != null">sale_num,</if>
            <if test="remanentNum != null">remanent_num,</if>
            <if test="limitNum != null">limit_num,</if>
            <if test="chefId != null">chef_id,</if>
            <if test="recommendFlag != null">recommend_flag,</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="recipeDetailId != null">#{recipeDetailId},</if>
            <if test="dishesId != null">#{dishesId},</if>
            <if test="price != null">#{price},</if>
            <if test="salePrice != null">#{salePrice},</if>
            <if test="sizeType != null">#{sizeType},</if>
            <if test="supplyNum != null">#{supplyNum},</if>
            <if test="saleNum != null">#{saleNum},</if>
            <if test="remanentNum != null">#{remanentNum},</if>
            <if test="limitNum != null">#{limitNum},</if>
            <if test="chefId != null">#{chefId},</if>
            <if test="recommendFlag != null">#{recommendFlag},</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="updateCookRecipeDishes" parameterType="com.bonus.canteen.core.cook.domain.CookRecipeDishes">
        update cook_recipe_dishes
        <trim prefix="SET" suffixOverrides=",">
            <if test="recipeDetailId != null">recipe_detail_id = #{recipeDetailId},</if>
            <if test="dishesId != null">dishes_id = #{dishesId},</if>
            <if test="price != null">price = #{price},</if>
            <if test="salePrice != null">sale_price = #{salePrice},</if>
            <if test="sizeType != null">size_type = #{sizeType},</if>
            <if test="supplyNum != null">supply_num = #{supplyNum},</if>
            <if test="saleNum != null">sale_num = #{saleNum},</if>
            <if test="remanentNum != null">remanent_num = #{remanentNum},</if>
            <if test="limitNum != null">limit_num = #{limitNum},</if>
            <if test="chefId != null">chef_id = #{chefId},</if>
            <if test="recommendFlag != null">recommend_flag = #{recommendFlag},</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 id = #{id}
    </update>

    <delete id="deleteCookRecipeDishesById" parameterType="Long">
        delete from cook_recipe_dishes where id = #{id}
    </delete>

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

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

    <update id="reduceCookRecipeDishesSupplyNum">
        update cook_recipe_dishes set
        sale_num = sale_num + #{quantity},
        remanent_num = remanent_num - #{quantity}
        where recipe_detail_id = #{dish.recipeDetailId} and dishes_id = #{dish.dishesId}
    </update>

    <update id="addCookRecipeDishesSupplyNum">
        update cook_recipe_dishes set
        sale_num = sale_num - #{quantity},
        remanent_num = remanent_num + #{quantity}
        where recipe_detail_id = #{dish.recipeDetailId} and dishes_id = #{dish.dishesId}
    </update>
</mapper>