<?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.CustomerPointsMapper">
  <!--返回结果-->
  <resultMap id="BaseResultMap" type="com.platform.yijia.pojo.CustomerPoints">
    <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="mobile_phone"             jdbcType="VARCHAR"    property="mobilePhone" />
    <result column="customer_name"            jdbcType="VARCHAR"    property="customerName" />
    <result column="points"                   jdbcType="INTEGER"    property="points" />
    <result column="consumption_points"       jdbcType="INTEGER"    property="consumptionPoints" />
    <result column="accumulate_points"        jdbcType="INTEGER"    property="accumulatePoints" />
    <result column="invalid_points"           jdbcType="INTEGER"    property="invalidPoints" />
    <result column="recent_consumption_date"  jdbcType="TIMESTAMP"  property="recentConsumptionDate" />
    <result column="station_id"               jdbcType="INTEGER"    property="stationId" />
    <result column="station_name"             jdbcType="VARCHAR"    property="stationName" />
    <result column="create_time"              jdbcType="TIMESTAMP"  property="createTime" />
  </resultMap>

  <!--查询列-->
  <sql id="Base_Column_List">
    id, union_id, blog_openid, mina_openid, mobile_phone, customer_name, points, consumption_points, accumulate_points, invalid_points,
    recent_consumption_date, station_id, station_name, create_time
  </sql>

  <!--查询客户积分信息-->
  <select id="getCustomerPointsInfo" resultMap="BaseResultMap" parameterType="com.platform.yijia.pojo.CustomerPoints">
    SELECT
        <include refid="Base_Column_List" />
    FROM customer_points
    <where>
        <if test="unionId !=null and unionId !=''">
          union_id = #{unionId}
        </if>
      <if test="stationId !=null and stationId !=''">
        AND station_id = #{stationId}
      </if>
    </where>
  </select>

  <!--根据小程序用户的openId查询客户积分信息-->
  <select id="getCustomerPointsInfoByUnionId" resultMap="BaseResultMap" parameterType="com.platform.yijia.pojo.CustomerPoints">
    SELECT
    <include refid="Base_Column_List" />
    FROM customer_points
    <where>
<!--      <if test="minaOpenId !=null and minaOpenId !=''">-->
<!--        mina_openid = #{minaOpenId}-->
<!--      </if>-->
      <if test="unionId !=null and unionId !=''">
        union_id = #{unionId}
      </if>
      <if test="stationId !=null and stationId !=''">
        AND station_id = #{stationId}
      </if>
    </where>
  </select>


  <!--插入客户积分表-->
  <insert id="insertCustomerPointsInfo" parameterType="com.platform.yijia.pojo.CustomerPoints">
    INSERT INTO customer_points
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="unionId !=null">
        union_id,
      </if>
      <if test="blogOpenId !=null">
        blog_openid,
      </if>
      <if test="minaOpenId !=null">
        mina_openid,
      </if>
      <if test="mobilePhone !=null">
        mobile_phone,
      </if>
      <if test="customerName !=null">
        customer_name,
      </if>
      <if test="points !=null">
        points,
      </if>
      <if test="consumptionPoints !=null">
        consumption_points,
      </if>
      <if test="accumulatePoints !=null">
        accumulate_points,
      </if>
      <if test="invalidPoints !=null">
        invalid_points,
      </if>
      <if test="recentConsumptionDate !=null">
        recent_consumption_date,
      </if>
      <if test="stationId !=null">
        station_id,
      </if>
      <if test="stationName !=null">
        station_name,
      </if>
      <if test="createTime !=null">
        create_time,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="unionId !=null">
        #{unionId},
      </if>
      <if test="blogOpenId !=null">
        #{blogOpenId},
      </if>
      <if test="minaOpenId !=null">
        #{minaOpenId},
      </if>
      <if test="mobilePhone !=null">
        #{mobilePhone},
      </if>
      <if test="customerName !=null">
        #{customerName},
      </if>
      <if test="points !=null">
        #{points},
      </if>
      <if test="consumptionPoints !=null">
        #{consumptionPoints},
      </if>
      <if test="accumulatePoints !=null">
        #{accumulatePoints},
      </if>
      <if test="invalidPoints !=null">
        #{invalidPoints},
      </if>
      <if test="recentConsumptionDate !=null">
        #{recentConsumptionDate},
      </if>
      <if test="stationId !=null">
        #{stationId},
      </if>
      <if test="stationName !=null">
        #{stationName},
      </if>
      <if test="createTime !=null">
        #{createTime},
      </if>
    </trim>
  </insert>

  <!--更新客户积分表-->
  <update id="updateCustomerPointsInfo" parameterType="com.platform.yijia.pojo.CustomerPoints">
    UPDATE
        customer_points
    <set>
      <if test="blogOpenId !=null">
        blog_openid =#{blogOpenId},
      </if>
      <if test="minaOpenId !=null">
        mina_openid =#{minaOpenId},
      </if>
      <if test="mobilePhone !=null">
        mobile_phone=#{mobilePhone},
      </if>
      <if test="customerName !=null">
        customer_name =#{customerName},
      </if>
      <if test="points !=null">
        points =#{points},
      </if>
      <if test="consumptionPoints !=null">
        consumption_points =#{consumptionPoints},
      </if>
      <if test="accumulatePoints !=null">
        accumulate_points =#{accumulatePoints},
      </if>
      <if test="invalidPoints !=null">
        invalid_points =#{invalidPoints},
      </if>
      <if test="recentConsumptionDate !=null">
        recent_consumption_date =#{recentConsumptionDate},
      </if>
      <if test="stationId !=null">
        station_id =#{stationId},
      </if>
      <if test="stationName !=null">
        station_name =#{stationName},
      </if>
      <if test="createTime !=null">
        create_time =#{createTime},
      </if>
      <if test="unionId !=null and unionId !=''">
        union_id = #{unionId},
      </if>
    </set>
    <where>
      <if test="stationId !='' and 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>-->
    </where>
  </update>

  <!--判断是否存在-->
  <select id="isExistCustomerPointsInfo" resultType="int" parameterType="com.platform.yijia.pojo.CustomerPoints">
    SELECT
        COUNT(id)
    FROM
        customer_points
    <where>
      <if test="stationId !='' and 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>-->
    </where>
  </select>

</mapper>
