<?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.iamberry.wechat.service.mapper.ChargerReceiveMapper" >
  <resultMap id="BaseResultMap" type="ChargerReceive" >
    <id column="receive_id" property="receiveId" jdbcType="INTEGER" />
    <result column="receive_order_id" property="receiveOrderId" jdbcType="VARCHAR" />
    <result column="receive_open_id" property="receiveOpenId" jdbcType="VARCHAR" />
    <result column="color_id" property="colorId" jdbcType="INTEGER" />
    <result column="color_name" property="colorName" jdbcType="VARCHAR" />
    <result column="receive_address_info" property="receiveAddressInfo" jdbcType="VARCHAR" />
    <result column="receive_address_postnum" property="receiveAddressPostnum" jdbcType="VARCHAR" />
    <result column="receive_address_name" property="receiveAddressName" jdbcType="VARCHAR" />
    <result column="receive_address_tel" property="receiveAddressTel" jdbcType="VARCHAR" />
    <result column="receive_send_time" property="receiveSendTime" jdbcType="TIMESTAMP" />
    <result column="receive_post_firm" property="receivePostFirm" jdbcType="VARCHAR" />
    <result column="receive_post_num" property="receivePostNum" jdbcType="VARCHAR" />
    <result column="receive_postage" property="receivePostage" jdbcType="INTEGER" />
    <result column="receive_status" property="receiveStatus" jdbcType="INTEGER" />
    <result column="receive_transaction_id" property="receiveTransactionId" jdbcType="VARCHAR" />
    <result column="receive_transaction_date" property="receiveTransactionDate" jdbcType="TIMESTAMP" />
    <result column="receive_create_time" property="receiveCreateTime" jdbcType="TIMESTAMP" />
    <result column="receive_update_time" property="receiveUpdateTime" jdbcType="TIMESTAMP" />
  </resultMap>
  <sql id="Base_Column_List" >
    receive_id, receive_order_id, receive_open_id, color_id, color_name, receive_address_info, 
    receive_address_postnum, receive_address_name, receive_address_tel, receive_send_time, 
    receive_post_firm, receive_post_num, receive_postage, receive_status, receive_transaction_id, 
    receive_transaction_date, receive_create_time, receive_update_time
  </sql>

  <select id="selectByPrimaryKey" resultMap="BaseResultMap" parameterType="java.lang.Integer" >
    select 
    <include refid="Base_Column_List" />
    from tb_iamberry_charger_receive
    where receive_id = #{receiveId,jdbcType=INTEGER}
  </select>

  <select id="selectByOrderId" resultMap="BaseResultMap" parameterType="java.lang.String" >
    select
    <include refid="Base_Column_List" />
    from tb_iamberry_charger_receive
    where receive_order_id = #{orderId}
  </select>

  <delete id="deleteByPrimaryKey" parameterType="java.lang.Integer" >
    delete from tb_iamberry_charger_receive
    where receive_id = #{receiveId,jdbcType=INTEGER}
  </delete>
  <insert id="insert" parameterType="ChargerReceive" >
    insert into tb_iamberry_charger_receive (receive_id, receive_order_id, receive_open_id, 
      color_id, color_name, receive_address_info, 
      receive_address_postnum, receive_address_name, 
      receive_address_tel, receive_send_time, receive_post_firm, 
      receive_post_num, receive_postage, receive_status, 
      receive_transaction_id, receive_transaction_date
      )
    values (#{receiveId,jdbcType=INTEGER}, #{receiveOrderId,jdbcType=VARCHAR}, #{receiveOpenId,jdbcType=VARCHAR}, 
      #{colorId,jdbcType=INTEGER}, #{colorName,jdbcType=VARCHAR}, #{receiveAddressInfo,jdbcType=VARCHAR}, 
      #{receiveAddressPostnum,jdbcType=VARCHAR}, #{receiveAddressName,jdbcType=VARCHAR}, 
      #{receiveAddressTel,jdbcType=VARCHAR}, #{receiveSendTime,jdbcType=TIMESTAMP}, #{receivePostFirm,jdbcType=VARCHAR}, 
      #{receivePostNum,jdbcType=VARCHAR}, #{receivePostage,jdbcType=INTEGER}, #{receiveStatus,jdbcType=INTEGER}, 
      #{receiveTransactionId,jdbcType=VARCHAR}, #{receiveTransactionDate,jdbcType=TIMESTAMP}
      )
  </insert>
  <insert id="insertSelective" parameterType="ChargerReceive" >
    insert into tb_iamberry_charger_receive
    <trim prefix="(" suffix=")" suffixOverrides="," >
      <if test="receiveId != null" >
        receive_id,
      </if>
      <if test="receiveOrderId != null" >
        receive_order_id,
      </if>
      <if test="receiveOpenId != null" >
        receive_open_id,
      </if>
      <if test="colorId != null" >
        color_id,
      </if>
      <if test="colorName != null" >
        color_name,
      </if>
      <if test="receiveAddressInfo != null" >
        receive_address_info,
      </if>
      <if test="receiveAddressPostnum != null" >
        receive_address_postnum,
      </if>
      <if test="receiveAddressName != null" >
        receive_address_name,
      </if>
      <if test="receiveAddressTel != null" >
        receive_address_tel,
      </if>
      <if test="receiveSendTime != null" >
        receive_send_time,
      </if>
      <if test="receivePostFirm != null" >
        receive_post_firm,
      </if>
      <if test="receivePostNum != null" >
        receive_post_num,
      </if>
      <if test="receivePostage != null" >
        receive_postage,
      </if>
      <if test="receiveStatus != null" >
        receive_status,
      </if>
      <if test="receiveTransactionId != null" >
        receive_transaction_id,
      </if>
      <if test="receiveTransactionDate != null" >
        receive_transaction_date
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides="," >
      <if test="receiveId != null" >
        #{receiveId,jdbcType=INTEGER},
      </if>
      <if test="receiveOrderId != null" >
        #{receiveOrderId,jdbcType=VARCHAR},
      </if>
      <if test="receiveOpenId != null" >
        #{receiveOpenId,jdbcType=VARCHAR},
      </if>
      <if test="colorId != null" >
        #{colorId,jdbcType=INTEGER},
      </if>
      <if test="colorName != null" >
        #{colorName,jdbcType=VARCHAR},
      </if>
      <if test="receiveAddressInfo != null" >
        #{receiveAddressInfo,jdbcType=VARCHAR},
      </if>
      <if test="receiveAddressPostnum != null" >
        #{receiveAddressPostnum,jdbcType=VARCHAR},
      </if>
      <if test="receiveAddressName != null" >
        #{receiveAddressName,jdbcType=VARCHAR},
      </if>
      <if test="receiveAddressTel != null" >
        #{receiveAddressTel,jdbcType=VARCHAR},
      </if>
      <if test="receiveSendTime != null" >
        #{receiveSendTime,jdbcType=TIMESTAMP},
      </if>
      <if test="receivePostFirm != null" >
        #{receivePostFirm,jdbcType=VARCHAR},
      </if>
      <if test="receivePostNum != null" >
        #{receivePostNum,jdbcType=VARCHAR},
      </if>
      <if test="receivePostage != null" >
        #{receivePostage,jdbcType=INTEGER},
      </if>
      <if test="receiveStatus != null" >
        #{receiveStatus,jdbcType=INTEGER},
      </if>
      <if test="receiveTransactionId != null" >
        #{receiveTransactionId,jdbcType=VARCHAR},
      </if>
      <if test="receiveTransactionDate != null" >
        #{receiveTransactionDate,jdbcType=TIMESTAMP}
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective" parameterType="ChargerReceive" >
    update tb_iamberry_charger_receive
    <set >
      <if test="receiveOrderId != null" >
        receive_order_id = #{receiveOrderId,jdbcType=VARCHAR},
      </if>
      <if test="receiveOpenId != null" >
        receive_open_id = #{receiveOpenId,jdbcType=VARCHAR},
      </if>
      <if test="colorId != null" >
        color_id = #{colorId,jdbcType=INTEGER},
      </if>
      <if test="colorName != null" >
        color_name = #{colorName,jdbcType=VARCHAR},
      </if>
      <if test="receiveAddressInfo != null" >
        receive_address_info = #{receiveAddressInfo,jdbcType=VARCHAR},
      </if>
      <if test="receiveAddressPostnum != null" >
        receive_address_postnum = #{receiveAddressPostnum,jdbcType=VARCHAR},
      </if>
      <if test="receiveAddressName != null" >
        receive_address_name = #{receiveAddressName,jdbcType=VARCHAR},
      </if>
      <if test="receiveAddressTel != null" >
        receive_address_tel = #{receiveAddressTel,jdbcType=VARCHAR},
      </if>
      <if test="receiveSendTime != null" >
        receive_send_time = #{receiveSendTime,jdbcType=TIMESTAMP},
      </if>
      <if test="receivePostFirm != null" >
        receive_post_firm = #{receivePostFirm,jdbcType=VARCHAR},
      </if>
      <if test="receivePostNum != null" >
        receive_post_num = #{receivePostNum,jdbcType=VARCHAR},
      </if>
      <if test="receivePostage != null" >
        receive_postage = #{receivePostage,jdbcType=INTEGER},
      </if>
      <if test="receiveStatus != null" >
        receive_status = #{receiveStatus,jdbcType=INTEGER},
      </if>
      <if test="receiveTransactionId != null" >
        receive_transaction_id = #{receiveTransactionId,jdbcType=VARCHAR},
      </if>
      <if test="receiveTransactionDate != null" >
        receive_transaction_date = #{receiveTransactionDate,jdbcType=TIMESTAMP}
      </if>
    </set>
    where receive_id = #{receiveId,jdbcType=INTEGER}
  </update>


  <update id="updateByOrderId" parameterType="ChargerReceive" >
    update tb_iamberry_charger_receive
    <set >
      <if test="receiveOrderId != null" >
        receive_order_id = #{receiveOrderId,jdbcType=VARCHAR},
      </if>
      <if test="receiveOpenId != null" >
        receive_open_id = #{receiveOpenId,jdbcType=VARCHAR},
      </if>
      <if test="colorId != null" >
        color_id = #{colorId,jdbcType=INTEGER},
      </if>
      <if test="colorName != null" >
        color_name = #{colorName,jdbcType=VARCHAR},
      </if>
      <if test="receiveAddressInfo != null" >
        receive_address_info = #{receiveAddressInfo,jdbcType=VARCHAR},
      </if>
      <if test="receiveAddressPostnum != null" >
        receive_address_postnum = #{receiveAddressPostnum,jdbcType=VARCHAR},
      </if>
      <if test="receiveAddressName != null" >
        receive_address_name = #{receiveAddressName,jdbcType=VARCHAR},
      </if>
      <if test="receiveAddressTel != null" >
        receive_address_tel = #{receiveAddressTel,jdbcType=VARCHAR},
      </if>
      <if test="receiveSendTime != null" >
        receive_send_time = #{receiveSendTime,jdbcType=TIMESTAMP},
      </if>
      <if test="receivePostFirm != null" >
        receive_post_firm = #{receivePostFirm,jdbcType=VARCHAR},
      </if>
      <if test="receivePostNum != null" >
        receive_post_num = #{receivePostNum,jdbcType=VARCHAR},
      </if>
      <if test="receivePostage != null" >
        receive_postage = #{receivePostage,jdbcType=INTEGER},
      </if>
      <if test="receiveStatus != null" >
        receive_status = #{receiveStatus,jdbcType=INTEGER},
      </if>
      <if test="receiveTransactionId != null" >
        receive_transaction_id = #{receiveTransactionId,jdbcType=VARCHAR},
      </if>
      <if test="receiveTransactionDate != null" >
        receive_transaction_date = #{receiveTransactionDate,jdbcType=TIMESTAMP}
      </if>
    </set>
    where receive_order_id = #{receiveOrderId}
  </update>

  <update id="ship" parameterType="ChargerReceive" >
    update tb_iamberry_charger_receive
    <set >
        receive_post_firm = #{receivePostFirm,jdbcType=VARCHAR},
        receive_post_num = #{receivePostNum,jdbcType=VARCHAR},
        receive_status = #{receiveStatus,jdbcType=INTEGER}
    </set>
    where receive_id = #{receiveId}
  </update>

 <!-- 用于前台领取充电器功能 前台专用 */-->
  <select id="listChargerReceive" resultType="ChargerReceive" parameterType="ChargerReceive" >
       SELECT
            t.receive_id,
            t.receive_order_id,
            t.receive_open_id,
            t.color_id,
            t.color_name,
            t.receive_address_info,
            t.receive_address_postnum,
            t.receive_address_name,
            t.receive_address_tel,
            t.receive_send_time,
            t.receive_post_firm,
            t.receive_post_num,
            t.receive_postage,
            t.receive_status,
            t.receive_transaction_id,
            t.receive_transaction_date,
            t.receive_create_time,
            t.receive_update_time,
            iuu.user_nickname
        FROM
          tb_iamberry_charger_receive t
        LEFT JOIN tb_iamberry_user_userinfo iuu ON t.receive_open_id = iuu.user_openid
      <where>
          t.receive_status != 1
          <if test="receiveId != null" >
            AND t.receive_id = #{receiveId}
          </if>
          <if test="receiveOrderId != null  and receiveOrderId != ''" >
            AND t.receive_order_id = #{receiveOrderId}
          </if>
          <if test="receiveOpenId != null and receiveOpenId != ''" >
            AND t.receive_open_id = #{receiveOpenId}
          </if>
          <if test="colorId != null" >
            AND t.color_id = #{colorId}
          </if>

          <if test="receiveCreateTime != null and receiveCreateTime != ''" >
            AND  DATE_FORMAT(t.receive_create_time,'%Y') =  DATE_FORMAT(#{receiveCreateTime},'%Y')
          </if>
      </where>
      ORDER BY t.receive_create_time
  </select>

  <!-- 用于前台领取充电器功能 后台专用 */-->
  <select id="listAdminChargerReceive" resultType="ChargerReceive" parameterType="ChargerReceive" >
    SELECT
    t.receive_id,
    t.receive_order_id,
    t.receive_open_id,
    t.color_id,
    t.color_name,
    t.receive_address_info,
    t.receive_address_postnum,
    t.receive_address_name,
    t.receive_address_tel,
    t.receive_send_time,
    t.receive_post_firm,
    t.receive_post_num,
    t.receive_postage,
    t.receive_status,
    t.receive_transaction_id,
    t.receive_transaction_date,
    t.receive_create_time,
    t.receive_update_time,
    iuu.user_nickname
    FROM
    tb_iamberry_charger_receive t
    LEFT JOIN tb_iamberry_user_userinfo iuu ON t.receive_open_id = iuu.user_openid
    <where>
      <if test="userNickname != null and userNickname != ''" >
        AND iuu.user_nickname LIKE CONCAT('%',#{userNickname},'%')
      </if>
      <if test="receiveStatus != null" >
        AND t.receive_status = #{receiveStatus}
      </if>
      <if test="receiveId != null" >
        AND t.receive_id = #{receiveId}
      </if>
      <if test="receiveOrderId != null  and receiveOrderId != ''" >
        AND t.receive_order_id = #{receiveOrderId}
      </if>
      <if test="receiveOpenId != null and receiveOpenId != ''" >
        AND t.receive_open_id = #{receiveOpenId}
      </if>
      <if test="colorId != null" >
        AND t.color_id = #{colorId}
      </if>
      <if test="receiveAddressTel != null and receiveAddressTel != ''" >
        AND t.receive_address_tel  LIKE CONCAT('%',#{receiveAddressTel},'%')
      </if>
      <if test="receivePostNum != null and receivePostNum != ''" >
        AND t.receive_post_num = #{receivePostNum}
      </if>
    </where>
    ORDER BY t.receive_id DESC
  </select>

  <!-- 获取用户所注册的机器数量 -->
  <select id="getApparatusNumber" resultType="Integer" parameterType="String" >
    SELECT
        COUNT(t.apparatus_id)
    FROM
        tb_iamberry_member_apparatus t
    WHERE
        t.user_openid = #{openId}
  </select>

</mapper>