<?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.charging.mapper.ThirdPartyConnectorInfoMapper">

    <!-- 充电接口详情结果映射 -->
    <resultMap id="ConnectorDetailResultMap" type="com.zsElectric.boot.business.model.vo.applet.AppletConnectorDetailVO">
        <result property="connectorId" column="connector_id"/>
        <result property="connectorCode" column="connector_code"/>
        <result property="connectorName" column="connector_name"/>
        <result property="stationId" column="station_id"/>
        <result property="stationName" column="station_name"/>
        <result property="stationAddress" column="station_address"/>
        <result property="equipmentId" column="equipment_id"/>
        <result property="equipmentCode" column="equipment_code"/>
        <result property="equipmentName" column="equipment_name"/>
        <result property="equipmentType" column="equipment_type"/>
        <result property="equipmentTypeName" column="equipment_type_name"/>
        <result property="parkNo" column="park_no"/>
        <result property="status" column="status"/>
        <result property="statusName" column="status_name"/>
        <result property="connectorType" column="connector_type"/>
        <result property="connectorTypeName" column="connector_type_name"/>
        <result property="voltageUpperLimits" column="voltage_upper_limits"/>
        <result property="voltageLowerLimits" column="voltage_lower_limits"/>
        <result property="current" column="current"/>
        <result property="power" column="power"/>
        <result property="nationalStandard" column="national_standard"/>
        <result property="nationalStandardName" column="national_standard_name"/>
        <result property="currentPrice" column="current_price"/>
        <result property="enterprisePrice" column="enterprise_price"/>
        <result property="isFirmUser" column="is_firm_user"/>
        <result property="currentPeriodDesc" column="current_period_desc"/>
        <result property="parkingTips" column="parking_tips"/>
        <result property="availableBalance" column="available_balance"/>
        <result property="newUserDiscount" column="new_user_discount"/>
        <result property="lastUpdateTime" column="last_update_time"/>
    </resultMap>

    <!-- 获取充电接口详情（一次SQL查询获取所有信息） -->
    <select id="selectConnectorDetailById" resultMap="ConnectorDetailResultMap">
        SELECT
            -- 充电接口基本信息
            tpci.id AS connector_id,
            tpci.connector_id AS connector_code,
            tpci.connector_name,
            tpci.park_no,
            tpci.status,
            CASE tpci.status
                WHEN 0 THEN '离网'
                WHEN 1 THEN '空闲'
                WHEN 2 THEN '占用(未充电)'
                WHEN 3 THEN '占用(充电中)'
                WHEN 4 THEN '占用(预约锁定)'
                WHEN 255 THEN '故障'
                ELSE '未知'
            END AS status_name,
            tpci.connector_type,
            CASE tpci.connector_type
                WHEN 1 THEN '家用插座'
                WHEN 2 THEN '交流接口插座'
                WHEN 3 THEN '交流接口插头'
                WHEN 4 THEN '直流接口枪头'
                WHEN 5 THEN '无线充电座'
                WHEN 6 THEN '其他'
                ELSE '未知'
            END AS connector_type_name,
            tpci.voltage_upper_limits,
            tpci.voltage_lower_limits,
            tpci.current,
            tpci.power,
            tpci.national_standard,
            CASE tpci.national_standard
                WHEN 1 THEN '2011版'
                WHEN 2 THEN '2015版'
                ELSE '未知'
            END AS national_standard_name,
            tpci.update_time AS last_update_time,
            
            -- 设备信息
            tpei.id AS equipment_id,
            tpei.equipment_id AS equipment_code,
            tpei.equipment_name,
            tpei.equipment_type,
            CASE tpei.equipment_type
                WHEN 1 THEN '直流设备'
                WHEN 2 THEN '交流设备'
                WHEN 3 THEN '交直流一体设备'
                WHEN 4 THEN '无线设备'
                WHEN 5 THEN '其他'
                ELSE '未知'
            END AS equipment_type_name,
            
            -- 站点信息
            tpsi.id AS station_id,
            tpsi.station_name,
            tpsi.address AS station_address,
            tpsi.station_tips AS parking_tips,
            
            -- 常规价格（综合销售费）
            ROUND(IFNULL(pf_common.comp_sales_fee, 0), 4) AS current_price,
            
            -- 企业价格（综合销售费）
            ROUND(IFNULL(pf.comp_sales_fee, 0), 4) AS enterprise_price,
            
            -- 是否企业用户（如果查询到企业价格则为true）
            CASE WHEN pf.id IS NOT NULL THEN 1 ELSE 0 END AS is_firm_user,
            
            -- 当前时段描述（格式：峰 HH:mm-HH:mm）
            (
                SELECT CONCAT(
                    CASE t1.period_flag
                        WHEN 1 THEN '尖 '
                        WHEN 2 THEN '峰 '
                        WHEN 3 THEN '平 '
                        WHEN 4 THEN '谷 '
                        ELSE ''
                    END,
                    CONCAT(SUBSTRING(t1.start_time, 1, 2), ':', SUBSTRING(t1.start_time, 3, 2)),
                    '-',
                    IFNULL(
                        CONCAT(SUBSTRING(t2.start_time, 1, 2), ':', SUBSTRING(t2.start_time, 3, 2)),
                        '24:00'
                    )
                )
                FROM third_party_policy_info t1
                LEFT JOIN third_party_policy_info t2 ON t2.price_policy_id = t1.price_policy_id
                    AND t2.is_deleted = 0
                    AND t2.start_time > t1.start_time
                    AND t2.id = (
                        SELECT id FROM third_party_policy_info
                        WHERE price_policy_id = t1.price_policy_id
                            AND is_deleted = 0
                            AND start_time > t1.start_time
                        ORDER BY start_time ASC
                        LIMIT 1
                    )
                WHERE t1.id = tppi.id
            ) AS current_period_desc,
            
            -- 用户余额（从参数传入）
            #{userBalance} AS available_balance,
            
            -- 新用户优惠金额（从参数传入）
            #{newUserDiscount} AS new_user_discount
            
        FROM third_party_connector_info tpci
        
        -- 关联设备信息
        LEFT JOIN third_party_equipment_info tpei 
            ON tpci.equipment_id = tpei.equipment_id 
            AND tpei.is_deleted = 0
        
        -- 关联站点信息
        LEFT JOIN third_party_station_info tpsi 
            ON tpei.station_id = tpsi.station_id 
            AND tpsi.is_deleted = 0
        
        -- 关联价格策略
        LEFT JOIN third_party_equipment_price_policy tpepp 
            ON tpci.connector_id = tpepp.connector_id 
            AND tpepp.is_deleted = 0
        
        -- 关联当前时段的价格信息（子查询获取当前时段）
        LEFT JOIN third_party_policy_info tppi ON tppi.id = (
            SELECT id
            FROM third_party_policy_info
            WHERE price_policy_id = tpepp.id
                AND is_deleted = 0
                AND start_time &lt;= #{currentTime}
            ORDER BY start_time DESC
            LIMIT 1
        )
        
        -- 关联常规价格（sales_type = 0，不限制企业）
        LEFT JOIN c_policy_fee pf_common ON pf_common.station_info_id = tpsi.id 
            AND pf_common.period_flag = tppi.period_flag
            AND pf_common.is_deleted = 0
            AND pf_common.sales_type = 0
        
        -- 关联企业价格（仅当用户ID不为空时关联）
        LEFT JOIN (
            SELECT pf1.*
            FROM c_policy_fee pf1
            INNER JOIN c_user_firm uf ON pf1.firm_id = uf.firm_id AND uf.is_deleted = 0
            WHERE pf1.is_deleted = 0
                AND pf1.sales_type = 1
                AND uf.user_id = #{userId}
        ) pf ON pf.station_info_id = tpsi.id 
            AND pf.period_flag = tppi.period_flag
        
        WHERE tpci.connector_id = #{connectorCode}
            AND tpci.is_deleted = 0
        LIMIT 1
    </select>

</mapper>
