<?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.account.mapper.CuMemberRechargeMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="com.hzs.common.domain.member.account.CuMemberRecharge">
        <result column="DEL_FLAG" property="delFlag"/>
        <result column="PK_COUNTRY" property="pkCountry"/>
        <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_ID" property="pkId"/>
        <result column="PK_MEMBER" property="pkMember"/>
        <result column="RECHARGE_AMOUNT" property="rechargeAmount"/>
        <result column="PK_ACCOUNT" property="pkAccount"/>
        <result column="RECHARGE_TYPE" property="rechargeType"/>
        <result column="RECHARGE_CODE" property="rechargeCode"/>
        <result column="APPROVE_STATE" property="approveState"/>
        <result column="APPROVE_TIME" property="approveTime"/>
        <result column="APPROVER" property="approver"/>
        <result column="REMARKS" property="remarks"/>
        <result column="PK_RATE" property="pkRate"/>
        <result column="PK_ROLE" property="pkRole"/>
        <result column="ROLE_NAME" property="roleName"/>
        <result column="APPROVE_REMARKS" property="approveRemarks"/>
        <result column="BATCH" property="batch"/>
        <result column="RECHARGE_METHOD" property="rechargeMethod"/>
        <result column="RECHARGE_SOURCE" property="rechargeSource"/>
        <result column="MEMBER_REMARKS" property="memberRemarks"/>
    </resultMap>
    <sql id="select_base">
        select m.member_code            memberCode,
               m.member_name            memberName,
               r.pk_account,
               d.payment_method         paymentMethod,
               d.payment_before_balance paymentBeforeBalance,
               d.payment_amount         paymentAmount,
               d.payment_balance        paymentBalance,
               r.pk_rate,
               d.recharge_code,
               r.batch,
               NVL(u.user_name, cm.member_name) creator,
               a.user_name              approverName,
               r.creation_time,
               r.remarks,
               r.approver,
               r.approve_time,
               r.approve_remarks,
               r.approve_state
          from cu_member_recharge_detail d, cu_member_recharge r
          left join cu_member m
            on r.pk_member = m.pk_id
          left join sys_user u
            on u.user_id = r.pk_creator
          LEFT JOIN cu_member cm
            ON r.pk_creator = cm.pk_id
          left join sys_user a
            on a.user_id = r.approver
    </sql>

    <sql id="list_where">
        <where>
            r.recharge_code = d.recharge_code
            and r.del_flag='0'
            and r.recharge_source = 0
            <if test="memberName != null and memberName != ''">
                and m.member_name like '%' || #{memberName} || '%'
            </if>
            <if test="memberCode != null and memberCode != ''">
                and m.member_code like '%'|| #{memberCode} || '%'
            </if>
            <if test="paymentBalance != null">
                and d.PAYMENT_BALANCE = #{paymentBalance}
            </if>
            <if test="paymentMethod != null">
                and d.PAYMENT_METHOD = #{paymentMethod}
            </if>
            <if test="approveState != null">
                and r.APPROVE_STATE = #{approveState}
            </if>
            <if test="batch != null and batch != ''">
                and r.BATCH   like '%' || #{batch} || '%'
            </if>
            <if test="approver != null and approver != ''">
                and r.APPROVER like '%' || #{approver} || '%'
            </if>
            <if test="approveRemarks != null and approveRemarks != ''">
                and r.APPROVE_REMARKS like '%' || #{approveRemarks} || '%'
            </if>
            <if test="creator != null and creator != ''">
                and r.pk_creator = #{creator}
            </if>
            <if test="remarks != null and remarks != ''">
                and r.REMARKS like '%' || #{remarks} || '%'
            </if>
            <if test="pkAccount != null">
                and r.PK_ACCOUNT = #{pkAccount}
            </if>
            <if test="pkCountry != null">
                and r.PK_COUNTRY = #{pkCountry}
            </if>
            <if test="startDate != null and startDate != ''">
                and r.CREATION_TIME >= to_date(#{startDate}, 'yyyy-mm-dd')
            </if>
            <if test="endDate != null and endDate != ''">
                and r.CREATION_TIME &lt;= to_date(#{endDate} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
            </if>
            <if test="startApproveTime != null and startApproveTime != ''">
                and r.APPROVE_TIME >= to_date(#{startApproveTime}, 'yyyy-mm-dd')
            </if>
            <if test="endApproveTime != null and endApproveTime != ''">
                and r.APPROVE_TIME &lt;= to_date(#{endApproveTime} || ' 23:59:59', 'yyyy-mm-dd hh24:mi:ss')
            </if>
        </where>
    </sql>

    <select id="selectMemberRechargeById" resultType="com.hzs.common.domain.member.account.CuMemberRechargeExt">
        <include refid="select_base"/>
        where r.pk_id = #{pkId}
    </select>

    <insert id="batchSaveAccountByMemberCode" parameterType="com.hzs.common.domain.member.account.CuMemberRecharge">
        insert into CU_MEMBER_RECHARGE (PK_ID, PK_MEMBER, RECHARGE_AMOUNT, PK_ACCOUNT, RECHARGE_TYPE,
        RECHARGE_CODE, APPROVE_STATE, REMARKS, MEMBER_REMARKS,PK_COUNTRY, CREATION_TIME, PK_RATE,PK_CREATOR,BATCH,RECHARGE_METHOD)
        SELECT CU_MEMBER_RECHARGE_SEQ.Nextval,r.* FROM
        (
        <foreach collection="cuMemberRecharges" item="item" separator="UNION ALL">
            SELECT #{item.pkMember} PK_MEMBER ,#{item.rechargeAmount} RECHARGE_AMOUNT ,#{item.pkAccount} PK_ACCOUNT,#{item.rechargeType} RECHARGE_TYPE
            ,#{item.rechargeCode} RECHARGE_CODE ,#{item.approveState} APPROVE_STATE
            <choose>
                <when test="item.remarks != null">
                    ,#{item.remarks} REMARKS
                </when>
                <otherwise>
                   ,null REMARKS
                </otherwise>
            </choose>
            <choose>
                <when test="item.memberRemarks != null">
                    ,#{item.memberRemarks} MEMBER_REMARKS
                </when>
                <otherwise>
                    ,null MEMBER_REMARKS
                </otherwise>
            </choose>
            ,#{item.pkCountry} PK_COUNTRY,#{item.creationTime} CREATION_TIME,#{item.pkRate} PK_RATE,#{item.pkCreator} PK_CREATOR,#{item.batch} BATCH,#{item.rechargeMethod} RECHARGE_METHOD
            FROM dual
        </foreach>
        ) r
    </insert>

    <update id="updateBathRechargeByRechargeCode" parameterType="com.hzs.common.domain.member.account.CuMemberRecharge">
        update cu_member_recharge
        <set>
            <if test="approveState != null">approve_state = #{approveState},</if>
            <if test="approver != null">approver = #{approver},</if>
            <if test="approveTime != null">approve_time = #{approveTime},</if>
            <if test="pkModified != null">pk_modified = #{pkModified},</if>
            <if test="pkRole != null">pk_role = #{pkRole},</if>
            <if test="roleName != null">role_name = #{roleName},</if>
            modified_time = sysdate
        </set>
        where BATCH = #{rechargeCode}
    </update>

    <select id="selectMemberRechargeList" resultType="com.hzs.common.domain.member.account.CuMemberRechargeExt">
        <include refid="select_base"/>
        <include refid="list_where"></include>
        order by r.creation_time Desc,r.approve_time Desc
    </select>


    <select id="selectMemberRechargeListTotal" resultType="com.hzs.common.domain.member.account.CuMemberRechargeExt">
        select sum(d.payment_amount) paymentAmount,
        round(sum(d.payment_amount) / c.in_exchange_rate, 4) settlePaymentAmount
        from cu_member_recharge_detail d, cu_member_recharge r
        left join cu_member m
        on r.pk_member = m.pk_id
        left join bd_currency c
        on c.pk_id = r.pk_rate
        and c.enable_state = 0
        left join sys_user u
        on u.user_id = r.pk_creator
        left join sys_user a
        on a.user_id = r.approver
        <include refid="list_where"></include>
        group by c.in_exchange_rate
    </select>

    <select id="selectMemberRechargeBonusBySourceCode" resultType="com.hzs.common.domain.member.account.CuMemberRecharge">
        select *
          from cu_member_recharge r
          left join bd_account a
            on r.pk_account = a.pk_id
           and a.del_flag = 0
         where r.del_flag = 0
           and a.field_value = 4
           and r.batch = #{sourceCode}
    </select>


    <select id="selectMemberRechargeApproveByUserCode" resultType="String">
        select distinct m.member_code
          from cu_member_recharge r
          left join cu_member m
            on r.pk_member = m.pk_id
           and m.del_flag = 0
           and m.pay_status = 1
         where r.del_flag = 0
           and r.approve_state = 1
           and r.recharge_source=0
           and m.pk_settle_country = #{pkCountry}
           and m.member_code in
        <foreach item="memberCodes" collection="memberCodes" open="(" separator="," close=")">
            #{memberCodes}
        </foreach>
    </select>


    <select id="selectOperator" resultType="com.hzs.common.domain.member.account.CuMemberRechargeExt">
         select distinct r.pk_creator pkCreator,u.user_name memberName
          from cu_member_recharge r, sys_user u
         where r.pk_creator = u.user_id
           and r.del_flag = 0
           and u.del_flag = 0
           and r.pk_country = #{pkCountry}
    </select>



    <select id="selectRechargeTotalAmount" resultType="com.hzs.common.domain.member.account.CuMemberRecharge">
        select sum(t.recharge_amount) rechargeAmount
        from CU_MEMBER_RECHARGE t
        where t.del_flag = 0
        and t.recharge_amount != 0
        and t.pk_account = (select t.pk_id
        from bd_account t
        where t.del_flag = 0
        and t.enable_state = 0
        and t.pk_country = 1
        and t.field_value = 2)
        and ((t.recharge_source = 1 and t.approve_state = 5) or
        (t.recharge_source = 0 and t.approve_state = 2))
        and t.modified_time  >= #{startDate}
        and t.modified_time &lt; #{endDate}
    </select>
</mapper>
