<?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.CookNutritionTypeMapper">
    <resultMap type="com.bonus.canteen.core.cook.domain.CookNutritionType" id="CookNutritionTypeResult">
        <result property="nutritionTypeId"    column="nutrition_type_id"    />
        <result property="nutritionTypeName"    column="nutrition_type_name"    />
        <result property="parentId"    column="parent_id"    />
        <result property="level"    column="level"    />
        <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="selectCookNutritionTypeVo">
        select nutrition_type_id, nutrition_type_name, parent_id, level, create_by, create_time, update_by, update_time from cook_nutrition_type
    </sql>

    <select id="selectCookNutritionTypeList" parameterType="com.bonus.canteen.core.cook.domain.CookNutritionType" resultMap="CookNutritionTypeResult">
        <include refid="selectCookNutritionTypeVo"/>
        <where>  
            <if test="nutritionTypeName != null  and nutritionTypeName != ''"> and nutrition_type_name like concat('%', #{nutritionTypeName}, '%')</if>
            <if test="parentId != null "> and parent_id = #{parentId}</if>
            <if test="level != null  and level != ''"> and level = #{level}</if>
        </where>
        order by create_time desc
    </select>
    
    <select id="selectCookNutritionTypeByNutritionTypeId" parameterType="Long" resultMap="CookNutritionTypeResult">
        <include refid="selectCookNutritionTypeVo"/>
        where nutrition_type_id = #{nutritionTypeId}
    </select>
    <select id="selectCookNutritionTypeByNutritionTypeName"
            resultType="com.bonus.canteen.core.cook.domain.CookNutritionType">
        <include refid="selectCookNutritionTypeVo"/>
        where nutrition_type_name = #{nutritionTypeName}
        <if test="nutritionTypeId != null and nutritionTypeId != '' "> and parent_id = #{nutritionTypeId}</if>
    </select>
    <select id="checkIsExistChildren" resultType="java.lang.Integer">
        select count(1) from cook_nutrition_type where parent_id in
        <foreach item="nutritionTypeId" collection="nutritionTypeIds" open="(" separator="," close=")">
            #{nutritionTypeId}
        </foreach>
    </select>
    <select id="selectBigTypeList" resultType="com.bonus.canteen.core.cook.vo.NutritionTypeVO">
        select nutrition_type_id as id, nutrition_type_name as name from cook_nutrition_type
        where level = 0
    </select>
    <select id="selectLittleTypeList" resultType="com.bonus.canteen.core.cook.vo.NutritionTypeVO">
        select nutrition_type_id as id, nutrition_type_name as name  from cook_nutrition_type
        where level = 1 and parent_id = #{parentId}
    </select>
    <insert id="insertCookNutritionType" parameterType="com.bonus.canteen.core.cook.domain.CookNutritionType" useGeneratedKeys="true" keyProperty="nutritionTypeId">
        insert into cook_nutrition_type
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="nutritionTypeName != null">nutrition_type_name,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="level != null">level,</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="nutritionTypeName != null">#{nutritionTypeName},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="level != null">#{level},</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="updateCookNutritionType" parameterType="com.bonus.canteen.core.cook.domain.CookNutritionType">
        update cook_nutrition_type
        <trim prefix="SET" suffixOverrides=",">
            <if test="nutritionTypeName != null">nutrition_type_name = #{nutritionTypeName},</if>
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="level != null">level = #{level},</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 nutrition_type_id = #{nutritionTypeId}
    </update>

    <delete id="deleteCookNutritionTypeByNutritionTypeId" parameterType="Long">
        delete from cook_nutrition_type where nutrition_type_id = #{nutritionTypeId}
    </delete>

    <delete id="deleteCookNutritionTypeByNutritionTypeIds" parameterType="String">
        delete from cook_nutrition_type where nutrition_type_id in 
        <foreach item="nutritionTypeId" collection="array" open="(" separator="," close=")">
            #{nutritionTypeId}
        </foreach>
    </delete>
</mapper>