<?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">
<!-- 命名，每一个映射对象不一样
	namespace:必须与对应的接口全类名一致
 -->
 <mapper namespace="com.iamberry.wechat.service.mapper.BindingMacMapper">
 
 	<sql id="allField">
 	 binding_id id,
 	 binding_mac_id macId,
 	 binding_openid openid,
 	 binding_drp_id  drpId,
 	 binding_create_date createDate,
 	 binding_update_date updateDate
 	</sql>
 	
 	<select id="getOneById" parameterType="Integer" resultType="BindingMac">
 	    SELECT 
		<include refid="allField"/>
        FROM tb_iamberry_binding_mac 
        WHERE binding_id=#{id}
 	</select>
 	
 	
 	<insert id="insert" >
 	 insert into 
 	  tb_iamberry_binding_mac(
 	  binding_mac_id  ,
 	  binding_openid  ,
 	  binding_drp_id  ,
 	  binding_create_date ,
 	  binding_update_date 
 	  )values(
 	  #{macId},#{openid},${drpId},#{createDate},#{updateDate}
 	  )
 	 </insert>
 	 
 	 <update id="update" >
 	    update tb_iamberry_binding_mac 
	     <set>
	     	<if test="macId!=null and macId!=''">
	          binding_mac_id=#{macId},
	        </if>
	        <if test="updateDate!=null and updateDate!=''">
	          binding_update_date =#{updateDate},
	        </if>
	        <if test="openid!=null and openid!=''">
	          binding_openid =#{openid},
	        </if>
	        <if test="drpId!=null and drpId!=''">
	          binding_drp_id =#{drpId},
	        </if>
	     </set> 
	      WHERE binding_mac_id=#{macId}
 	 </update>
 	 
 	<select id="selectAll" resultType="BindingMacInfo" parameterType="BindingMacInfo">
 	select 
 		c.id ,c.openid,c.macId , c.drpId , c.bindTime , c.drpName ,c.drpPhone ,
 		(select user_nickname from tb_iamberry_user_userinfo where user_openid=c.openid) userNickName
 	from 
 		(
			select 
				t.binding_id id,
		 		t.binding_openid openid , 
		 		t.binding_mac_id macId,
		 		t.binding_drp_id drpId,
		 		t.binding_update_date bindTime,
		 		b.place_username drpName,
		 		b.place_tel drpPhone 
		 	from 
		 		tb_iamberry_binding_mac t left join tb_iamberry_place_info b 
		 	on t.binding_drp_id =b.place_id 
		 	<where>
		 		<if test="macId!=null and macId!=''">
		        	t.binding_mac_id like '%${macId}%'
		        </if>
		        <if test="beginDate!=null and beginDate!=''">
		        	AND t.binding_update_date <![CDATA[>]]> #{beginDate}
		        </if>
		        <if test="endDate!=null and endDate!=''">
		        	AND t.binding_update_date <![CDATA[<]]> #{endDate}
		        </if>
		        <if test="openid!=null and openid!=''">
		        	AND t.binding_openid =#{openid}
		        </if>
		        <if test="drpId!=null and drpId!=''">
		        	AND t.binding_drp_id =#{drpId}
				</if>
		 	</where>
	 	    order by binding_create_date desc
		    <if test="page!=null and page.recordBegin>0 and page.pageSize>0 ">
		     limit ${page.recordBegin},${page.pageSize}
		    </if>
		     <if test="page!=null and page.recordBegin==0 and page.pageSize>0 ">
		     limit ${page.pageSize}
		    </if>  
		) c 
 	</select>
 	
 	
	<select id="getAllCount" resultType="integer" parameterType="BindingMac">
	 	 select count(t.binding_id)
	 	 from 
	 	 tb_iamberry_binding_mac t 
		 <where>
			<if test="macId!=null and macId!=''">
		  		t.binding_mac_id like '%${macId}%'
	        </if>
	        <if test="beginDate!=null and beginDate!=''">
	        	AND t.binding_update_date <![CDATA[>]]> #{beginDate}
	        </if>
	        <if test="endDate!=null and endDate!=''">
	        	AND t.binding_update_date <![CDATA[<]]> #{endDate}
	        </if>
	        <if test="openid!=null and openid!=''">
	        	AND t.binding_openid =#{openid}
	        </if>
	        <if test="drpId!=null and drpId!=''">
	        	AND t.binding_drp_id =#{drpId}
	        </if>
	 	  </where>
	</select>
 	
 	
 
    <select id="testExist" parameterType="String"  resultType="Integer">
     select IFNULL(count(binding_id),0) 
     FROM tb_iamberry_binding_mac 
      WHERE binding_mac_id=#{macId}
    </select>
 
 
	<delete id="deleteBindingById" parameterType="Integer">
		DELETE FROM TB_IAMBERRY_BINDING_MAC
		WHERE BINDING_ID=#{id}
	</delete>
 
 
 </mapper>