<?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.ElectronicMembershipCardMapper">
  <!--储蓄卡返回结果-->
  <resultMap id="CardBaseResultMap" type="com.platform.yijia.pojo.CustomerElectronicCard">
      <id     column="id"                      jdbcType="INTEGER"   property="id" />
      <result column="union_id"                jdbcType="VARCHAR"   property="unionId" />
      <result column="blog_openid"             jdbcType="VARCHAR"   property="blogOpenid" />
      <result column="mina_openid"             jdbcType="VARCHAR"   property="minaOpenid" />
      <result column="customer_no"             jdbcType="VARCHAR"   property="customerNo" />
      <result column="customer_name"           jdbcType="VARCHAR"   property="customerName" />
      <result column="mobile_phone"            jdbcType="VARCHAR"    property="mobilePhone" />
      <result column="amt"                     jdbcType="DECIMAL"   property="amt" />
      <result column="card_oils_type"          jdbcType="VARCHAR"   property="cardOilsType" />
      <result column="station_id"              jdbcType="INTEGER"   property="stationId" />
      <result column="station_name"            jdbcType="VARCHAR"   property="stationName" />
      <result column="create_time"             jdbcType="TIMESTAMP" property="createTime" />
      <result column="recently_time"           jdbcType="TIMESTAMP" property="recentlyTime" />
  </resultMap>

    <!--储蓄卡使用记录返回结果-->
    <resultMap id="CardRecordBaseResultMap" type="com.platform.yijia.pojo.CustomerCardUseRecord">
        <id     column="id"                    jdbcType="INTEGER"   property="id" />
        <result column="order_no"              jdbcType="VARCHAR"   property="orderNo" />
        <result column="blog_openid"           jdbcType="VARCHAR"   property="blogOpenid" />
        <result column="mina_openid"           jdbcType="VARCHAR"   property="minaOpenid" />
        <result column="union_id"              jdbcType="VARCHAR"   property="unionId" />
        <result column="ali_id"                jdbcType="VARCHAR"   property="aliId" />
        <result column="customer_no"           jdbcType="VARCHAR"   property="customerNo" />
        <result column="customer_name"         jdbcType="VARCHAR"   property="customerName" />
        <result column="mobile_phone"          jdbcType="VARCHAR"   property="mobilePhone" />
        <result column="usage_type"            jdbcType="VARCHAR"   property="usageType" />
        <result column="pay_type"              jdbcType="VARCHAR"   property="payType" />
        <result column="card_oils_type"        jdbcType="VARCHAR"   property="cardOilsType" />
        <result column="amt"                   jdbcType="DECIMAL"   property="amt" />
        <result column="present_amt"           jdbcType="DECIMAL"   property="presentAmt" />
        <result column="balance"               jdbcType="DECIMAL"   property="balance" />
        <result column="create_time"           jdbcType="TIMESTAMP" property="createTime" />
        <result column="station_id"            jdbcType="INTEGER"   property="stationId" />
        <result column="station_name"          jdbcType="INTEGER"   property="stationName" />
        <result column="status"                jdbcType="VARCHAR"   property="status" />
        <result column="oilsCardName"          jdbcType="VARCHAR"   property="oilsCardName" />
    </resultMap>

    <!--储蓄卡充值规则返回结果-->
    <resultMap id="CardSettingBaseResultMap" type="com.platform.yijia.pojo.CustomerCardSetting">
        <id     column="id"                         jdbcType="INTEGER"    property="id" />
        <result column="card_oils_type"             jdbcType="VARCHAR"    property="cardOilsType" />
        <result column="discount_time_setting"      jdbcType="VARCHAR"    property="discountTimeSetting" />
        <result column="discount_time_type"         jdbcType="VARCHAR"    property="discountTimeType" />
        <result column="discount_time"              jdbcType="VARCHAR"    property="discountTime" />
        <result column="present_scale"              jdbcType="VARCHAR"    property="presentScale" />
        <result column="is_discount_coupon"         jdbcType="VARCHAR"    property="isDiscountCoupon" />
        <result column="enjoy_integral_multiple"    jdbcType="DATE"       property="enjoyIntegralMultiple" />
        <result column="station_id"                 jdbcType="INTEGER"    property="stationId" />
        <result column="create_time"                jdbcType="VARCHAR"    property="createTime" />
        <result column="is_market"                  jdbcType="VARCHAR"    property="isMarket" />
        <result column="is_grade_setting"           jdbcType="VARCHAR"    property="isGradeSetting" />
        <result column="is_label"                   jdbcType="VARCHAR"    property="isLabel" />
        <collection property="customerCardSettingDetailList" resultMap="CardSettingDetailResultMap"/>
    </resultMap>

    <!--储蓄卡充值规则明细表-->
    <resultMap id="CardSettingDetailResultMap" type="com.platform.yijia.pojo.CustomerCardSettingDetail">
        <id     column="cardSettingDetailId"        jdbcType="INTEGER"      property="cardSettingDetailId" />
        <result column="parent_id"                  jdbcType="INTEGER"      property="parentId"/>
        <result column="setting_rule_type"          jdbcType="VARCHAR"      property="settingRuleType" />
        <result column="discount_amt_start"         jdbcType="DECIMAL"      property="discountAmtStart" />
        <result column="discount_amt_end"           jdbcType="DECIMAL"      property="discountAmtEnd" />
        <result column="present_amt"                jdbcType="DECIMAL"      property="presentAmt"/>
        <result column="card_type"                  jdbcType="VARCHAR"      property="cardType"/>
        <result column="status"                     jdbcType="VARCHAR"      property="status" />
    </resultMap>

    <!--查询油站电子会员卡（储蓄卡）充值设置规则-->
    <select id="getCardRechargeSettingList" parameterType="com.platform.yijia.pojo.CustomerCardSetting" resultMap="CardSettingBaseResultMap">
        SELECT
            T1.id,
            T1.card_oils_type,
            T1.discount_time_setting,
            T1.discount_time_type,
            T1.discount_time,
            T1.present_scale,
            T1.is_discount_coupon,
            T1.enjoy_integral_multiple,
            T1.station_id,
            T1.create_time,
            T1.is_market,
            T1.is_grade_setting,
            T1.is_label,
            T2.id                   AS      cardSettingDetailId,
            T2.parent_id,
            T2.setting_rule_type,
            T2.discount_amt_start,
            T2.discount_amt_end,
            T2.present_amt,
            T2.card_type,
            T2.status
        FROM
            customer_card_setting AS T1 LEFT JOIN customer_card_setting_detail AS T2 ON T1.id = T2.parent_id
        <where>
            <if test="delFlag !=null and delFlag !=''">
                T2.del_flag = #{delFlag}
            </if>
            <if test="stationId !=null">
                AND T1.station_id = #{stationId}
            </if>
            <if test="cardOilsType !=null">
                AND FIND_IN_SET(#{cardOilsType}, T1.card_oils_type)
            </if>
            <if test="cardOilsType !=null">
                AND T2.card_type = #{cardOilsType}
            </if>
        </where>
        <if test="orderBy !=null and orderBy !=''">
            ORDER BY T2.discount_amt_start ${orderBy}
        </if>
    </select>

    <!--查询客户充值的订单信息-->
    <select id="getCardRecordByOrderNo" parameterType="com.platform.yijia.pojo.CustomerCardUseRecord" resultMap="CardRecordBaseResultMap">
        SELECT
            T1.id,
            T1.order_no,
            T1.union_id,
            T1.ali_id,
            T2.blog_nick_name           AS customer_name,
            T2.blog_openid,
            T2.mina_openid,
            T2.mobile_phone,
            T1.usage_type,
            T1.pay_type,
            T1.card_oils_type,
            T1.amt,
            T1.balance,
            T1.present_amt,
            T1.create_time,
            T1.station_id,
            T1.status,
            T3.dept_name                AS station_name
        FROM
            customer_card_use_record    AS T1
        LEFT JOIN app_user_info         AS T2 ON T1.station_id = T2.station_id AND T1.union_id =T2.union_id
        LEFT JOIN sys_dept              AS T3 ON T1.station_id = T3.dept_id
        <where>
            <if test=" orderNo !=null and orderNo !=''">
                AND T1.order_no = #{orderNo}
            </if>
        </where>
    </select>

    <!--根据订单号更新用户充值记录的状态-->
    <update id="updateCardRecordStatusByOrderNo" parameterType="com.platform.yijia.pojo.CustomerCardUseRecord">
        UPDATE
            customer_card_use_record
        <set>
            <if test="status !=null">
                status = #{status},
            </if>
            <if test="payType !=null">
                pay_type = #{payType},
            </if>
            <if test="balance !=null">
                balance = #{balance}
            </if>
        </set>
        <where>
            <if test=" orderNo !=null and orderNo !=''">
                order_no = #{orderNo}
            </if>
        </where>
    </update>

    <!--插入用户电子会员卡的充值使用记录-->
    <insert id="insetCardUseRecordRecharge" parameterType="com.platform.yijia.pojo.CustomerCardUseRecord">
        INSERT INTO customer_card_use_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="orderNo !=null">
                order_no,
            </if>
            <if test="unionId !=null">
                union_id,
            </if>
            <if test="aliId !=null">
                ali_id,
            </if>
            <if test="customerNo !=null">
                customer_no,
            </if>
            <if test="customerName !=null">
                customer_name,
            </if>
            <if test="usageType !=null">
                usage_type,
            </if>
            <if test="payType !=null">
                pay_type,
            </if>
            <if test="cardOilsType !=null">
                card_oils_type,
            </if>
            <if test="amt !=null">
                amt,
            </if>
            <if test="presentAmt !=null">
                present_amt,
            </if>
            <if test="balance !=null">
                balance,
            </if>
            <if test="createTime !=null">
                create_time,
            </if>
            <if test="stationId !=null">
                station_id,
            </if>
            <if test="status !=null">
                status,
            </if>
            <if test="mobilePhone !=null">
                mobile_phone,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="orderNo !=null">
                #{orderNo},
            </if>
            <if test="unionId !=null">
                #{unionId},
            </if>
            <if test="aliId !=null">
                #{aliId},
            </if>
            <if test="customerNo !=null">
                #{customerNo},
            </if>
            <if test="customerName !=null">
                #{customerName},
            </if>
            <if test="usageType !=null">
                #{usageType},
            </if>
            <if test="payType !=null">
                #{payType},
            </if>
            <if test="cardOilsType !=null">
                #{cardOilsType},
            </if>
            <if test="amt !=null">
                #{amt},
            </if>
            <if test="presentAmt !=null">
                #{presentAmt},
            </if>
            <if test="balance !=null">
                #{balance},
            </if>
            <if test="createTime !=null">
                #{createTime},
            </if>
            <if test="stationId !=null">
                #{stationId},
            </if>
            <if test="status !=null">
                #{status},
            </if>
            <if test="mobilePhone !=null">
                #{mobilePhone},
            </if>
        </trim>
    </insert>

    <!--插入用户电子会员卡的消费使用记录-->
    <insert id="insetCardUseRecordConsumption" parameterType="com.platform.yijia.pojo.CustomerCardUseRecord">
        INSERT INTO customer_card_use_record
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="orderNo !=null">
                order_no,
            </if>
            <if test="unionId !=null">
                union_id,
            </if>
            <if test="customerNo !=null">
                customer_no,
            </if>
            <if test="customerName !=null">
                customer_name,
            </if>
            <if test="usageType !=null">
                usage_type,
            </if>
            <if test="payType !=null">
                pay_type,
            </if>
            <if test="cardOilsType !=null">
                card_oils_type,
            </if>
            <if test="amt !=null">
                amt,
            </if>
            <if test="balance !=null">
                balance,
            </if>
            <if test="createTime !=null">
                create_time,
            </if>
            <if test="stationId !=null">
                station_id,
            </if>
            <if test="status !=null">
                status,
            </if>
            <if test="mobilePhone !=null">
                mobile_phone,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="orderNo !=null">
                #{orderNo},
            </if>
            <if test="unionId !=null">
                #{unionId},
            </if>
            <if test="customerNo !=null">
                #{customerNo},
            </if>
            <if test="customerName !=null">
                #{customerName},
            </if>
            <if test="usageType !=null">
                #{usageType},
            </if>
            <if test="payType !=null">
                #{payType},
            </if>
            <if test="cardOilsType !=null">
                #{cardOilsType},
            </if>
            <if test="amt !=null">
                #{amt},
            </if>
            <if test="balance !=null">
                #{balance},
            </if>
            <if test="createTime !=null">
                #{createTime},
            </if>
            <if test="stationId !=null">
                #{stationId},
            </if>
            <if test="status !=null">
                #{status},
            </if>
            <if test="mobilePhone !=null">
                #{mobilePhone},
            </if>
        </trim>
    </insert>


    <!--充值-->
    <sql id="Base_Column_List_recharge">
        SELECT
            id,
            order_no,
            union_id,
            customer_no,
            customer_name,
            usage_type,
            pay_type,
            card_oils_type,
            amt,
            present_amt,
            balance,
            create_time,
            station_id,
            status
         FROM
            customer_card_record_recharge
        <where>
            status ="1"
            <if test="stationId !=null and stationId !=''">
                AND station_id = #{stationId}
            </if>
            <if test="unionId !=null and unionId !=''">
                AND union_id = #{unionId}
            </if>
        </where>
        ORDER BY create_time DESC
    </sql>
    <!--消费-->
    <sql id="Base_Column_List_consumption">
        SELECT
            id,
            order_no,
            union_id,
            customer_no,
            customer_name,
            usage_type,
            pay_type,
            card_oils_type,
            amt,
            balance,
            create_time,
            station_id,
            status
         FROM
            customer_card_record_consumption
        <where>
            <if test="stationId !=null and stationId !=''">
                AND station_id = #{stationId}
            </if>
            <if test="unionId !=null and unionId !=''">
                AND union_id = #{unionId}
            </if>
        </where>
        ORDER BY create_time DESC
    </sql>

    <!--全部-->
    <sql id="Base_Column_List">
        SELECT
            union_id, station_id, amt, present_amt, balance, card_oils_type, usage_type, create_time, `status`
        FROM customer_card_record_recharge
        <where>
            status ="1"
            <if test="stationId !=null and stationId !=''">
                AND station_id = #{stationId}
            </if>
            <if test="unionId !=null and unionId !=''">
                AND union_id = #{unionId}
            </if>
        </where>
        UNION ALL
        SELECT
            union_id, station_id, amt, "0" AS present_amt, balance, card_oils_type, usage_type,create_time,  `status`
        FROM customer_card_record_consumption
        <where>
            status ="1"
            <if test="stationId !=null and stationId !=''">
                AND station_id = #{stationId}
            </if>
            <if test="unionId !=null and unionId !=''">
                AND union_id = #{unionId}
            </if>
        </where>
        ORDER BY create_time DESC
    </sql>

    <!--查询客户电子会员卡的使用记录-->
    <select id="getCardUseRecordList" resultMap="CardRecordBaseResultMap" parameterType="com.platform.yijia.pojo.CustomerCardUseRecord">
        SELECT
            T1.union_id,
            T1.station_id,
            T1.amt,
            T1.present_amt,
            T1.usage_type,
            T1.balance,
            T1.card_oils_type,
            T1.usage_type,
            T1.create_time,
            T1.status,
            T2.dept_name    AS station_name
        FROM customer_card_use_record  AS T1
        LEFT JOIN   sys_dept           AS T2  ON T1.station_id = T2.dept_id
        <where>
            T1.status ="1"
            <if test="stationId !=null and stationId !=''">
                AND T1.station_id = #{stationId}
            </if>
            <if test="groupId !=null and groupId !=''">
                AND T2.parent_id = #{groupId}
            </if>
            <if test="unionId !=null and unionId !=''">
                AND T1.union_id = #{unionId}
            </if>
            <if test="usageType !=null and usageType !=''">
                AND T1.usage_type = #{usageType}
            </if>
        </where>
        ORDER BY T1.create_time DESC
    </select>

    <!--  根据设置的卡规则来查询用户所拥有的储蓄卡    -->
    <select id="getElectronicCardListBycardOilsTypeArray" resultMap="CardBaseResultMap" parameterType="com.platform.yijia.pojo.CustomerElectronicCard">
        SELECT
            id,
            union_id,
            customer_no,
            customer_name,
            amt,
            card_oils_type,
            station_id,
            create_time,
            recently_time
        FROM   customer_electronic_card
        <where>
            <if test="stationId !=null and stationId !=''">
                station_id = #{stationId}
            </if>
            <if test="unionId !=null and unionId !=''">
                AND union_id = #{unionId}
            </if>
            <if test="cardOilsTypeArray !=null and cardOilsTypeArray !=''">
                AND card_oils_type IN
                <foreach collection="cardOilsTypeArray" item="item" index="index" open="(" close=")" separator=",">
                    #{item}
                </foreach>
            </if>
        </where>
        ORDER BY (CASE WHEN card_oils_type = 3 THEN 100 ELSE 0 END), card_oils_type
    </select>


    <!--查询用户所拥有的储蓄卡-->
    <select id="getElectronicCardList" resultMap="CardBaseResultMap" parameterType="com.platform.yijia.pojo.CustomerElectronicCard">
        SELECT
            id,
            union_id,
            customer_no,
            customer_name,
            amt,
            card_oils_type,
            station_id,
            create_time,
            recently_time
        FROM
            customer_electronic_card
        <where>
              <if test="stationId !=null and stationId !=''">
                 station_id = #{stationId}
              </if>
              <if test="unionId !=null and unionId !=''">
                AND union_id = #{unionId}
              </if>
              <if test="cardOilsType !=null and cardOilsType !=''">
                AND card_oils_type = #{cardOilsType}
              </if>
        </where>
    </select>

    <!--插入用户电子会员卡-->
    <insert id="insetElectronicCard" parameterType="com.platform.yijia.pojo.CustomerElectronicCard">
        INSERT INTO
            customer_electronic_card
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="unionId !=null">
                union_id,
            </if>
            <if test="customerNo !=null">
                customer_no,
            </if>
            <if test="customerName !=null">
                customer_name,
            </if>
            <if test="mobilePhone !=null">
                mobile_phone,
            </if>
            <if test="amt !=null">
                amt,
            </if>
            <if test="cardOilsType !=null">
                card_oils_type,
            </if>
            <if test="stationId !=null">
                station_id,
            </if>
            <if test="createTime !=null">
                create_time,
            </if>
            <if test="recentlyTime !=null">
                recently_time,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="unionId !=null">
                #{unionId},
            </if>
            <if test="customerNo !=null">
                #{customerNo},
            </if>
            <if test="customerName !=null">
                #{customerName},
            </if>
            <if test="mobilePhone !=null">
                #{mobilePhone},
            </if>
            <if test="amt !=null">
                #{amt},
            </if>
            <if test="cardOilsType !=null">
                #{cardOilsType},
            </if>
            <if test="stationId !=null">
                #{stationId},
            </if>
            <if test="createTime !=null">
                #{createTime},
            </if>
            <if test="recentlyTime !=null">
                #{recentlyTime},
            </if>
        </trim>
    </insert>

    <!--根据unionId获取用户用要充值电子会员卡(储蓄卡)信息-->
    <select id="getElectronicCardInfoByUnionId" resultMap="CardBaseResultMap" parameterType="com.platform.yijia.pojo.CustomerElectronicCard">
        SELECT
            id,
            union_id,
            customer_no,
            customer_name,
            amt,
            card_oils_type,
            station_id,
            create_time,
            recently_time
        FROM
            customer_electronic_card
        <where>
            <if test="stationId != null" >
                station_id = #{stationId}
            </if>
            <if test="unionId !=null and unionId !=''">
                AND union_id = #{unionId}
            </if>
<!--            <if test="blogOpenid !=null and blogOpenid !=''">-->
<!--                AND blog_openid = #{blogOpenid}-->
<!--            </if>-->
<!--            <if test="minaOpenid !=null and minaOpenid !=''">-->
<!--                AND mina_openid = #{minaOpenid}-->
<!--            </if>-->
            <if test="cardOilsType !=null and cardOilsType !=''">
                AND card_oils_type = #{cardOilsType}
            </if>
        </where>
    </select>

    <!--获取用户用要充值电子会员卡(储蓄卡)信息-->
    <select id="getElectronicCardInfo" resultMap="CardBaseResultMap" parameterType="com.platform.yijia.pojo.CustomerElectronicCard">
        SELECT
            id,
            union_id,
            customer_no,
            customer_name,
            amt,
            card_oils_type,
            station_id,
            create_time,
            recently_time
        FROM
            customer_electronic_card
        <where>
            <if test="id !=null and id !=''">
                id = #{id}
            </if>
        </where>
    </select>

    <!--更新用户的电子会员卡的信息-->
    <update id="updateElectronicCardInfo" parameterType="com.platform.yijia.pojo.CustomerElectronicCard">
        UPDATE
            customer_electronic_card
        <set>
            <if test="amt !=null">
                amt = #{amt},
            </if>
            <if test="recentlyTime !=null">
                recently_time = #{recentlyTime}
            </if>
        </set>
        <where>
            <if test="id !=null and id != ''">
                id = #{id}
            </if>
        </where>
    </update>

    <!--根据unionId更新用户的电子会员卡的信息-->
    <update id="updateElectronicCardInfoByUnionId" parameterType="com.platform.yijia.pojo.CustomerElectronicCard">
        UPDATE customer_electronic_card
        <set>
            <if test="unionId !=null">
                union_id = #{unionId},
            </if>
            <if test="customerNo !=null">
                customer_no = #{customerNo},
            </if>
            <if test="customerName !=null">
                customer_name = #{customerName},
            </if>
            <if test="mobilePhone !=null">
                mobile_phone = #{mobilePhone},
            </if>
            <if test="amt !=null">
                amt = #{amt},
            </if>
            <if test="cardOilsType !=null">
                card_oils_type = #{cardOilsType},
            </if>
            <if test="stationId !=null">
                station_id = #{stationId},
            </if>
            <if test="createTime !=null">
                create_time = #{createTime},
            </if>
            <if test="recentlyTime !=null">
                recently_time = #{recentlyTime}
            </if>
        </set>
        <where>
            <if test="stationId !='' and stationId != null" >
                station_id = #{stationId}
            </if>
<!--            <if test="blogOpenid !=null and blogOpenid !=''">-->
<!--                AND blog_openid = #{blogOpenid}-->
<!--            </if>-->
<!--            <if test="minaOpenid !=null and minaOpenid !=''">-->
<!--                AND mina_openid = #{minaOpenid}-->
<!--            </if>-->
            <if test="unionId !=null and unionId != ''">
                AND union_id = #{unionId}
            </if>
            <if test="cardOilsType !='' and cardOilsType != null" >
                AND card_oils_type = #{cardOilsType}
            </if>
        </where>
    </update>

    <!--查询油站的会员卡启用标识 （1、启动 2，停用)-->
    <select id="getStationCardEnabledFlag" parameterType="Integer" resultType="String">
        SELECT
            card_enabled_flag AS cardEnabledFlag
        FROM
            sys_dept
        <where>
            <if test="stationId !='' and stationId != null" >
                dept_id = #{stationId}
            </if>
        </where>
    </select>

    <!--根据小程序用户openId查询客户电子会员卡信息-->
    <select id="getElectronicCardInfoByMinaOpenId" resultMap="CardBaseResultMap" parameterType="com.platform.yijia.pojo.CustomerElectronicCard">
        SELECT
            id,
            union_id,
            customer_no,
            customer_name,
            amt,
            card_oils_type,
            station_id,
            create_time,
            recently_time
        FROM
            customer_electronic_card
        <where>
            <if test="stationId !='' and stationId != null" >
                station_id = #{stationId}
            </if>
            <if test="minaOpenid !='' and minaOpenid != null" >
                AND mina_openid = #{minaOpenid}
            </if>
            <if test="cardOilsType !='' and cardOilsType != null" >
                AND card_oils_type = #{cardOilsType}
            </if>
        </where>
    </select>

</mapper>
