<?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.CookDishesTypeMapper">
    <resultMap type="com.bonus.canteen.core.cook.domain.CookDishesType" id="CookDishesTypeResult">
        <result property="dishesTypeId"    column="dishes_type_id"    />
        <result property="dishesTypeName"    column="dishes_type_name"    />
        <result property="dishesTypeCode"    column="dishes_type_code"    />
        <result property="parentId"    column="parent_id"    />
        <result property="level"    column="level"    />
        <result property="areaId"    column="area_id"    />
        <result property="dishesAliasName"    column="dishes_alias_name"    />
        <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="selectCookDishesTypeVo">
        select dishes_type_id, dishes_type_name, dishes_type_code, parent_id, level, area_id, dishes_alias_name, create_by, create_time, update_by, update_time from cook_dishes_type
    </sql>

    <select id="selectCookDishesTypeList" parameterType="com.bonus.canteen.core.cook.vo.CookDishesTypeVO" resultMap="CookDishesTypeResult">
        <include refid="selectCookDishesTypeVo"/>
        <where>  
            <if test="dishesTypeName != null  and dishesTypeName != ''"> and dishes_type_name like concat('%', #{dishesTypeName}, '%')</if>
            <if test="dishesTypeCode != null  and dishesTypeCode != ''"> and dishes_type_code = #{dishesTypeCode}</if>
            <if test="parentId != null "> and parent_id = #{parentId}</if>
            <if test="level != null "> and level = #{level}</if>
            <if test="areaId != null "> and area_id = #{areaId}</if>
            <if test="dishesAliasName != null  and dishesAliasName != ''"> and dishes_alias_name like concat('%', #{dishesAliasName}, '%')</if>
        </where>
        order by create_time desc
    </select>
    
    <select id="selectCookDishesTypeByDishesTypeId" parameterType="Long" resultMap="CookDishesTypeResult">
        <include refid="selectCookDishesTypeVo"/>
        where dishes_type_id = #{dishesTypeId}
    </select>
    <select id="checkIsExistByName" resultType="java.lang.Integer">
        select count(1) from cook_dishes_type
        where dishes_type_name = #{dishesTypeName}
        <if test="dishesTypeId != null">and dishes_type_id != #{dishesTypeId}</if>
    </select>
    <select id="checkIsUse" resultType="java.lang.Integer">
        select count(1) from cook_dishes
        where type_id in
        <foreach item="typeId" collection="dishesTypeIds" open="(" separator="," close=")">
            #{typeId}
        </foreach>
    </select>
    <select id="selectList" resultType="com.bonus.canteen.core.cook.vo.CookDishesTypeVO">
        select dishes_type_id as dishesTypeId, dishes_type_name as dishesTypeName,dishes_type_id as id,parent_id as parentId
               from cook_dishes_type
    </select>

    <insert id="insertCookDishesType" parameterType="com.bonus.canteen.core.cook.domain.CookDishesType" useGeneratedKeys="true" keyProperty="dishesTypeId">
        insert into cook_dishes_type
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="dishesTypeName != null and dishesTypeName != ''">dishes_type_name,</if>
            <if test="dishesTypeCode != null">dishes_type_code,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="level != null">level,</if>
            <if test="areaId != null">area_id,</if>
            <if test="dishesAliasName != null">dishes_alias_name,</if>
            <if test="createBy != null and createBy != ''">create_by,</if>
            <if test="createTime != null">create_time,</if>
            <if test="updateBy != null and updateBy != ''">update_by,</if>
            <if test="updateTime != null">update_time,</if>
         </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="dishesTypeName != null and dishesTypeName != ''">#{dishesTypeName},</if>
            <if test="dishesTypeCode != null">#{dishesTypeCode},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="level != null">#{level},</if>
            <if test="areaId != null">#{areaId},</if>
            <if test="dishesAliasName != null">#{dishesAliasName},</if>
            <if test="createBy != null and createBy != ''">#{createBy},</if>
            <if test="createTime != null">#{createTime},</if>
            <if test="updateBy != null and updateBy != ''">#{updateBy},</if>
            <if test="updateTime != null">#{updateTime},</if>
         </trim>
    </insert>

    <update id="updateCookDishesType" parameterType="com.bonus.canteen.core.cook.domain.CookDishesType">
        update cook_dishes_type
        <trim prefix="SET" suffixOverrides=",">
            <if test="dishesTypeName != null and dishesTypeName != ''">dishes_type_name = #{dishesTypeName},</if>
            <if test="dishesTypeCode != null">dishes_type_code = #{dishesTypeCode},</if>
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="level != null">level = #{level},</if>
            <if test="areaId != null">area_id = #{areaId},</if>
            <if test="dishesAliasName != null">dishes_alias_name = #{dishesAliasName},</if>
            <if test="createBy != null and createBy != ''">create_by = #{createBy},</if>
            <if test="createTime != null">create_time = #{createTime},</if>
            <if test="updateBy != null and updateBy != ''">update_by = #{updateBy},</if>
            <if test="updateTime != null">update_time = #{updateTime},</if>
        </trim>
        where dishes_type_id = #{dishesTypeId}
    </update>

    <delete id="deleteCookDishesTypeByDishesTypeId" parameterType="Long">
        delete from cook_dishes_type where dishes_type_id = #{dishesTypeId}
    </delete>

    <delete id="deleteCookDishesTypeByDishesTypeIds" parameterType="String">
        delete from cook_dishes_type where dishes_type_id in 
        <foreach item="dishesTypeId" collection="array" open="(" separator="," close=")">
            #{dishesTypeId}
        </foreach>
    </delete>
</mapper>