<?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.CardKycStatusMapper">

    <select id="countList" resultType="integer"
            parameterType="com.crm.rely.backend.core.entity.ucard.CardKycStatusPageEntity">
        SELECT COUNT(*)
        from ucard_kyc_status a
        left join ucard_merchant_user uc on a.unique_id = uc.unique_id
        left join sys_reasons_refusal rr on rr.id = a.approve_desc
        <where>
            <if test="status!=null">
                and a.`status` = #{status}
            </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 uc.`c_id` = #{cId}
            </if>
            <if test="photoStatus != null and photoStatus > 0">
                <choose>
                    <when test="photoStatus == 2">
                        and (uc.`photo_status` = 0 or uc.`photo_status` is null)
                    </when>
                    <otherwise>
                        and uc.`photo_status` = 1
                    </otherwise>
                </choose>
            </if>
            <if test="kycStatus != null and kycStatus > 0">
                <choose>
                    <when test="kycStatus == 1">
                        and (uc.auth_status = 0 or uc.auth_status is null)
                    </when>
                    <when test="kycStatus == 2">
                        and uc.auth_status = 1
                    </when>
                </choose>
            </if>
        </where>
    </select>

    <select id="pageList" resultType="com.crm.rely.backend.core.dto.ucard.CardKycStatusDto"
            parameterType="com.crm.rely.backend.core.entity.ucard.CardKycStatusPageEntity">
        select
        a.*,
        uc.c_id,
        uc.area_code,
        uc.mobile,
        uc.email,
        uc.last_name,
        uc.country,
        uc.first_name,
        uc.photo_status,
        if(uc.auth_status=0,1,if(uc.auth_status=1,2,null)) as kyc_status,
        rr.content as approve_desc_cn
        from ucard_kyc_status a
        left join ucard_merchant_user uc on a.unique_id = uc.unique_id
        left join sys_reasons_refusal rr on rr.id = a.approve_desc
        <where>
            <if test="status!=null">
                and a.`status` = #{status}
            </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 uc.`c_id` = #{cId}
            </if>
            <if test="photoStatus != null and photoStatus > 0">
                <choose>
                    <when test="photoStatus == 2">
                        and (uc.`photo_status` = 0 or uc.`photo_status` is null)
                    </when>
                    <otherwise>
                        and uc.`photo_status` = 1
                    </otherwise>
                </choose>
            </if>
            <if test="kycStatus != null and kycStatus > 0">
                <choose>
                    <when test="kycStatus == 1">
                        and (uc.auth_status = 0 or uc.auth_status is null)
                    </when>
                    <when test="kycStatus == 2">
                        and uc.auth_status = 1
                    </when>
                </choose>
            </if>
        </where>
        order by a.id desc
        <if test="page != null">
            limit #{page.offset},#{page.row}
        </if>
    </select>

    <select id="getKycApproveById" resultType="com.crm.rely.backend.core.dto.ucard.CardKycSingleStatusDto">
        select a.status,
               b.auth_status   kyc_auth_status,
               b.id         as bank_id,
               b.c_id,
               b.area_code,
               b.mobile,
               b.last_name,
               b.first_name,
               b.id_type,
               b.id_number,
               b.id_front_url,
               b.id_back_url,
               b.id_hold_url,
               b.issue_date,
               b.id_no_expiry_date,
               b.country    as merchant_country,
               b.external_user_id,
               d.last_name  as info_last_name,
               d.first_name as info_first_name,
               d.identity,
               d.email,
               d.phone,
               d.id         as custom_id,
               d.middle,
               d.country
        from ucard_kyc_status a
                 left join ucard_merchant_user b on a.unique_id = b.unique_id
                 left join custom_info d on b.c_id = d.c_id
        where a.id = #{id}
    </select>

    <select id="getKycApproveByUniqueId" resultType="com.crm.rely.backend.core.dto.ucard.CardKycStatusDto">
        select *
        from ucard_kyc_status
        where unique_id = #{uniqueId}
        order by add_time desc limit 1
    </select>

</mapper> 