<?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.ylx.project.mapper.ProjectMapper">

    <!-- 结果集映射 -->
    <resultMap id="OptionVOMap" type="com.ylx.massage.domain.vo.ProductOptionVO">
        <result column="id" property="id"/>
        <result column="title" property="title"/>
        <result column="productType" property="productType"/>
    </resultMap>

    <!-- 核心 SQL：UNION ALL 分页查询 -->
    <select id="selectOptionUnionPage" resultMap="OptionVOMap">
        SELECT * FROM (
        <!-- 第一部分：积分商品 -->
        SELECT
        CAST(id AS CHAR) as id,
        name as title,
        1 as productType
        FROM product
        WHERE status = 1 AND deleted = 0
        <if test="dto.title != null and dto.title != ''">
            AND name LIKE CONCAT('%', #{dto.title}, '%')
        </if>
        UNION ALL
        <!-- 第二部分：项目商品 -->
        SELECT
        id,
        title as title,
        0 as productType
        FROM project
        WHERE is_delete = 0
        <if test="dto.title != null and dto.title != ''">
            AND title LIKE CONCAT('%', #{dto.title}, '%')
        </if>
        ) as temp_table
    </select>

    <!-- 结果集映射保持不变 -->
    <resultMap id="ServiceOptionsVOMap" type="com.ylx.massage.domain.vo.ProductServiceOptionVO">
        <result column="id" property="id"/>
        <result column="title" property="title"/>
        <result column="productType" property="productType"/>
        <result column="serviceAreaCode" property="areaCode"/>
        <result column="serviceAreaName" property="areaName"/>
        <result column="price" property="price"/>
        <result column="merchantName" property="merchantName"/>
        <result column="merchantId" property="merchantId"/>
    </resultMap>

    <resultMap id="ProjectDetailVoMap" type="com.ylx.project.domain.vo.ProjectDetailVo">
        <id column="id" property="id"/>
        <result column="title" property="title"/>
        <result column="category_id" property="categoryId"/>
        <result column="category_name" property="categoryName"/>
        <result column="cover" property="cover"/>
        <result column="price" property="price"/>
        <result column="price_min" property="priceMin"/>
        <result column="price_max" property="priceMax"/>
        <result column="merchant_share_ratio" property="merchantShareRatio"/>
        <result column="standard_duration" property="standardDuration"/>
        <result column="unit_type" property="unitType"/>
        <result column="status" property="status"/>
        <result column="is_recommended" property="isRecommended"/>
        <result column="is_price_custom" property="isPriceCustom"/>
        <result column="highlight_ids" property="highlightIds"/>
        <result column="target_audience" property="targetAudience"/>
        <result column="detail" property="detail"/>
        <result column="create_time" property="createTime"/>
        <result column="update_time" property="updateTime"/>
    </resultMap>

    <select id="selectProjectDetailById" resultMap="ProjectDetailVoMap">
        SELECT
            p.id,
            p.title AS title,
            p.category_id,
            sc.name AS category_name,
            p.cover AS cover,
            p.price AS price,
            p.price_min,
            p.price_max,
            p.standard_duration,
            p.unit_type,
            p.merchant_share_ratio,
            p.status,
            p.is_recommended,
            p.is_price_custom,
            p.highlight_ids,
            p.target_audience,
            p.detail,
            p.create_time,
            p.update_time
        FROM project p
        LEFT JOIN service_category sc ON sc.id = p.category_id AND sc.is_delete = 0
        WHERE p.id = #{id}
          AND p.is_delete = 0
    </select>

    <select id="selectServiceOptionsPage" resultMap="ServiceOptionsVOMap">
        SELECT
        p.id AS id,
        ANY_VALUE(p.title) AS title,
        CASE WHEN p.type = '1' THEN 0 ELSE 0 END AS productType,
        ANY_VALUE(p.price) AS price,
        '广誉源' AS merchantName,
        '10000' AS merchantId,
        ANY_VALUE(
        CASE
        WHEN #{dto.areaCode} IS NOT NULL THEN (SELECT name FROM area WHERE code = #{dto.areaCode})
        ELSE agg.area_names
        END
        ) AS serviceAreaName,
        ANY_VALUE(
        CASE
        WHEN #{dto.areaCode} IS NOT NULL THEN #{dto.areaCode}
        ELSE agg.area_codes
        END
        ) AS serviceAreaCode
        FROM project p
        INNER JOIN (
        SELECT
        j_inner.c_bh_list,
        GROUP_CONCAT(DISTINCT a.code SEPARATOR ',') AS area_codes,  -- code拼接
        GROUP_CONCAT(DISTINCT a.name SEPARATOR ',') AS area_names  -- 名称拼接
        FROM t_js j_inner
        JOIN area a ON FIND_IN_SET(a.code, j_inner.service_area_codes)
        WHERE j_inner.is_delete = 0
        AND j_inner.n_tong = 1
        GROUP BY j_inner.c_bh_list
        ) agg ON FIND_IN_SET(p.id, agg.c_bh_list)
        WHERE
        p.is_delete = 0
        <if test="dto.productType != null and dto.productType == 0">
            AND p.type = 1
        </if>
        <if test="dto.productType != null and dto.productType == 1">
            AND 1 = 0
        </if>
        <if test="dto.productType != null and dto.productType != 0 and dto.productType != 1">
            AND p.type = #{dto.productType}
        </if>
        <if test="dto.productType == null">
            AND p.type = 1
        </if>
        <if test="dto.title != null and dto.title != ''">
            AND p.title LIKE CONCAT('%', #{dto.title}, '%')
        </if>
        <if test="dto.ids != null and dto.ids.size() > 0">
            AND p.id IN
            <foreach collection="dto.ids" item="id" open="(" separator="," close=")">
                #{id}
            </foreach>
        </if>
        AND EXISTS (
        SELECT 1
        FROM t_js j
        WHERE j.is_delete = 0
        AND j.n_tong = 1
        AND (#{dto.areaCode} IS NULL OR FIND_IN_SET(#{dto.areaCode}, j.service_area_codes))
        AND FIND_IN_SET(p.id, j.c_bh_list)
        )
        GROUP BY
        p.id
        ORDER BY
        p.create_time DESC
    </select>


</mapper>
