<?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.corewing.app.mapper.TutorialMapper">

    <!-- 结果映射 -->
    <resultMap id="VOResultMap" type="com.corewing.app.vo.TutorialVO">
        <id column="id" property="id"/>
        <result column="tutorial_title" property="tutorialTitle"/>
        <result column="description" property="description"/>
        <result column="content" property="content"/>
        <result column="view_count" property="viewCount"/>
        <result column="recommend_status" property="recommendStatus"/>
        <result column="status" property="status"/>
        <result column="lang" property="lang"/>
        <result column="knowledge_url" property="knowledgeUrl"/>
        <result column="category_title" property="categoryTitle"/>
        <result column="category_id" property="categoryId"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
    </resultMap>

    <!-- 基础查询SQL片段 -->
    <sql id="selectVOSql">
        select c.*, cc.category_title, cc.id as category_id
            from app_tutorial c
        left join app_tutorial_category_relation ccr on c.id = ccr.tutorial_id
        left join app_tutorial_category cc on cc.id = ccr.category_id
    </sql>

    <!-- 分页查询 -->
    <select id="pageList" resultMap="VOResultMap">
        <include refid="selectVOSql"/>
        <where>
            and c.status = 1
            and c.lang = #{lang}
            <if test="categoryId != null and categoryId != 0">
                AND cc.id = #{categoryId}
            </if>
            <if test="tutorialTitle != null and tutorialTitle != ''">
                AND c.tutorial_title like CONCAT('%', #{tutorialTitle}, '%')
            </if>
        </where>
        ORDER BY c.recommend_status,c.create_time,c.id asc
    </select>

    <!-- 分页查询 -->
    <select id="page" resultMap="VOResultMap">
        <include refid="selectVOSql"/>
        <where>
            <if test="tutorial.categoryId != null and tutorial.categoryId != 0">
                AND cc.id = #{tutorial.categoryId}
            </if>
            <if test="tutorial.tutorialTitle != null and tutorial.tutorialTitle != ''">
                AND c.tutorial_title like CONCAT('%', #{tutorial.tutorialTitle}, '%')
            </if>
            <if test="tutorial.lang != null and tutorial.lang != ''">
                AND c.lang = #{tutorial.lang}
            </if>
            <if test="tutorial.status != null">
                AND c.status = #{tutorial.status}
            </if>

        </where>
    </select>

</mapper>
