<?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.detail.mapper.CuMemberServiceLogMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="resultMap" type="com.hzs.common.domain.member.detail.CuMemberServiceLogExt">
        <id column="PK_ID" property="pkId"/>
        <result column="PK_MEMBER" property="pkMember"/>
        <result column="EDIT_TYPE" property="editType"/>
        <result column="OLD_VALUE" property="oldValue"/>
        <result column="NEW_VALUE" property="newValue"/>
        <result column="DEL_FLAG" property="delFlag"/>
        <result column="PK_CREATOR" property="pkCreator"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="PK_MODIFIED" property="pkModified"/>
        <result column="MODIFIED_TIME" property="modifiedTime"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="EDIT_TYPE_PAY" property="editTypePay"/>
        <result column="MEMBER_CODE" property="memberCode"/>
        <result column="MEMBER_NAME" property="memberName"/>
    </resultMap>

    <resultMap id="CuMemberServiceLog" type="com.hzs.common.domain.member.detail.CuMemberServiceLog">
        <id column="PK_ID" property="pkId"/>
        <result column="PK_MEMBER" property="pkMember"/>
        <result column="EDIT_TYPE" property="editType"/>
        <result column="OLD_VALUE" property="oldValue"/>
        <result column="NEW_VALUE" property="newValue"/>
        <result column="DEL_FLAG" property="delFlag"/>
        <result column="PK_CREATOR" property="pkCreator"/>
        <result column="CREATION_TIME" property="creationTime"/>
        <result column="PK_MODIFIED" property="pkModified"/>
        <result column="MODIFIED_TIME" property="modifiedTime"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="EDIT_TYPE_PAY" property="editTypePay"/>
        <result column="SINGLE_AMOUNT" property="singleAmount"/>
        <result column="BUSINESS_SCOPE" property="businessScope"/>
    </resultMap>

    <resultMap id="CuMemberServiceLogVO" type="com.hzs.member.detail.vo.CuMemberServiceLogVO">
        <id column="PK_ID" property="pkId"/>
        <result column="PK_MEMBER" property="pkMember"/>
        <result column="EDIT_TYPE" property="editType"/>
        <result column="OLD_VALUE" property="oldValue"/>
        <result column="NEW_VALUE" property="newValue"/>
        <result column="APPROVE_NUMBER" property="approveNumber"/>
        <result column="APPROVE_STATUS" property="approveStatus"/>
        <result column="EDIT_TYPE_PAY" property="editTypePay"/>
        <result column="MEMBER_CODE" property="memberCode"/>
        <result column="MEMBER_NAME" property="memberName"/>
    </resultMap>

    <resultMap id="BusinessMemberVO" type="com.hzs.member.handlebusiness.vo.BusinessMemberVO">
        <id column="PK_ID" property="pkId"/>
        <result column="PK_MEMBER" property="pkMember"/>
        <result column="MEMBER_CODE" property="memberCode"/>
        <result column="MEMBER_NAME" property="memberName"/>
        <result column="PHONE" property="phone"/>
        <result column="PK_REGISTER_GRADE" property="pkRegisterGrade"/>
        <result column="GRADE_NAME" property="gradeName"/>
        <result column="PK_SETTLE_GRADE" property="pkSettleGrade"/>
        <result column="SETTLE_GRADE_NAME" property="settleGradeName"/>
        <result column="PK_AWARDS" property="pkAwards"/>
        <result column="AWARDS_NAME" property="awardsName"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
        <result column="SHORT_NAME" property="shortName"/>
        <result column="PK_SETTLE_COUNTRY" property="pkSettleCountry"/>
        <result column="SETTLE_SHORT_NAME" property="settleShortName"/>
        <result column="ACCOUNT_STATUS" property="accountStatus"/>
        <result column="INCOME_STATUS" property="incomeStatus"/>
        <result column="PURCHASE_STATUS" property="purchaseStatus"/>
        <result column="SHARE_STATUS" property="shareStatus"/>
        <result column="REGISTER_AUTHORITY" property="registerAuthority"/>
        <result column="IS_TRANSFER" property="isTransfer"/>
        <result column="IS_WITHDRAW" property="isWithdraw"/>
        <result column="IS_CONSUME" property="isConsume"/>
        <result column="TRANSFER_STATUS" property="transferStatus"/>
    </resultMap>


    <select id="selectByMaxApproveNumber" resultType="integer">
        select nvl(max(APPROVE_NUMBER),0) from CU_MEMBER_SERVICE_LOG
        where EDIT_TYPE = #{editType}
        and PK_MEMBER = #{pkMember}
        <if test="approveStatus!= null ">
            and APPROVE_STATUS =#{approveStatus}
        </if>
    </select>

    <!--查询会员办理业务日志表最新数据-->
    <select id="selectTopData" resultMap="CuMemberServiceLog">
        select *
        from (
                 select *
                 from CU_MEMBER_SERVICE_LOG
                 where EDIT_TYPE = #{editType}
                   and PK_MEMBER = #{pkMember}
                   and APPROVE_STATUS = #{approveStatus}
                   and del_flag = 0
                 order by CREATION_TIME desc)
        where rownum = 1
    </select>

    <!--查询用户是否已经存在未审核业务-->
    <select id="selectServiceLogList" resultType="com.hzs.member.detail.vo.CuMemberServiceLogVO"
            parameterType="com.hzs.member.detail.vo.CuMemberServiceLogVO">
        select b.MEMBER_CODE as memberCode,
        b.MEMBER_NAME as memberName
        from CU_MEMBER_SERVICE_LOG a LEFT JOIN CU_MEMBER b on a.PK_MEMBER=b.PK_ID
        where a.DEL_FLAG=0 and b.DEL_FLAG=0
        and (a.APPROVE_STATUS=1 or a.APPROVE_STATUS=2)
        <if test="pkMemberList != null and pkMemberList != ''">
            and a.PK_MEMBER in
            <foreach item="pkMember" collection="pkMemberList" open="(" separator="," close=")">
                #{pkMember}
            </foreach>
        </if>
        <if test="editTypeList != null and editTypeList != ''">
            and a.EDIT_TYPE in
            <foreach item="editType" collection="editTypeList" open="(" separator="," close=")">
                #{editType}
            </foreach>
        </if>
        <if test="pkMember!= null ">
            and a.PK_MEMBER =#{pkMember}
        </if>
        <if test="editType!= null ">
            and a.EDIT_TYPE =#{editType}
        </if>
    </select>

    <select id="selectByServiceLogList" resultMap="resultMap">
        select msl.*,cm.MEMBER_CODE,cm.MEMBER_NAME from CU_MEMBER_SERVICE_LOG msl
        left join CU_MEMBER cm on cm.PK_ID = msl.PK_MEMBER
        left join CU_HANDLE_BUSINESS hb on hb.BUSINESS_NO = msl.BUSINESS_NO
        where 1 = 1 and msl.DEL_FLAG = 0
        <if test="editType != null and editType != ''">
            and msl.EDIT_TYPE = #{editType}
        </if>
        <if test="pkMember != null">
            and msl.PK_MEMBER = #{pkMember}
        </if>
        <if test="businessNo != null and businessNo != ''">
            and msl.BUSINESS_NO = #{businessNo}
        </if>
        <if test="orderNumber != null and orderNumber != ''">
            and hb.ORDER_NUMBER = #{orderNumber}
        </if>
    </select>


    <!--查询审核通过的最新数据-->
    <select id="selectNewestServiceLog" resultMap="CuMemberServiceLogVO"
            parameterType="com.hzs.member.detail.vo.CuMemberServiceLogVO">
        select *
        from (select b.PK_ID,
                     b.pk_member,
                     b.OLD_VALUE,
                     b.NEW_VALUE,
                     b.APPROVE_NUMBER,
                     ROW_NUMBER() OVER(PARTITION BY b.pk_member ORDER BY b.APPROVE_NUMBER DESC) rw
              from CU_MEMBER_SERVICE_LOG b
              where b.EDIT_TYPE = #{editType}
                and b.APPROVE_STATUS = 5) a
        where a.rw = 1
          and a.pk_member = #{pkMember}
    </select>

    <!--查询会员原值-->
    <select id="selectOldValue"
            resultMap="BusinessMemberVO"
            parameterType="com.hzs.member.handlebusiness.vo.BusinessMemberVO">
        SELECT
        a.PK_ID pk_member,
        a.MEMBER_CODE,
        a.MEMBER_NAME,
        a.PHONE,
        a.PK_REGISTER_GRADE,
        c.GRADE_NAME,
        a.PK_SETTLE_GRADE,
        d.GRADE_NAME as SETTLE_GRADE_NAME,
        a.PK_AWARDS,
        e.AWARDS_NAME,
        a.PK_COUNTRY,
        f.SHORT_NAME,
        a.PK_SETTLE_COUNTRY,
        g.SHORT_NAME as SETTLE_SHORT_NAME,
        a.ACCOUNT_STATUS,
        a.INCOME_STATUS,
        a.PURCHASE_STATUS,
        a.SHARE_STATUS,
        a.REGISTER_AUTHORITY,
        a.IS_MAKER_SPACE,
        a.IS_DEALER,
        a.IS_REAL_NAME isRealName,
        a.SUPER_STUDIO,
        a.PK_MAX_AWARDS pkMaxAwards,
        b.IS_TRANSFER,
        b.IS_WITHDRAW,
        b.IS_CONSUME,
        b.TRANSFER_STATUS,
        a.IS_REGION isRegion,
        a.IS_ACTIVATE isActivate
        FROM
        CU_MEMBER a
        LEFT JOIN CU_MEMBER_ACCOUNT b ON a.PK_ID = b.PK_MEMBER
        LEFT JOIN BD_GRADE c ON a.PK_REGISTER_GRADE = c.PK_ID
        LEFT JOIN BD_GRADE d ON a.PK_SETTLE_GRADE = d.PK_ID
        LEFT JOIN BD_AWARDS e ON e.PK_ID = a.PK_AWARDS
        LEFT JOIN BD_COUNTRY f ON a.PK_COUNTRY = f.PK_ID
        LEFT JOIN BD_COUNTRY g ON a.PK_SETTLE_COUNTRY = g.PK_ID
        WHERE a.DEL_FLAG = 0
        <if test="pkMemberList != null and pkMemberList != ''">
            and a.PK_ID in
            <foreach item="pkMember" collection="pkMemberList" open="(" separator="," close=")">
                #{pkMember}
            </foreach>
        </if>
    </select>

    <select id="queryMaxCuMemberServiceLog" resultMap="CuMemberServiceLog">
        select to_number(ml.pk_member) pk_member,ml.edit_type,ml.old_value,ml.new_value from CU_MEMBER_SERVICE_LOG ml
        inner join(
        select pk_member,edit_type,max(pk_id) pk_id from CU_MEMBER_SERVICE_LOG where del_flag = 0
        and pk_member = #{pkMember} and BUSINESS_SCOPE = 1 and edit_type in
        <foreach collection="editTypeList" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        group by pk_member,edit_type) b
        on ml.pk_id = b.pk_id
        union
        select cm.pk_id pk_member,a.edit_type,a.old_value,a.new_value from bd_vertex bv
        inner join(
        select ml.pk_member,ml.edit_type,ml.old_value,ml.new_value from CU_MEMBER_SERVICE_LOG ml
        inner join(
        select pk_member,edit_type,max(pk_id) pk_id from CU_MEMBER_SERVICE_LOG where del_flag = 0
        and edit_type in
        <foreach collection="editTypeList" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        and BUSINESS_SCOPE=2
        group by pk_member,edit_type
        ) b
        on ml.pk_id = b.pk_id) a
        on bv.pk_member=a.pk_member
        inner join cu_member cm
        on cm.pk_vertex=bv.pk_id
        where cm.pk_id = #{pkMember} and cm.del_flag=0
    </select>


    <select id="selectServiceValue" resultMap="resultMap">
        select *
        from (select l.old_value,
        l.new_value,
        l.edit_type,
        l.creation_time,
        l.business_scope,
        ROW_NUMBER() OVER(PARTITION BY l.pk_member, l.edit_type, l.business_scope ORDER BY l.creation_time DESC) rw
        from cu_member_service_log l
        where l.del_flag = 0
        and l.pk_country = #{pkCountry}
        and l.edit_type in
        <foreach collection="editTypeList" item="item" open="(" close=")" separator=",">
            #{item}
        </foreach>
        and l.business_scope = 1
        and l.approve_status = 5
        and l.pk_member = #{pkMember}) tt
        where tt.rw = 1
        <if test="pkVertex != null">
            union all
            select *
            from (select l.old_value,
            l.new_value,
            l.edit_type,
            l.creation_time,
            l.business_scope,
            ROW_NUMBER() OVER(PARTITION BY l.pk_member, l.edit_type, l.business_scope ORDER BY l.creation_time DESC) rw
            from cu_member_service_log l
            where l.del_flag = 0
            and l.pk_country = #{pkCountry}
            and l.edit_type in
            <foreach collection="editTypeList" item="item" open="(" close=")" separator=",">
                #{item}
            </foreach>
            and l.business_scope = 2
            and l.approve_status = 5
            and l.pk_member = (select v.pk_member from bd_vertex v where v.pk_id =#{pkVertex})) tt
            where tt.rw = 1
        </if>
    </select>

    <select id="queryList" resultMap="CuMemberServiceLog">
        select cms.* from cu_member_service_log cms
        left join CU_MEMBER cm on cm.PK_ID = cms.PK_MEMBER
        where 1 = 1 and cms.del_flag = 0 and cms.PK_COUNTRY =#{pkCountry}
        <if test="approveStatus != null">
            and cms.APPROVE_STATUS = #{approveStatus}
        </if>
        <if test="editType != null">
            and cms.EDIT_TYPE = #{editType}
        </if>
        <if test="memberCode != null  and memberCode != ''">
            and cm.MEMBER_CODE = #{memberCode}
        </if>
        <if test="memberName != null  and memberName != ''">
            and cm.MEMBER_NAME = #{memberName}
        </if>
        order by cms.pk_id desc
    </select>

    <select id="selectByList" resultMap="CuMemberServiceLog">
        select cms.* from cu_member_service_log cms
        left join CU_MEMBER cm on cm.PK_ID = cms.PK_MEMBER
        where 1 = 1
        <if test="approveStatus != null">
            and cms.APPROVE_STATUS = #{approveStatus}
        </if>
        <if test="editType != null">
            and cms.EDIT_TYPE = #{editType}
        </if>
        <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="pkCountry != null">
            and cm.PK_COUNTRY = #{pkCountry}
        </if>
        order by cms.CREATION_TIME desc
    </select>

    <!-- 查询指定会员政策币信息 -->
    <select id="queryPolicy" resultType="com.hzs.common.domain.member.detail.CuMemberServiceLogExt">
        select *
        from (select *
              from CU_MEMBER_SERVICE_LOG
              where EDIT_TYPE = 125
                and PK_MEMBER = #{pkMember}
                and APPROVE_STATUS = 5
              order by CREATION_TIME desc)
        where rownum = 1
    </select>

    <!--查询会员办理业务日志表最新数据-->
    <select id="selectTopDataList" resultMap="CuMemberServiceLog">
        select *
        from (
                 select *
                 from CU_MEMBER_SERVICE_LOG
                 where 1=1
                <if test="editTypeList != null and editTypeList != ''">
                    and EDIT_TYPE in
                    <foreach item="editType" collection="editTypeList" open="(" separator="," close=")">
                        #{editType}
                    </foreach>
                </if>
                   and PK_MEMBER = #{pkMember}
                   and APPROVE_STATUS = #{approveStatus}
                   and del_flag = 0
                   <if test="actEndDateStr != null and actEndDateStr != '' ">
                        and CREATION_TIME &lt;= to_date(#{actEndDateStr} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
                   </if>
                 order by CREATION_TIME desc)
        where rownum &lt;= 2
    </select>

</mapper>
