<?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.CmsTagMapper">

    <resultMap type="com.kexue.skills.entity.CmsTag" id="CmsTagMap">
        <result property="tagId" column="tag_id" jdbcType="BIGINT"/>
        <result property="tagName" column="tag_name" jdbcType="VARCHAR"/>
        <result property="description" column="description" jdbcType="VARCHAR"/>
        <result property="useCount" column="use_count" 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="CmsTagMap">
        select
          tag_id, tag_name, description, use_count, status, create_time, update_time, create_by, update_by, delete_flag
        from cms_tag
        where tag_id = #{tagId}
    </select>

    <!--查询分页列表-->
    <select id="getPageList" resultMap="CmsTagMap">
        select
          tag_id, tag_name, description, use_count, status, create_time, update_time, create_by, update_by, delete_flag
        from cms_tag
        <where>
            <if test="tagId != null">
                and tag_id = #{tagId}
            </if>
            <if test="tagName != null and tagName != ''">
                and tag_name like concat('%', #{tagName}, '%')
            </if>
            <if test="status != null">
                and status = #{status}
            </if>
            <if test="deleteFlag != null">
                and delete_flag = #{deleteFlag}
            </if>
        </where>
        order by use_count desc, create_time desc
    </select>

    <!--查询列表-->
    <select id="getList" resultMap="CmsTagMap">
        select
          tag_id, tag_name, description, use_count, status, create_time, update_time, create_by, update_by, delete_flag
        from cms_tag
        <where>
            <if test="tagId != null">
                and tag_id = #{tagId}
            </if>
            <if test="tagName != null and tagName != ''">
                and tag_name like concat('%', #{tagName}, '%')
            </if>
            <if test="status != null">
                and status = #{status}
            </if>
            <if test="deleteFlag != null">
                and delete_flag = #{deleteFlag}
            </if>
        </where>
        order by use_count desc, create_time desc
    </select>

    <!--新增所有列-->
    <insert id="insert" keyProperty="tagId" useGeneratedKeys="true">
        insert into cms_tag(tag_name, description, use_count, status, create_time, update_time, create_by, update_by, delete_flag)
        values (#{tagName}, #{description}, #{useCount}, #{status}, #{createTime}, #{updateTime}, #{createBy}, #{updateBy}, #{deleteFlag})
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        update cms_tag
        <set>
            <if test="tagName != null and tagName != ''">
                tag_name = #{tagName},
            </if>
            <if test="description != null">
                description = #{description},
            </if>
            <if test="useCount != null">
                use_count = #{useCount},
            </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 tag_id = #{tagId}
    </update>

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

    <!--通过主键物理删除-->
    <delete id="deleteById">
        delete from cms_tag where tag_id = #{tagId}
    </delete>

</mapper>