<?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.ai.da.mapper.primary.UserLikeGroupMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.ai.da.mapper.primary.entity.UserLikeGroup">
        <id column="id" property="id" />
        <result column="account_id" property="accountId" />
        <result column="collection_id" property="collectionId" />
        <result column="name" property="name" />
        <result column="create_date" property="createDate" />
        <result column="update_date" property="updateDate" />
    </resultMap>

    <insert id="insertUserLikeGroup" parameterType="com.ai.da.mapper.primary.entity.UserLikeGroup" useGeneratedKeys="true" keyProperty="id" keyColumn="id">
        insert into t_user_like_group
            (account_id,collection_id,`name`,create_date,update_date, original, project_id)
        values(
                  #{accountId},
                  #{collectionId},
                  #{name},
                  #{createDate},
                  #{updateDate},
                  #{original},
                  #{projectId});
    </insert>

    <select id="getMoreThan50UserLikeAccount" resultMap="BaseResultMap">
        SELECT d.account_id
        FROM t_design_item_detail d
                 JOIN t_user_like ul ON d.design_item_id = ul.design_item_id
                 JOIN t_user_like_group ulg ON ul.user_like_group_id = ulg.id
        WHERE ulg.account_id != -1
        GROUP BY d.account_id
        ORDER BY COUNT(*) DESC
        ;
    </select>

    <!-- 批量查询模块内容，使用JOIN优化避免N+1查询 -->
    <select id="getModuleContentBatch" resultType="java.util.Map">
        SELECT
            cs.id as sort_id,
            cs.parent_id,
            cs.relation_id,
            cs.relation_type,
            cs.sort,
            -- UserLike相关字段
            ul.id as user_like_id,
            ul.design_item_id,
            ul.design_outfit_id,
            -- TDesignPythonOutfit相关字段
            tdpo.design_url,
            -- ToProductImageResult相关字段
            tpir.id as to_product_result_id,
            tpir.url as result_url,
            tpir.element_type,
            tpir.element_id,
            tpir.status as result_status,
            tpir.create_time as result_create_time,
            tpir.to_product_image_record_id,
            -- ToProductImageRecord相关字段
            tpir2.prompt,
            -- ToProductElement相关字段
            tpe.url as element_url,
            -- PoseTransformation相关字段
            pt.unique_id as pose_task_id,
            pt.product_image as pose_product_image,
            pt.gif_url as pose_gif_url,
            pt.video_url as pose_video_url,
            pt.first_frame_url as pose_first_frame_url,
            pt.is_liked as pose_is_liked,
            pt.model_name as pose_model_name,
            pt.pose_id,
            pt.task_status as pose_status,
            pt.create_time as pose_create_time
        FROM collection_sort cs
        LEFT JOIN t_user_like ul ON cs.relation_type = 'Design' AND cs.relation_id = ul.id
        LEFT JOIN t_design_python_outfit tdpo ON ul.design_outfit_id = tdpo.id
        LEFT JOIN to_product_image_result tpir ON cs.relation_type IN ('ToProductImage', 'Relight') AND cs.relation_id = tpir.id
        LEFT JOIN to_product_image_record tpir2 ON tpir.to_product_image_record_id = tpir2.id
        LEFT JOIN to_product_element tpe ON tpir.element_type = 'ProductElement' AND tpir.element_id = tpe.id
        LEFT JOIN t_pose_transformation pt ON cs.relation_type = 'PoseTransform' AND cs.relation_id = pt.id
        WHERE cs.project_id = #{projectId}
        <if test="relationTypes != null and relationTypes.size() > 0">
            AND cs.relation_type IN
            <foreach collection="relationTypes" item="type" open="(" separator="," close=")">
                #{type}
            </foreach>
        </if>
        ORDER BY cs.parent_id, cs.sort
    </select>

    <!-- 批量查询ToProductImageResult及相关数据 -->
    <select id="getToProductImageResultBatch" resultType="java.util.Map">
        SELECT
            tpir.id,
            tpir.url,
            tpir.element_type,
            tpir.element_id,
            tpir.status,
            tpir.create_time,
            tpir.is_like,
            tpir.to_product_image_record_id,
            tpir.project_id,
            tpir.result_type,
            -- ToProductImageRecord相关字段
            tpir2.prompt,
            -- ToProductElement相关字段
            tpe.url as element_url,
            -- TDesignPythonOutfit相关字段
            tdpo.design_url as outfit_url
        FROM to_product_image_result tpir
        LEFT JOIN to_product_image_record tpir2 ON tpir.to_product_image_record_id = tpir2.id
        LEFT JOIN to_product_element tpe ON tpir.element_type = 'ProductElement' AND tpir.element_id = tpe.id
        LEFT JOIN t_design_python_outfit tdpo ON tpir.element_type = 'DesignOutfit' AND tpir.element_id = tdpo.id
        WHERE tpir.project_id = #{projectId}
        AND tpir.result_type = #{resultType}
    </select>

    <!-- 批量查询UserLike及相关设计数据 -->
    <select id="getUserLikeBatch" resultType="java.util.Map">
        SELECT
            ul.id,
            ul.design_item_id,
            ul.design_outfit_id,
            ul.account_id,
            ul.create_date,
            ul.update_date,
            -- TDesignPythonOutfit相关字段
            tdpo.design_url,
            tdpo.design_item_id as outfit_design_item_id
        FROM t_user_like ul
        LEFT JOIN t_design_python_outfit tdpo ON ul.design_outfit_id = tdpo.id
        WHERE ul.id IN
        <foreach collection="relationIds" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <!-- 批量查询ToProductElement数据 -->
    <select id="getToProductElementBatch" resultType="java.util.Map">
        SELECT
            id,
            url,
            project_id,
            create_time,
            update_time
        FROM to_product_element
        WHERE id IN
        <foreach collection="elementIds" item="id" open="(" separator="," close=")">
            #{id}
        </foreach>
    </select>

    <!-- 批量查询design模块的python outfit数据 -->
    <select id="getDesignPythonOutfitBatch" resultType="java.util.Map">
        SELECT
            tpo.id as outfit_id,
            tpo.parent_id,
            tpo.url as outfit_url,
            tpo.is_liked as outfit_is_liked,
            cs.sort,
            cs.id as sort_id
        FROM t_design_python_outfit tpo
        LEFT JOIN collection_sort cs ON cs.relation_id = tpo.id AND cs.relation_type = 'DesignPythonOutfit'
        WHERE tpo.parent_id IN
        <foreach collection="outfitIds" item="outfitId" open="(" close=")" separator=",">
            #{outfitId}
        </foreach>
        ORDER BY tpo.parent_id, cs.sort
    </select>

    <!-- 批量查询ToProductImageRecord数据 -->
    <select id="getToProductImageRecordBatch" resultType="java.util.Map">
        SELECT
            id as record_id,
            prompt
        FROM to_product_image_record
        WHERE id IN
        <foreach collection="recordIds" item="recordId" open="(" close=")" separator=",">
            #{recordId}
        </foreach>
    </select>

    <!-- 批量查询CollectionElementRelModel -->
    <select id="getCollectionElementRelModelBatch" resultType="com.ai.da.mapper.primary.entity.CollectionElementRelModel">
        SELECT id, collection_element_id, relation_id, relation_type
        FROM collection_element_rel_model
        WHERE collection_element_id IN
        <foreach collection="collectionElementIds" item="collectionElementId" open="(" separator="," close=")">
            #{collectionElementId}
        </foreach>
    </select>
</mapper>
