<?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.platform.yijia.dao.CustomerGradeMapper">

    <!--根据油站ID查询该油站会员成长规则-->
    <select id="customerGrouthRuleByStationId" parameterType="String" resultType="map">
        SELECT
            grouth_value_name       AS grouthValueName,
            grouth_value_consume    AS grouthValueConsume,
            grouth_value            AS grouthValue
        FROM
            customer_grouth_rule
        <where>
            <if test="stationId != null and stationId != ''">
                station_id = #{stationId}
            </if>
        </where>
    </select>

    <!-- 查询立减满减优惠方案 -->
    <select id="getCustomerMarkertPlanList"  resultType="map" parameterType="map">
        SELECT
            id                          AS id,
            grade                       AS grade,
            discount_term               AS discountTerm,
            discount_amt                AS discountAmt,
            gasoil_discount_amt         AS gasoilDiscountAmt,
            station_id                  AS stationId,
            vip_discounty_plus          AS vipDiscountyPlus,
            coupon_plus                 AS couponPlus,
            discount_plan_type          AS discountPlanType,
            oil_name                    AS oilName
        FROM markert_plan
        <where>
            status ="1"
            <if test="stationId != null and stationId != ''">
                AND station_id = #{stationId}
            </if>
            <if test="oilName != null and oilName != ''">
                AND oil_name = #{oilName}
            </if>
            <if test="discountPlanType != null and discountPlanType !=''">
                AND discount_plan_type = #{discountPlanType}
            </if>
        </where>
        ORDER BY discount_amt DESC
    </select>

    <!-- 查询直降优惠方案 -->
    <select id="getCustomerGradeList"  resultType="map" parameterType="map">
    SELECT
        id                          AS id,
        grade                       AS grade,
        station_id                  AS stationId,
        discount_amt                AS discountAmt,
        member_condit_start         AS memberConditStart,
        member_condit_end           AS memberConditEnd,
        oil_name                    AS oilName
    FROM customer_grade_setting
    <where>
        del_flag ="0"
      <if test="stationId != null and stationId != ''">
          station_id = #{stationId}
      </if>
      <if test="oilName != null and oilName != ''">
        AND oil_name = #{oilName}
      </if>
    </where>
      ORDER BY member_condit_start ASC
  </select>

    <!-- 查询直降优惠方案 -->
    <select id="getCustomerGradeListOrderByDesc"  resultType="map" parameterType="map">
        SELECT
            id                          AS id,
            grade                       AS grade,
            station_id                  AS stationId,
            discount_amt                AS discountAmt,
            member_condit_start         AS memberConditStart,
            member_condit_end           AS memberConditEnd,
            oil_name                    AS oilName
        FROM customer_grade_setting
        <where>
            del_flag ="0"
            <if test="stationId != null and stationId != ''">
                AND station_id = #{stationId}
            </if>
            <if test="oilName != null and oilName != ''">
                AND oil_name = #{oilName}
            </if>
        </where>
        ORDER BY member_condit_start DESC
    </select>

    <!--查询客户等级直降信息-->
    <select id="getCustomerGradeInfo" parameterType="map" resultType="map">
        SELECT
            T2.station_name			        AS stationName,
            T2.member_grade			        AS memberGradeId,
            T3.grade					    AS grade,
            T3.oil_name                     AS oilName,
            T3.member_condit_start          AS memberConditStart,
            T3.member_condit_end            AS memberConditEnd
        FROM
            app_user_info 					AS T1
        LEFT JOIN customer_manage 			AS T2   ON T1.mobile_phone = T2.phone_number
        LEFT JOIN customer_grade_setting 	AS T3   ON T2.station_id = T3.station_id AND T2.oil_name = T3.oil_name AND T3.id = T2.member_grade
        <where>
            <if test="mobilePhone != null and mobilePhone !=''">
                T1.mobile_phone = #{mobilePhone}
            </if>
            <if test="userType != null and userType !=''">
                AND T1.user_type = #{userType}
            </if>
            <if test="oilName != null and oilName !=''">
                AND T2.oil_name = #{oilName}
            </if>
            <if test="stationId != null and stationId != ''">
                AND T2.station_id = #{stationId}
            </if>
            <if test="blogOpenid != null and blogOpenid != ''">
                AND T1.blog_openid = #{blogOpenid}
            </if>
            <if test="minaOpenid != null and minaOpenid != ''">
                AND T1.mina_openid = #{minaOpenid}
            </if>
            <if test="grade != null and grade != ''">
                AND T3.id = #{grade}
            </if>
        </where>
        ORDER BY T3.member_condit_end ASC
    </select>

    <!-- 判断客户表是否已存在-->
    <select id="isExistCustomerByStationIdAndPhoneNumber" parameterType="com.platform.yijia.pojo.CustomerManage" resultType="com.platform.yijia.pojo.CustomerManage">
        SELECT
            id              AS id,
            member_id       AS memberId,
            member_grade    AS memberGrade,
            phone_number    AS phoneNumber,
            regtime         AS regtime,
            station_name    AS stationName,
            oil_name        AS oilName,
            liters          AS liters,
            amt             AS amt
        FROM
            customer_manage
        <where>
            <if test="phoneNumber !=null and phoneNumber !='' ">
                phone_number = #{phoneNumber}
            </if>
            <if test="stationId !=null and stationId !='' ">
                AND station_id = #{stationId}
            </if>
            <if test="oilName !=null and oilName !='' ">
                AND oil_name = #{oilName}
            </if>
        </where>
    </select>

    <!-- 营销立减-->
    <select id="getMarkertPlanListByDiscountPlanType"  resultType="map">
        SELECT
            id                          AS id,
            grade                       AS grade,
            discount_term               AS discountWay,
            discount_amt                AS discountAmt,
            gasoil_discount_amt         AS gasoilDiscountAmt,
            dieseloil_discount_amt      AS dieseloilDiscountAmt,
            vip_discounty_plus          AS vipDiscountyPlus,
            coupon_plus                 AS couponPlus,
            discount_plan_type          AS discountPlanType
        FROM
            markert_plan
        <where>
            discount_plan_type = "2"
        </where>
    </select>

</mapper>
