#namespace("YltAnalytics")
    #sql("getOperatorsPriceTrends")
        SELECT 
            p.operator,
            DATE(sc.valid_start_time) as date_str,
            sc.schedule_json
        FROM t_station_price_schedule_scd sc
        JOIN t_station_profile_scd p ON sc.station_hash = p.station_hash AND p.is_current = 1
        WHERE sc.valid_start_time >= DATE_SUB(CURDATE(), INTERVAL #para(days) DAY)
        ORDER BY date_str ASC
    #end

    #sql("listYltStations")
        SELECT
            p.station_hash,
            p.operator,
            p.station_name,
            p.address,
            p.coord_x,
            p.coord_y,
            s.current_price
        FROM t_station_profile_scd p
        LEFT JOIN t_station_status_scd s
            ON p.station_hash = s.station_hash AND s.is_current = 1
        WHERE p.operator = '驿来特'
        #if(q)
            AND (p.station_name LIKE #para(kw) OR p.address LIKE #para(kw))
        #end
        ORDER BY p.station_name
    #end

    #sql("fetchCurrentStations")
        SELECT
            p.station_hash,
            p.operator,
            p.station_name,
            p.address,
            p.coord_x,
            p.coord_y,
            s.current_price
        FROM t_station_profile_scd p
        LEFT JOIN t_station_status_scd s
            ON p.station_hash = s.station_hash AND s.is_current = 1
        WHERE p.is_current = 1
    #end

    #sql("fetchStationScheduleJson")
        SELECT sc.schedule_json, p.operator
        FROM t_station_price_schedule_scd sc
        JOIN t_station_profile_scd p ON sc.station_hash = p.station_hash AND p.is_current = 1
        WHERE sc.station_hash = #para(h) AND sc.is_current = 1
        ORDER BY sc.valid_start_time DESC
        LIMIT 1
    #end

    #sql("fetchCurrentStationRows")
        SELECT
            p.station_hash,
            p.station_name,
            p.address,
            p.coord_x,
            p.coord_y,
            s.total_piles AS total_guns,
            s.free_piles AS free_guns,
            s.current_price,
            s.pro_price,
            s.parking_info,
            s.distance,
            s.valid_start_time AS status_update_time,
            sc.schedule_json,
            sc.valid_start_time AS schedule_update_time,
            s.piles_detail_json
        FROM t_station_profile_scd p
        LEFT JOIN t_station_status_scd s ON p.station_hash = s.station_hash AND s.is_current = 1
        LEFT JOIN t_station_price_schedule_scd sc ON p.station_hash = sc.station_hash AND sc.is_current = 1
        WHERE p.operator = #para(op) AND p.is_current = 1
        ORDER BY p.station_name ASC
    #end
#end
