<?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.WithdrawMapper">
 	<resultMap type="Withdraw" id="withdraws">
		<id column="withdrawals_logs_id" property="withdrawalsLogsId" />
		<result column="withdrawals_openid" property="withdrawalsOpenid" />
		<result column="withdrawals_logs_integral_num" property="withdrawalsLogsIntegralNum" />
		<result column="withdrawals_logs_amount" property="withdrawalsLogsAmount" />
		<result column="withdrawals_logs_date" property="withdrawalsLogsDate" />
		<result column="withdrawals_username" property="withdrawalsUsername" />
		<result column="withdrawals_tel" property="withdrawalsTel" />
		<result column="withdrawals_status" property="withdrawalsStatus" />
		<result column="withdrawals_remark" property="withdrawalsRemark" />
	</resultMap> 
	<sql id="allField">
		withdrawals_logs_id withdrawalsLogsId,
		withdrawals_openid withdrawalsOpenid,
		withdrawals_logs_integral_num withdrawalsLogsIntegralNum,
		withdrawals_logs_amount withdrawalsLogsAmount,
		withdrawals_logs_date withdrawalsLogsDate,
		withdrawals_username withdrawalsUsername,
		withdrawals_tel withdrawalsTel,
		withdrawals_status withdrawalsStatus,
		withdrawals_remark withdrawalsRemark,
		WITHDRAWALS_CARD_NO WITHDRAWALSCARDNO,
		WITHDRAWALS_BANK_NAME WITHDRAWALSBANKNAME
	</sql>
	<select id="getCount" resultType="int">
		select count(withdrawals_logs_id) 
		from 
		tb_iamberry_user_integral_withdrawals_logs
	</select>
	<!-- 用户提现修改 -->
	<update id="updateWithdraw" parameterType="Withdraw">
		update tb_iamberry_user_integral_withdrawals_logs
		<set>
			<if test="withdrawals_openid!=null">
				withdrawals_openid = #{withdrawalsOpenid}
			</if>
			<if test="withdrawals_logs_integral_num!=null">
				withdrawals_logs_integral_num = #{withdrawalsLogsIntegralNum}
			</if>
			<if test="withdrawals_logs_amount!=null">
				withdrawals_logs_amount = #{withdrawalsLogsAmount}
			</if>
			<if test="withdrawals_logs_date!=null">
				withdrawals_logs_date = #{withdrawalsLogsDate}
			</if>
			<if test="withdrawals_username!=null">
				withdrawals_username = #{withdrawalsUsername}
			</if>
			<if test="withdrawals_tel!=null">
				withdrawals_tel = #{withdrawalsTel}
			</if>
			<if test="withdrawals_status!=null">
				withdrawals_status = #{withdrawalsStatus}
			</if>
		</set>
		where withdrawals_logs_id = #{withdrawalsLogsId}
	</update>
	
	<!-- 添加一条申请记录 -->
	<insert id="insertApplyLogs" parameterType="Withdraw"
		useGeneratedKeys="true" keyProperty="withdrawalsLogsId">
		INSERT INTO 
		 	TB_IAMBERRY_USER_INTEGRAL_WITHDRAWALS_LOGS
		 		(
			 		WITHDRAWALS_OPENID, WITHDRAWALS_LOGS_INTEGRAL_NUM,
			 		WITHDRAWALS_LOGS_AMOUNT, WITHDRAWALS_LOGS_DATE, WITHDRAWALS_USERNAME,
			 		WITHDRAWALS_TEL, WITHDRAWALS_STATUS,WITHDRAWALS_CARD_NO,
			 		WITHDRAWALS_BANK_NAME
				)
		 VALUES 
		  	( 
			  #{withdrawalsOpenid},  
			  #{withdrawalsLogsIntegralNum},
			  #{withdrawalsLogsAmount}, 
			  #{withdrawalsLogsDate},
			  #{withdrawalsUsername},
			  #{withdrawalsTel},
			  #{withdrawalsStatus},
			  #{withdrawalsCardNo},
			  #{withdrawalsBankName}
		  	)
	</insert>
	
	<!-- 分页查询用户提现记录 -->
	<select id="selectLogsByOpenid" parameterType="Page" resultType="Withdraw">
		SELECT 
			<include refid="allField"/>
		FROM
			TB_IAMBERRY_USER_INTEGRAL_WITHDRAWALS_LOGS
		<if test="params.openid != null">
		WHERE
			WITHDRAWALS_OPENID = #{params.openid}
		</if>
		ORDER BY
			WITHDRAWALS_LOGS_DATE DESC
		LIMIT #{pageNo}, #{pageSize}
	</select>
	
	<!-- 根据ID，查询提现记录 -->
	<select id="selectWithdrawLogsById" parameterType="Integer" resultType="Withdraw">
		SELECT 
			<include refid="allField"/>
		FROM
			TB_IAMBERRY_USER_INTEGRAL_WITHDRAWALS_LOGS
		WHERE
			WITHDRAWALS_LOGS_ID = #{logsid}
	</select>
	
	<!-- 条件总数 -->
	<select id="selectLogsCountByOpenid" parameterType="Page" resultType="Integer">
		SELECT 
			COUNT(WITHDRAWALS_LOGS_ID)
		FROM
			TB_IAMBERRY_USER_INTEGRAL_WITHDRAWALS_LOGS
		<if test="params.openid != null">
		WHERE
			WITHDRAWALS_OPENID = #{params.openid}
		</if>
	</select>
	
	<!-- 当前用户，已提现次数 -->
	<select id="selectUseLogsCount" parameterType="Withdraw" resultType="Integer">
		SELECT 
			COUNT(WITHDRAWALS_LOGS_ID)
		FROM
			TB_IAMBERRY_USER_INTEGRAL_WITHDRAWALS_LOGS
		WHERE
			WITHDRAWALS_LOGS_DATE >= #{withdrawalsLogsDate} AND WITHDRAWALS_OPENID = #{withdrawalsOpenid}
	</select>
	
	
	
	<select id="selectWithDrawAllSumMoney" parameterType="Page" resultType="Withdraw">
		SELECT
			withdrawals_status withdrawalsStatus,count(1)  withdrawalsLogsIntegralNum,IFNULL(sum(withdrawals_logs_amount),0)  sumMoney
		FROM 
			TB_IAMBERRY_USER_INTEGRAL_WITHDRAWALS_LOGS 
		where 1=1
			<if test="params.userInput!=null and params.userInput!=''">
				 AND withdrawals_username LIKE CONCAT('%',#{params.userInput},'%')  
 				 OR withdrawals_tel LIKE  CONCAT('%',#{params.userInput},'%')
			</if>
			<if test="params.beginDate!=null and params.beginDate!='' and params.endDate!=null and params.endDate!=''">
				 AND DATE(withdrawals_logs_date) BETWEEN #{params.beginDate} AND #{params.endDate}
			</if>
				 group by  withdrawals_status 
				
	</select>
	
	
	<select id="selectWithDraw" parameterType="Page" resultType="Withdraw">
		SELECT
			<include refid="allField" />
		FROM 
			TB_IAMBERRY_USER_INTEGRAL_WITHDRAWALS_LOGS 
		where 1=1
			<if test="params.userInput!=null and params.userInput!=''">
				 AND withdrawals_username LIKE CONCAT('%',#{params.userInput},'%')  
 				 OR withdrawals_tel LIKE  CONCAT('%',#{params.userInput},'%')
			</if>
			<if test="params.beginDate!=null and params.beginDate!='' and params.endDate!=null and params.endDate!=''">
				 AND DATE(withdrawals_logs_date) BETWEEN #{params.beginDate} AND #{params.endDate}
			</if>
			<if test="params.status!=null and params.status!=''">
				 AND withdrawals_status != #{params.status}
			</if>
			
			
		ORDER BY withdrawals_logs_date DESC 
		<if test="pageNo>=0 and pageSize>0 ">
		LIMIT 
			${pageNo},${pageSize}
		</if>	
	</select>
	<!-- 修改订单信息 -->
	<update id="updateWithdrawStatusById" parameterType="Withdraw">
		UPDATE 
			TB_IAMBERRY_USER_INTEGRAL_WITHDRAWALS_LOGS 
		SET 
			WITHDRAWALS_STATUS = #{withdrawalsStatus},
			withdrawals_remark = #{withdrawalsRemark}
		WHERE
			WITHDRAWALS_LOGS_ID = #{withdrawalsLogsId}
	</update>
</mapper>