<?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.massage.mapper.TXiangmuMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.ylx.massage.domain.TXiangmu">
        <id column="c_id" property="cId" />
        <result column="c_title" property="cTitle" />
        <result column="c_cover" property="cCover" />
        <result column="d_yuan_price" property="dYuanPrice" />
        <result column="d_price" property="dPrice" />
        <result column="n_minute" property="nMinute" />
        <result column="c_ld_list" property="cLdList" />
        <result column="n_sex_limit" property="nSexLimit" />
        <result column="c_syrq" property="cSyrq" />
        <result column="c_detail" property="cDetail" />
        <result column="n_sale_number" property="nSaleNumber" />
        <result column="dt_create_time" property="dtCreateTime" />
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        c_id, c_title, c_cover, d_yuan_price, d_price, n_minute, c_ld_list, n_sex_limit, c_syrq, c_detail, n_sale_number, dt_create_time
    </sql>

    <!-- 结果集映射 -->
    <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
        c_id as id,
        c_title as title,
        0 as productType
        FROM t_xiangmu
        WHERE is_delete = 0
        <if test="dto.title != null and dto.title != ''">
            AND c_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>

    <select id="selectServiceOptionsPage" resultMap="ServiceOptionsVOMap">
        SELECT
            p.c_id AS id,
            ANY_VALUE(p.c_title) AS title,
            CASE WHEN p.project_type = '1' THEN 0 ELSE 0 END AS productType,
            ANY_VALUE(p.d_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 t_xiangmu 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.c_id, agg.c_bh_list)
        WHERE
            p.is_delete = 0
            <if test="dto.productType != null and dto.productType == 0">
                AND p.project_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.project_type = #{dto.productType}
            </if>
            <if test="dto.productType == null">
                AND p.project_type = 1
            </if>
            <if test="dto.title != null and dto.title != ''">
                AND p.c_title LIKE CONCAT('%', #{dto.title}, '%')
            </if>
            <if test="dto.ids != null and dto.ids.size() > 0">
                AND p.c_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.c_id, j.c_bh_list)
        )
        GROUP BY
            p.c_id
        ORDER BY
            p.create_time DESC
    </select>

</mapper>
