<?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.zsElectric.boot.business.mapper.ThirdPartyStationInfoMapper">

    <resultMap id="StationInfoResultMap" type="com.zsElectric.boot.business.model.vo.ThirdPartyStationInfoVO">
        <id property="id" column="id"/>
        <result property="stationId" column="station_id"/>
        <result property="stationName" column="station_name"/>
        <result property="areaCode" column="area_code"/>
        <result property="equipmentOwnerId" column="equipment_owner_id"/>
        <result property="address" column="address"/>
        <result property="construction" column="construction"/>
        <result property="equipmentCount" column="equipment_count"/>
        <result property="stationStatus" column="station_status"/>
        <result property="serviceTel" column="service_tel"/>
        <result property="stationType" column="station_type"/>
        <result property="stationLng" column="station_lng"/>
        <result property="stationLat" column="station_lat"/>
        <result property="siteGuide" column="site_guide"/>
        <result property="stationTips" column="station_tips"/>
        <result property="salesType" column="sales_type"/>
        <result property="firmId" column="firm_id"/>
        <result property="thirdPartyId" column="third_party_id"/>
    </resultMap>

    <select id="selectStationInfoPage" resultMap="StationInfoResultMap">
        SELECT
            tpsi.id,
            tpsi.station_id,
            tpsi.station_name,
            tpsi.area_code,
            tpsi.equipment_owner_id,
            tpsi.address,
            tpsi.construction,
            COUNT(DISTINCT tpei.id) AS equipment_count,
            tpsi.station_status,
            tpsi.service_tel,
            tpsi.station_type,
            tpsi.station_lng,
            tpsi.station_lat,
            tpsi.site_guide
        FROM third_party_station_info tpsi
        LEFT JOIN third_party_equipment_info tpei ON tpsi.station_id = tpei.station_id AND tpei.is_deleted = 0
        WHERE tpsi.is_deleted = 0
        <if test="query.stationId != null and query.stationId != ''">
            AND tpsi.station_id = #{query.stationId}
        </if>
        <if test="query.stationName != null and query.stationName != ''">
            AND tpsi.station_name LIKE CONCAT('%', #{query.stationName}, '%')
        </if>
        <if test="query.areaCode != null and query.areaCode != ''">
            AND tpsi.area_code = #{query.areaCode}
        </if>
        <if test="query.equipmentOwnerId != null and query.equipmentOwnerId != ''">
            AND tpsi.equipment_owner_id = #{query.equipmentOwnerId}
        </if>
        <if test="query.stationStatus != null and query.stationStatus != 0">
            AND tpsi.station_status = #{query.stationStatus}
        </if>
        <if test="query.stationType != null and query.stationType != 0">
            AND tpsi.station_type = #{query.stationType}
        </if>
        <if test="query.serviceTel != null and query.serviceTel != ''">
            AND tpsi.service_tel = #{query.serviceTel}
        </if>
        GROUP BY tpsi.id
        ORDER BY tpsi.update_time DESC
    </select>

    <select id="selectStationInfoPageByEquipment" resultMap="StationInfoResultMap">
        SELECT
            tpsi.id,
            tpsi.station_id,
            tpsi.station_name,
            tpsi.area_code,
            tpsi.equipment_owner_id,
            tpsi.address,
            tpsi.construction,
            COUNT(DISTINCT tpei.id) AS equipment_count,
            tpsi.station_status,
            tpsi.service_tel,
            tpsi.station_type,
            tpsi.station_lng,
            tpsi.station_lat,
            tpsi.site_guide,
            tpsi.station_tips,
            CASE
                WHEN pf.firm_id IS NOT NULL THEN 1
                WHEN pf.third_party_id IS NOT NULL THEN 2
                ELSE 0
            END AS sales_type,
            pf.firm_id,
            pf.third_party_id
        FROM third_party_station_info tpsi
        LEFT JOIN third_party_equipment_info tpei ON tpsi.station_id = tpei.station_id AND tpei.is_deleted = 0
        INNER JOIN (
            SELECT p1.station_info_id, p1.firm_id, p1.third_party_id
            FROM c_policy_fee p1
            INNER JOIN (
                SELECT station_info_id, 
                    IFNULL(firm_id, 0) AS grp_firm_id,
                    IFNULL(third_party_id, 0) AS grp_third_party_id,
                    MIN(id) AS min_id
                FROM c_policy_fee
                WHERE is_deleted = 0
                GROUP BY station_info_id, IFNULL(firm_id, 0), IFNULL(third_party_id, 0)
            ) p2 ON p1.id = p2.min_id
            WHERE p1.is_deleted = 0
        ) pf ON pf.station_info_id = tpsi.id
        WHERE tpsi.is_deleted = 0
        <if test="query.stationId != null and query.stationId != ''">
            AND tpsi.station_id = #{query.stationId}
        </if>
        <if test="query.stationName != null and query.stationName != ''">
            AND tpsi.station_name LIKE CONCAT('%', #{query.stationName}, '%')
        </if>
        <if test="query.areaCode != null and query.areaCode != ''">
            AND tpsi.area_code = #{query.areaCode}
        </if>
        <if test="query.equipmentOwnerId != null and query.equipmentOwnerId != ''">
            AND tpsi.equipment_owner_id = #{query.equipmentOwnerId}
        </if>
        <if test="query.stationStatus != null and query.stationStatus != 0">
            AND tpsi.station_status = #{query.stationStatus}
        </if>
        <if test="query.stationType != null and query.stationType != 0">
            AND tpsi.station_type = #{query.stationType}
        </if>
        <if test="query.serviceTel != null and query.serviceTel != ''">
            AND tpsi.service_tel = #{query.serviceTel}
        </if>
        <if test="query.salesType != null and query.salesType == 0">
            AND pf.firm_id IS NULL AND pf.third_party_id IS NULL
        </if>
        <if test="query.salesType != null and query.salesType == 1">
            AND pf.firm_id IS NOT NULL
        </if>
        <if test="query.salesType != null and query.salesType == 2">
            AND pf.third_party_id IS NOT NULL AND pf.firm_id IS NULL
        </if>
        GROUP BY tpsi.id, pf.firm_id, pf.third_party_id
        ORDER BY tpsi.update_time DESC
    </select>

    <!-- 小程序首页站点信息查询结果映射 -->
    <resultMap id="AppletStationInfoResultMap" type="com.zsElectric.boot.business.model.vo.StationInfoVO">
        <result property="stationId" column="station_info_id"/>
        <result property="stationName" column="station_name"/>
        <result property="tips" column="station_tips"/>
        <result property="distance" column="distance"/>
        <result property="fastCharging" column="fast_charging"/>
        <result property="slowCharging" column="slow_charging"/>
        <result property="peakValue" column="peak_value"/>
        <result property="peakTime" column="peak_time"/>
        <result property="periodFlag" column="period_flag"/>
        <result property="thirdPartPrice" column="third_part_price"/>
        <result property="platformPrice" column="platform_price"/>
        <result property="enterprisePrice" column="enterprise_price"/>
    </resultMap>

    <!-- 小程序首页分页查询站点信息 -->
    <select id="selectAppletStationInfoPage" resultMap="AppletStationInfoResultMap">
        SELECT
            tpsi.id AS station_info_id,
            tpsi.station_name,
            tpsi.station_tips,
            <!-- 计算距离(km) -->
            <if test="query.longitude != null and query.latitude != null">
                ROUND(
                    6371 * ACOS(
                        COS(RADIANS(#{query.latitude})) * COS(RADIANS(tpsi.station_lat))
                        * COS(RADIANS(tpsi.station_lng) - RADIANS(#{query.longitude}))
                        + SIN(RADIANS(#{query.latitude})) * SIN(RADIANS(tpsi.station_lat))
                    ), 2
                ) AS distance,
            </if>
            <if test="query.longitude == null or query.latitude == null">
                NULL AS distance,
            </if>
            <!-- 快充统计(空闲/总数) -->
            CONCAT(
                COUNT(tpci.id),
                '/',
                COUNT(tpci.id)
            ) AS fast_charging,
            <!-- 慢充统计(直接0/0) -->
            '0/0' AS slow_charging,
            <!-- 峰值(根据当前时段获取) -->
            (
                SELECT
                    CASE tppi.period_flag
                        WHEN 1 THEN '尖'
                        WHEN 2 THEN '峰'
                        WHEN 3 THEN '平'
                        WHEN 4 THEN '谷'
                        ELSE ''
                    END
                FROM third_party_policy_info tppi
                INNER JOIN third_party_equipment_price_policy tpepp ON tppi.price_policy_id = tpepp.id AND tpepp.is_deleted = 0
                INNER JOIN third_party_connector_info tpci2 ON tpepp.connector_id = tpci2.connector_id AND tpci2.is_deleted = 0
                WHERE tpci2.station_id = tpsi.station_id
                    AND tppi.is_deleted = 0
                    AND tppi.start_time &lt;= #{currentTime}
                ORDER BY tppi.start_time DESC
                LIMIT 1
            ) AS peak_value,
            <!-- 当前时段时间，格式: HH:mm - HH:mm -->
            (
                SELECT CONCAT(
                    CONCAT(SUBSTRING(tppi.start_time, 1, 2), ':', SUBSTRING(tppi.start_time, 3, 2)),
                    ' - ',
                    IFNULL(
                        (
                            SELECT CONCAT(SUBSTRING(next_tppi.start_time, 1, 2), ':', SUBSTRING(next_tppi.start_time, 3, 2))
                            FROM third_party_policy_info next_tppi
                            WHERE next_tppi.price_policy_id = tppi.price_policy_id
                                AND next_tppi.is_deleted = 0
                                AND next_tppi.start_time > tppi.start_time
                            ORDER BY next_tppi.start_time ASC
                            LIMIT 1
                        ),
                        '24:00'
                    )
                )
                FROM third_party_policy_info tppi
                INNER JOIN third_party_equipment_price_policy tpepp ON tppi.price_policy_id = tpepp.id AND tpepp.is_deleted = 0
                INNER JOIN third_party_connector_info tpci5 ON tpepp.connector_id = tpci5.connector_id AND tpci5.is_deleted = 0
                WHERE tpci5.station_id = tpsi.station_id
                    AND tppi.is_deleted = 0
                    AND tppi.start_time &lt;= #{currentTime}
                ORDER BY tppi.start_time DESC
                LIMIT 1
            ) AS peak_time,
            <!-- 时段标志(1-尖,2-峰,3-平,4-谷) -->
            (
                SELECT tppi.period_flag
                FROM third_party_policy_info tppi
                INNER JOIN third_party_equipment_price_policy tpepp ON tppi.price_policy_id = tpepp.id AND tpepp.is_deleted = 0
                INNER JOIN third_party_connector_info tpci7 ON tpepp.connector_id = tpci7.connector_id AND tpci7.is_deleted = 0
                WHERE tpci7.station_id = tpsi.station_id
                    AND tppi.is_deleted = 0
                    AND tppi.start_time &lt;= #{currentTime}
                ORDER BY tppi.start_time DESC
                LIMIT 1
            ) AS period_flag,
            <!-- 三方价/原价(电费+服务费) -->
            (
                SELECT
                    ROUND(IFNULL(tppi.elec_price, 0) + IFNULL(tppi.service_price, 0), 4)
                FROM third_party_policy_info tppi
                INNER JOIN third_party_equipment_price_policy tpepp ON tppi.price_policy_id = tpepp.id AND tpepp.is_deleted = 0
                INNER JOIN third_party_connector_info tpci6 ON tpepp.connector_id = tpci6.connector_id AND tpci6.is_deleted = 0
                WHERE tpci6.station_id = tpsi.station_id
                    AND tppi.is_deleted = 0
                    AND tppi.start_time &lt;= #{currentTime}
                ORDER BY tppi.start_time DESC
                LIMIT 1
            ) AS third_part_price,
            <!-- 平台价(综合销售费) -->
            (
                SELECT
                    ROUND(IFNULL(pf.comp_sales_fee, 0), 4)
                FROM c_policy_fee pf
                INNER JOIN third_party_policy_info tppi ON pf.period_flag = tppi.period_flag
                INNER JOIN third_party_equipment_price_policy tpepp ON tppi.price_policy_id = tpepp.id AND tpepp.is_deleted = 0
                INNER JOIN third_party_connector_info tpci3 ON tpepp.connector_id = tpci3.connector_id AND tpci3.is_deleted = 0
                WHERE tpci3.station_id = tpsi.station_id
                    AND pf.station_info_id = tpsi.id
                    AND pf.sales_type = 0
                    AND pf.is_deleted = 0
                    AND tppi.is_deleted = 0
                    AND tppi.start_time &lt;= #{currentTime}
                ORDER BY tppi.start_time DESC
                LIMIT 1
            ) AS platform_price,
            <!-- 企业价(如果有企业ID) -->
            <if test="firmId != null">
            (
                SELECT
                    ROUND(IFNULL(pf.comp_sales_fee, 0), 4)
                FROM c_policy_fee pf
                INNER JOIN third_party_policy_info tppi ON pf.period_flag = tppi.period_flag
                INNER JOIN third_party_equipment_price_policy tpepp ON tppi.price_policy_id = tpepp.id AND tpepp.is_deleted = 0
                INNER JOIN third_party_connector_info tpci4 ON tpepp.connector_id = tpci4.connector_id AND tpci4.is_deleted = 0
                WHERE tpci4.station_id = tpsi.station_id
                    AND pf.station_info_id = tpsi.id
                    AND pf.sales_type = 1
                    AND pf.firm_id = #{firmId}
                    AND pf.is_deleted = 0
                    AND tppi.is_deleted = 0
                    AND tppi.start_time &lt;= #{currentTime}
                ORDER BY tppi.start_time DESC
                LIMIT 1
            ) AS enterprise_price
            </if>
            <if test="firmId == null">
            NULL AS enterprise_price
            </if>
        FROM third_party_station_info tpsi
        LEFT JOIN third_party_connector_info tpci ON tpsi.station_id = tpci.station_id AND tpci.is_deleted = 0
        WHERE tpsi.is_deleted = 0
            <!-- 确保存在平台价配置 -->
            AND EXISTS (
                SELECT 1 FROM c_policy_fee pf_check
                WHERE pf_check.station_info_id = tpsi.id
                    AND pf_check.sales_type = 0
                    AND pf_check.is_deleted = 0
                    AND (pf_check.op_fee IS NOT NULL OR pf_check.comp_sales_fee IS NOT NULL)
            )
        GROUP BY tpsi.id
        <if test="query.sortType != null">
            <if test="query.sortType == 1 and query.longitude != null and query.latitude != null">
                ORDER BY distance ASC
            </if>
            <if test="query.sortType == 3">
                ORDER BY platform_price ASC
            </if>
        </if>
        <if test="query.sortType == null or (query.sortType == 1 and (query.longitude == null or query.latitude == null))">
            ORDER BY tpsi.update_time DESC
        </if>
    </select>

    <!-- 小程序首页地图模式站点信息查询结果映射 -->
    <resultMap id="AppletStationInfoMapResultMap" type="com.zsElectric.boot.business.model.vo.StationInfoMapVO">
        <result property="stationId" column="station_info_id"/>
        <result property="stationName" column="station_name"/>
        <result property="tips" column="station_tips"/>
        <result property="distance" column="distance"/>
        <result property="fastCharging" column="fast_charging"/>
        <result property="slowCharging" column="slow_charging"/>
        <result property="peakValue" column="peak_value"/>
        <result property="peakTime" column="peak_time"/>
        <result property="platformPrice" column="platform_price"/>
        <result property="enterprisePrice" column="enterprise_price"/>
        <result property="parkingFee" column="parking_fee"/>
        <result property="address" column="address"/>
        <result property="longitude" column="station_lng"/>
        <result property="latitude" column="station_lat"/>
    </resultMap>

    <!-- 小程序首页地图模式查询站点列表（按距离排序） -->
    <select id="selectStationMapList" resultMap="AppletStationInfoMapResultMap">
        SELECT
            tpsi.id AS station_info_id,
            tpsi.station_name,
            tpsi.station_tips,
            tpsi.address,
            tpsi.station_lng,
            tpsi.station_lat,
            CAST(tpsi.park_fee AS DECIMAL(10,2)) AS parking_fee,
            <!-- 计算距离(km) -->
            ROUND(
                6371 * ACOS(
                    COS(RADIANS(#{latitude})) * COS(RADIANS(tpsi.station_lat))
                    * COS(RADIANS(tpsi.station_lng) - RADIANS(#{longitude}))
                    + SIN(RADIANS(#{latitude})) * SIN(RADIANS(tpsi.station_lat))
                ), 2
            ) AS distance,
            <!-- 快充统计(空闲/总数) -->
            CONCAT(
                COUNT(tpci.id),
                '/',
                COUNT(tpci.id)
            ) AS fast_charging,
            <!-- 慢充统计(直接0/0) -->
            '0/0' AS slow_charging,
            <!-- 峰值(根据当前时段获取) -->
            (
                SELECT
                    CASE tppi.period_flag
                        WHEN 1 THEN '尖'
                        WHEN 2 THEN '峰'
                        WHEN 3 THEN '平'
                        WHEN 4 THEN '谷'
                        ELSE ''
                    END
                FROM third_party_policy_info tppi
                INNER JOIN third_party_equipment_price_policy tpepp ON tppi.price_policy_id = tpepp.id AND tpepp.is_deleted = 0
                INNER JOIN third_party_connector_info tpci2 ON tpepp.connector_id = tpci2.connector_id AND tpci2.is_deleted = 0
                WHERE tpci2.station_id = tpsi.station_id
                    AND tppi.is_deleted = 0
                    AND tppi.start_time &lt;= #{currentTime}
                ORDER BY tppi.start_time DESC
                LIMIT 1
            ) AS peak_value,
            <!-- 当前时段时间，格式: HH:mm - HH:mm -->
            (
                SELECT CONCAT(
                    CONCAT(SUBSTRING(tppi.start_time, 1, 2), ':', SUBSTRING(tppi.start_time, 3, 2)),
                    ' - ',
                    IFNULL(
                        (
                            SELECT CONCAT(SUBSTRING(next_tppi.start_time, 1, 2), ':', SUBSTRING(next_tppi.start_time, 3, 2))
                            FROM third_party_policy_info next_tppi
                            WHERE next_tppi.price_policy_id = tppi.price_policy_id
                                AND next_tppi.is_deleted = 0
                                AND next_tppi.start_time > tppi.start_time
                            ORDER BY next_tppi.start_time ASC
                            LIMIT 1
                        ),
                        '24:00'
                    )
                )
                FROM third_party_policy_info tppi
                INNER JOIN third_party_equipment_price_policy tpepp ON tppi.price_policy_id = tpepp.id AND tpepp.is_deleted = 0
                INNER JOIN third_party_connector_info tpci5 ON tpepp.connector_id = tpci5.connector_id AND tpci5.is_deleted = 0
                WHERE tpci5.station_id = tpsi.station_id
                    AND tppi.is_deleted = 0
                    AND tppi.start_time &lt;= #{currentTime}
                ORDER BY tppi.start_time DESC
                LIMIT 1
            ) AS peak_time,
            <!-- 平台价(综合销售费) -->
            (
                SELECT
                    ROUND(IFNULL(pf.comp_sales_fee, 0), 4)
                FROM c_policy_fee pf
                INNER JOIN third_party_policy_info tppi ON pf.period_flag = tppi.period_flag
                INNER JOIN third_party_equipment_price_policy tpepp ON tppi.price_policy_id = tpepp.id AND tpepp.is_deleted = 0
                INNER JOIN third_party_connector_info tpci3 ON tpepp.connector_id = tpci3.connector_id AND tpci3.is_deleted = 0
                WHERE tpci3.station_id = tpsi.station_id
                    AND pf.station_info_id = tpsi.id
                    AND pf.sales_type = 0
                    AND pf.is_deleted = 0
                    AND tppi.is_deleted = 0
                    AND tppi.start_time &lt;= #{currentTime}
                ORDER BY tppi.start_time DESC
                LIMIT 1
            ) AS platform_price,
            <!-- 企业价(地图模式不需要) -->
            NULL AS enterprise_price
        FROM third_party_station_info tpsi
        LEFT JOIN third_party_connector_info tpci ON tpsi.station_id = tpci.station_id AND tpci.is_deleted = 0
        WHERE tpsi.is_deleted = 0
            <!-- 确保存在平台价配置 -->
            AND EXISTS (
                SELECT 1 FROM c_policy_fee pf_check
                WHERE pf_check.station_info_id = tpsi.id
                    AND pf_check.sales_type = 0
                    AND pf_check.is_deleted = 0
                    AND (pf_check.op_fee IS NOT NULL OR pf_check.comp_sales_fee IS NOT NULL)
            )
        GROUP BY tpsi.id
        ORDER BY distance ASC
    </select>

    <!-- 小程序站点详情查询结果映射 -->
    <resultMap id="AppletStationDetailResultMap" type="com.zsElectric.boot.business.model.vo.AppletStationDetailVO">
        <result property="stationId" column="station_info_id"/>
        <result property="stationName" column="station_name"/>
        <result property="tips" column="station_tips"/>
        <result property="distance" column="distance"/>
        <result property="address" column="address"/>
        <result property="longitude" column="longitude"/>
        <result property="latitude" column="latitude"/>
        <result property="pictures" column="pictures"/>
        <result property="currentPrice" column="current_price"/>
        <result property="currentPeriod" column="current_period"/>
        <result property="enterprisePrice" column="enterprise_price"/>
        <result property="originalPrice" column="original_price"/>
        <result property="thirdPartPrice" column="third_part_price"/>
        <result property="periodFlag" column="period_flag"/>
        <result property="businessHours" column="business_hours"/>
        <result property="serviceProvider" column="service_provider"/>
        <result property="invoiceProvider" column="invoice_provider"/>
        <result property="customerServiceHotline" column="customer_service_hotline"/>
    </resultMap>

    <!-- 小程序查询站点详情 -->
    <select id="selectStationDetail" resultMap="AppletStationDetailResultMap">
        SELECT
            tpsi.id AS station_info_id,
            tpsi.station_name,
            tpsi.station_tips,
            tpsi.address,
            tpsi.station_lng AS longitude,
            tpsi.station_lat AS latitude,
            tpsi.banner_pictures AS pictures,
            tpsi.own_business_hours AS business_hours,
            tpsi.customer_service_hotline,
            '华能贵州盘州市风电有限责任公司' AS service_provider,
            '华能贵州盘州市风电有限责任公司' AS invoice_provider,
            <!-- 计算距离(km) -->
            <if test="longitude != null and latitude != null">
                ROUND(
                    6371 * ACOS(
                        COS(RADIANS(#{latitude})) * COS(RADIANS(tpsi.station_lat))
                        * COS(RADIANS(tpsi.station_lng) - RADIANS(#{longitude}))
                        + SIN(RADIANS(#{latitude})) * SIN(RADIANS(tpsi.station_lat))
                    ), 2
                ) AS distance,
            </if>
            <if test="longitude == null or latitude == null">
                NULL AS distance,
            </if>
            <!-- 当前价(综合销售费) -->
            (
                SELECT
                    ROUND(IFNULL(pf.comp_sales_fee, 0), 4)
                FROM c_policy_fee pf
                INNER JOIN third_party_policy_info tppi ON pf.period_flag = tppi.period_flag
                INNER JOIN third_party_equipment_price_policy tpepp ON tppi.price_policy_id = tpepp.id AND tpepp.is_deleted = 0
                INNER JOIN third_party_connector_info tpci3 ON tpepp.connector_id = tpci3.connector_id AND tpci3.is_deleted = 0
                WHERE tpci3.station_id = tpsi.station_id
                    AND pf.station_info_id = tpsi.id
                    AND pf.sales_type = 0
                    AND pf.is_deleted = 0
                    AND tppi.is_deleted = 0
                    AND tppi.start_time &lt;= #{currentTime}
                ORDER BY tppi.start_time DESC
                LIMIT 1
            ) AS current_price,
            <!-- 当前时段名称 -->
            (
                SELECT
                    CONCAT(
                        CASE tppi.period_flag
                            WHEN 1 THEN '尖'
                            WHEN 2 THEN '峰'
                            WHEN 3 THEN '平'
                            WHEN 4 THEN '谷'
                            ELSE ''
                        END,
                        CONCAT(SUBSTRING(tppi.start_time, 1, 2), ':', SUBSTRING(tppi.start_time, 3, 2)),
                        '-',
                        IFNULL(
                            (
                                SELECT CONCAT(SUBSTRING(next_tppi.start_time, 1, 2), ':', SUBSTRING(next_tppi.start_time, 3, 2))
                                FROM third_party_policy_info next_tppi
                                WHERE next_tppi.price_policy_id = tppi.price_policy_id
                                    AND next_tppi.is_deleted = 0
                                    AND next_tppi.start_time > tppi.start_time
                                ORDER BY next_tppi.start_time ASC
                                LIMIT 1
                            ),
                            '24:00'
                        )
                    )
                FROM third_party_policy_info tppi
                INNER JOIN third_party_equipment_price_policy tpepp ON tppi.price_policy_id = tpepp.id AND tpepp.is_deleted = 0
                INNER JOIN third_party_connector_info tpci2 ON tpepp.connector_id = tpci2.connector_id AND tpci2.is_deleted = 0
                WHERE tpci2.station_id = tpsi.station_id
                    AND tppi.is_deleted = 0
                    AND tppi.start_time &lt;= #{currentTime}
                ORDER BY tppi.start_time DESC
                LIMIT 1
            ) AS current_period,
            <!-- 原价(电费+服务费) -->
            (
                SELECT
                    ROUND(IFNULL(tppi.elec_price, 0) + IFNULL(tppi.service_price, 0), 4)
                FROM third_party_policy_info tppi
                INNER JOIN third_party_equipment_price_policy tpepp ON tppi.price_policy_id = tpepp.id AND tpepp.is_deleted = 0
                INNER JOIN third_party_connector_info tpci4 ON tpepp.connector_id = tpci4.connector_id AND tpci4.is_deleted = 0
                WHERE tpci4.station_id = tpsi.station_id
                    AND tppi.is_deleted = 0
                    AND tppi.start_time &lt;= #{currentTime}
                ORDER BY tppi.start_time DESC
                LIMIT 1
            ) AS original_price,
            <!-- 三方价/原价(电费+服务费) -->
            (
                SELECT
                    ROUND(IFNULL(tppi.elec_price, 0) + IFNULL(tppi.service_price, 0), 4)
                FROM third_party_policy_info tppi
                INNER JOIN third_party_equipment_price_policy tpepp ON tppi.price_policy_id = tpepp.id AND tpepp.is_deleted = 0
                INNER JOIN third_party_connector_info tpci7 ON tpepp.connector_id = tpci7.connector_id AND tpci7.is_deleted = 0
                WHERE tpci7.station_id = tpsi.station_id
                    AND tppi.is_deleted = 0
                    AND tppi.start_time &lt;= #{currentTime}
                ORDER BY tppi.start_time DESC
                LIMIT 1
            ) AS third_part_price,
            <!-- 时段标志(1-尖,2-峰,3-平,4-谷) -->
            (
                SELECT tppi.period_flag
                FROM third_party_policy_info tppi
                INNER JOIN third_party_equipment_price_policy tpepp ON tppi.price_policy_id = tpepp.id AND tpepp.is_deleted = 0
                INNER JOIN third_party_connector_info tpci6 ON tpepp.connector_id = tpci6.connector_id AND tpci6.is_deleted = 0
                WHERE tpci6.station_id = tpsi.station_id
                    AND tppi.is_deleted = 0
                    AND tppi.start_time &lt;= #{currentTime}
                ORDER BY tppi.start_time DESC
                LIMIT 1
            ) AS period_flag,
            <!-- 企业价 -->
            <if test="firmId != null">
            (
                SELECT
                    ROUND(IFNULL(pf.comp_sales_fee, 0), 4)
                FROM c_policy_fee pf
                INNER JOIN third_party_policy_info tppi ON pf.period_flag = tppi.period_flag
                INNER JOIN third_party_equipment_price_policy tpepp ON tppi.price_policy_id = tpepp.id AND tpepp.is_deleted = 0
                INNER JOIN third_party_connector_info tpci5 ON tpepp.connector_id = tpci5.connector_id AND tpci5.is_deleted = 0
                WHERE tpci5.station_id = tpsi.station_id
                    AND pf.station_info_id = tpsi.id
                    AND pf.sales_type = 1
                    AND pf.firm_id = #{firmId}
                    AND pf.is_deleted = 0
                    AND tppi.is_deleted = 0
                    AND tppi.start_time &lt;= #{currentTime}
                ORDER BY tppi.start_time DESC
                LIMIT 1
            ) AS enterprise_price
            </if>
            <if test="firmId == null">
            NULL AS enterprise_price
            </if>
        FROM third_party_station_info tpsi
        WHERE tpsi.is_deleted = 0
            AND tpsi.id = #{stationId}
    </select>

    <!-- 查询用户当前正在充电中的订单实时费用 -->
    <resultMap id="AppletChargingCostResultMap" type="com.zsElectric.boot.business.model.vo.applet.AppChargingCostVO">
        <result property="chargeOrderNo" column="charge_order_no"/>
        <result property="stationName" column="station_name"/>
        <result property="connectorName" column="connector_name"/>
        <result property="orderStatus" column="order_status"/>
        <result property="orderStatusDesc" column="order_status_desc"/>
        <result property="chargingDuration" column="charging_duration"/>
        <result property="chargingDurationDesc" column="charging_duration_desc"/>
        <result property="totalPower" column="total_power"/>
        <result property="elecMoney" column="elec_money"/>
        <result property="serviceMoney" column="service_money"/>
        <result property="totalMoney" column="total_money"/>
        <result property="soc" column="soc"/>
        <result property="current" column="current"/>
        <result property="voltage" column="voltage"/>
        <result property="power" column="power"/>
        <result property="startTime" column="start_time"/>
        <result property="lastUpdateTime" column="last_update_time"/>
        <result property="connectorCode" column="connector_code"/>
    </resultMap>

    <select id="selectCurrentChargingCost" resultMap="AppletChargingCostResultMap">
        SELECT
            coi.charge_order_no,
            tpsi.station_name,
            coi.status,
            tpci.connector_name,
            tcs.start_charge_seq_stat AS order_status,
            CASE tcs.start_charge_seq_stat
                WHEN 1 THEN '启动中'
                WHEN 2 THEN '充电中'
                WHEN 3 THEN '停止中'
                WHEN 4 THEN '已结束'
                WHEN 5 THEN '未知'
                ELSE '未知'
            END AS order_status_desc,
            TIMESTAMPDIFF(SECOND, tcs.start_time, tcs.end_time) AS charging_duration,
            CONCAT(
                LPAD(FLOOR(TIMESTAMPDIFF(SECOND, tcs.start_time, tcs.end_time) / 3600), 2, '0'), ':',
                LPAD(FLOOR(MOD(TIMESTAMPDIFF(SECOND, tcs.start_time, tcs.end_time), 3600) / 60), 2, '0'), ':',
                LPAD(MOD(TIMESTAMPDIFF(SECOND, tcs.start_time, tcs.end_time), 60), 2, '0')
            ) AS charging_duration_desc,
            IFNULL(tcs.total_power, 0) AS total_power,
            IFNULL(tcs.elec_money, 0) AS elec_money,
            IFNULL(tcs.service_money, 0) AS service_money,
            IFNULL(tcs.total_money, 0) AS total_money,
            tcs.charge_details,
            tcs.soc,
            COALESCE(tcs.current_a, tcs.current_b, tcs.current_c) AS `current`,
            COALESCE(tcs.voltage_a, tcs.voltage_b, tcs.voltage_c) AS voltage,
            ROUND(
                COALESCE(tcs.voltage_a, tcs.voltage_b, tcs.voltage_c) *
                COALESCE(tcs.current_a, tcs.current_b, tcs.current_c) / 1000,
                2
            ) AS power,
            DATE_FORMAT(tcs.start_time, '%Y-%m-%d %H:%i:%s') AS start_time,
            DATE_FORMAT(tcs.update_time, '%Y-%m-%d %H:%i:%s') AS last_update_time,
            tpci.connector_id AS connector_code
        FROM c_charge_order_info coi
        INNER JOIN third_party_charge_status tcs
            ON coi.start_charge_seq = tcs.start_charge_seq
        INNER JOIN third_party_connector_info tpci
            ON tcs.connector_id = tpci.connector_id
            AND tpci.is_deleted = 0
        INNER JOIN third_party_station_info tpsi
            ON tpci.station_id = tpsi.station_id
            AND tpsi.is_deleted = 0
        WHERE coi.user_id = #{userId}
            AND coi.charge_order_no = #{orderNo}
            AND coi.is_deleted = 0
        ORDER BY tcs.update_time DESC
        LIMIT 1
    </select>

    <!-- 搜索充电站结果映射 -->
    <resultMap id="AppletStationSearchResultMap" type="com.zsElectric.boot.business.model.vo.applet.AppStationSearchVO">
        <result property="stationId" column="station_id"/>
        <result property="stationName" column="station_name"/>
        <result property="address" column="address"/>
        <result property="tips" column="station_tips"/>
        <result property="distance" column="distance"/>
        <result property="fastIdleCount" column="fast_idle_count"/>
        <result property="fastTotalCount" column="fast_total_count"/>
        <result property="slowIdleCount" column="slow_idle_count"/>
        <result property="slowTotalCount" column="slow_total_count"/>
    </resultMap>

    <!-- 搜索充电站（根据站点名称和地址模糊查询） -->
    <select id="searchStations" resultMap="AppletStationSearchResultMap">
        SELECT
            tpsi.id AS station_id,
            tpsi.station_name,
            tpsi.address,
            tpsi.station_tips,
            <if test="longitude != null and latitude != null">
            ROUND(
                ST_Distance_Sphere(
                    point(tpsi.station_lng, tpsi.station_lat),
                    point(#{longitude}, #{latitude})
                ) / 1000,
                2
            ) AS distance,
            </if>
            <if test="longitude == null or latitude == null">
            NULL AS distance,
            </if>
            <!-- 快充空闲数 -->
            IFNULL((
                SELECT COUNT(1)
                FROM third_party_connector_info tpci
                WHERE tpci.station_id = tpsi.station_id
                    AND tpci.connector_type = 1
                    AND tpci.status = 1
                    AND tpci.is_deleted = 0
            ), 0) AS fast_idle_count,
            <!-- 快充总数 -->
            IFNULL((
                SELECT COUNT(1)
                FROM third_party_connector_info tpci
                WHERE tpci.station_id = tpsi.station_id
                    AND tpci.connector_type = 1
                    AND tpci.is_deleted = 0
            ), 0) AS fast_total_count,
            <!-- 慢充空闲数 -->
            IFNULL((
                SELECT COUNT(1)
                FROM third_party_connector_info tpci
                WHERE tpci.station_id = tpsi.station_id
                    AND tpci.connector_type = 2
                    AND tpci.status = 1
                    AND tpci.is_deleted = 0
            ), 0) AS slow_idle_count,
            <!-- 慢充总数 -->
            IFNULL((
                SELECT COUNT(1)
                FROM third_party_connector_info tpci
                WHERE tpci.station_id = tpsi.station_id
                    AND tpci.connector_type = 2
                    AND tpci.is_deleted = 0
            ), 0) AS slow_total_count
        FROM third_party_station_info tpsi
        WHERE tpsi.is_deleted = 0
            AND (
                tpsi.station_name LIKE CONCAT('%', #{keyword}, '%')
                OR tpsi.address LIKE CONCAT('%', #{keyword}, '%')
            )
        <if test="longitude != null and latitude != null">
        ORDER BY distance ASC
        </if>
        <if test="longitude == null or latitude == null">
        ORDER BY tpsi.id DESC
        </if>
        LIMIT 50
    </select>
    <select id="selectChargingCost" resultType="com.zsElectric.boot.business.model.vo.applet.AppChargingCostVO">
        SELECT
            coi.charge_order_no,
            tpsi.station_name,
            coi.status,
            tpci.connector_name,
            tcs.start_charge_seq_stat AS order_status,
            CASE tcs.start_charge_seq_stat
                WHEN 1 THEN '启动中'
                WHEN 2 THEN '充电中'
                WHEN 3 THEN '停止中'
                WHEN 4 THEN '已结束'
                WHEN 5 THEN '未知'
                ELSE '未知'
                END AS order_status_desc,
            TIMESTAMPDIFF(SECOND, tcs.start_time, tcs.end_time) AS charging_duration,
            CONCAT(
                    LPAD(FLOOR(TIMESTAMPDIFF(SECOND, tcs.start_time, tcs.end_time) / 3600), 2, '0'), ':',
                    LPAD(FLOOR(MOD(TIMESTAMPDIFF(SECOND, tcs.start_time, tcs.end_time), 3600) / 60), 2, '0'), ':',
                    LPAD(MOD(TIMESTAMPDIFF(SECOND, tcs.start_time, tcs.end_time), 60), 2, '0')
            ) AS charging_duration_desc,
            IFNULL(tcs.total_power, 0) AS total_power,
            IFNULL(tcs.elec_money, 0) AS elec_money,
            IFNULL(tcs.service_money, 0) AS service_money,
            IFNULL(tcs.total_money, 0) AS total_money,
            tcs.charge_details,
            tcs.soc,
            COALESCE(tcs.current_a, tcs.current_b, tcs.current_c) AS `current`,
            COALESCE(tcs.voltage_a, tcs.voltage_b, tcs.voltage_c) AS voltage,
            ROUND(
                    COALESCE(tcs.voltage_a, tcs.voltage_b, tcs.voltage_c) *
                    COALESCE(tcs.current_a, tcs.current_b, tcs.current_c) / 1000,
                    2
            ) AS power,
            DATE_FORMAT(tcs.start_time, '%Y-%m-%d %H:%i:%s') AS start_time,
            DATE_FORMAT(tcs.update_time, '%Y-%m-%d %H:%i:%s') AS last_update_time,
            tpci.connector_id AS connector_code
        FROM c_charge_order_info coi
                 INNER JOIN third_party_charge_status tcs
                            ON coi.start_charge_seq = tcs.start_charge_seq
                 INNER JOIN third_party_connector_info tpci
                            ON tcs.connector_id = tpci.connector_id
                                AND tpci.is_deleted = 0
                 INNER JOIN third_party_station_info tpsi
                            ON tpci.station_id = tpsi.station_id
                                AND tpsi.is_deleted = 0
        WHERE
            coi.charge_order_no = #{orderNo}
            AND coi.is_deleted = 0
        ORDER BY tcs.update_time DESC
            LIMIT 1
    </select>

</mapper>
