package com.hfkj.dao;

import com.hfkj.model.GasListModel;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;

/**
 * mapper扩展类
 */
public interface BsMerchantMapperExt {
    
    @Select("<script>" +
            " select * from (SELECT" +
            " b.source_type sourceType," +
            " b.mer_no merNo," +
            " b.mer_name merName," +
            " b.mer_logo merLogo," +
            " b.address," +
            " b.longitude," +
            " b.latitude," +
            " round(ST_Distance_Sphere(" +
            "     point(#{longitude}, #{latitude})," +
            "     point(b.longitude, b.latitude)" +
            "   ) / 1000, 2) as distance," +
            " a.oil_no oilNo," +
            " a.price_official priceOfficial," +
            " a.price_gun priceGun," +
            " a.price_vip priceVip," +
            " a.gas_station_drop gasStationDrop," +
            " b.list_show listShow" +
            " FROM" +
            "   bs_gas_oil_price a" +
            " LEFT JOIN bs_merchant b on b.id = a.mer_id" +
            " LEFT JOIN bs_gas_channel_config c on c.gas_channel_type = b.source_type" +
            " where a.oil_no = #{oilNo} and a.`status` = 1 and c.show_status = 1" +
            " ORDER BY distance asc) a where 1 = 1" +
            "<if test='gasName != null'> and a.merName like concat('%',#{gasName},'%') </if>"  +
            "<if test='distanceLimit != null'> <![CDATA[ and a.distance <= #{distanceLimit} ]]> </if>"  +
            "<if test='distanceLimit != null'> <![CDATA[ and a.distance <= #{distanceLimit} ]]> </if>"  +
            "<if test='listShow != null'> and a.listShow = #{listShow} </if>"  +
            "</script>")
    List<GasListModel> queryGasList(@Param("longitude") String longitude,
                                    @Param("latitude") String latitude,
                                    @Param("oilNo") String oilNo,
                                    @Param("gasName") String gasName,
                                    @Param("distanceLimit") Integer distanceLimit,
                                    @Param("listShow") Boolean listShow);

    @Select("<script>" +
            " select * from (SELECT" +
            " b.mer_no merNo," +
            " b.mer_name merName," +
            " b.address," +
            " b.longitude," +
            " b.latitude," +
            " round(ST_Distance_Sphere(" +
            "     point(#{longitude}, #{latitude})," +
            "     point(b.longitude, b.latitude)" +
            "   ) / 1000, 2) as distance," +
            " a.oil_no oilNo," +
            " a.price_official priceOfficial," +
            " a.price_gun priceGun," +
            " a.price_vip priceVip," +
            " a.gas_station_drop gasStationDrop," +
            " b.list_show listShow" +
            " FROM" +
            "   bs_gas_oil_price a" +
            " LEFT JOIN bs_merchant b on b.id = a.mer_id" +
            " where a.oil_no = #{oilNo} and a.`status` = 1" +
            " and a.mer_id in (select mer_id from bs_discount_use_mer where discount_no = #{discountNo}) " +
            " ORDER BY distance asc) a where 1 = 1" +
            "<if test='gasName != null'> and a.merName like concat('%',#{gasName},'%') </if>"  +
            "<if test='distanceLimit != null'> <![CDATA[ and a.distance <= #{distanceLimit} ]]> </if>"  +
            "<if test='distanceLimit != null'> <![CDATA[ and a.distance <= #{distanceLimit} ]]> </if>"  +
            "<if test='listShow != null'> and a.listShow = #{listShow} </if>"  +
            "</script>")
    List<GasListModel> queryGasListByDiscount(@Param("discountNo") String discountNo,
                                              @Param("longitude") String longitude,
                                              @Param("latitude") String latitude,
                                              @Param("oilNo") String oilNo,
                                              @Param("gasName") String gasName,
                                              @Param("distanceLimit") Integer distanceLimit,
                                              @Param("listShow") Boolean listShow);
}
