<?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">
<mapper namespace="com.hzs.member.base.mapper.CuMemberRegisterMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="CuMemberRegisterVO" type="com.hzs.member.base.vo.CuMemberVO">
        <result column="NEW_MEMBER_CODE" property="newMemberCode"/>
        <result column="MEMBER_CODE" property="memberCode"/>
        <result column="MEMBER_NAME" property="memberName"/>
        <result column="CUSTOMER_TYPE" property="customerType"/>
        <result column="PHONE" property="phone"/>
        <result column="ID_CARD" property="idCard"/>
        <result column="SEX" property="sex"/>
        <result column="BIRTH_DATE" property="birthDate"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="PK_SETTLE_COUNTRY" property="pkSettleCountry"/>
        <result column="PAR_MEMBER_CODE" property="parMemberCode"/>
        <result column="PAR_MEMBER_NAME" property="parMemberName"/>
        <result column="PLACE_MEMBER_CODE" property="placeMemberCode"/>
        <result column="PLACE_MEMBER_NAME" property="placeMemberName"/>
        <result column="PLACE_DEPT" property="placeDept"/>
        <result column="CENTER_MEMBER_CODE" property="centerMemberCode"/>
        <result column="CENTER_MEMBER_NAME" property="centerMemberName"/>
        <result column="SOURCE" property="source"/>
        <result column="PK_REGISTER_GRADE" property="pkRegisterGrade"/>
        <result column="CONSUME_ACHIEVE" property="consumeAchieve"/>
        <result column="PAY_TIME" property="payTime"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="TOP_MEMBER_CODE" property="topMemberCode"/>
        <result column="TOP_MEMBER_NAME" property="topMemberName"/>
        <result column="WALLET_STATUS" property="walletStatus"/>
        <result column="ACCOUNT_STATUS" property="accountStatus"/>
        <result column="EDIT_NUMBER" property="editNumber"/>
    </resultMap>

    <select id="queryCuMemberRegisterExeByCondition" resultMap="CuMemberRegisterVO">
        select cm.member_code new_member_code,cr.member_code,cr.member_name,cr.phone,cr.customer_type,
        cr.pk_register_grade,par.member_code par_member_code,par.member_name par_member_name,
        place.member_code place_member_code,place.member_name place_member_name,place.place_dept,
        center.member_code center_member_code,center.member_name center_member_name,
        team.member_code team_member_code,team.member_name team_member_name,cm.source,
        cr.consume_achieve,cr.pay_time,
        top.member_code top_member_code,top.member_name top_member_name,
        ca.wallet_status,cm.account_status,cm.edit_number,cm.creation_time from cu_member_register cr
        inner join cu_member cm
        on cr.pk_member=cm.pk_id
        inner join cu_member_account ca
        on cm.pk_id=ca.pk_member
        inner join cu_member par
        on par.pk_id = cr.pk_parent
        inner join cu_member place
        on place.pk_id=cr.pk_place_parent
        left join cu_member_team ct
        on ct.pk_id=cr.pk_team_code
        left join cu_member team
        on team.pk_id=ct.pk_member
        left join cu_member center
        on center.pk_id =cr.pk_center_code
        left join cu_member top
        on top.pk_id = cm.pk_vertex
        where cr.del_flag=0 and cm.del_flag=0
        and ca.del_flag=0
        and cm.pk_settle_country=#{pkCountry}
        <if test="memberCode != null and memberCode != ''">
            and cm.member_code=#{memberCode}
        </if>
        <if test="memberName != null and memberName != ''">
            and cm.member_name=#{memberName}
        </if>
        <if test="phone != null and phone != ''">
            and cm.phone=#{phone}
        </if>
        <if test="pkGrade != null ">
            and cr.pk_register_grade =#{pkGrade}
        </if>
        <if test="pkAwards != null">
            and cm.pk_awards=#{pkAwards}
        </if>
        <if test="parent != null and parent != ''">
            and par.member_code=#{parent}
        </if>
        <if test="parentName != null and parentName !=''">
            and par.member_name=#{parentName}
        </if>
        <if test="placeParent != null and placeParent != ''">
            and place.member_code=#{placeParent}
        </if>
        <if test="placeParentName != null and placeParentName !=''">
            and place.member_name=#{placeParentName}
        </if>
        <if test="placeDept != null ">
            and cm.place_dept=#{placeDept}
        </if>
        <if test="teamCode != null and teamCode != ''">
            and team.member_code=#{teamCode}
        </if>
        <if test="teamName != null and teamName !=''">
            and ct.team_name =#{teamName}
        </if>
        <if test="centerCode != null and centerCode != ''">
            and center.member_code=#{centerCode}
        </if>
        <if test="vertexCode != null and vertexCode != ''">
            and top.member_code=#{vertexCode}
        </if>
        <if test="startDate != null">
            and cm.creation_time >= #{startDate, jdbcType=DATE}
        </if>
        <if test="endDate != null">
            and cm.creation_time &lt; #{endDate, jdbcType=DATE}
        </if>
    </select>

    <update id="updateCuMemberRegister">
        update cu_member_register
        set customer_type = 1,
            pk_modified   = #{pkModified},
            modified_time=sysdate
        where pk_member = #{pkMember}
          and pk_country = #{pkCountry}
          and del_flag = 0
    </update>

    <!-- 批量插入会员注册信息 -->
    <insert id="insertBatchMemberRegister">
        insert into CU_MEMBER_REGISTER (
        PK_ID,
        MEMBER_CODE,
        MEMBER_NAME,
        CUSTOMER_TYPE,
        PHONE,
        PK_COUNTRY,
        PK_SETTLE_COUNTRY,
        SOURCE,
        PK_REGISTER_GRADE,
        CONSUME_ACHIEVE,
        CREATION_TIME,
        PK_CREATOR,
        PK_PARENT,
        PK_TEAM_CODE,
        PK_VERTEX,
        PK_MEMBER
        )
        select CU_MEMBER_REGISTER_SEQ.NEXTVAL, t.*
        from (
        <foreach collection="registerMemberList" item="item" separator=" union all ">
            select
            #{item.memberCode} memberCode,
            #{item.memberName} memberName,
            #{item.customerType} customerType,
            #{item.phone} phone,
            #{item.pkCountry} pkCountry,
            #{item.pkSettleCountry} pkSettleCountry,
            #{item.source} source,
            #{item.pkRegisterGrade} pkRegisterGrade,
            #{item.consumeAchieve} consumeAchieve,
            #{item.creationTime} creationTime,
            #{item.pkCreator} pkCreator,
            #{item.pkParent} pkParent,
            #{item.pkTeamCode, jdbcType=NUMERIC} pkTeamCode,
            #{item.pkVertex, jdbcType=NUMERIC} pkVertex,
            #{item.pkMember} pkMember
            from dual
        </foreach>
        ) t
    </insert>


</mapper>
