<?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.menu.mapper.MenuDishesMapper">
    <resultMap type="com.bonus.canteen.core.menu.domain.MenuDishes" id="MenuDishesResult">
        <result property="id"    column="id"    />
        <result property="dishesId"    column="dishes_id"    />
        <result property="baseDishesId"    column="base_dishes_id"    />
        <result property="mealType"    column="meal_type"    />
        <result property="dishesNum"    column="dishes_num"    />
        <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="pinyinInitials"    column="pinyin_initials"    />
        <result property="pinyinFull"    column="pinyin_full"    />
        <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="shopstallId"    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="revision"    column="revision"    />
        <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="remark"    column="remark"    />
        <result property="typeName"    column="type_name"    />

        <result property="areaName"    column="area_name"    />
        <result property="canteenName"    column="canteen_name"    />
        <result property="stallName"    column="stall_name"    />

    </resultMap>

    <sql id="selectMenuDishesVo">
        select 	b.*,a.type_name,aa.area_name,ac.canteen_name,als.stall_name
        from menu_dishes b
        LEFT JOIN menu_dishes_type a on a.type_id = b.type_id
        LEFT JOIN alloc_area aa on aa.area_id = b.area_id
        LEFT JOIN alloc_canteen ac on ac.canteen_id = b.canteen_id
        LEFT JOIN alloc_stall als on als.stall_id = b.shopstall_id
    </sql>

    <select id="selectMenuDishesList" parameterType="com.bonus.canteen.core.menu.domain.MenuDishes" resultMap="MenuDishesResult">
        <include refid="selectMenuDishesVo"/>
        <where>
            b.del_flag = 0
            <if test="dishesId != null "> and b.dishes_id = #{dishesId}</if>
            <if test="baseDishesId != null "> and b.base_dishes_id = #{baseDishesId}</if>
            <if test="canteenId != null "> and b.canteen_id = #{canteenId}</if>
            <if test="shopstallId != null "> and b.shopstall_id = #{shopstallId}</if>
            <if test="areaId != null "> and b.area_id = #{areaId}</if>
            <if test="typeId != null "> and b.type_id = #{typeId}</if>

            <if test="dishesName !=null and dishesName !=''"> and b.dishes_name like concat('%',#{dishesName},'%')</if>
            <if test="typeIdList != null and typeIdList.size() > 0">
                and b.type_id in
                <foreach item="id" collection="typeIdList" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
        </where>
    </select>

    <select id="selectMenuDishesById" parameterType="Long" resultMap="MenuDishesResult">
        <include refid="selectMenuDishesVo"/>
        where a.id = #{id}
    </select>

    <insert id="insertMenuDishes" parameterType="com.bonus.canteen.core.menu.domain.MenuDishes" useGeneratedKeys="true" keyProperty="id">
        insert into menu_dishes
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="dishesId != null">dishes_id,</if>
            <if test="baseDishesId != null">base_dishes_id,</if>
            <if test="mealType != null">meal_type,</if>
            <if test="dishesNum != null and dishesNum != ''">dishes_num,</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="pinyinInitials != null and pinyinInitials != ''">pinyin_initials,</if>
            <if test="pinyinFull != null and pinyinFull != ''">pinyin_full,</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="shopstallId != 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="revision != null">revision,</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="remark != null">remark,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="dishesId != null">#{dishesId},</if>
            <if test="baseDishesId != null">#{baseDishesId},</if>
            <if test="mealType != null">#{mealType},</if>
            <if test="dishesNum != null and dishesNum != ''">#{dishesNum},</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="pinyinInitials != null and pinyinInitials != ''">#{pinyinInitials},</if>
            <if test="pinyinFull != null and pinyinFull != ''">#{pinyinFull},</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="shopstallId != null">#{shopstallId},</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="revision != null">#{revision},</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="remark != null">#{remark},</if>
         </trim>
    </insert>

    <update id="updateMenuDishes" parameterType="com.bonus.canteen.core.menu.domain.MenuDishes">
        update menu_dishes
        <trim prefix="SET" suffixOverrides=",">
            <if test="dishesId != null">dishes_id = #{dishesId},</if>
            <if test="baseDishesId != null">base_dishes_id = #{baseDishesId},</if>
            <if test="mealType != null">meal_type = #{mealType},</if>
            <if test="dishesNum != null and dishesNum != ''">dishes_num = #{dishesNum},</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="pinyinInitials != null and pinyinInitials != ''">pinyin_initials = #{pinyinInitials},</if>
            <if test="pinyinFull != null and pinyinFull != ''">pinyin_full = #{pinyinFull},</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="shopstallId != null">shopstall_id = #{shopstallId},</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="revision != null">revision = #{revision},</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="remark != null">remark = #{remark},</if>
        </trim>
        where id = #{id}
    </update>

    <delete id="deleteMenuDishesById" parameterType="Long">
        delete from menu_dishes where dishes_id = #{id}
    </delete>

    <delete id="deleteMenuDishesByIds" parameterType="long">
        delete from menu_dishes where dishes_id in
        <foreach collection="dishesIds" separator="," open="(" close=")" item="item">
            #{item}
        </foreach>
    </delete>

    <select id="getBassIdByDishesId"  resultType="long">
        select base_dishes_id from menu_dishes where dishes_id in
        <foreach collection="dishesIds" separator="," open="(" close=")" item="item">
            #{item}
        </foreach>
    </select>

    <delete id="deleteBaseDishesByIds">
        update menu_dishes_base set del_flag = '2'where base_dishes_id in
        <foreach collection="baseDishesIds" separator="," open="(" close=")" item="item">
            #{item}
        </foreach>
    </delete>

    <select id="getDishesType" resultType="com.bonus.canteen.core.menu.domain.MapBean" parameterType="com.bonus.canteen.core.menu.domain.MapBean">
        select dict_label as `key`,dict_value as `value` from sys_dict_data where is_default ='Y' and dict_type = #{key} order by  dict_sort
    </select>

    <select id="getDishesTypeByList" resultType="com.bonus.canteen.core.menu.domain.MapBean" >
        select dict_label as `key`,dict_value as `value` from sys_dict_data where is_default ='Y'
        and dict_type = #{key}
        and dict_value in
        <foreach collection="keyList" separator="," open="(" close=")" item="item">
            #{item}
        </foreach>
       order by  dict_sort
    </select>



    <select id="getOneByName" parameterType="com.bonus.canteen.core.menu.dto.MenuDishesDTO" resultMap="MenuDishesResult">
        select * from menu_dishes
        where 1 = 1
        <if test="areaId != null and areaId != ''">
           and area_id = #{areaId}
        </if>
        and (dishes_name = #{dishesName})
        <if test="dishesId != null and dishesId != ''">
            and dishes_id != #{dishesId}
        </if>
        limit 1
    </select>

    <select id="getOneByNum" parameterType="com.bonus.canteen.core.menu.dto.MenuDishesDTO" resultMap="MenuDishesResult">
        select * from menu_dishes where 1 = 1
        <if test="areaId != null and areaId != ''">
            and area_id = #{areaId}
        </if>
        <if test="dishesNum !=null and dishesNum !=''">
            and dishes_num =#{dishesNum}
        </if>
        <if test="dishesId != null and dishesId != ''">
            and dishes_id != #{dishesId}
        </if>
          limit 1
    </select>

    <insert id="addMenuDishesBase"  parameterType="com.bonus.canteen.core.menu.dto.MenuDishesDTO">
        insert into menu_dishes_base
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="baseDishesId != null">base_dishes_id,</if>
            <if test="dishesName != null">dishes_name,</if>
            <if test="createBy != null">create_by,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test=" baseDishesId!= null">#{baseDishesId},</if>
            <if test=" dishesName!= null">#{dishesName},</if>
            <if test="createBy != null">#{createBy},</if>
        </trim>
    </insert>

    <insert id="addMenuDishes"  parameterType="com.bonus.canteen.core.menu.dto.MenuDishesDTO">
        insert into menu_dishes
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="dishesId != null">dishes_id,</if>
            <if test="baseDishesId != null">base_dishes_id,</if>
            <if test="sizeType != null">size_type,</if>
            <if test="materialCost != null">material_cost,</if>
            <if test="publicDishes != null">public_dishes,</if>
            <if test="areaId != null">area_id,</if>
            <if test="canteenId != null">canteen_id,</if>
            <if test="shopstallId != null">shopstall_id,</if>
            <if test="dishesNum != null">dishes_num,</if>
            <if test="dishesName != null">dishes_name,</if>
            <if test="price != null">price,</if>
            <if test="weight != null">weight,</if>
            <if test="weightDeviation != null">weight_deviation,</if>
            <if test="imageUrl != null">image_url,</if>
            <if test="effectId != null">effect_id,</if>
            <if test="styleId != null">style_id,</if>
            <if test="cookId != null">cook_id,</if>
            <if test="classifyId != null">classify_id,</if>
            <if test="createBy != null">create_by,</if>
            <if test="customId != null">custom_id,</if>
            <if test="typeId != null">type_id,</if>

            <if test="labelId != null">label_id,</if>
            <if test="tasteId != null">taste_id,</if>
            <if test="seasonId != null">season_id,</if>
            <if test="suitId != null">suit_id,</if>
            <if test="mealId != null">meal_id,</if>
            <if test="remark != null">remark,</if>


        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="dishesId != null">#{dishesId},</if>
            <if test="baseDishesId != null">#{baseDishesId},</if>
            <if test="sizeType != null">#{sizeType},</if>
            <if test="materialCost != null">#{materialCost},</if>
            <if test="publicDishes != null">#{publicDishes},</if>
            <if test="areaId != null">#{areaId},</if>
            <if test="canteenId != null">#{canteenId},</if>
            <if test="shopstallId != null">#{shopstallId},</if>
            <if test="dishesNum != null">#{dishesNum},</if>
            <if test="dishesName != null">#{dishesName},</if>
            <if test="price != null">#{price},</if>
            <if test="weight != null">#{weight},</if>
            <if test="weightDeviation != null">#{weightDeviation},</if>
            <if test="imageUrl != null">#{imageUrl},</if>
            <if test="effectId != null">#{effectId},</if>
            <if test="styleId != null">#{styleId},</if>
            <if test="cookId != null">#{cookId},</if>
            <if test="classifyId != null">#{classifyId},</if>
            <if test="createBy != null">#{createBy},</if>
            <if test="customId != null">#{customId},</if>
            <if test="typeId != null">#{typeId},</if>

            <if test="labelId != null">#{labelId},</if>
            <if test="tasteId != null">#{tasteId},</if>
            <if test="seasonId != null">#{seasonId},</if>
            <if test="suitId != null">#{suitId},</if>
            <if test="mealId != null">#{mealId},</if>
            <if test="remark != null">#{remark}</if>
        </trim>
    </insert>

    <select id="getNutritionEntity" resultType="com.bonus.canteen.core.utils.NutritionEntity" parameterType="com.bonus.canteen.core.menu.dto.MenuDishesAddMaterialDTO">
        select calories,fat,protein,carbohydrate,dietary_fiber as dietaryFiber,
        cholesterol,calcium,sodium,purine,iron,
        iodine,kalium,vitamin_a as vitaminA,vitamin_c as vitaminC,vitamin_e as vitaminE,
        magnesium,zinc,glycemic_index as glycemicIndex
        from menu_material_nutrition where material_id = #{materialId}
    </select>

    <update id="addFinalNutrition" >

        update menu_dishes set
        calories =#{nutritionEntity.calories}, protein =#{nutritionEntity.protein}, fat =#{nutritionEntity.fat},
        calcium =#{nutritionEntity.calcium},sodium=#{nutritionEntity.sodium},purine=#{nutritionEntity.purine},iron=#{nutritionEntity.iron},
        iodine=#{nutritionEntity.iodine},kalium=#{nutritionEntity.kalium},zinc=#{nutritionEntity.zinc},
        carbohydrate =#{nutritionEntity.carbohydrate}, dietary_fiber =#{nutritionEntity.dietaryFiber}, cholesterol =#{nutritionEntity.cholesterol}, vitamin_a =#{nutritionEntity.vitaminA},
        vitamin_c =#{nutritionEntity.vitaminC},magnesium = #{nutritionEntity.magnesium},
         vitamin_e  =#{nutritionEntity.vitaminE}, purine =#{nutritionEntity.purine}, glycemic_index =#{nutritionEntity.glycemicIndex}
        where dishes_id = #{dishesId}

    </update>

    <update id="updateMenuDishesBase" parameterType="com.bonus.canteen.core.menu.dto.MenuDishesDTO">
        update  menu_dishes_base set dishes_name = #{dishesName},update_by = #{updateBy}
        where base_dishes_id = #{baseDishesId}
    </update>

    <update id="editMenuDishes" parameterType="com.bonus.canteen.core.menu.dto.MenuDishesDTO">
        update menu_dishes
        <trim prefix="SET" suffixOverrides=",">
            <if test="sizeType != null">size_type = #{sizeType},</if>
            <if test="materialCost != null">material_cost = #{materialCost},</if>
            <if test="publicDishes != null">public_dishes = #{publicDishes},</if>
            <if test="areaId != null">area_id = #{areaId},</if>
            <if test="canteenId != null">canteen_id =#{canteenId},</if>
            <if test="shopstallId != null">shopstall_id =#{shopstallId},</if>
            <if test="dishesNum != null">dishes_num = #{dishesNum},</if>
            <if test="dishesName != null">dishes_name = #{dishesName},</if>
            <if test="price != null">price = #{price},</if>
            <if test="weight != null">weight = #{weight},</if>
            <if test="weightDeviation != null">weight_deviation = #{weightDeviation},</if>
            <if test="imageUrl != null">image_url = #{imageUrl},</if>
            <if test="effectId != null">effect_id = #{effectId},</if>
            <if test="styleId != null">style_id = #{styleId},</if>
            <if test="cookId != null">cook_id = #{cookId},</if>
            <if test="classifyId != null">classify_id = #{classifyId},</if>
            <if test="customId != null">custom_id = #{customId},</if>
            <if test="typeId != null">type_id = #{typeId},</if>
            <if test="remark != null">remark = #{remark},</if>

            <if test="labelId != null">label_id = #{labelId},</if>
            <if test="tasteId != null">taste_id = #{tasteId},</if>
            <if test="seasonId != null">season_id = #{seasonId},</if>
            <if test="suitId != null">suit_id = #{suitId},</if>
            <if test="mealId != null">meal_id = #{mealId},</if>
        </trim>

        where dishes_id = #{dishesId}
    </update>

    <insert id="addMenuMaterialDishes"  parameterType="java.util.List">
        insert into menu_material_dishes(dishes_id,material_id,weight,material_type)
        values
        <foreach collection="materialList" item="item" separator=",">
            (#{dishesId}, #{item.materialId}, #{item.weight}, #{item.materialType})
        </foreach>
    </insert>

    <select id="getMenuMaterialDishes" resultType="com.bonus.canteen.core.menu.dto.MenuDishesAddMaterialDTO">
        select mmd.material_id as materialId,mmd.weight,mmd.material_type as materialType,mm.material_name as materialName
        from menu_material_dishes mmd
        LEFT JOIN menu_material mm on mm.material_id = mmd.material_id
        where mmd.dishes_id = #{dishesId}
    </select>

    <select id="getDishMessageByDishesId" resultType="com.bonus.canteen.core.menu.domain.MenuDishes">
        <include refid="selectMenuDishesVo"/>
        where b.dishes_id = #{dishesId}
    </select>

    <delete id="updateMenuMaterialDishes">
        delete from menu_material_dishes where dishes_id = #{dishesId};
    </delete>

    <select id="getBassIdByIds" resultType="java.lang.Long">
        select base_dishes_id from menu_dishes where id in
        <foreach collection="ids" separator="," open="(" close=")" item="item">
            #{item}
        </foreach>
    </select>
    <select id="getDishesIds" resultType="java.lang.Long">
        select dishes_id  from menu_dishes where id in
        <foreach collection="ids" separator="," open="(" close=")" item="item">
            #{item}
        </foreach>
    </select>
    <select id="getDetailsIds" resultType="java.lang.Long">
        select detail_id  from menu_recipe_dishes where dishes_id in
        <foreach collection="dishesIds" separator="," open="(" close=")" item="item">
            #{item}
        </foreach>
    </select>
    <select id="checkIsExistRelation" resultType="java.lang.Integer">
        select count(1) from menu_recipe_detail where detail_type = #{type} and detail_id in
        <foreach collection="ids" separator="," open="(" close=")" item="item">
            #{item}
        </foreach>
        <if test="nowDate != null">
            and apply_date &gt;= #{nowDate}
        </if>
    </select>

    <delete id="deleteMenuByIds" parameterType="long">
        update menu_dishes set del_flag = '2' where id in
        <foreach collection="ids" separator="," open="(" close=")" item="item">
            #{item}
        </foreach>
    </delete>


    <select id="listDishesSizeModel" resultType="com.bonus.canteen.core.supply.purchase.domain.bo.MenuDishesSizeModel">
        SELECT
            dishes_id,
            base_dishes_id,
            CASE
                WHEN size_json = '{"sizeType":"大份"}' THEN
                    2
                WHEN size_json = '{"sizeType":"小份"}' THEN
                    3 ELSE 1
                END sizeType
        FROM
            menu_dishes
        WHERE
            sales_mode = #{salesMode}
<!--          AND del_flag = #{delFlag}-->
    </select>

    <select id="listDishesMaterial" resultType="com.bonus.canteen.core.supply.purchase.domain.bo.MenuDishesMaterialModel">
        SELECT
        a.dishes_id,
        a.dishes_name,
        b.material_id,
        c.material_name,
        b.weight,
        d.edible,
        c.reserve_rate,
        a.base_dishes_id
        FROM
        menu_dishes a
        LEFT JOIN menu_material_dishes b ON b.dishes_id = a.dishes_id
        LEFT JOIN menu_material c ON c.material_id = b.material_id
        LEFT JOIN menu_nutrition d ON d.nutrition_id = c.nutrition_id
        WHERE
        a.sales_mode = #{salesMode}
        AND b.material_id IS NOT NULL
        AND a.dishes_id IN
        <foreach collection="dishesIdList" item="dishesId" separator="," open="(" close=")">
            #{dishesId}
        </foreach>
    </select>

</mapper>
