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

	<sql id="agentApplyAllField">
		apply_id applyId,
		apply_openid applyOpenid,
		apply_is_read applyIsRead,
		apply_status applyStatus,
		apply_create_date applyCreateDate
	</sql>
	
	<select id="getAllApplyByOpenid" parameterType="AgentApply" 
			resultType="AgentApply">
		SELECT
			a.apply_id applyId,
			a.apply_openid applyOpenid,
			a.apply_is_read applyIsRead,
			a.apply_status applyStatus,
			a.apply_create_date applyCreateDate,
			member.user_id as 'member.userId',
			member.user_openid as 'member.userOpenid',
			member.user_head as 'member.userHead',
			member.user_nickname as 'member.userNickName',
			member.user_addr as 'member.userAddr',
			member.user_sex as 'member.userSex',
			member.user_integral as 'member.userIntegral',
			member.user_account_num as 'member.userAccountNum',
			member.user_pwd as 'member.userPwd',
			member.user_tel as 'member.userTel',
			member.user_name as 'member.userName',
			member.user_birth_date as 'member.userBirthDate',
			member.user_surplus_integral as 'member.userSurplusIntegral',
			member.user_use_integral as 'member.userUseIntegral',
			member.user_status as 'member.userStatus',
			member.user_sub_date as 'member.userSubDate',
			member.user_create_date as 'member.userCreateDate',
			member.user_identity as 'member.userIdentity',
			member.user_qrcode as 'member.userQrcode',
			member.user_is_flag as 'member.userIsFlag'
		FROM tb_iamberry_user_apply_logs a
		JOIN TB_IAMBERRY_USER_USERINFO member
		ON a.apply_openid=member.user_openid 
		<where> 
			<if test="applyId != null and applyId != ''">
				a.apply_id = #{applyId}
			</if>
			<if test="applyStatus != null and applyStatus != ''">
				AND a.apply_status = #{applyStatus}
			</if>
			ORDER BY a.apply_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>
	    </where>
	</select>
		
	<!-- 查询符合条件的记录总数 -->
	<select id="getAllApplyCount" parameterType="AgentApply" 
			resultType="int">
		SELECT
			COUNT(a.apply_id)
		FROM tb_iamberry_user_apply_logs a
		JOIN TB_IAMBERRY_USER_USERINFO member
		ON a.apply_openid=member.user_openid
		<where> 
			<if test="applyId != null and applyId != ''">
				a.apply_id = #{applyId}
			</if>
			<if test="applyStatus != null and applyStatus != ''">
				AND a.apply_status = #{applyStatus}
			</if>
		</where>
	</select>
	<!-- 根据id查一个申请 -->
	<select id="getAgentApplyById" parameterType="int" resultType="AgentApply">
		SELECT 
		<include refid="agentApplyAllField"></include>
		FROM tb_iamberry_user_apply_logs
		WHERE apply_id=#{id}
	</select>
	
	<!-- 根据openid查所有申请 -->
	<select id="getAgentApplyByOpenId" parameterType="String" resultType="AgentApply">
		SELECT 
		<include refid="agentApplyAllField"></include>
		FROM tb_iamberry_user_apply_logs 
		WHERE apply_openid=#{id} 
	</select>
	
	<!-- 添加一个申请 -->
	<insert id="addAgentApply" parameterType="AgentApply"
			useGeneratedKeys="true" keyProperty="applyId">
		INSERT INTO tb_iamberry_user_apply_logs
		(
			apply_id,
			apply_openid,
			apply_is_read,
			apply_status,
			apply_create_date
		)
		VALUES
		(
			#{applyId},#{applyOpenid},#{applyIsRead},#{applyStatus},#{applyCreateDate}
		)
	</insert>
	
	<!-- 根据applyId 修改一个申请 -->
	<update id="updateAgentApplyByApplyId" parameterType="AgentApply">
		UPDATE tb_iamberry_user_apply_logs 
		<set>
	     	<if test="applyOpenid!=null and applyOpenid!=''">
				apply_openid=#{applyOpenid},
	        </if>
	        <if test="applyIsRead!=null and applyIsRead!=''">
				apply_is_read=#{applyIsRead},
	        </if>
	        <if test="applyStatus!=null and applyStatus!=''">
				apply_status=#{applyStatus},
	        </if>
	        <if test="applyCreateDate!=null and applyCreateDate!=''">
				apply_create_date=#{applyCreateDate}
	        </if>
		</set>
		WHERE apply_id=#{applyId}
	</update>
</mapper>
