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


    <!-- 基础查询SQL片段 -->
    <sql id="selectVOSql">
        select m.*,mc.name as categoryTitle
            from app_model m
        left join app_model_category mc on m.category_id = mc.id
    </sql>

    <!-- 分页查询 -->
    <select id="page" resultType="com.corewing.app.entity.AppModel">
        <include refid="selectVOSql"/>
        <where>
            <if test="modelPageRequest.userId != null">
                AND m.user_id = #{modelPageRequest.userId}
            </if>
            <if test="modelPageRequest.status != null and modelPageRequest.status != ''">
                AND m.status = #{modelPageRequest.status}
            </if>
            <if test="modelPageRequest.isPublic != null and modelPageRequest.isPublic != ''">
                AND m.is_public = #{modelPageRequest.isPublic}
            </if>
            <if test="modelPageRequest.searchKey != null and modelPageRequest.searchKey != ''">
                AND m.title like CONCAT('%', #{modelPageRequest.searchKey}, '%')
            </if>
            <if test="modelPageRequest.categoryId != null and modelPageRequest.categoryId != ''">
                AND m.category_id = #{modelPageRequest.categoryId}
            </if>
        </where>
    </select>

    <select id="list" resultType="com.corewing.app.entity.AppModel">
        <include refid="selectVOSql"/>
        <where>
            <if test="modelListRequest.userId != null">
                AND m.user_id = #{modelListRequest.userId}
            </if>
            <if test="modelListRequest.status != null and modelListRequest.status != ''">
                AND m.status = #{modelListRequest.status}
            </if>
            <if test="modelListRequest.isPublic != null and modelListRequest.isPublic != ''">
                AND m.is_public = #{modelListRequest.isPublic}
            </if>
            <if test="modelListRequest.searchKey != null and modelListRequest.searchKey != ''">
                AND m.title like CONCAT('%', #{modelListRequest.searchKey}, '%')
            </if>
            <if test="modelListRequest.categoryId != null and modelListRequest.categoryId != ''">
                AND m.category_id = #{modelListRequest.categoryId}
            </if>
        </where>
    </select>

    <select id="getModelById" resultType="com.corewing.app.entity.AppModel">
        <include refid="selectVOSql"/>
        <where>
            and m.id = #{modelId}
        </where>
    </select>

    <select id="favoriteList" resultType="com.corewing.app.entity.AppModel">
        <include refid="selectVOSql"/>
        left join app_model_favorite mf on mf.model_id = m.id
        <where>
            AND mf.user_id = #{userId}
            <if test="modelFavoriteListRequest.searchKey != null and modelFavoriteListRequest.searchKey != ''">
                AND m.title like CONCAT('%', #{modelFavoriteListRequest.searchKey}, '%')
            </if>
        </where>
    </select>

    <select id="favoritePage" resultType="com.corewing.app.entity.AppModel">
        <include refid="selectVOSql"/>
        left join app_model_favorite mf on mf.model_id = m.id
        <where>
            AND mf.user_id = #{userId}
            <if test="modelFavoritePageRequest.searchKey != null and modelFavoritePageRequest.searchKey != ''">
                AND m.title like CONCAT('%', #{modelFavoritePageRequest.searchKey}, '%')
            </if>
        </where>
    </select>

    <select id="downloadLogList" resultType="com.corewing.app.entity.AppModel">
        <include refid="selectVOSql"/>
        left join app_model_download_log mol on mol.model_id = m.id
        <where>
            AND mf.user_id = #{userId}
            <if test="modelDownloadLogListRequest.searchKey != null and modelDownloadLogListRequest.searchKey != ''">
                AND m.title like CONCAT('%', #{modelDownloadLogListRequest.searchKey}, '%')
            </if>
        </where>
    </select>

    <select id="downloadLogPage" resultType="com.corewing.app.entity.AppModel">
        <include refid="selectVOSql"/>
        left join app_model_download_log mol on mol.model_id = m.id
        <where>
            AND mf.user_id = #{userId}
            <if test="modelDownloadLogPageRequest.searchKey != null and modelDownloadLogPageRequest.searchKey != ''">
                AND m.title like CONCAT('%', #{modelDownloadLogPageRequest.searchKey}, '%')
            </if>
        </where>
    </select>

</mapper>
