<?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.kexue.skills.mapper.CmsCategoryMapper">

    <resultMap type="com.kexue.skills.entity.CmsCategory" id="CmsCategoryMap">
        <result property="categoryId" column="category_id" jdbcType="BIGINT"/>
        <result property="categoryName" column="category_name" jdbcType="VARCHAR"/>
        <result property="parentId" column="parent_id" jdbcType="BIGINT"/>
        <result property="level" column="level" jdbcType="INTEGER"/>
        <result property="sort" column="sort" jdbcType="INTEGER"/>
        <result property="status" column="status" jdbcType="INTEGER"/>
        <result property="createTime" column="create_time" jdbcType="TIMESTAMP"/>
        <result property="updateTime" column="update_time" jdbcType="TIMESTAMP"/>
        <result property="createBy" column="create_by" jdbcType="VARCHAR"/>
        <result property="updateBy" column="update_by" jdbcType="VARCHAR"/>
        <result property="deleteFlag" column="delete_flag" jdbcType="INTEGER"/>
    </resultMap>

    <!--查询单个-->
    <select id="queryById" resultMap="CmsCategoryMap">
        select
          category_id, category_name, parent_id, level, sort, status, create_time, update_time, create_by, update_by, delete_flag
        from cms_category
        where category_id = #{categoryId}
    </select>

    <!--查询分页列表-->
    <select id="getPageList" resultMap="CmsCategoryMap">
        select
          category_id, category_name, parent_id, level, sort, status, create_time, update_time, create_by, update_by, delete_flag
        from cms_category
        <where>
            <if test="categoryId != null">
                and category_id = #{categoryId}
            </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="level != null">
                and level = #{level}
            </if>
            <if test="status != null">
                and status = #{status}
            </if>
            <if test="deleteFlag != null">
                and delete_flag = #{deleteFlag}
            </if>
        </where>
        order by sort asc, create_time desc
    </select>

    <!--查询列表-->
    <select id="getList" resultMap="CmsCategoryMap">
        select
          category_id, category_name, parent_id, level, sort, status, create_time, update_time, create_by, update_by, delete_flag
        from cms_category
        <where>
            <if test="categoryId != null">
                and category_id = #{categoryId}
            </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="level != null">
                and level = #{level}
            </if>
            <if test="status != null">
                and status = #{status}
            </if>
            <if test="deleteFlag != null">
                and delete_flag = #{deleteFlag}
            </if>
        </where>
        order by sort asc, create_time desc
    </select>

    <!--新增所有列-->
    <insert id="insert" keyProperty="categoryId" useGeneratedKeys="true">
        insert into cms_category(category_name, parent_id, level, sort, status, create_time, update_time, create_by, update_by, delete_flag)
        values (#{categoryName}, #{parentId}, #{level}, #{sort}, #{status}, #{createTime}, #{updateTime}, #{createBy}, #{updateBy}, #{deleteFlag})
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        update cms_category
        <set>
            <if test="categoryName != null and categoryName != ''">
                category_name = #{categoryName},
            </if>
            <if test="parentId != null">
                parent_id = #{parentId},
            </if>
            <if test="level != null">
                level = #{level},
            </if>
            <if test="sort != null">
                sort = #{sort},
            </if>
            <if test="status != null">
                status = #{status},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
            <if test="updateBy != null">
                update_by = #{updateBy},
            </if>
        </set>
        where category_id = #{categoryId}
    </update>

    <!--通过主键逻辑删除-->
    <update id="logicDeleteById">
        update cms_category
        set delete_flag = 1, update_by = #{updateBy}, update_time = now()
        where category_id = #{categoryId}
    </update>

    <!--通过主键物理删除-->
    <delete id="deleteById">
        delete from cms_category where category_id = #{categoryId}
    </delete>

</mapper>