<?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.yiqi.recharge.mapper.RechargeCardBatchRecordMapper">

    <select id="listCardUseRecordByBatchNo" resultType="com.yiqi.recharge.domain.vo.RechargeBatchUseRecordListQueryVO">
        select aubm.batch_no as batchNo, aubm.recharge_card_id as rechargeCardId,
               aubm.org_id as orgId, aubm.source_type as sourceType, aubm.bill_type as billType,
               aubm.order_no as orderNo, aubm.app_user_id as appUserId, au.real_name as appUserName,au.phone_number as phoneNumber,
               aubm.create_time as createTime, aubm.title as title, aubm.order_type as orderType, aubm.is_refund as isRefund,
               aubm.pay_total_amount as payTotalAmount, aubm.recharge_pay_amount as rechargePayAmount, aubm.give_pay_amount as givePayAmount,
               aubm.welfare_pay_amount as welfarePayAmount

            from  app_user_bill_mstr aubm
            left join app_user au on aubm.app_user_id = au.id
        <where>
            aubm.batch_no = #{batchNo}
            <if test="rechargeCardId != null  and rechargeCardId != ''">
                and aubm.recharge_card_id= #{rechargeCardId}
            </if>
            <if test="orderNo != null  and orderNo != ''">
                and aubm.order_no = #{orderNo}
            </if>
            <if test="beginCreateTime != null and beginCreateTime !=''">
                <![CDATA[and aubm.create_time >= concat(#{beginCreateTime}, ' 00:00:00')]]>
            </if>
            <if test="endCreateTime != null and endCreateTime !=''">
                <![CDATA[and aubm.create_time <= concat(#{endCreateTime}, ' 23:59:59')]]>
            </if>
            <if test="appUserName != null  and appUserName != ''">
                and au.real_name like concat('%', #{appUserName}, '%')
            </if>
            <if test="phoneNumber != null  and phoneNumber != ''">
                and au.phone_number = #{phoneNumber}
            </if>
            <if test="orgId != null  and orgId != ''">
                and aubm.org_id = #{orgId}
            </if>
            <if test="sourceType != null  and sourceType != ''">
                and aubm.source_type = #{sourceType}
            </if>
        </where>
        order by aubm.create_time desc
        limit #{startRow},#{pageSize}
    </select>
    <select id="countCardUseRecordByBatchNo" resultType="java.lang.Integer">
        select count(1)
        from  app_user_bill_mstr aubm
        left join app_user au on aubm.app_user_id = au.id
        <where>
            aubm.batch_no = #{batchNo}
            <if test="rechargeCardId != null  and rechargeCardId != ''">
                and aubm.recharge_card_id= #{rechargeCardId}
            </if>
            <if test="orderNo != null  and orderNo != ''">
                and aubm.order_no = #{orderNo}
            </if>
            <if test="beginCreateTime != null and beginCreateTime !=''">
                <![CDATA[and aubm.create_time >= concat(#{beginCreateTime}, ' 00:00:00')]]>
            </if>
            <if test="endCreateTime != null and endCreateTime !=''">
                <![CDATA[and aubm.create_time <= concat(#{endCreateTime}, ' 23:59:59')]]>
            </if>
            <if test="appUserName != null  and appUserName != ''">
                and au.real_name like concat('%', #{appUserName}, '%')
            </if>
            <if test="phoneNumber != null  and phoneNumber != ''">
                and au.phone_number = #{phoneNumber}
            </if>
            <if test="orgId != null  and orgId != ''">
                and aubm.org_id = #{orgId}
            </if>
            <if test="sourceType != null  and sourceType != ''">
                and aubm.source_type = #{sourceType}
            </if>
        </where>
    </select>

    <select id="listCardDetailByBatchNo"
            resultType="com.yiqi.recharge.domain.vo.RechargeBatchCardDetailListQueryVO">
        select rpc.batch_no as batchNo, rpc.id as rechargeCardId, rpc.card_no as cardNo, rpc.recharge_status as rechargeStatus,
        rpc.recharge_org_id as rechargeOrgId, rpc.recharge_source_type as rechargeSourceType, rpc.id as rechargeUserId,
        rpc.recharge_amount as rechargeAmount, rpc.recharge_balance as rechargeBalance, rpc.give_balance as giveBalance,
        rpc.welfare_balance as welfareBalance, rpc.give_amount as giveAmount, rpc.welfare_amount as welfareAmount,
        rpc.is_support_cloth as isSupportCloth, rpc.is_support_goods as isSupportGoods, rpc.is_support_life as isSupportLife,
        rpc.is_support_car as isSupportCar, rpc.star_level as starLevel, rpc.expire_time as expireTime, rpc.create_time as createTime,
        au.real_name as appUserName, au.phone_number as phoneNumber
        from recharge_password_card rpc
        left join app_user au on rpc.recharge_user_id = au.id
        <where>
            rpc.batch_no = #{batchNo} and rpc.del_flag = '0' and rpc.status = '0'
            <if test="rechargeCardIds != null  and rechargeCardIds.size > 0 ">
                and rpc.id in
                <foreach item="id" collection="rechargeCardIds" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
            <if test="cardNo != null  and cardNo != ''">
                and rpc.card_no = #{cardNo}
            </if>
            <if test="phoneNumber != null  and phoneNumber != ''">
                and au.phone_number = #{phoneNumber}
            </if>
            <if test="orgId != null  and orgId != ''">
                and rpc.recharge_org_id = #{orgId}
            </if>
            <if test="sourceType != null  and sourceType != ''">
                and rpc.recharge_source_type = #{sourceType}
            </if>
        </where>
        order by rpc.create_time desc
        limit #{startRow},#{pageSize}
    </select>
    <select id="countCardDetailByBatchNo" resultType="java.lang.Integer">
        select count(1)
        from recharge_password_card rpc
        left join app_user au on rpc.recharge_user_id = au.id
        <where>
            rpc.batch_no = #{batchNo} and rpc.del_flag = '0' and rpc.status = '0'
            <if test="rechargeCardIds != null  and rechargeCardIds.size > 0 ">
                and rpc.id in
                <foreach item="id" collection="rechargeCardIds" open="(" separator="," close=")">
                    #{id}
                </foreach>
            </if>
            <if test="cardNo != null  and cardNo != ''">
                and rpc.card_no = #{cardNo}
            </if>
            <if test="phoneNumber != null  and phoneNumber != ''">
                and au.phone_number = #{phoneNumber}
            </if>
            <if test="orgId != null  and orgId != ''">
                and rpc.recharge_org_id = #{orgId}
            </if>
            <if test="sourceType != null  and sourceType != ''">
                and rpc.recharge_source_type = #{sourceType}
            </if>
        </where>
    </select>
</mapper>