<?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.PlaceWithDrawalsLogsMapper">
 
 <sql id="allField" >
 	w.PLACE_WITHDRAWALS_ID id,
	w.PLACE_OPENID openid,
	w.PLACE_WITHDRAWALS_PLACE_ID placeId,
	w.PLACE_WITHDRAWALS_NUM  withDrawalsNum,
	w.PLACE_WITHDRAWALS_NAME withDrawalsName,
	w.PLACE_LOGS_TEL tel,
	w.PLACE_LOGS_CREATE_DATE createDate,
	w.PLACE_ACCOUNT_NO  accountNo,
	w.PLACE_BANK_LOCAL  bankLocal,
	w.PLACE_STATUS  status
 </sql>
 
 <select id="getOneWithDrawalsLogs" parameterType="integer" resultType="PlaceWithDrawalsLogs">
	SELECT 
	<include refid="allField"/>
       FROM tb_iamberry_place_withdrawals_logs w WHERE w.PLACE_WITHDRAWALS_ID = #{id}
  </select>
 
  <select id="selectAll" resultType="PlaceWithDrawalsLogs" parameterType="PlaceWithDrawalsLogs">
		  select 
		<include refid="allField"/>
          from tb_iamberry_place_withdrawals_logs w
		<where>
			<if test="openid!=null and openid!=''" >
			   w.PLACE_OPENID=#{openid}
			</if>
			<if test="placeId!=null and placeId!=''" >
			   and w.PLACE_WITHDRAWALS_PLACE=#{placeId}
			</if>
			<if test="tel!=null and tel!=''" >
			  and w.PLACE_LOGS_TELR=#{tel}
			</if>
	    </where>
	    order by w.PLACE_LOGS_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> 
	</select>

   <select id="selectAllMore" resultType="PlaceWithDrawalsLogs" parameterType="PlaceWithDrawalsLogs">
		  select 
		<include refid="allField"/>,
		  (select role_name from tb_iamberry_place_role where role_id=p.place_role_id )  roleName,
		  p.place_name name
          from tb_iamberry_place_withdrawals_logs w
          left join tb_iamberry_place_info p
          on p.place_id=w.place_withdrawals_place_id
		<where>
		   <if test="openid!=null and openid!=''" >
			  w.PLACE_OPENID=#{openid}
		   </if>
			<if test="placeId!=null and placeId!=''" >
			   and w.PLACE_WITHDRAWALS_PLACE_id=#{placeId}
			</if>
			<if test="tel!=null and tel!=''" >
			  and w.PLACE_LOGS_TELR=#{tel}
			</if>
			<if test="status!=null and status!=''" >
			  and w.PLACE_STATUS=${status}
			</if>
			<if test="name!=null and name!=''" >
			  and p.place_name like CONCAT('%',#{name},'%')
			</if>
			<if test="beginDate!=null and beginDate!=''">
			  and w.place_logs_create_date &gt; #{beginDate}
			</if>
			<if test="endDate!=null and endDate!=''">
			  and w.place_logs_create_date &lt; #{endDate}
			</if>  
	       </where>
	    order by PLACE_LOGS_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> 
	</select>
   

	<select id="getAllCount" resultType="integer">
	  select count(PLACE_WITHDRAWALS_ID) from tb_iamberry_place_withdrawals_logs
		<where>
			<if test="openid!=null and openid!=''" >
			   PLACE_OPENID=#{openid}
			</if>
			<if test="placeId!=null and placeId!=''" >
			   and PLACE_WITHDRAWALS_PLACE_ID=#{placeId}
			</if>
			<if test="tel!=null and tel!=''" >
			  and PLACE_LOGS_TEL=#{tel}
			</if>
			<if test="status!=null and status!=''" >
			  and PLACE_STATUS=${status}
			</if>  
	    </where>
	  
	  </select>
	  
	  <select id="getAllCountMore" resultType="integer">
	  select count(PLACE_WITHDRAWALS_ID) from tb_iamberry_place_withdrawals_logs w
          left join tb_iamberry_place_info p
          on p.place_id=w.place_withdrawals_place_id
		<where>
			<if test="openid!=null and openid!=''" >
			 and  w.PLACE_OPENID=#{openid}
			</if>
			<if test="placeId!=null and placeId!=''" >
			   and w.PLACE_WITHDRAWALS_PLACE_ID=#{placeId}
			</if>
			<if test="tel!=null and tel!=''" >
			  and w.PLACE_LOGS_TEL=#{tel}
			</if>
			<if test="status!=null and status!=''" >
			  and w.PLACE_STATUS=${status}
			</if>
			<if test="name!=null and name!=''" >
			  and p.place_name like CONCAT('%',#{name},'%')
			</if>
			<if test="beginDate!=null and beginDate!=''">
			  and w.place_logs_create_date &gt; #{beginDate}
			</if>
			<if test="endDate!=null and endDate!=''">
			  and w.place_logs_create_date &lt; #{endDate}
			</if>  
	       </where>
	  </select>
	  
	 
	  <select id="getApplyCount" resultType="integer">
	    select count(PLACE_WITHDRAWALS_ID) from tb_iamberry_place_withdrawals_logs
		where DATE_FORMAT(place_logs_create_date,'%Y%m')=#{dateMonth}
		and place_openid=#{id}
		and place_status in(1,2)
	  </select>

   <insert id="insert">
 	  insert into 
 	  tb_iamberry_place_withdrawals_logs(
 	   	PLACE_OPENID ,
		PLACE_WITHDRAWALS_PLACE_ID ,
		PLACE_WITHDRAWALS_NUM  ,
		PLACE_WITHDRAWALS_NAME ,
		PLACE_LOGS_TEL ,
		PLACE_LOGS_CREATE_DATE ,
		PLACE_ACCOUNT_NO ,
	    PLACE_BANK_LOCAL ,
	    REMARK ,
		PLACE_STATUS 
 	   )
      values(
	   #{openid},
	   #{placeId},
	   #{withDrawalsNum},
	   #{withDrawalsName},
	   #{tel},
	   #{createDate},
	   #{accountNo},
	   #{bankLocal},
	   #{remark},
	   #{status}
	  )
 	</insert>
 
    <update id="update" >
	     update tb_iamberry_place_withdrawals_logs 
	     <set>
	     	<if test="status>0">
	          PLACE_STATUS =#{status},
	        </if>
	        <if test="remark!=null and remark!=''">
	          REMARK =#{remark}
	        </if>
	     </set> 
	      WHERE place_withdrawals_id = ${id}
     </update>
 
 
      <delete id="delete" parameterType="integer">
	      delete from tb_iamberry_place_withdrawals_logs  
	       WHERE place_withdrawals_id = #{id}
      </delete>

 <select id="getTotalMoneyByPlaceId" parameterType="Integer" resultType="Integer">
 		select sum(PLACE_WITHDRAWALS_NUM) toTalMoney from tb_iamberry_place_withdrawals_logs 
 		where PLACE_WITHDRAWALS_PLACE_ID = #{id} and PLACE_STATUS =2
  </select>

</mapper>
