<?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.MenuNutritionTypeMapper">
    <resultMap type="com.bonus.canteen.core.menu.domain.MenuNutritionType" id="MenuNutritionTypeResult">
        <result property="id"    column="id"    />
        <result property="name"    column="name"    />
        <result property="parentId"    column="parent_id"    />
        <result property="delFlag"    column="del_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="selectMenuNutritionTypeVo">
        select id, name, parent_id, del_flag, create_by, create_time, update_by, update_time from menu_nutrition_type
    </sql>

    <select id="selectMenuNutritionTypeList" parameterType="com.bonus.canteen.core.menu.domain.MenuNutritionType" resultMap="MenuNutritionTypeResult">
        <include refid="selectMenuNutritionTypeVo"/>
        <where>
            <if test="name != null  and name != ''"> and name like concat('%', #{name}, '%')</if>
            <if test="parentId != null "> and parent_id = #{parentId}</if>
        </where>
    </select>

    <select id="selectMenuNutritionTypeById" parameterType="Long" resultMap="MenuNutritionTypeResult">
        <include refid="selectMenuNutritionTypeVo"/>
        where id = #{id}
    </select>

    <insert id="insertMenuNutritionType" parameterType="com.bonus.canteen.core.menu.domain.MenuNutritionType" useGeneratedKeys="true" keyProperty="id">
        insert into menu_nutrition_type
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="name != null">name,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="delFlag != null">del_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>
            <if test="level != null">level,</if>

         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="name != null">#{name},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="delFlag != null">#{delFlag},</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="level != null">#{level},</if>
         </trim>
    </insert>

    <update id="updateMenuNutritionType" parameterType="com.bonus.canteen.core.menu.domain.MenuNutritionType">
        update menu_nutrition_type
        <trim prefix="SET" suffixOverrides=",">
            <if test="name != null">name = #{name},</if>
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="delFlag != null">del_flag = #{delFlag},</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="level != null">level = #{level},</if>

        </trim>
        where id = #{id}
    </update>

    <delete id="deleteMenuNutritionTypeById" parameterType="Long">
        delete from menu_nutrition_type where id = #{id}
    </delete>

    <update id="deleteMenuNutritionTypeByIds" parameterType="String">
        update menu_nutrition_type set del_flag = 0  where id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <select id="selectTypeList" parameterType="String" resultType="String">
        select distinct big_type from menu_nutrition
    </select>

    <select id="selectTypeListDetails" parameterType="String"  resultType="String">
        select distinct little_type from menu_nutrition where big_type = #{bigType}
    </select>

    <select id="selectBigTypeList" resultMap="MenuNutritionTypeResult">
        select *  from menu_nutrition_type where level = 1 and del_flag = 2
    </select>

    <select id="selectLittleTypeList" resultType="com.bonus.canteen.core.menu.vo.NutritionTypeV2VO">
        select id,name as bigType  from menu_nutrition_type where level = 2 and del_flag = 2 and parent_id = #{parentId}
    </select>

    <select id="getOne" resultMap="MenuNutritionTypeResult" parameterType="com.bonus.canteen.core.menu.domain.MenuNutritionType">
        select * from menu_nutrition_type where name = #{name} and parent_id = #{parentId}  and del_flag = 2 limit 1
    </select>

    <select id="getParentOne" resultMap="MenuNutritionTypeResult" parameterType="com.bonus.canteen.core.menu.domain.MenuNutritionType">
        select * from menu_nutrition_type where id = #{parentId}  and del_flag = 2 limit 1
    </select>
    <select id="checkIsExistRelation" resultType="java.lang.Integer">
        select count(1) from menu_nutrition where category_id = #{id}
    </select>


</mapper>
