<?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.CashLogMapper">


 	<sql id="allCashLogField">
		CASH_LOGS_ID cashLogsId ,
		CASH_LOGS_OPENID cashLogsOpenid,
		CASH_LOGS_NUM cashLogsNum,
		CASH_LOGS_TYPE cashLogsType,
		CASH_LOGS_INTRODUCTION cashLogsIntroduction,
		CASH_LOGS_RES_TYPE cashLogsResType,
		CASH_LOGS_CREATE_DATE cashLogsCreateDate
	</sql>
	<!-- 	根据openid查询金额记录 -->
	<select id="getAllCashLog" resultType="CashLog"
		parameterType="int">
		SELECT 
			CASH_LOGS_OPENID cashLogsOpenid,
			CASH_LOGS_RES_TYPE cashLogsResType,
			CASH_LOGS_CREATE_DATE cashLogsCreateDate,
			CASH_LOGS_NUM cashLogsNum 
		FROM TB_IAMBERRY_USER_CASH_LOGS
		WHERE CASH_LOGS_OPENID = #{cashLogsOpenid}
		ORDER BY CASH_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>
	
	<insert id="addCashLog" parameterType="CashLog">
		INSERT INTO TB_IAMBERRY_USER_CASH_LOGS
		(
			CASH_LOGS_OPENID,
			CASH_LOGS_NUM ,
			CASH_LOGS_TYPE ,
			CASH_LOGS_INTRODUCTION ,
			CASH_LOGS_RES_TYPE ,
			CASH_LOGS_ORDERID,
			CASH_LOGS_CREATE_DATE 
		)
		VALUES
		(
			#{cashLogsOpenid},
			#{cashLogsNum},
			#{cashLogsType},
			#{cashLogsIntroduction},
			#{cashLogsResType},
			#{cashLogsOrderid},
			#{cashLogsCreateDate}
		)
	</insert>
	
	<!-- 根据openid和orderId查询信息 -->
	<select id="selectCashLogsInfo" parameterType="CashLog" resultType="CashLog">
		SELECT  
			<include refid="allCashLogField"/> 
		FROM 
			TB_IAMBERRY_USER_CASH_LOGS 
		WHERE 
			CASH_LOGS_OPENID = #{cashLogsOpenid} 
		AND 
			CASH_LOGS_ORDERID = #{cashLogsOrderid} 
		AND 
			CASH_LOGS_RES_TYPE = 4 
	</select>
	
	<!-- 修改会员返利日志表 -->
	<update id="updateCashLogs" parameterType="CashLog">
		UPDATE 
			TB_IAMBERRY_USER_CASH_LOGS 
		SET 
			CASH_LOGS_NUM = (CASH_LOGS_NUM + #{cashLogsNum}) 
		WHERE 
			CASH_LOGS_OPENID = #{cashLogsOpenid} 
		AND 
			CASH_LOGS_ORDERID = #{cashLogsOrderid} 
	</update>
	
	<!-- 根据 cashLogsOrderid 删除类型是 支出、提现的 现金记录 -->
	<delete id="deleteCashLogs" parameterType="Integer">
		DELETE FROM TB_IAMBERRY_USER_CASH_LOGS
		WHERE CASH_LOGS_ORDERID=#{cashLogsOrderid}
		AND CASH_LOGS_TYPE=2
		AND CASH_LOGS_RES_TYPE=2
	</delete>
</mapper> 
