<?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.younike.system.mapper.CategoryMapper">

    <resultMap type="Category" id="CategoryResult">
        <result property="categoryId" column="category_id"/>
        <result property="bizType" column="biz_type"/>
        <result property="categoryName" column="category_name"/>
        <result property="parentId" column="parent_id"/>
        <result property="icon" column="icon"/>
        <result property="url" column="url"/>
        <result property="isEnable" column="is_enable"/>
        <result property="indexShow" column="index_show"/>
        <result property="type" column="type"/>
        <result property="sorting" column="sorting"/>
        <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"/>
        <result property="column1" column="column1"/>
        <result property="column2" column="column2"/>
        <result property="column3" column="column3"/>
    </resultMap>

    <resultMap type="CategoryVo" id="CategoryListResult">
        <id property="categoryId" column="category_id"/>
        <result property="bizType" column="biz_type"/>
        <result property="categoryName" column="category_name"/>
        <result property="parentId" column="parent_id"/>
        <result property="icon" column="icon"/>
        <result property="url" column="url"/>
        <result property="isEnable" column="is_enable"/>
        <result property="indexShow" column="index_show"/>
        <result property="type" column="type"/>
        <result property="sorting" column="sorting"/>
        <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"/>
        <result property="column1" column="column1"/>
        <result property="column2" column="column2"/>
        <result property="column3" column="column3"/>
        <collection property="dimensionList" resultMap="com.younike.system.mapper.DimensionMapper.DimensionResult"
                    notNullColumn="dimension_id"/>
    </resultMap>

    <sql id="selectCategoryVo">
        select category_id,biz_type, category_name, parent_id, icon, url, is_enable, index_show, type, sorting, del_flag, create_by, create_time, update_by, update_time, column1, column2, column3 from category
    </sql>


    <select id="selectCategoryList" parameterType="Category" resultMap="CategoryResult">
        <include refid="selectCategoryVo"/>
        <where>
            <if test="categoryName != null  and categoryName != ''">and category_name like concat('%', #{categoryName},
                '%')
            </if>
            <if test="bizType != null  and bizType != ''">and biz_type = #{bizType}</if>
            <if test="parentId != null  and parentId != ''">and parent_id = #{parentId}</if>
            <if test="icon != null  and icon != ''">and icon = #{icon}</if>
            <if test="url != null  and url != ''">and url = #{url}</if>
            <if test="isEnable != null ">and is_enable = #{isEnable}</if>
            <if test="indexShow != null ">and index_show = #{indexShow}</if>
            <if test="type != null ">and type = #{type}</if>
            <if test="sorting != null ">and sorting = #{sorting}</if>
            <if test="column1 != null  and column1 != ''">and column1 = #{column1}</if>
            <if test="column2 != null  and column2 != ''">and column2 = #{column2}</if>
            <if test="column3 != null  and column3 != ''">and column3 = #{column3}</if>
        </where>
    </select>

    <select id="selectCategoryByCategoryId" parameterType="Long" resultMap="CategoryResult">
        <include refid="selectCategoryVo"/>
        where category_id = #{categoryId}
    </select>

    <insert id="insertCategory" parameterType="Category" useGeneratedKeys="true" keyProperty="categoryId">
        insert into category
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="categoryName != null and categoryName != ''">category_name,</if>
            <if test="bizType != null  and bizType != ''">biz_type,</if>
            <if test="parentId != null">parent_id,</if>
            <if test="icon != null">icon,</if>
            <if test="url != null">url,</if>
            <if test="isEnable != null">is_enable,</if>
            <if test="indexShow != null">index_show,</if>
            <if test="type != null">type,</if>
            <if test="sorting != null">sorting,</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="column1 != null">column1,</if>
            <if test="column2 != null">column2,</if>
            <if test="column3 != null">column3,</if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="categoryName != null and categoryName != ''">#{categoryName},</if>
            <if test="bizType != null  and bizType != ''">#{bizType},</if>
            <if test="parentId != null">#{parentId},</if>
            <if test="icon != null">#{icon},</if>
            <if test="url != null">#{url},</if>
            <if test="isEnable != null">#{isEnable},</if>
            <if test="indexShow != null">#{indexShow},</if>
            <if test="type != null">#{type},</if>
            <if test="sorting != null">#{sorting},</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="column1 != null">#{column1},</if>
            <if test="column2 != null">#{column2},</if>
            <if test="column3 != null">#{column3},</if>
        </trim>
    </insert>

    <update id="updateCategory" parameterType="Category">
        update category
        <trim prefix="SET" suffixOverrides=",">
            <if test="categoryName != null and categoryName != ''">category_name = #{categoryName},</if>
            <if test="bizType != null  and bizType != ''">biz_type = #{bizType},</if>
            <if test="parentId != null">parent_id = #{parentId},</if>
            <if test="icon != null">icon = #{icon},</if>
            <if test="url != null">url = #{url},</if>
            <if test="isEnable != null">is_enable = #{isEnable},</if>
            <if test="indexShow != null">index_show = #{indexShow},</if>
            <if test="type != null">type = #{type},</if>
            <if test="sorting != null">sorting = #{sorting},</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="column1 != null">column1 = #{column1},</if>
            <if test="column2 != null">column2 = #{column2},</if>
            <if test="column3 != null">column3 = #{column3},</if>
        </trim>
        where category_id = #{categoryId}
    </update>

    <delete id="deleteCategoryByCategoryId" parameterType="Long">
        delete from category where category_id = #{categoryId}
    </delete>

    <delete id="deleteCategoryByCategoryIds" parameterType="String">
        delete from category where category_id in
        <foreach item="categoryId" collection="array" open="(" separator="," close=")">
            #{categoryId}
        </foreach>
    </delete>

    <select id="getCategoryList" resultMap="CategoryListResult">
        SELECT
            c.*, d.dimension_id, d.name
        FROM
            category c
        LEFT JOIN cate_dimension cd ON c.category_id = cd.category_id AND cd.del_flag = 0
        LEFT JOIN dimension d on cd.dimension_id = d.dimension_id AND d.del_flag = 0
        WHERE
            c.del_flag = 0
            and c.is_enable = 1
            <if test="bizType != null and bizType != ''">
                and c.biz_type = #{bizType}
            </if>
        ORDER BY
            c.sorting
    </select>

    <update id="updateSortingBatch">
        <foreach collection="categoryList" item="item" index="index" open="" close="" separator=";">
            update category
            set sorting = #{item.sorting}
            where category_id = ${item.categoryId}
        </foreach>
    </update>

    <select id="getByParentId" resultMap="CategoryResult">
        SELECT *
        FROM category
        WHERE del_flag = 0
        <if test="parentId == null or parentId == ''">
            AND parent_id is null
        </if>
        <if test="parentId != null and parentId != ''">
            AND parent_id = #{parentId}
        </if>
    </select>

    <select id="selectCategoriesList" resultMap="CategoryListResult">
        SELECT
            c.*, d.dimension_id, d.name
        FROM
            category c
        LEFT JOIN cate_dimension cd ON c.category_id = cd.category_id AND cd.del_flag = 0
        LEFT JOIN dimension d on cd.dimension_id = d.dimension_id AND d.del_flag = 0
        WHERE
        c.del_flag = 0
        and c.is_enable=1
        <if test="bizType != null and bizType != ''">
            AND c.biz_type = #{bizType}
        </if>
        ORDER BY
        c.sorting
    </select>
