<?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.MenuMaterialCategoryMapper">
    <resultMap type="com.bonus.canteen.core.menu.domain.MenuMaterialCategory" id="MenuMaterialCategoryResult">
        <result property="id"    column="id"    />
        <result property="areaId"    column="area_id"    />
        <result property="categoryId"    column="category_id"    />
        <result property="categoryNum"    column="category_num"    />
        <result property="categoryName"    column="category_name"    />
        <result property="parentId"    column="parent_id"    />
        <result property="categoryType"    column="category_type"    />
        <result property="revision"    column="revision"    />
        <result property="level"    column="level"    />
        <result property="bigCategoryId"    column="big_category_id"    />
        <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"    />
    </resultMap>

    <sql id="selectMenuMaterialCategoryVo">
        select id, area_id, category_id, category_num, category_name, parent_id, category_type, revision, level, big_category_id, create_by, create_time, update_by, update_time, remark from menu_material_category
    </sql>

    <select id="selectMenuMaterialCategoryList" parameterType="com.bonus.canteen.core.menu.domain.MenuMaterialCategory" resultMap="MenuMaterialCategoryResult">
        <include refid="selectMenuMaterialCategoryVo"/>
        <where>
            <if test="areaId != null "> and area_id = #{areaId}</if>
            <if test="categoryId != null "> and category_id = #{categoryId}</if>
            <if test="categoryNum != null  and categoryNum != ''"> and category_num = #{categoryNum}</if>
            <if test="categoryName != null  and categoryName != ''"> and category_name like concat('%', #{categoryName}, '%')</if>
            <if test="parentId != null "> and parent_id = #{parentId}</if>
            <if test="categoryType != null "> and category_type = #{categoryType}</if>
            <if test="revision != null "> and revision = #{revision}</if>
            <if test="level != null "> and level = #{level}</if>
            <if test="bigCategoryId != null "> and big_category_id = #{bigCategoryId}</if>
        </where>
    </select>

    <select id="selectMenuMaterialCategoryById" parameterType="Long" resultMap="MenuMaterialCategoryResult">
        <include refid="selectMenuMaterialCategoryVo"/>
        where category_id = #{id}
    </select>

    <insert id="insertMenuMaterialCategory" parameterType="com.bonus.canteen.core.menu.domain.MenuMaterialCategory" useGeneratedKeys="true" keyProperty="id">
        insert into menu_material_category
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="areaId != null">area_id,</if>
            <if test="categoryId != null">category_id,</if>
            <if test="categoryNum != null">category_num,</if>
            <if test="categoryName != null and categoryName != ''">category_name,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="categoryType != null">category_type,</if>
            <if test="revision != null">revision,</if>
            <if test="level != null">level,</if>
            <if test="bigCategoryId != null">big_category_id,</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="areaId != null">#{areaId},</if>
            <if test="categoryId != null">#{categoryId},</if>
            <if test="categoryNum != null">#{categoryNum},</if>
            <if test="categoryName != null and categoryName != ''">#{categoryName},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="categoryType != null">#{categoryType},</if>
            <if test="revision != null">#{revision},</if>
            <if test="level != null">#{level},</if>
            <if test="bigCategoryId != null">#{bigCategoryId},</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="updateMenuMaterialCategory" parameterType="com.bonus.canteen.core.menu.domain.MenuMaterialCategory">
        update menu_material_category
        <trim prefix="SET" suffixOverrides=",">
            <if test="areaId != null">area_id = #{areaId},</if>
            <if test="categoryId != null">category_id = #{categoryId},</if>
            <if test="categoryNum != null">category_num = #{categoryNum},</if>
            <if test="categoryName != null and categoryName != ''">category_name = #{categoryName},</if>
<!--            <if test="parentId != null">parent_id = #{parentId},</if>-->
            <if test="categoryType != null">category_type = #{categoryType},</if>
            <if test="revision != null">revision = #{revision},</if>
            <if test="level != null">level = #{level},</if>
            <if test="bigCategoryId != null">big_category_id = #{bigCategoryId},</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 category_id = #{id}
    </update>

    <update id="deleteMenuMaterialCategoryById" parameterType="Long">
        delete from menu_material_category where id = #{id}
    </update>

    <update id="deleteMenuMaterialCategoryByIds" parameterType="java.lang.String">
        update  menu_material_category  set del_flag =1 where category_id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>

    <select id="getChildCategoryIds" resultType="java.lang.String">
        select category_id from menu_material_category where parent_id in
        <foreach item="id" collection="array" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <select id="selectCategoryTrees" resultType="com.bonus.canteen.core.menu.vo.MenuCategoryTreeVO">
        select category_id as id, parent_id as parentId, category_name as categoryName,category_type as categoryType,area_id as areaId from menu_material_category
        <where>
            <if test="categoryType != null"> and category_type = #{categoryType}</if>
        </where>
</select>

    <select id="getTenantAreaIdList" resultType="java.lang.Long">
        SELECT r.area_id
        FROM sys_role_area r
        LEFT JOIN sys_role o ON o.role_id = r.role_id
        LEFT JOIN sys_user_role u ON r.role_id = u.role_id
        WHERE u.user_id = #{authPO.userId}
        AND o.role_key = #{authPO.roleCode}
        AND o.del_flag = 0
    </select>

    <select id="getOne" resultMap="MenuMaterialCategoryResult">
        select * from menu_material_category where parent_id =#{parentId} and category_name =#{categoryName} and category_type =#{categoryType} and area_id =#{areaId}
    </select>


</mapper>
