<?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.CouponMapper">
    <!--优惠券-->
  <resultMap id="BaseResultMap"                type="com.platform.yijia.pojo.Coupon">
    <id     column="id"                       jdbcType="INTEGER"   property="couponId" />
    <result column="station_id"               jdbcType="INTEGER"   property="stationId" />
    <result column="coupon_name"              jdbcType="VARCHAR"   property="couponName" />
    <result column="coupon_type"              jdbcType="VARCHAR"   property="couponType" />
    <result column="coupon_threshold_amt"     jdbcType="DECIMAL"   property="couponThresholdAmt" />

    <result column="oil_name"                 jdbcType="VARCHAR"   property="oilName" />
    <result column="oil_type"                 jdbcType="VARCHAR"   property="oilType" />
    <result column="coupon_amt"               jdbcType="DECIMAL"   property="couponAmt" />
    <result column="coupon_num"               jdbcType="INTEGER"   property="couponNum" />
    <result column="effective_time_type"      jdbcType="VARCHAR"   property="effectiveTimeType" />
    <result column="effective_time_start"     jdbcType="TIMESTAMP" property="effectiveTimeStart" />
    <result column="effective_time_end"       jdbcType="TIMESTAMP" property="effectiveTimeEnd" />
    <result column="effective_day_num"        jdbcType="INTEGER"   property="effectiveDayNum" />

    <result column="coupon_receive_num"       jdbcType="INTEGER"   property="couponReceiveNum" />
    <result column="coupon_hold_num"          jdbcType="INTEGER"   property="couponHoldNum" />
    <result column="coupon_cumulative_num"    jdbcType="INTEGER"   property="couponCumulativeNum" />
    <result column="coupon_use_num"           jdbcType="INTEGER"   property="couponUseNum" />

    <result column="open_platform"            jdbcType="VARCHAR"   property="openPlatform" />
    <result column="grant_status"             jdbcType="VARCHAR"   property="grantStatus" />
    <result column="status"                   jdbcType="VARCHAR"   property="status" />
    <result column="create_by"                jdbcType="VARCHAR"   property="createBy" />
    <result column="create_time"              jdbcType="TIMESTAMP" property="createTime" />
    <result column="update_by"                jdbcType="VARCHAR"   property="updateBy" />

      <result column="update_by"              jdbcType="TIMESTAMP"   property="updateTime" />
      <result column="del_flag"               jdbcType="VARCHAR"   property="delFlag" />
      <result column="update_by"              jdbcType="VARCHAR"   property="couponDetails" />
      <result column="verification"           jdbcType="VARCHAR"   property="verification" />

      <result column="is_card_flag"           jdbcType="VARCHAR"   property="isCardFlag" />
      <result column="is_grade_flag"          jdbcType="VARCHAR"   property="isGradeFlag" />
      <result column="is_market_flag"         jdbcType="VARCHAR"   property="isMarketFlag" />
  </resultMap>

    <!--发放方式，优惠劵信息-->
   <resultMap id="CouponIssueBaseResultMap"       type="com.platform.yijia.pojo.CouponIssue">
       <result column="coupon_issue_id"           jdbcType="INTEGER"   property="couponIssueId" />
       <result column="issue_type"                jdbcType="VARCHAR"   property="issueType" />
       <result column="issue_name"                jdbcType="VARCHAR"   property="issueName" />
       <result column="discount_threshold_amt"    jdbcType="DECIMAL"   property="discountThresholdAmt" />
       <result column="status"                    jdbcType="VARCHAR"   property="status" />
       <result column="station_id"                jdbcType="INTEGER"   property="stationId" />
       <result column="create_by"                 jdbcType="VARCHAR"   property="createBy" />
       <result column="create_time"               jdbcType="TIMESTAMP" property="createTime" />
       <result column="update_by"                 jdbcType="VARCHAR"   property="updateBy" />
       <result column="update_time"               jdbcType="TIMESTAMP" property="updateTime" />
       <result column="show_id"                   jdbcType="VARCHAR"   property="showId" />