<!--    首页服务数据-->
    <select id="getHomeCategoriesList"   resultMap="CategoryResult">
        <include refid="selectCategoryVo"/>
        where parent_id is null
        and is_enable=1
        and index_show=1
        and type=0
        and del_flag=0
        <if test="bizType != null and bizType != ''">
            AND biz_type = #{bizType}
        </if>
        ORDER BY
            sorting
    </select>
<!--  （服务端）查询全部一级分类  -->
    <select id="selectCategoriesPublishList"   resultMap="CategoryResult">
        <include refid="selectCategoryVo"/>
        where parent_id is null
        and is_enable=1
        and type=0
        and del_flag=0
        ORDER BY
        sorting
    </select>

<!--    -->
    <select id="serverCategoryList"   resultMap="CategoryResult">
        select
            p.category_id,
            p.biz_type,
            p.category_name,
            p.parent_id,
            p.icon,
            p.url,
            p.is_enable,
            p.index_show,
            p.type,
            p.sorting,
            p.del_flag,
            p.create_by,
            p.create_time,
            p.update_by,
            p.update_time
        from merchant_category mc
        left join category p on mc.category_id=p.category_id
        where p.parent_id is null
        and mc.del_flag =0
        <if test="merchantId != null and merchantId != ''">
            AND mc.merchant_id = #{merchantId}
        </if>
        and p.is_enable=1
        and p.type=0
        and p.del_flag=0
        ORDER BY
        p.sorting
    </select>
</mapper>
