<?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.crm.ucard.dao.mapper.CardTransacOrderMapper">

    <select id="countList" resultType="integer"
            parameterType="com.crm.rely.backend.core.entity.ucard.CardTransacOrderPageEntity">
        SELECT COUNT(*)
        FROM ucard_transac_order t
        LEFT JOIN ucard_card c ON t.card_no = c.card_no
        LEFT JOIN ucard_type b ON c.card_type_id = b.card_type_id
        LEFT JOIN ucard_type_config ut ON ut.card_type_id = b.card_type_id
        LEFT JOIN ucard_merchant_user uc ON c.unique_id = uc.unique_id
        <where>
            <if test="cardNumber!=null and cardNumber!=''">
                and c.`card_number` like concat(#{cardNumber},'%')
            </if>
            <if test="currency!=null and currency!=''">
                and t.`currency` = #{currency}
            </if>
            <if test="type != null and type != ''">
                and t.`type` = #{type}
            </if>
            <if test="status!=null and status!=''">
                <choose>
                    <when test="status == 'success'">
                        and (t.`status` = 'success' or t.`status` = 'succeed')
                    </when>
                    <when test="status == 'fail'">
                        and (t.`status` = 'fail' or t.`status` = 'failed')
                    </when>
                    <otherwise>
                        and t.`status` = #{status}
                    </otherwise>
                </choose>
            </if>
            <if test="beginDate != null and beginDate != '' and beginDate > 0">
                and t.settle_date <![CDATA[ >= ]]> #{beginDate}
            </if>
            <if test="endDate != null and endDate != '' and endDate > 0">
                and t.settle_date <![CDATA[ <= ]]> #{endDate}
            </if>
            <if test="cardType != null and cardType != ''">
                and b.`type` = #{type}
            </if>
            <if test="email!=null and email!=''">
                and uc.`email` like concat(#{email},'%')
            </if>
            <if test="mobile!=null and mobile!=''">
                and uc.`mobile` like concat(#{mobile},'%')
            </if>
            <if test="cId != null and cId > 0">
                and uc.`c_id` = #{cId}
            </if>
        </where>
    </select>

    <select id="pageList" resultType="com.crm.rely.backend.core.dto.ucard.CardTransacOrderDto"
            parameterType="com.crm.rely.backend.core.entity.ucard.CardTransacOrderPageEntity">
        SELECT t.*,
               b.`type` AS card_type,
               b.card_type_id,
               COALESCE(ut.card_name, b.card_name) as card_name,
               uc.c_id,uc.mobile,
               uc.email,
               uc.area_code,
               uc.last_name,
               uc.first_name,
               uc.`country` AS user_country,
               c.card_number AS card_number
        FROM ucard_transac_order t
        LEFT JOIN ucard_card c ON t.card_no = c.card_no
        LEFT JOIN ucard_type b ON c.card_type_id = b.card_type_id
        LEFT JOIN ucard_type_config ut ON ut.card_type_id = b.card_type_id
        LEFT JOIN ucard_merchant_user uc ON c.unique_id = uc.unique_id
        <where>
            <if test="cardNumber!=null and cardNumber!=''">
                and c.`card_number` like concat(#{cardNumber},'%')
            </if>
            <if test="currency!=null and currency!=''">
                and t.`currency` = #{currency}
            </if>
            <if test="type != null and type != ''">
                and t.`type` = #{type}
            </if>
            <if test="status!=null and status!=''">
                <choose>
                    <when test="status == 'success'">
                        and (t.`status` = 'success' or t.`status` = 'succeed')
                    </when>
                    <when test="status == 'fail'">
                        and (t.`status` = 'fail' or t.`status` = 'failed')
                    </when>
                    <otherwise>
                        and t.`status` = #{status}
                    </otherwise>
                </choose>
            </if>
            <if test="beginDate != null and beginDate != '' and beginDate > 0">
                and t.settle_date <![CDATA[ >= ]]> #{beginDate}
            </if>
            <if test="endDate != null and endDate != '' and endDate > 0">
                and t.settle_date <![CDATA[ <= ]]> #{endDate}
            </if>
            <if test="cardType != null and cardType != ''">
                and b.`type` = #{type}
            </if>
            <if test="email!=null and email!=''">
                and uc.`email` like concat(#{email},'%')
            </if>
            <if test="mobile!=null and mobile!=''">
                and uc.`mobile` like concat(#{mobile},'%')
            </if>
            <if test="cId != null and cId > 0">
                and uc.`c_id` = #{cId}
            </if>
        </where>
        ORDER BY t.id DESC
        <if test="page != null">
            limit #{page.offset},#{page.row}
        </if>
    </select>

</mapper> 