<?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.PolicyFeeMapper">

    <!-- 时段价格结果映射 -->
    <resultMap id="TimePeriodPriceResultMap" type="com.zsElectric.boot.business.model.vo.TimePeriodPriceVO">
        <result property="timePeriod" column="time_period"/>
        <result property="periodFlag" column="period_flag"/>
        <result property="electricityPrice" column="elec_price"/>
        <result property="settlementServiceFee" column="service_price"/>
        <result property="settlementTotalPrice" column="settlement_total_price"/>
        <result property="operationServiceFee" column="op_fee"/>
        <result property="valueAddedFees" column="value_added_fees"/>
        <result property="saleTotalPrice" column="sale_total_price"/>
    </resultMap>

    <!-- 查询时段价格列表 -->
    <select id="selectTimePeriodPriceList" resultMap="TimePeriodPriceResultMap">
        SELECT
            tppi.start_time AS time_period,
            tppi.period_flag,
            tppi.elec_price,
            tppi.service_price,
            ROUND(tppi.elec_price + tppi.service_price, 4) AS settlement_total_price,
            IFNULL(pf.op_fee, 0) AS op_fee,
            IFNULL(
                (SELECT CAST(di.value AS DECIMAL(10,4))
                 FROM sys_dict_item di
                 WHERE di.dict_code = 'time_period_flag'
                   AND di.status = 1
                   AND di.label = CASE tppi.period_flag
                       WHEN 1 THEN '尖'
                       WHEN 2 THEN '峰'
                       WHEN 3 THEN '平'
                       WHEN 4 THEN '谷'
                       ELSE ''
                   END
                 LIMIT 1),
                0
            ) AS value_added_fees,
            ROUND(
                tppi.elec_price + tppi.service_price + IFNULL(pf.op_fee, 0) +
                IFNULL(
                    (SELECT CAST(di.value AS DECIMAL(10,4))
                     FROM sys_dict_item di
                     WHERE di.dict_code = 'time_period_flag'
                       AND di.status = 1
                       AND di.label = CASE tppi.period_flag
                           WHEN 1 THEN '尖'
                           WHEN 2 THEN '峰'
                           WHEN 3 THEN '平'
                           WHEN 4 THEN '谷'
                           ELSE ''
                       END
                     LIMIT 1),
                    0
                ),
                4
            ) AS sale_total_price
        FROM third_party_station_info tpsi
        INNER JOIN third_party_connector_info tpci ON tpci.station_id = tpsi.station_id AND tpci.is_deleted = 0
        INNER JOIN third_party_equipment_price_policy tpepp ON tpepp.connector_id = tpci.connector_id AND tpepp.is_deleted = 0
            AND tpepp.id = (
                SELECT tpepp2.id
                FROM third_party_connector_info tpci2
                INNER JOIN third_party_equipment_price_policy tpepp2 ON tpepp2.connector_id = tpci2.connector_id AND tpepp2.is_deleted = 0
                INNER JOIN third_party_policy_info tppi2 ON tppi2.price_policy_id = tpepp2.id AND tppi2.is_deleted = 0
                WHERE tpci2.station_id = tpsi.station_id AND tpci2.is_deleted = 0
                LIMIT 1
            )
        INNER JOIN third_party_policy_info tppi ON tppi.price_policy_id = tpepp.id AND tppi.is_deleted = 0
        LEFT JOIN c_policy_fee pf ON pf.station_info_id = tpsi.id
            AND pf.start_time = tppi.start_time
            AND pf.is_deleted = 0
            AND pf.sales_type = #{salesType}
            <if test="salesType == 1">
                AND pf.firm_id = #{firmId}
            </if>
            <if test="salesType == 2">
                AND pf.third_party_id = #{thirdPartyId}
            </if>
        WHERE tpsi.id = #{stationId}
            AND tpsi.is_deleted = 0
        GROUP BY tppi.start_time
        ORDER BY tppi.start_time ASC
    </select>

</mapper>
