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

    <resultMap type="com.kexue.skills.entity.CmsCategoryTag" id="CmsCategoryTagMap">
        <result property="id" column="id" jdbcType="BIGINT"/>
        <result property="categoryId" column="category_id" jdbcType="BIGINT"/>
        <result property="tagId" column="tag_id" jdbcType="BIGINT"/>
        <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="CmsCategoryTagMap">
        select
          id, category_id, tag_id, create_time, update_time, create_by, update_by, delete_flag
        from cms_category_tag
        where id = #{id}
    </select>

    <!--查询分页列表-->
    <select id="getPageList" resultMap="CmsCategoryTagMap">
        select
          id, category_id, tag_id, create_time, update_time, create_by, update_by, delete_flag
        from cms_category_tag
        <where>
            <if test="id != null">
                and id = #{id}
            </if>
            <if test="categoryId != null">
                and category_id = #{categoryId}
            </if>
            <if test="tagId != null">
                and tag_id = #{tagId}
            </if>
            <if test="deleteFlag != null">
                and delete_flag = #{deleteFlag}
            </if>
        </where>
        order by create_time desc
    </select>

    <!--查询列表-->
    <select id="getList" resultMap="CmsCategoryTagMap">
        select
          id, category_id, tag_id, create_time, update_time, create_by, update_by, delete_flag
        from cms_category_tag
        <where>
            <if test="id != null">
                and id = #{id}
            </if>
            <if test="categoryId != null">
                and category_id = #{categoryId}
            </if>
            <if test="tagId != null">
                and tag_id = #{tagId}
            </if>
            <if test="deleteFlag != null">
                and delete_flag = #{deleteFlag}
            </if>
        </where>
        order by create_time desc
    </select>

    <!--新增所有列-->
    <insert id="insert" keyProperty="id" useGeneratedKeys="true">
        insert into cms_category_tag(category_id, tag_id, create_time, update_time, create_by, update_by, delete_flag)
        values (#{categoryId}, #{tagId}, #{createTime}, #{updateTime}, #{createBy}, #{updateBy}, #{deleteFlag})
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        update cms_category_tag
        <set>
            <if test="categoryId != null">
                category_id = #{categoryId},
            </if>
            <if test="tagId != null">
                tag_id = #{tagId},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
            <if test="updateBy != null">
                update_by = #{updateBy},
            </if>
        </set>
        where id = #{id}
    </update>

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

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

    <!--根据分类ID删除关联-->
    <update id="deleteByCategoryId">
        update cms_category_tag
        set delete_flag = 1, update_by = #{updateBy}, update_time = now()
        where category_id = #{categoryId}
    </update>

    <!--根据标签ID删除关联-->
    <update id="deleteByTagId">
        update cms_category_tag
        set delete_flag = 1, update_by = #{updateBy}, update_time = now()
        where tag_id = #{tagId}
    </update>

    <!--批量插入关联-->
    <insert id="batchInsert" useGeneratedKeys="true" keyProperty="id">
        insert into cms_category_tag(category_id, tag_id, create_time, update_time, create_by, update_by, delete_flag)
        values
        <foreach collection="list" item="item" separator=",">
            (#{item.categoryId}, #{item.tagId}, #{item.createTime}, #{item.updateTime}, #{item.createBy}, #{item.updateBy}, #{item.deleteFlag})
        </foreach>
    </insert>

</mapper>
