<?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.IntegralRuleMapper">
  <!--积分规则返回结果-->
  <resultMap id="BaseResultMap" type="com.platform.yijia.pojo.IntegralRule">
      <id column="id" jdbcType="INTEGER" property="id" />
      <result column="station_id"             jdbcType="INTEGER" property="stationId" />
      <result column="term_date_manage"       jdbcType="VARCHAR" property="termDateManage" />
      <result column="empty_date"             jdbcType="DATE"    property="emptyDate" />
      <result column="expiration_reminder"    jdbcType="VARCHAR" property="expirationReminder" />
      <result column="remind_date"            jdbcType="DATE"    property="remindDate" />
      <result column="integral_activity"      jdbcType="VARCHAR" property="integralActivity" />
      <result column="date_picker"            jdbcType="VARCHAR" property="datePicker" />
      <result column="date_picker_time"       jdbcType="VARCHAR" property="datePickerTime" />
      <result column="integral_proportion"    jdbcType="VARCHAR" property="integralProportion" />
      <collection property="integralRuleDetailList" resultMap="IntegralRuleDetailResultMap"/>
  </resultMap>

  <!--积分规则明细表-->
  <resultMap id="IntegralRuleDetailResultMap" type="com.platform.yijia.pojo.IntegralRuleDetail">
      <id     column="integralRuleDetailId"   jdbcType="INTEGER" property="integralRuleDetailId" />
      <result column="parent_id"              jdbcType="INTEGER" property="parentId"/>
      <result column="rule_type"              jdbcType="VARCHAR" property="ruleType" />
      <result column="rule_terms"             jdbcType="DECIMAL" property="ruleTerms" />
      <result column="grade"                  jdbcType="VARCHAR" property="gread" />
      <result column="oil_name"               jdbcType="VARCHAR" property="oilName" />
      <result column="sale_amt"               jdbcType="DECIMAL" property="saleAmt" />
      <result column="integral"               jdbcType="DECIMAL" property="integral" />
      <result column="oil_type"               jdbcType="VARCHAR" property="oilType" />
  </resultMap>

  <!--查询油站积分规则信息-->
  <select id="getIntegralRule" resultMap="BaseResultMap" parameterType="com.platform.yijia.pojo.IntegralRule">
    SELECT
        T1.id,
        T1.station_id,
        T1.term_date_manage,
        T1.empty_date,
        T1.expiration_reminder,
        T1.remind_date,
        T1.integral_activity,
        T1.date_picker,
        T1.date_picker_time,
        T1.integral_proportion,
        T2.id                           AS      integralRuleDetailId,
        T2.rule_type,
        T2.rule_terms,
        T2.parent_id,
        T2.grade,
        T2.oil_name,
        T2.sale_amt,
        T2.integral,
        T2.oil_type
    FROM
        integral_rule AS T1 LEFT JOIN integral_rule_detail AS T2 ON T1.id = T2.parent_id
    <where>
          <if test="stationId !=null and stationId !=''">
             T1.station_id = #{stationId}
          </if>
          <if test="oilName !=null and oilName !=''">
            AND T2.oil_name = #{oilName}
          </if>
          <if test="emptyDate !=null">
              <![CDATA[ AND T1.empty_date >=  #{emptyDate}  ]]>
          </if>
          <if test="oilType !=null and oilType !=''">
            AND T2.oil_type = #{oilType}
          </if>
    </where>
    ORDER BY T2.rule_terms DESC
  </select>

</mapper>
