<?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.CookDishesMapper">
    <resultMap type="com.bonus.canteen.core.cook.domain.CookDishes" id="CookDishesResult">
        <result property="dishesId"    column="dishes_id"    />
        <result property="mealType"    column="meal_type"    />
        <result property="customId"    column="custom_id"    />
        <result property="inventoryId"    column="inventory_id"    />
        <result property="dishesName"    column="dishes_name"    />
        <result property="aliasName"    column="alias_name"    />
        <result property="barCode"    column="bar_code"    />
        <result property="intro"    column="intro"    />
        <result property="cookId"    column="cook_id"    />
        <result property="typeId"    column="type_id"    />
        <result property="classifyId"    column="classify_id"    />
        <result property="effectId"    column="effect_id"    />
        <result property="styleId"    column="style_id"    />
        <result property="ifLocalFeature"    column="if_local_feature"    />
        <result property="dishesDepart"    column="dishes_depart"    />
        <result property="sequence"    column="sequence"    />
        <result property="pungencyDegree"    column="pungency_degree"    />
        <result property="salesMode"    column="sales_mode"    />
        <result property="sizeType"    column="size_type"    />
        <result property="sizeJson"    column="size_json"    />
        <result property="weight"    column="weight"    />
        <result property="largeWeight"    column="large_weight"    />
        <result property="littleWeight"    column="little_weight"    />
        <result property="weightDeviation"    column="weight_deviation"    />
        <result property="price"    column="price"    />
        <result property="unitPrice"    column="unit_price"    />
        <result property="largePrice"    column="large_price"    />
        <result property="littlePrice"    column="little_price"    />
        <result property="imageUrl"    column="image_url"    />
        <result property="particulars"    column="particulars"    />
        <result property="recommend"    column="recommend"    />
        <result property="indexRecommend"    column="index_recommend"    />
        <result property="likeSurvey"    column="like_survey"    />
        <result property="likeBatch"    column="like_batch"    />
        <result property="likeNum"    column="like_num"    />
        <result property="initialScore"    column="initial_score"    />
        <result property="calories"    column="calories"    />
        <result property="protein"    column="protein"    />
        <result property="fat"    column="fat"    />
        <result property="carbohydrate"    column="carbohydrate"    />
        <result property="dietaryFiber"    column="dietary_fiber"    />
        <result property="cholesterol"    column="cholesterol"    />
        <result property="calcium"    column="calcium"    />
        <result property="sodium"    column="sodium"    />
        <result property="purine"    column="purine"    />
        <result property="iron"    column="iron"    />
        <result property="iodine"    column="iodine"    />
        <result property="kalium"    column="kalium"    />
        <result property="vitaminA"    column="vitamin_a"    />
        <result property="vitaminC"    column="vitamin_c"    />
        <result property="vitaminE"    column="vitamin_e"    />
        <result property="magnesium"    column="magnesium"    />
        <result property="zinc"    column="zinc"    />
        <result property="glycemicIndex"    column="glycemic_index"    />
        <result property="sort"    column="sort"    />
        <result property="canteenId"    column="canteen_id"    />
        <result property="stallId"    column="shopstall_id"    />
        <result property="hideFlag"    column="hide_flag"    />
        <result property="convertFlag"    column="convert_flag"    />
        <result property="materialCost"    column="material_cost"    />
        <result property="grossProfit"    column="gross_profit"    />
        <result property="grossProfitRate"    column="gross_profit_rate"    />
        <result property="publicDishes"    column="public_dishes"    />
        <result property="areaId"    column="area_id"    />
        <result property="mealId"    column="meal_id"    />
        <result property="createBy"    column="create_by"    />
        <result property="createTime"    column="create_time"    />
        <result property="updateBy"    column="update_by"    />
        <result property="updateTime"    column="update_time"    />
        <result property="canteenName"    column="canteen_name"    />
        <result property="stallName"    column="stall_name"    />
        <result property="areaName"    column="area_name"    />
        <result property="dishesTypeName"    column="dishes_type_name"    />
        <result property="processingMethod" column="processing_method"/>
        <result property="processingTime" column="processing_time"/>
        <result property="edible" column="edible"/>
        <result property="water" column="water"/>
        <result property="ash" column="ash"/>
        <result property="retinol" column="retinol"/>
        <result property="thiamine" column="thiamine"/>
        <result property="riboflavin" column="riboflavin"/>
        <result property="niacin" column="niacin"/>
        <result property="vitaminD" column="vitamin_d"/>
        <result property="phosphorus" column="phosphorus"/>
        <result property="selenium" column="selenium"/>
        <result property="salesNum" column="sales_num"/>
        <result property="carotene" column="carotene"/>

    </resultMap>

    <resultMap id="appletDishesDetailVO" type="com.bonus.canteen.core.cook.vo.AppletDishesDetailVO">
        <result property="dishesId" column="dishes_id"/>
        <result property="mealType" column="meal_type"/>
        <result property="customId" column="custom_d"/>
        <result property="dishesName" column="dishes_name"/>
        <result property="aliasName" column="alias_name"/>
        <result property="imageUrl" column="image_url"/>
        <result property="intro" column="intro"/>
        <result property="particulars" column="particulars"/>
        <collection property="dishesDetailList" ofType="com.bonus.canteen.core.cook.vo.AppletDishesSizeV2VO">
            <result property="dishesId" column="dishes_id"/>
            <result property="dishesName" column="dishes_name"/>
            <result property="remanentNum" column="remanent_num"/>
            <result property="limitNum" column="limit_num"/>
            <result property="dishesPrice" column="dishes_price"/>
            <result property="prefPrice" column="pref_price"/>
            <result property="sizeType" column="size_type"/>
            <result property="sizeJson" column="size_json"/>
            <result property="calories" column="calories"/>
            <result property="protein" column="protein"/>
            <result property="fat" column="fat"/>
            <result property="carbohydrate" column="carbohydrate"/>
            <result property="dietaryFiber" column="dietary_fiber"/>
            <result property="cholesterol" column="cholesterol"/>
            <result property="calcium" column="calcium"/>
            <result property="sodium" column="sodium"/>
        </collection>
    </resultMap>

    <sql id="selectCookDishesVo">
        select dishes_id, meal_type, custom_id, inventory_id, dishes_name, alias_name, bar_code, intro, cook_id, type_id,cdt.dishes_type_name, classify_id,
               effect_id, style_id, if_local_feature, dishes_depart, sequence, pungency_degree, sales_mode, size_type, size_json,
               weight, large_weight, little_weight, weight_deviation, price, unit_price, large_price, little_price, image_url,
               particulars, recommend, index_recommend, like_survey, like_batch, like_num, initial_score, calories, protein, fat,
               carbohydrate, dietary_fiber, cholesterol, calcium, sodium, purine, iron, iodine, kalium, vitamin_a, vitamin_c,
               vitamin_e, magnesium, zinc, glycemic_index, sort, cd.canteen_id,bc.canteen_name,cd.shopstall_id,bs.stall_name, hide_flag, convert_flag, material_cost,
               gross_profit, gross_profit_rate, public_dishes, cd.area_id,ba.area_name, meal_id, cd.create_by, cd.create_time, cd.update_by, cd.update_time,
                cd.processing_method, cd.processing_time, cd.edible, cd.water, cd.ash, cd.retinol, cd.thiamine, cd.riboflavin, cd.niacin, vitamin_d,
                cd.phosphorus, cd.selenium,cd.carotene
        from cook_dishes cd
        left join basic_area ba on cd.area_id = ba.area_id
        left join basic_canteen bc on cd.canteen_id = bc.canteen_id
        left join basic_stall bs on cd.shopstall_id = bs.stall_id
        left join cook_dishes_type cdt on cd.type_id = cdt.dishes_type_id
    </sql>

    <select id="selectCookDishesList" parameterType="com.bonus.canteen.core.cook.domain.CookDishes" resultMap="CookDishesResult">
        <include refid="selectCookDishesVo"/>
        <where>
            and cd.del_flag = 0
            <if test="typeIds != null and typeIds.size() > 0">
                and cd.type_id in
                <foreach collection="typeIds" item="typeId" separator="," open="(" close=")">
                    #{typeId}
                </foreach>
            </if>
            <if test="dishesNames != null and dishesNames.size() > 0">
                and cd.dishes_name in
                <foreach item="dishesName" collection="dishesNames" open="(" separator="," close=")">
                    #{dishesName}
                </foreach>
            </if>
            <if test="mealType != null "> and cd.meal_type = #{mealType}</if>
            <if test="customId != null "> and cd.custom_id = #{customId}</if>
            <if test="inventoryId != null  and inventoryId != ''"> and cd.inventory_id = #{inventoryId}</if>
            <if test="dishesName != null  and dishesName != ''"> and cd.dishes_name like concat('%', #{dishesName}, '%')</if>
            <if test="aliasName != null  and aliasName != ''"> and cd.alias_name like concat('%', #{aliasName}, '%')</if>
            <if test="barCode != null  and barCode != ''"> and cd.bar_code = #{barCode}</if>
            <if test="intro != null  and intro != ''"> and cd.intro = #{intro}</if>
            <if test="cookId != null "> and cd.cook_id = #{cookId}</if>
            <if test="typeId != null "> and cd.type_id = #{typeId}</if>
            <if test="classifyId != null "> and cd.classify_id = #{classifyId}</if>
            <if test="effectId != null "> and cd.effect_id = #{effectId}</if>
            <if test="styleId != null "> and cd.style_id = #{styleId}</if>
            <if test="ifLocalFeature != null "> and cd.if_local_feature = #{ifLocalFeature}</if>
            <if test="dishesDepart != null "> and cd.dishes_depart = #{dishesDepart}</if>
            <if test="sequence != null "> and cd.sequence = #{sequence}</if>
            <if test="pungencyDegree != null "> and cd.pungency_degree = #{pungencyDegree}</if>
            <if test="salesMode != null "> and cd.sales_mode = #{salesMode}</if>
            <if test="sizeType != null "> and cd.size_type = #{sizeType}</if>
            <if test="sizeJson != null  and sizeJson != ''"> and cd.size_json = #{sizeJson}</if>
            <if test="weight != null "> and cd.weight = #{weight}</if>
            <if test="largeWeight != null "> and cd.large_weight = #{largeWeight}</if>
            <if test="littleWeight != null "> and cd.little_weight = #{littleWeight}</if>
            <if test="weightDeviation != null "> and cd.weight_deviation = #{weightDeviation}</if>
            <if test="price != null "> and cd.price = #{price}</if>
            <if test="unitPrice != null "> and cd.unit_price = #{unitPrice}</if>
            <if test="largePrice != null "> and cd.large_price = #{largePrice}</if>
            <if test="littlePrice != null "> and cd.little_price = #{littlePrice}</if>
            <if test="imageUrl != null  and imageUrl != ''"> and cd.image_url = #{imageUrl}</if>
            <if test="particulars != null  and particulars != ''"> and cd.particulars = #{particulars}</if>
            <if test="recommend != null  and recommend != ''"> and cd.recommend = #{recommend}</if>
            <if test="indexRecommend != null "> and cd.index_recommend = #{indexRecommend}</if>
            <if test="likeSurvey != null "> and cd.like_survey = #{likeSurvey}</if>
            <if test="likeBatch != null "> and cd.like_batch = #{likeBatch}</if>
            <if test="likeNum != null "> and cd.like_num = #{likeNum}</if>
            <if test="initialScore != null "> and cd.initial_score = #{initialScore}</if>
            <if test="calories != null "> and cd.calories = #{calories}</if>
            <if test="protein != null "> and cd.protein = #{protein}</if>
            <if test="fat != null "> and cd.fat = #{fat}</if>
            <if test="carbohydrate != null "> and cd.carbohydrate = #{carbohydrate}</if>
            <if test="dietaryFiber != null "> and cd.dietary_fiber = #{dietaryFiber}</if>
            <if test="cholesterol != null "> and cd.cholesterol = #{cholesterol}</if>
            <if test="calcium != null "> and cd.calcium = #{calcium}</if>
            <if test="sodium != null "> and cd.sodium = #{sodium}</if>
            <if test="purine != null "> and cd.purine = #{purine}</if>
            <if test="iron != null "> and cd.iron = #{iron}</if>
            <if test="iodine != null "> and cd.iodine = #{iodine}</if>
            <if test="kalium != null "> and cd.kalium = #{kalium}</if>
            <if test="vitaminA != null "> and cd.vitamin_a = #{vitaminA}</if>
            <if test="vitaminC != null "> and cd.vitamin_c = #{vitaminC}</if>
            <if test="vitaminE != null "> and cd.vitamin_e = #{vitaminE}</if>
            <if test="magnesium != null "> and cd.magnesium = #{magnesium}</if>
            <if test="zinc != null "> and cd.zinc = #{zinc}</if>
            <if test="glycemicIndex != null "> and cd.glycemic_index = #{glycemicIndex}</if>
            <if test="sort != null "> and cd.sort = #{sort}</if>
            <if test="canteenId != null "> and cd.canteen_id = #{canteenId}</if>
            <if test="stallId != null "> and (cd.shopstall_id = #{stallId} or cd.shopstall_id is null or cd.shopstall_id = -1)</if>
            <if test="hideFlag != null "> and cd.hide_flag = #{hideFlag}</if>
            <if test="convertFlag != null "> and cd.convert_flag = #{convertFlag}</if>
            <if test="materialCost != null "> and cd.material_cost = #{materialCost}</if>
            <if test="grossProfit != null "> and cd.gross_profit = #{grossProfit}</if>
            <if test="grossProfitRate != null "> and cd.gross_profit_rate = #{grossProfitRate}</if>
            <if test="publicDishes != null "> and cd.public_dishes = #{publicDishes}</if>
            <if test="areaId != null "> and cd.area_id = #{areaId}</if>
            <if test="mealId != null  and mealId != ''"> and cd.meal_id = #{mealId}</if>
        </where>
        order by cd.create_time desc
    </select>
    
    <select id="selectCookDishesByDishesId" parameterType="Long" resultMap="CookDishesResult">
        <include refid="selectCookDishesVo"/>
        where cd.dishes_id = #{dishesId}
    </select>
    <select id="checkDishIsExist" resultType="java.lang.Integer">
        select count(1) from cook_dishes where dishes_name = #{dishesName} and area_id = #{areaId}
        and canteen_id = #{canteenId} and shopstall_id = #{stallId}
        <if test="dishesId != null">
            and dishes_id != #{dishesId}
        </if>

    </select>
    <select id="selectDishPlanDetailIdByDishId" resultType="java.lang.Long">
        select recipe_detail_id from cook_recipe_detail where apply_date <![CDATA[ >= ]]> #{applyDate} and detail_type = '2'
    </select>
    <select id="selectCookDishesMaterialList"
            resultType="com.bonus.canteen.core.cook.domain.CookDishesMaterial">
        select
            cdm.id,
            cdm.dishes_id as dishesId,
            cdm.material_id as materialId,
            cm.material_name as materialName,
            cdm.weight,
            cdm.material_type as materialType
        from cook_dishes_material cdm
        left join cook_material cm on cdm.material_id = cm.material_id
        where cdm.dishes_id = #{dishesId}
    </select>
    <select id="selectDishesNameAndCanteenName" resultType="java.lang.String">
        SELECT
            concat_ws( '-', md.dishes_name, IFNULL(aa.area_name,'*'),IFNULL(ac.canteen_name,'*') , IFNULL( ass.stall_name,'*') )
        FROM
            cook_dishes md
                LEFT JOIN basic_area aa ON aa.area_id = md.area_id
                LEFT JOIN basic_canteen ac ON md.canteen_id = ac.canteen_id
                LEFT JOIN basic_stall ass ON ass.stall_id = md.shopstall_id
    </select>
    <select id="getMenuMaterialMap" resultType="com.bonus.canteen.core.cook.domain.MapBean">
        select material_name as `key`,material_id as `value` from cook_material
    </select>
    <select id="selectAreaList" resultType="com.bonus.canteen.core.cook.domain.AllocArea">
        select
            area_id as areaId,
            area_name as areaName
        from basic_area
        where del_flag = '0'
    </select>
    <select id="selectCanteenList" resultType="com.bonus.canteen.core.cook.domain.AllocCanteen">
        select
            canteen_id as canteenId,
            canteen_name as canteenName,
            area_id as areaId
        from basic_canteen
        where del_flag = '0'
    </select>
    <select id="selectStallList" resultType="com.bonus.canteen.core.cook.domain.AllocStall">
        select
            stall_id as stallId,
            stall_name as stallName,
            area_id as areaId,
            canteen_id as canteenId

        from basic_stall
        where del_flag = '0'
    </select>
    <select id="getCookMaterialTypeMap" resultType="com.bonus.canteen.core.cook.domain.MapBean">
        select dishes_type_name as `key` ,dishes_type_id as `value` from cook_dishes_type where parent_id != 0
    </select>

    <insert id="insertCookDishes" parameterType="com.bonus.canteen.core.cook.domain.CookDishes" useGeneratedKeys="true" keyProperty="dishesId">
        insert into cook_dishes
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="mealType != null">meal_type,</if>
            <if test="customId != null">custom_id,</if>
            <if test="inventoryId != null">inventory_id,</if>
            <if test="dishesName != null and dishesName != ''">dishes_name,</if>
            <if test="aliasName != null">alias_name,</if>
            <if test="barCode != null and barCode != ''">bar_code,</if>
            <if test="intro != null and intro != ''">intro,</if>
            <if test="cookId != null">cook_id,</if>
            <if test="typeId != null">type_id,</if>
            <if test="classifyId != null">classify_id,</if>
            <if test="effectId != null">effect_id,</if>
            <if test="styleId != null">style_id,</if>
            <if test="ifLocalFeature != null">if_local_feature,</if>
            <if test="dishesDepart != null">dishes_depart,</if>
            <if test="sequence != null">sequence,</if>
            <if test="pungencyDegree != null">pungency_degree,</if>
            <if test="salesMode != null">sales_mode,</if>
            <if test="sizeType != null">size_type,</if>
            <if test="sizeJson != null">size_json,</if>
            <if test="weight != null">weight,</if>
            <if test="largeWeight != null">large_weight,</if>
            <if test="littleWeight != null">little_weight,</if>
            <if test="weightDeviation != null">weight_deviation,</if>
            <if test="price != null">price,</if>
            <if test="unitPrice != null">unit_price,</if>
            <if test="largePrice != null">large_price,</if>
            <if test="littlePrice != null">little_price,</if>
            <if test="imageUrl != null and imageUrl != ''">image_url,</if>
            <if test="particulars != null">particulars,</if>
            <if test="recommend != null and recommend != ''">recommend,</if>
            <if test="indexRecommend != null">index_recommend,</if>
            <if test="likeSurvey != null">like_survey,</if>
            <if test="likeBatch != null">like_batch,</if>
            <if test="likeNum != null">like_num,</if>
            <if test="initialScore != null">initial_score,</if>
            <if test="calories != null">calories,</if>
            <if test="protein != null">protein,</if>
            <if test="fat != null">fat,</if>
            <if test="carbohydrate != null">carbohydrate,</if>
            <if test="dietaryFiber != null">dietary_fiber,</if>
            <if test="cholesterol != null">cholesterol,</if>
            <if test="calcium != null">calcium,</if>
            <if test="sodium != null">sodium,</if>
            <if test="purine != null">purine,</if>
            <if test="iron != null">iron,</if>
            <if test="iodine != null">iodine,</if>
            <if test="kalium != null">kalium,</if>
            <if test="vitaminA != null">vitamin_a,</if>
            <if test="vitaminC != null">vitamin_c,</if>
            <if test="vitaminE != null">vitamin_e,</if>
            <if test="magnesium != null">magnesium,</if>
            <if test="zinc != null">zinc,</if>
            <if test="glycemicIndex != null">glycemic_index,</if>
            <if test="sort != null">sort,</if>
            <if test="canteenId != null">canteen_id,</if>
            <if test="stallId != null">shopstall_id,</if>
            <if test="hideFlag != null">hide_flag,</if>
            <if test="convertFlag != null">convert_flag,</if>
            <if test="materialCost != null">material_cost,</if>
            <if test="grossProfit != null">gross_profit,</if>
            <if test="grossProfitRate != null">gross_profit_rate,</if>
            <if test="publicDishes != null">public_dishes,</if>
            <if test="areaId != null">area_id,</if>
            <if test="mealId != null">meal_id,</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>
            <if test="processingMethod != null">processing_method,</if>
            <if test="processingTime != null">processing_time,</if>
            <if test="edible != null">edible,</if>
            <if test="water != null">water,</if>
            <if test="ash != null">ash,</if>
            <if test="retinol != null">retinol,</if>
            <if test="thiamine != null">thiamine,</if>
            <if test="riboflavin != null">riboflavin,</if>
            <if test="niacin != null">niacin,</if>
            <if test="vitaminD != null">vitamin_d,</if>
            <if test="phosphorus != null">phosphorus,</if>
            <if test="selenium != null">selenium,</if>
            <if test="carotene != null">carotene,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="mealType != null">#{mealType},</if>
            <if test="customId != null">#{customId},</if>
            <if test="inventoryId != null">#{inventoryId},</if>
            <if test="dishesName != null and dishesName != ''">#{dishesName},</if>
            <if test="aliasName != null">#{aliasName},</if>
            <if test="barCode != null and barCode != ''">#{barCode},</if>
            <if test="intro != null and intro != ''">#{intro},</if>
            <if test="cookId != null">#{cookId},</if>
            <if test="typeId != null">#{typeId},</if>
            <if test="classifyId != null">#{classifyId},</if>
            <if test="effectId != null">#{effectId},</if>
            <if test="styleId != null">#{styleId},</if>
            <if test="ifLocalFeature != null">#{ifLocalFeature},</if>
            <if test="dishesDepart != null">#{dishesDepart},</if>
            <if test="sequence != null">#{sequence},</if>
            <if test="pungencyDegree != null">#{pungencyDegree},</if>
            <if test="salesMode != null">#{salesMode},</if>
            <if test="sizeType != null">#{sizeType},</if>
            <if test="sizeJson != null">#{sizeJson},</if>
            <if test="weight != null">#{weight},</if>
            <if test="largeWeight != null">#{largeWeight},</if>
            <if test="littleWeight != null">#{littleWeight},</if>
            <if test="weightDeviation != null">#{weightDeviation},</if>
            <if test="price != null">#{price},</if>
            <if test="unitPrice != null">#{unitPrice},</if>
            <if test="largePrice != null">#{largePrice},</if>
            <if test="littlePrice != null">#{littlePrice},</if>
            <if test="imageUrl != null and imageUrl != ''">#{imageUrl},</if>
            <if test="particulars != null">#{particulars},</if>
            <if test="recommend != null and recommend != ''">#{recommend},</if>
            <if test="indexRecommend != null">#{indexRecommend},</if>
            <if test="likeSurvey != null">#{likeSurvey},</if>
            <if test="likeBatch != null">#{likeBatch},</if>
            <if test="likeNum != null">#{likeNum},</if>
            <if test="initialScore != null">#{initialScore},</if>
            <if test="calories != null">#{calories},</if>
            <if test="protein != null">#{protein},</if>
            <if test="fat != null">#{fat},</if>
            <if test="carbohydrate != null">#{carbohydrate},</if>
            <if test="dietaryFiber != null">#{dietaryFiber},</if>
            <if test="cholesterol != null">#{cholesterol},</if>
            <if test="calcium != null">#{calcium},</if>
            <if test="sodium != null">#{sodium},</if>
            <if test="purine != null">#{purine},</if>
            <if test="iron != null">#{iron},</if>
            <if test="iodine != null">#{iodine},</if>
            <if test="kalium != null">#{kalium},</if>
            <if test="vitaminA != null">#{vitaminA},</if>
            <if test="vitaminC != null">#{vitaminC},</if>
            <if test="vitaminE != null">#{vitaminE},</if>
            <if test="magnesium != null">#{magnesium},</if>
            <if test="zinc != null">#{zinc},</if>
            <if test="glycemicIndex != null">#{glycemicIndex},</if>
            <if test="sort != null">#{sort},</if>
            <if test="canteenId != null">#{canteenId},</if>
            <if test="stallId != null">#{stallId},</if>
            <if test="hideFlag != null">#{hideFlag},</if>
            <if test="convertFlag != null">#{convertFlag},</if>
            <if test="materialCost != null">#{materialCost},</if>
            <if test="grossProfit != null">#{grossProfit},</if>
            <if test="grossProfitRate != null">#{grossProfitRate},</if>
            <if test="publicDishes != null">#{publicDishes},</if>
            <if test="areaId != null">#{areaId},</if>
            <if test="mealId != null">#{mealId},</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>
            <if test="processingMethod != null">#{processingMethod},</if>
            <if test="processingTime != null">#{processingTime},</if>
            <if test="edible != null">#{edible},</if>
            <if test="water != null">#{water},</if>
            <if test="ash != null">#{ash},</if>
            <if test="retinol != null">#{retinol},</if>
            <if test="thiamine != null">#{thiamine},</if>
            <if test="riboflavin != null">#{riboflavin},</if>
            <if test="niacin != null">#{niacin},</if>
            <if test="vitaminD != null">#{vitaminD},</if>
            <if test="phosphorus != null">#{phosphorus},</if>
            <if test="selenium != null">#{selenium},</if>
            <if test="carotene != null">#{carotene},</if>
         </trim>
    </insert>

    <insert id="insertDishesMaterial">
        insert into cook_dishes_material (dishes_id, material_id, weight,material_type, create_by, create_time, update_by, update_time)
        values
        <foreach collection="list" item="item" separator=",">
            (#{dishesId}, #{item.materialId}, #{item.weight},#{item.materialType}, #{item.createBy}, #{item.createTime}, #{item.updateBy}, #{item.updateTime})
        </foreach>
    </insert>

    <update id="updateCookDishes" parameterType="com.bonus.canteen.core.cook.domain.CookDishes">
        update cook_dishes
        <trim prefix="SET" suffixOverrides=",">
            <if test="mealType != null">meal_type = #{mealType},</if>
            <if test="customId != null">custom_id = #{customId},</if>
            <if test="inventoryId != null">inventory_id = #{inventoryId},</if>
            <if test="dishesName != null and dishesName != ''">dishes_name = #{dishesName},</if>
            <if test="aliasName != null">alias_name = #{aliasName},</if>
            <if test="barCode != null and barCode != ''">bar_code = #{barCode},</if>
            <if test="intro != null and intro != ''">intro = #{intro},</if>
            <if test="cookId != null">cook_id = #{cookId},</if>
            <if test="typeId != null">type_id = #{typeId},</if>
            <if test="classifyId != null">classify_id = #{classifyId},</if>
            <if test="effectId != null">effect_id = #{effectId},</if>
            <if test="styleId != null">style_id = #{styleId},</if>
            <if test="ifLocalFeature != null">if_local_feature = #{ifLocalFeature},</if>
            <if test="dishesDepart != null">dishes_depart = #{dishesDepart},</if>
            <if test="sequence != null">sequence = #{sequence},</if>
            <if test="pungencyDegree != null">pungency_degree = #{pungencyDegree},</if>
            <if test="salesMode != null">sales_mode = #{salesMode},</if>
            <if test="sizeType != null">size_type = #{sizeType},</if>
            <if test="sizeJson != null">size_json = #{sizeJson},</if>
            <if test="weight != null">weight = #{weight},</if>
            <if test="largeWeight != null">large_weight = #{largeWeight},</if>
            <if test="littleWeight != null">little_weight = #{littleWeight},</if>
            <if test="weightDeviation != null">weight_deviation = #{weightDeviation},</if>
            <if test="price != null">price = #{price},</if>
            <if test="unitPrice != null">unit_price = #{unitPrice},</if>
            <if test="largePrice != null">large_price = #{largePrice},</if>
            <if test="littlePrice != null">little_price = #{littlePrice},</if>
            <if test="imageUrl != null and imageUrl != ''">image_url = #{imageUrl},</if>
            <if test="particulars != null">particulars = #{particulars},</if>
            <if test="recommend != null and recommend != ''">recommend = #{recommend},</if>
            <if test="indexRecommend != null">index_recommend = #{indexRecommend},</if>
            <if test="likeSurvey != null">like_survey = #{likeSurvey},</if>
            <if test="likeBatch != null">like_batch = #{likeBatch},</if>
            <if test="likeNum != null">like_num = #{likeNum},</if>
            <if test="initialScore != null">initial_score = #{initialScore},</if>
            <if test="calories != null">calories = #{calories},</if>
            <if test="protein != null">protein = #{protein},</if>
            <if test="fat != null">fat = #{fat},</if>
            <if test="carbohydrate != null">carbohydrate = #{carbohydrate},</if>
            <if test="dietaryFiber != null">dietary_fiber = #{dietaryFiber},</if>
            <if test="cholesterol != null">cholesterol = #{cholesterol},</if>
            <if test="calcium != null">calcium = #{calcium},</if>
            <if test="sodium != null">sodium = #{sodium},</if>
            <if test="purine != null">purine = #{purine},</if>
            <if test="iron != null">iron = #{iron},</if>
            <if test="iodine != null">iodine = #{iodine},</if>
            <if test="kalium != null">kalium = #{kalium},</if>
            <if test="vitaminA != null">vitamin_a = #{vitaminA},</if>
            <if test="vitaminC != null">vitamin_c = #{vitaminC},</if>
            <if test="vitaminE != null">vitamin_e = #{vitaminE},</if>
            <if test="magnesium != null">magnesium = #{magnesium},</if>
            <if test="zinc != null">zinc = #{zinc},</if>
            <if test="glycemicIndex != null">glycemic_index = #{glycemicIndex},</if>
            <if test="sort != null">sort = #{sort},</if>
            <if test="canteenId != null">canteen_id = #{canteenId},</if>
            <if test="stallId != null">shopstall_id = #{stallId},</if>
            <if test="hideFlag != null">hide_flag = #{hideFlag},</if>
            <if test="convertFlag != null">convert_flag = #{convertFlag},</if>
            <if test="materialCost != null">material_cost = #{materialCost},</if>
            <if test="grossProfit != null">gross_profit = #{grossProfit},</if>
            <if test="grossProfitRate != null">gross_profit_rate = #{grossProfitRate},</if>
            <if test="publicDishes != null">public_dishes = #{publicDishes},</if>
            <if test="areaId != null">area_id = #{areaId},</if>
            <if test="mealId != null">meal_id = #{mealId},</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>
            <if test="processingMethod != null">processing_method = #{processingMethod},</if>
            <if test="processingTime != null">processing_time = #{processingTime},</if>
            <if test="edible != null">edible = #{edible},</if>
            <if test="water != null">water = #{water},</if>
            <if test="ash != null">ash = #{ash},</if>
            <if test="retinol != null">retinol = #{retinol},</if>
            <if test="thiamine != null">thiamine = #{thiamine},</if>
            <if test="riboflavin != null">riboflavin = #{riboflavin},</if>
            <if test="niacin != null">niacin = #{niacin},</if>
            <if test="vitaminD != null">vitamin_d = #{vitaminD},</if>
            <if test="phosphorus != null">phosphorus = #{phosphorus},</if>
            <if test="selenium != null">selenium = #{selenium},</if>
            <if test="carotene != null">carotene = #{carotene},</if>
        </trim>
        where dishes_id = #{dishesId}
    </update>

    <update id="updateFinalNutrition">
        update cook_dishes
        <trim prefix="SET" suffixOverrides=",">
            <if test="bean.calories != null">calories = #{bean.calories},</if>
            <if test="bean.protein != null">protein = #{bean.protein},</if>
            <if test="bean.fat != null">fat = #{bean.fat},</if>
            <if test="bean.carbohydrate != null">carbohydrate = #{bean.carbohydrate},</if>
            <if test="bean.dietaryFiber != null">dietary_fiber = #{bean.dietaryFiber},</if>
            <if test="bean.cholesterol != null">cholesterol = #{bean.cholesterol},</if>
            <if test="bean.calcium != null">calcium = #{bean.calcium},</if>
            <if test="bean.sodium != null">sodium = #{bean.sodium},</if>
            <if test="bean.purine != null">purine = #{bean.purine},</if>
            <if test="bean.iron != null">iron = #{bean.iron},</if>
            <if test="bean.iodine != null">iodine = #{bean.iodine},</if>
            <if test="bean.kalium != null">kalium = #{bean.kalium},</if>
            <if test="bean.vitaminA != null">vitamin_a = #{bean.vitaminA},</if>
            <if test="bean.vitaminC != null">vitamin_c = #{bean.vitaminC},</if>
            <if test="bean.vitaminE != null">vitamin_e = #{bean.vitaminE},</if>
            <if test="bean.magnesium != null">magnesium = #{bean.magnesium},</if>
            <if test="bean.glycemicIndex != null">glycemic_index = #{bean.glycemicIndex},</if>
            <if test="bean.zinc != null">zinc = #{bean.zinc},</if>
            <if test="bean.phosphorus != null">phosphorus = #{bean.phosphorus},</if>
            <if test="bean.selenium != null">selenium = #{bean.selenium},</if>
            <if test="bean.water != null">water = #{bean.water},</if>
            <if test="bean.ash != null">ash = #{bean.ash},</if>
            <if test="bean.retinol != null">retinol = #{bean.retinol},</if>
            <if test="bean.thiamine != null">thiamine = #{bean.thiamine},</if>
            <if test="bean.riboflavin != null">riboflavin = #{bean.riboflavin},</if>
            <if test="bean.niacin != null">niacin = #{bean.niacin},</if>
            <if test="bean.edible != null">edible = #{bean.edible},</if>
            <if test="bean.vitaminD != null">vitamin_d = #{bean.vitaminD},</if>
            <if test="bean.processingTime != null">processing_time = #{bean.processingTime},</if>
            <if test="bean.carotene != null">carotene = #{bean.carotene},</if>
        </trim>
        where dishes_id = #{dishesId}
    </update>

    <delete id="deleteCookDishesByDishesId" parameterType="Long">
        delete from cook_dishes where dishes_id = #{dishesId}
    </delete>

    <delete id="deleteCookDishesByDishesIds" parameterType="String">
        update cook_dishes set del_flag = '2' where dishes_id in
        <foreach item="dishesId" collection="array" open="(" separator="," close=")">
            #{dishesId}
        </foreach>
    </delete>
    <delete id="deleteDishesMaterialByDishesId">
        delete from cook_dishes_material where dishes_id = #{dishesId}
    </delete>
    <delete id="deleteCookDishesByDishPlanDetailIds">
        delete from cook_recipe_dishes where dishes_id in
        <foreach item="detailId" collection="dishesIds" open="(" separator="," close=")">
            #{detailId}
        </foreach>
        and recipe_detail_id in
        <foreach item="detailId" collection="detailIdList" open="(" separator="," close=")">
            #{detailId}
        </foreach>
    </delete>

    <!-- 根据菜品id获取菜品详情 -->
    <select id="selectDishesDetailByDishesId" resultMap="appletDishesDetailVO">
        select
            md.*,
            mrd.size_type,
            mrd.price as dishes_price,
            mrd.sale_price as pref_price,
            mrd.supply_num - mrd.remanent_num as sales_num,
            mrd.remanent_num,
            mrd.limit_num
        from
            cook_dishes md
            left join cook_recipe_dishes mrd on md.dishes_id = mrd.dishes_id
        where
            md.dishes_id = #{dishesId} and mrd.recipe_detail_id = #{detailId} and md.del_flag = #{delFlag}
    </select>

    <select id="getCookMaterialById" resultType="com.bonus.canteen.core.cook.vo.CookMaterialBasVO">
        SELECT cm.material_id,
               cm.material_name,
               cm.material_code,
               cm.img_url,
               cdm.weight,
               cdm.material_type,
               cn.nutrition_type_id as little_type
        FROM cook_material cm
                 LEFT JOIN cook_dishes_material cdm ON cm.material_id = cdm.material_id
                 LEFT JOIN cook_nutrition cn ON cn.nutrition_id = cm.nutrition_id
        WHERE cdm.dishes_id = #{dishesId}
    </select>
</mapper>