<?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.LabelMapper">
    <!--标签用户返回结果-->
    <resultMap id="CustomerLabelBaseResultMap" type="com.platform.yijia.pojo.CustomerLabel">
        <id     column="id"               jdbcType="INTEGER" property="id" />
        <result column="station_id"       jdbcType="INTEGER" property="stationId" />
        <result column="phone"            jdbcType="VARCHAR" property="phone" />
        <result column="label_id"         jdbcType="VARCHAR" property="labelId" />
        <result column="create_by"        jdbcType="VARCHAR" property="createBy" />
        <result column="create_time"      jdbcType="DATE"    property="createTime" />
        <result column="update_by"        jdbcType="VARCHAR" property="updateBy" />
        <result column="update_time"      jdbcType="DATE"    property="updateTime" />
        <result column="del_flag"         jdbcType="VARCHAR" property="delFlag" />
    </resultMap>

  <!--标签规则返回结果-->
  <resultMap id="LabelRuleBaseResultMap" type="com.platform.yijia.pojo.LabelRule">
      <id     column="id"               jdbcType="INTEGER"      property="id" />
      <result column="station_id"       jdbcType="INTEGER"      property="stationId" />
      <result column="label_name"       jdbcType="VARCHAR"      property="labelName" />
      <result column="is_add_label"     jdbcType="VARCHAR"      property="isAddLabel" />

      <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="del_flag"         jdbcType="VARCHAR"      property="delFlag" />
      <collection property="labelRuleDetailList" resultMap="LabelRuleDetailResultMap"/>
  </resultMap>

  <!--标签规则明细表-->
  <resultMap id="LabelRuleDetailResultMap" type="com.platform.yijia.pojo.LabelRuleDetail">
      <id     column="labelRuleDetailId"      jdbcType="INTEGER"   property="labelRuleDetailId" />
      <result column="parent_id"              jdbcType="INTEGER"   property="parentId"/>
      <result column="oil_name"               jdbcType="VARCHAR"   property="oilName" />
      <result column="discount_term"          jdbcType="VARCHAR"   property="discountTerm" />
      <result column="discount_liters_amt"    jdbcType="VARCHAR"   property="discountLitersAmt" />
      <result column="discount_amt"           jdbcType="VARCHAR"   property="discountAmt" />
      <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="del_flag"               jdbcType="VARCHAR"   property="delFlag" />
  </resultMap>

    <!--查询油站标签规则信息-->
<!--    <select id="getLabelRuleDetailList" resultMap="LabelRuleBaseResultMap" parameterType="com.platform.yijia.pojo.LabelRule">-->
<!--        SELECT-->
<!--        T1.id,-->
<!--        T1.station_id,-->
<!--        T1.label_name,-->
<!--        T1.is_add_label,-->
<!--        T2.id                           AS      labelRuleDetailId,-->
<!--        T2.parent_id,-->
<!--        T2.oil_name,-->
<!--        T2.discount_term,-->
<!--        T2.discount_amt,-->
<!--        T2.discount_liters_amt,-->
<!--        T2.create_by,-->
<!--        T2.create_time-->
<!--        FROM-->
<!--        label_rule AS T1 LEFT JOIN label_rule_detail AS T2 ON T1.id = T2.parent_id-->
<!--        <where>-->
<!--            T2.del_flag ="0"-->
<!--            <if test="labelId !=null and labelId !=''">-->
<!--                AND T1.id = #{labelId}-->
<!--            </if>-->
<!--            <if test="stationId !=null and stationId !=''">-->
<!--                AND T1.station_id = #{stationId}-->
<!--            </if>-->
<!--            <if test="oilName !=null and oilName !=''">-->
<!--                AND T2.oil_name = #{oilName}-->
<!--            </if>-->
<!--        </where>-->
<!--    </select>-->


  <!--查询油站标签规则信息-->
  <select id="getLabelRuleList" resultMap="LabelRuleBaseResultMap" parameterType="com.platform.yijia.pojo.LabelRule">
    SELECT
        T1.id,
        T1.station_id,
        T1.label_name,
        T1.is_add_label,
        T1.create_by,
        T2.id                           AS      labelRuleDetailId,
        T2.parent_id,
        T2.oil_name,
        T2.discount_term,
        T2.discount_amt,
        T2.discount_liters_amt,
        T2.create_by,
        T2.create_time
    FROM
      label_rule AS T1 LEFT JOIN label_rule_detail AS T2 ON T1.id = T2.parent_id
    <where>
          <if test="delFlag !=null and delFlag !=''">
             T1.del_flag =#{delFlag}
          </if>
          <if test="delFlagDetail !=null and delFlagDetail !=''">
             AND T2.del_flag =#{delFlagDetail}
          </if>
          <if test="labelId !=null and labelId !=''">
             AND T1.id = #{labelId}
          </if>
          <if test="stationId !=null and stationId !=''">
             AND T1.station_id = #{stationId}
          </if>
          <if test="oilName !=null and oilName !=''">
            AND T2.oil_name = #{oilName}
          </if>
    </where>
  </select>

    <!--查询油站标签用户信息-->
    <select id="getCustomerLabel" resultMap="CustomerLabelBaseResultMap" parameterType="com.platform.yijia.pojo.CustomerLabel">
        SELECT
            id,
            phone,
            label_id,
            station_id,
            del_flag
        FROM
            customer_label
        <where>
                del_flag = "0"
            <if test="stationId !=null and stationId !=''">
                AND station_id = #{stationId}
            </if>
            <if test="phone !=null and phone !=''">
                AND phone = #{phone}
            </if>
            <if test="labelId !=null and labelId !=''">
                AND label_id = #{labelId}
            </if>
        </where>
    </select>

    <!--更新用户新标签组-->
    <update id="updateCustomerLabel" parameterType="com.platform.yijia.pojo.CustomerLabel">
        UPDATE customer_label
        <set>
            <if test="labelId !=null">
                label_id = #{labelId},
            </if>
            <if test="updateBy !=null">
                update_by = #{updateBy},
            </if>
            <if test="updateTime !=null">
                update_time = #{updateTime}
            </if>
        </set>
        <where>
            del_flag = "0"
            <if test="stationId !=null and stationId !=''">
                AND station_id = #{stationId}
            </if>
            <if test="phone !=null and phone !=''">
                AND phone = #{phone}
            </if>
        </where>
    </update>

    <!--标签插入新的用户-->
    <insert id="insertCustomerLabel" parameterType="com.platform.yijia.pojo.CustomerLabel">
        INSERT INTO customer_label
        <trim prefix="(" suffix=")" suffixOverrides=",">
            <if test="labelId !=null">
                label_id,
            </if>
            <if test="stationId !=null">
                station_id,
            </if>
            <if test="phone !=null">
                phone,
            </if>
            <if test="delFlag !=null">
                del_flag,
            </if>
            <if test="createTime !=null">
                create_time,
            </if>
            <if test="createBy !=null">
                create_by,
            </if>
        </trim>
        <trim prefix="values (" suffix=")" suffixOverrides=",">
            <if test="labelId !=null">
                #{labelId},
            </if>
            <if test="stationId !=null">
                #{stationId},
            </if>
            <if test="phone !=null">
                #{phone},
            </if>
            <if test="delFlag !=null">
                #{delFlag},
            </if>
            <if test="createTime !=null">
                #{createTime},
            </if>
            <if test="createBy !=null">
                #{createBy},
            </if>
        </trim>
    </insert>

</mapper>
