<?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.CookMaterialTypeMapper">
    <resultMap type="com.bonus.canteen.core.cook.domain.CookMaterialType" id="CookMaterialTypeResult">
        <result property="materialTypeId"    column="material_type_id"    />
        <result property="materialTypeName"    column="material_type_name"    />
        <result property="parentId"    column="parent_id"    />
        <result property="level"    column="level"    />
        <result property="areaId"    column="area_id"    />
        <result property="goodsType"    column="goods_type"    />
        <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="selectCookMaterialTypeVo">
        select material_type_id, material_type_name, parent_id, level, area_id, goods_type, create_by, create_time, update_by, update_time from cook_material_type
    </sql>

    <select id="selectCookMaterialTypeList" parameterType="com.bonus.canteen.core.cook.domain.CookMaterialType" resultMap="CookMaterialTypeResult">
        <include refid="selectCookMaterialTypeVo"/>
        <where>  
            <if test="materialTypeName != null  and materialTypeName != ''"> and material_type_name like concat('%', #{materialTypeName}, '%')</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="goodsType != null "> and goods_type = #{goodsType}</if>
        </where>
        order by create_time desc
    </select>
    
    <select id="selectCookMaterialTypeByMaterialTypeId" parameterType="Long" resultMap="CookMaterialTypeResult">
        <include refid="selectCookMaterialTypeVo"/>
        where material_type_id = #{materialTypeId}
    </select>
    <select id="selectCookMaterialTypeByMaterialTypeName"
            resultType="com.bonus.canteen.core.cook.domain.CookMaterialType">
        <include refid="selectCookMaterialTypeVo"/>
        where material_type_name = #{materialTypeName}
        <if test="materialTypeId != null and materialTypeId != ''">and material_type_id != #{materialTypeId}</if>
    </select>
    <select id="selectCookMaterialTypeByParentId" resultType="java.lang.Integer">
        select count(1) from cook_material_type where parent_id in
        <foreach item="materialTypeId" collection="materialTypeIds" open="(" separator="," close=")">
            #{materialTypeId}
        </foreach>
    </select>
    <select id="getTree" resultType="com.bonus.canteen.core.cook.vo.CookMaterialTypeTreeVO">
        select material_type_id as id, parent_id as parentId, material_type_name as categoryName,
               goods_type as categoryType,area_id as areaId from cook_material_type
    </select>

    <insert id="insertCookMaterialType" parameterType="com.bonus.canteen.core.cook.domain.CookMaterialType" useGeneratedKeys="true" keyProperty="materialTypeId">
        insert into cook_material_type
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="materialTypeName != null and materialTypeName != ''">material_type_name,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="level != null">level,</if>
            <if test="areaId != null">area_id,</if>
            <if test="goodsType != null">goods_type,</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="materialTypeName != null and materialTypeName != ''">#{materialTypeName},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="level != null">#{level},</if>
            <if test="areaId != null">#{areaId},</if>
            <if test="goodsType != null">#{goodsType},</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="updateCookMaterialType" parameterType="com.bonus.canteen.core.cook.domain.CookMaterialType">
        update cook_material_type
        <trim prefix="SET" suffixOverrides=",">
            <if test="materialTypeName != null and materialTypeName != ''">material_type_name = #{materialTypeName},</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="goodsType != null">goods_type = #{goodsType},</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 material_type_id = #{materialTypeId}
    </update>

    <delete id="deleteCookMaterialTypeByMaterialTypeId" parameterType="Long">
        delete from cook_material_type where material_type_id = #{materialTypeId}
    </delete>

    <delete id="deleteCookMaterialTypeByMaterialTypeIds" parameterType="String">
        delete from cook_material_type where material_type_id in 
        <foreach item="materialTypeId" collection="array" open="(" separator="," close=")">
            #{materialTypeId}
        </foreach>
    </delete>
</mapper>