package com.hfkj.dao;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;

import java.util.List;
import java.util.Map;

/**
 * mapper扩展类
 */
public interface BsAgentPriceMapperExt {

    @Select("<script>" +
            " select * from (select " +
            " c.agent_name agentName," +
            " c.agent_staff_name agentStaffName," +
            " b.province_name provinceName," +
            " b.source_type sourceType, " +
            " b.mer_no merNo," +
            " b.mer_name merName," +
            " a.oil_no oilNo," +
            " a.price_official priceOfficial," +
            " a.price_gun priceGun," +
            " a.price_vip priceVip," +
            " ROUND(a.price_vip * (d.price_rate / 100),2) terminalPrice," +
            " d.price_rate priceRate," +
            " d.service_fee_rate  serviceFeeRate" +
            " from bs_gas_oil_price a" +
            " LEFT JOIN bs_merchant b on b.id = a.mer_id" +
            " LEFT JOIN bs_agent_mer c on c.mer_no = b.mer_no and c.`status` = 1" +
            " LEFT JOIN bs_agent_price d on d.agent_mer_id = c.id" +
            " where a.`status` = 1 and c.`status` = 1 and c.agent_staff_id is not null and d.`status` = 1" +
            " <if test='param.agentId != null'> and c.agent_id = #{param.agentId} </if>" +
            " <if test='param.agentStaffId != null'> and c.agent_staff_id = #{param.agentStaffId} </if>" +
            " <if test='param.provinceCode != null'> and b.province_code = #{param.provinceCode} </if>" +
            " <if test='param.merNo != null'> and b.mer_no like concat('%',#{param.merNo},'%') </if>" +
            " <if test='param.merName != null'> and b.mer_name like concat('%',#{param.merName},'%') </if>" +
            " <if test='param.sourceType != null'> and b.source_type = #{param.sourceType} </if>" +
            " <if test='param.oilNo != null'> and a.oil_no = #{param.oilNo} </if>" +
            " ORDER BY c.agent_staff_id,b.mer_no) a " +
            " GROUP BY a.merNo,a.oilNo,a.merNo" +
            " ORDER BY a.merNo,a.merNo" +
            "</script>")
    List<Map<String,Object>> queryAgentPriceList(@Param("param") Map<String,Object> param);

    @Select("<script>" +
            " select a.*," +
            " case when b.price_rate is not null then b.price_rate else 100 end priceRate," +
            " ROUND(a.priceGun * ((case when b.price_rate is not null then b.price_rate else 100 end) / 100),2) terminalPrice" +
            " from " +
            " (select " +
            " c.agent_name agentName," +
            " b.province_name provinceName," +
            " b.source_type sourceType, " +
            " b.mer_no merNo," +
            " b.mer_name merName," +
            " a.oil_no oilNo," +
            " a.price_official priceOfficial," +
            " a.price_gun priceGun," +
            " a.price_vip priceVip," +
            " c.id agentMerId" +
            " from bs_gas_oil_price a" +
            " LEFT JOIN bs_merchant b on b.id = a.mer_id" +
            " LEFT JOIN bs_agent_mer c on c.mer_id = a.mer_id and c.`status` = 1" +
            " where a.`status` = 1 and c.`status` = 1 and c.agent_staff_id is null" +
            " <if test='param.agentId != null'> and c.agent_id = #{param.agentId} </if>" +
            " <if test='param.provinceCode != null'> and b.province_code = #{param.provinceCode} </if>" +
            " <if test='param.merNo != null'> and b.mer_no like concat('%',#{param.merNo},'%') </if>" +
            " <if test='param.merName != null'> and b.mer_name like concat('%',#{param.merName},'%') </if>" +
            " <if test='param.sourceType != null'> and b.source_type = #{param.sourceType} </if>" +
            " <if test='param.oilNo != null'> and a.oil_no = #{param.oilNo} </if>" +
            " ORDER BY b.mer_no,oil_no) a" +
            " LEFT JOIN bs_agent_price b on b.agent_mer_id = a.agentMerId and b.oil_no = a.oilNo and b.status = 1 " +
            " ORDER BY a.merNo" +
            "</script>")
    List<Map<String,Object>> queryApiAgentDiscountList(@Param("param") Map<String,Object> param);

}
