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

    <resultMap type="com.kexue.skills.entity.CmsContent" id="CmsContentMap">
        <result property="contentId" column="content_id" jdbcType="BIGINT"/>
        <result property="title" column="title" jdbcType="VARCHAR"/>
        <result property="subtitle" column="subtitle" jdbcType="VARCHAR"/>
        <result property="contentType" column="content_type" jdbcType="INTEGER"/>
        <result property="categoryIds" column="category_ids" jdbcType="VARCHAR"/>
        <result property="summary" column="summary" jdbcType="VARCHAR"/>
        <result property="content" column="content" jdbcType="LONGVARCHAR"/>
        <result property="coverImage" column="cover_image" jdbcType="VARCHAR"/>
        <result property="authorId" column="author_id" jdbcType="BIGINT"/>
        <result property="authorName" column="author_name" jdbcType="VARCHAR"/>
        <result property="reviewerId" column="reviewer_id" jdbcType="BIGINT"/>
        <result property="reviewerName" column="reviewer_name" jdbcType="VARCHAR"/>
        <result property="auditStatus" column="audit_status" jdbcType="INTEGER"/>
        <result property="auditComment" column="audit_comment" jdbcType="VARCHAR"/>
        <result property="publishStatus" column="publish_status" jdbcType="INTEGER"/>
        <result property="publishTime" column="publish_time" jdbcType="TIMESTAMP"/>
        <result property="viewCount" column="view_count" jdbcType="INTEGER"/>
        <result property="likeCount" column="like_count" jdbcType="INTEGER"/>
        <result property="commentCount" column="comment_count" jdbcType="INTEGER"/>
        <result property="sort" column="sort" jdbcType="INTEGER"/>
        <result property="isPaid" column="is_paid" jdbcType="BIT"/>
        <result property="price" column="price" jdbcType="DECIMAL"/>
        <result property="requiredPoints" column="required_points" jdbcType="INTEGER"/>
        <result property="supportPointsPay" column="support_points_pay" jdbcType="BIT"/>
        <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"/>
        <result property="isOfficial" column="is_official" jdbcType="BIT"/>
        <result property="shareCount" column="share_count" jdbcType="INTEGER"/>
        <result property="fileUrl" column="file_url" jdbcType="VARCHAR"/>
        <result property="icon" column="icon" jdbcType="VARCHAR"/>
        <result property="background" column="background" jdbcType="VARCHAR"/>
    </resultMap>

    <!--查询单个-->
    <select id="queryById" resultMap="CmsContentMap">
        select
          content_id, title, subtitle, content_type, category_ids, summary, content, cover_image, author_id, author_name, 
          reviewer_id, reviewer_name, audit_status, audit_comment, publish_status, publish_time, 
          view_count, like_count, comment_count, sort, is_paid, price, required_points, support_points_pay, is_official, share_count, file_url, icon, background, create_time, update_time, create_by, update_by, delete_flag
        from cms_content
        where content_id = #{contentId}
    </select>

    <!--查询分页列表-->
    <select id="getPageList" resultMap="CmsContentMap">
        select
          content_id, title, subtitle, content_type, category_ids, summary, content, cover_image, author_id, author_name, 
          reviewer_id, reviewer_name, audit_status, audit_comment, publish_status, publish_time, 
          view_count, like_count, comment_count, sort, is_paid, price, required_points, support_points_pay, is_official, share_count, file_url, icon, background, create_time, update_time, create_by, update_by, delete_flag
        from cms_content
        <where>
            <if test="contentId != null">
                and content_id = #{contentId}
            </if>
            <if test="title != null and title != ''">
                and title like concat('%', #{title}, '%')
            </if>
            <if test="contentType != null">
                and content_type = #{contentType}
            </if>
            <if test="categoryId != null">
                and find_in_set(#{categoryId}, category_ids)
            </if>
            <if test="categoryIds != null and categoryIds != ''">
                <foreach collection="categoryIds.split(',')" item="catId" separator="and" open="and " close="">
                    find_in_set(#{catId}, category_ids)
                </foreach>
            </if>
            <if test="categoryIdList != null and categoryIdList.size() > 0">
                <foreach collection="categoryIdList" item="catId" separator="and" open="and " close="">
                    find_in_set(#{catId}, category_ids)
                </foreach>
            </if>
            <if test="authorId != null">
                and author_id = #{authorId}
            </if>
            <if test="authorName != null and authorName != ''">
                and author_name like concat('%', #{authorName}, '%')
            </if>
            <if test="auditStatus != null">
                and audit_status = #{auditStatus}
            </if>
            <if test="publishStatus != null">
                and publish_status = #{publishStatus}
            </if>
            <if test="createBy != null and createBy != ''">
                and create_by like concat('%', #{createBy}, '%')
            </if>
            <if test="deleteFlag != null">
                and delete_flag = #{deleteFlag}
            </if>
        </where>
        <if test="sortBy != null and sortBy != ''">
            order by ${sortBy} ${sortDesc ? 'desc' : 'asc'}
        </if>
        <if test="sortBy == null or sortBy == ''">
            order by sort asc, create_time desc
        </if>
    </select>

    <!--查询列表-->
    <select id="getList" resultMap="CmsContentMap">
        select
          content_id, title, subtitle, content_type, category_ids, summary, content, cover_image, author_id, author_name, 
          reviewer_id, reviewer_name, audit_status, audit_comment, publish_status, publish_time, 
          view_count, like_count, comment_count, sort, is_paid, price, required_points, support_points_pay, is_official, share_count, file_url, icon, background, create_time, update_time, create_by, update_by, delete_flag
        from cms_content
        <where>
            <if test="contentId != null">
                and content_id = #{contentId}
            </if>
            <if test="title != null and title != ''">
                and title like concat('%', #{title}, '%')
            </if>
            <if test="contentType != null">
                and content_type = #{contentType}
            </if>
            <if test="categoryId != null">
                and find_in_set(#{categoryId}, category_ids)
            </if>
            <if test="categoryIds != null and categoryIds != ''">
                <foreach collection="categoryIds.split(',')" item="catId" separator="and" open="and " close="">
                    find_in_set(#{catId}, category_ids)
                </foreach>
            </if>
            <if test="categoryIdList != null and categoryIdList.size() > 0">
                <foreach collection="categoryIdList" item="catId" separator="and" open="and " close="">
                    find_in_set(#{catId}, category_ids)
                </foreach>
            </if>
            <if test="authorId != null">
                and author_id = #{authorId}
            </if>
            <if test="auditStatus != null">
                and audit_status = #{auditStatus}
            </if>
            <if test="publishStatus != null">
                and publish_status = #{publishStatus}
            </if>
            <if test="deleteFlag != null">
                and delete_flag = #{deleteFlag}
            </if>
        </where>
        order by sort asc, create_time desc
    </select>

    <!--新增所有列-->
    <insert id="insert" keyProperty="contentId" useGeneratedKeys="true">
        insert into cms_content(title, subtitle, content_type, category_ids, summary, content, cover_image, author_id, author_name, 
        reviewer_id, reviewer_name, audit_status, audit_comment, publish_status, publish_time, 
        view_count, like_count, comment_count, sort, is_paid, price, required_points, support_points_pay, is_official, share_count, file_url, icon, background, create_time, update_time, create_by, update_by, delete_flag)
        values (#{title}, #{subtitle}, #{contentType}, #{categoryIds}, #{summary}, #{content}, #{coverImage}, #{authorId}, #{authorName}, 
        #{reviewerId}, #{reviewerName}, #{auditStatus}, #{auditComment}, #{publishStatus}, #{publishTime}, 
        #{viewCount}, #{likeCount}, #{commentCount}, #{sort}, #{isPaid}, #{price}, #{requiredPoints}, #{supportPointsPay}, #{isOfficial}, #{shareCount}, #{fileUrl}, #{icon}, #{background}, #{createTime}, #{updateTime}, #{createBy}, #{updateBy}, #{deleteFlag})
    </insert>

    <!--通过主键修改数据-->
    <update id="update">
        update cms_content
        <set>
            <if test="title != null and title != ''">
                title = #{title},
            </if>
            <if test="subtitle != null">
                subtitle = #{subtitle},
            </if>
            <if test="contentType != null">
                content_type = #{contentType},
            </if>
            <if test="categoryIds != null and categoryIds != ''">
                category_ids = #{categoryIds},
            </if>
            <if test="summary != null">
                summary = #{summary},
            </if>
            <if test="content != null">
                content = #{content},
            </if>
            <if test="coverImage != null">
                cover_image = #{coverImage},
            </if>
            <if test="authorId != null">
                author_id = #{authorId},
            </if>
            <if test="authorName != null and authorName != ''">
                author_name = #{authorName},
            </if>
            <if test="reviewerId != null">
                reviewer_id = #{reviewerId},
            </if>
            <if test="reviewerName != null and reviewerName != ''">
                reviewer_name = #{reviewerName},
            </if>
            <if test="auditStatus != null">
                audit_status = #{auditStatus},
            </if>
            <if test="auditComment != null">
                audit_comment = #{auditComment},
            </if>
            <if test="publishStatus != null">
                publish_status = #{publishStatus},
            </if>
            <if test="publishTime != null">
                publish_time = #{publishTime},
            </if>
            <if test="viewCount != null">
                view_count = #{viewCount},
            </if>
            <if test="likeCount != null">
                like_count = #{likeCount},
            </if>
            <if test="commentCount != null">
                comment_count = #{commentCount},
            </if>
            <if test="sort != null">
                sort = #{sort},
            </if>
            <if test="isPaid != null">
                is_paid = #{isPaid},
            </if>
            <if test="price != null">
                price = #{price},
            </if>
            <if test="requiredPoints != null">
                required_points = #{requiredPoints},
            </if>
            <if test="supportPointsPay != null">
                support_points_pay = #{supportPointsPay},
            </if>
            <if test="isOfficial != null">
                is_official = #{isOfficial},
            </if>
            <if test="shareCount != null">
                share_count = #{shareCount},
            </if>
            <if test="fileUrl != null">
                file_url = #{fileUrl},
            </if>
            <if test="icon != null">
                icon = #{icon},
            </if>
            <if test="background != null">
                background = #{background},
            </if>
            <if test="updateTime != null">
                update_time = #{updateTime},
            </if>
            <if test="updateBy != null">
                update_by = #{updateBy},
            </if>
        </set>
        where content_id = #{contentId}
    </update>

    <!--更新审核状态-->
    <update id="updateAuditStatus">
        update cms_content
        set audit_status = #{auditStatus},
            reviewer_id = #{reviewerId},
            reviewer_name = #{reviewerName},
            audit_comment = #{auditComment},
            update_by = #{updateBy},
            update_time = now()
        where content_id = #{contentId}
    </update>

    <!--更新发布状态-->
    <update id="updatePublishStatus">
        update cms_content
        set publish_status = #{publishStatus},
            publish_time = #{publishTime},
            update_by = #{updateBy},
            update_time = now()
        where content_id = #{contentId}
    </update>

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

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

    <!--增加阅读量-->
    <update id="increaseViewCount">
        update cms_content
        set view_count = view_count + 1
        where content_id = #{contentId}
    </update>
    
    <!--查询用户最近创建的内容ID列表-->
    <select id="queryRecentCreatedByUserId" resultType="Long">
        select content_id from cms_content
        where author_id = #{userId} and delete_flag = 0
        order by create_time desc
        limit #{limit}
    </select>

</mapper>