<!--       <collection property="couponInfoList" resultMap="BaseResultMap"/>-->
   </resultMap>

    <!--用户优惠券-->
  <resultMap id="UserCouponsBaseResultMap"    type="com.platform.yijia.pojo.UserCoupon">
    <result column="coupon_id"                jdbcType="INTEGER"   property="couponId" />
    <result column="union_id"                 jdbcType="VARCHAR"   property="unionId" />
    <result column="get_coupon_origin"        jdbcType="VARCHAR"   property="getCouponOrigin" />
    <result column="coupon_is_used"           jdbcType="VARCHAR"   property="couponIsUsed" />
    <result column="coupon_no"                jdbcType="VARCHAR"   property="couponNo" />
    <result column="coupon_is_effective"      jdbcType="VARCHAR"   property="couponIsEffective" />
    <result column="grant_coupon_shop"        jdbcType="INTEGER"   property="grantCouponShop" />
    <result column="get_coupon_time"          jdbcType="TIMESTAMP" property="getCouponTime" />
    <result column="grant_coupon_person"      jdbcType="INTEGER"   property="grantCouponPerson" />

    <result column="station_id"               jdbcType="INTEGER"   property="stationId" />
    <result column="coupon_name"              jdbcType="VARCHAR"   property="couponName" />
    <result column="coupon_type"              jdbcType="VARCHAR"   property="couponType" />
    <result column="coupon_threshold_amt"     jdbcType="DECIMAL"   property="couponThresholdAmt" />
    <result column="oil_type"                 jdbcType="VARCHAR"   property="oilType" />
    <result column="coupon_amt"               jdbcType="VARCHAR"   property="couponAmt" />
    <result column="coupon_num"               jdbcType="INTEGER"   property="couponNum" />
    <result column="effective_time_type"      jdbcType="VARCHAR"   property="effectiveTimeType" />
    <result column="effective_time_start"     jdbcType="TIMESTAMP" property="effectiveTimeStart" />
    <result column="effective_time_end"       jdbcType="TIMESTAMP" property="effectiveTimeEnd" />
    <result column="effective_day_num"        jdbcType="INTEGER"   property="effectiveDayNum" />
    <result column="coupon_receive_num"       jdbcType="INTEGER"   property="couponReceiveNum" />
    <result column="open_platform"            jdbcType="VARCHAR"   property="openPlatform" />
    <result column="grant_status"             jdbcType="VARCHAR"   property="grantStatus" />
    <result column="status"                   jdbcType="VARCHAR"   property="status" />

    <result column="dept_name"                jdbcType="VARCHAR"   property="stationName" />
  </resultMap>


  <sql id="Base_Column_List">
    id, station_id, coupon_name, coupon_type, coupon_threshold_amt, oil_name, oil_type, coupon_amt, coupon_num, effective_time_type,
    effective_time_start, effective_time_end, effective_day_num, coupon_receive_num, coupon_hold_num, coupon_cumulative_num, coupon_use_num,
    open_platform, status, create_by, create_time, update_by, update_time, del_flag, coupon_details, verification,
    is_card_flag, is_grade_flag, is_market_flag, edit_flag
  </sql>

    <!--获取油站所分发的加油券-->
    <select id="selectStaGrantCouponList" parameterType="com.platform.yijia.pojo.Coupon" resultMap="BaseResultMap">
        SELECT
            T2.id                                AS coupon_issue_id,
            T2.issue_name,
            T2.issue_type,
            T2.discount_threshold_amt,
            T3.coupon_amt,
            T3.coupon_type,
            T3.oil_name,
            T3.use_discount_type,
            T3.effective_time_type,
            T3.effective_time_start,
            T3.effective_time_end,
            T3.effective_day_num,
            T3.coupon_receive_num,
            T3.coupon_hold_num,
            T3.coupon_cumulative_num,
            T3.coupon_use_num,
            T3.open_platform,
            T3.coupon_details,
            T3.available_control,
            T3.appointed_days,
            T3.cycle_days
        FROM            coupon_issue_relation     AS T1
            LEFT JOIN   coupon_issue              AS T2 ON T1.issue_id = T2.id
            LEFT JOIN   coupon_info               AS T3 ON T1.coupon_id = T2.id
        <where>
            T2.status = "1"
            AND T3.del_flag = "0"
            AND T3.status = "1"
            <if test="stationId !=null">
                AND T2.station_id = #{stationId}
            </if>
            <if test="issueType !=null and issueType !=''">
                AND T2.issue_type = #{issueType}
            </if>
        </where>
    </select>


  <!--获取加油券信息-->
  <select id="selectCouponInfo" parameterType="com.platform.yijia.pojo.Coupon" resultMap="BaseResultMap">
        SELECT
            <include refid="Base_Column_List"/>
        FROM
            coupon_info
        <where>
          <if test="id !=null">
            id = #{id}
          </if>
        </where>
  </select>

  <!--获取用户所拥有的加油券-->
  <select id="selectUserCoupons" parameterType="com.platform.yijia.pojo.UserCoupon" resultMap="UserCouponsBaseResultMap">
    SELECT
        T1.union_id,
        T1.coupon_id,
        T1.coupon_is_effective,
        T1.coupon_is_used,
        T1.grant_coupon_shop,
        T1.grant_coupon_person,
        T1.get_coupon_origin,
        T1.get_coupon_time,
        T3.dept_name,
        T2.station_id,
        T2.oil_type,
        T2.coupon_name,
        T2.coupon_threshold_amt,
        T2.coupon_amt,
        T2.effective_time_type,
        T2.effective_time_start,
        T2.effective_time_end,
        T2.effective_day_num,
        T2.status
    FROM          coupon_user   AS  T1
        LEFT JOIN coupon_info   AS  T2 ON T1.coupon_id = T2.id
        LEFT JOIN sys_dept      AS  T3 ON T2.station_id = T3.dept_id
    <where>
      <if test="stationId !=null and stationId !=''">
        T1.station_id = #{stationId}
      </if>
      <if test="unionId !=null and unionId !=''">
       AND T1.union_id = #{unionId}
      </if>
    </where>
  </select>

    <!--用户获取优惠券-->
    <insert id="addUserCoupon" parameterType="com.platform.yijia.pojo.UserCoupon">
        INSERT INTO coupon_user
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="unionId !=null">
                union_id,
            </if>
            <if test="couponId !=null">
                coupon_id,
            </if>
            <if test="couponNo !=null">
                coupon_no,
            </if>
            <if test="getCouponOrigin !=null">
                get_coupon_origin,
            </if>
            <if test="couponIsUsed !=null">
                coupon_is_used,
            </if>
            <if test="couponIsEffective !=null">
                coupon_is_effective,
            </if>
            <if test="grantCouponShop !=null">
                grant_coupon_shop,
            </if>
            <if test="getCouponTime !=null">
                get_coupon_time,
            </if>
            <if test="grantCouponPerson !=null">
                grant_coupon_person,
            </if>
            <if test="stationId !=null">
                station_id,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="unionId !=null">
                #{unionId},
            </if>
            <if test="couponId !=null">
                #{couponId},
            </if>
            <if test="couponNo !=null">
                #{couponNo},
            </if>
            <if test="getCouponOrigin !=null">
                #{getCouponOrigin},
            </if>
            <if test="couponIsUsed !=null">
                #{couponIsUsed},
            </if>
            <if test="couponIsEffective !=null">
                #{couponIsEffective},
            </if>
            <if test="grantCouponShop !=null">
                #{grantCouponShop},
            </if>
            <if test="getCouponTime !=null">
                #{getCouponTime},
            </if>
            <if test="grantCouponPerson !=null">
                #{grantCouponPerson},
            </if>
            <if test="stationId !=null">
                #{stationId},
            </if>
        </trim>
    </insert>

</mapper>
