<?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.pay.dao.mapper.SysRemittanceChannelMapper">
    <select id="getAllByValidOrderBySubIndex"
            parameterType="com.crm.rely.backend.model.entity.sys.remittance.channel.SysRemittanceChannelListEntity"
            resultType="com.crm.rely.backend.model.pojo.view.SysRemittanceChannelView">
        SELECT rate_view.rate finance_rate,rc.*
        from sys_remittance_channel rc
        JOIN (
        SELECT
        `finance_rate`.`type` AS `type`,
        `finance_rate`.`rate` AS `rate`,
        `finance_rate`.`currency` AS `currency`,
        `finance_rate`.`transform_currency` AS `transform_currency`
        FROM
        `finance_rate` UNION ALL
        SELECT
        `finance_rate`.`type` AS `type`,(1 / `finance_rate`.`rate` ) AS `1/rate`,
        `finance_rate`.`transform_currency` AS `currency`,
        `finance_rate`.`currency` AS `transform_currency`
        FROM
        `finance_rate`
        )  rate_view
        where rc.valid=1
        AND rate_view.type = 0
        AND rate_view.transform_currency = rc.currency
        and (( ISNULL(rc.transform_currency)
        or rc.transform_currency='') or rate_view.currency = rc.transform_currency )

        AND (
        -- 都为空：不限制
        ((rc.start_time IS NULL OR rc.start_time = '') AND (rc.end_time IS NULL OR rc.end_time = ''))

        -- 只设置 start_time：从 start_time 之后生效
        OR ((rc.start_time IS NOT NULL AND rc.start_time != '') AND (rc.end_time IS NULL OR rc.end_time = '') AND
        TIME(NOW()) <![CDATA[>=]]> rc.start_time)

        -- 只设置 end_time：在 end_time 之前生效
        OR ((rc.start_time IS NULL OR rc.start_time = '') AND (rc.end_time IS NOT NULL AND rc.end_time != '') AND
        TIME(NOW()) <![CDATA[<=]]> rc.end_time)

        -- 正常区间：start  <![CDATA[<=]]> end
        OR ((rc.start_time IS NOT NULL AND rc.start_time != '')
        AND (rc.end_time IS NOT NULL AND rc.end_time != '')
        AND rc.start_time <![CDATA[<=]]> rc.end_time
        AND TIME(NOW()) BETWEEN rc.start_time AND rc.end_time)

        -- 跨午夜区间：start > end
        OR ((rc.start_time IS NOT NULL AND rc.start_time != '')
        AND (rc.end_time IS NOT NULL AND rc.end_time != '')
        AND rc.start_time <![CDATA[>]]> rc.end_time
        AND (TIME(NOW()) <![CDATA[>=]]> rc.start_time OR TIME(NOW()) <![CDATA[<=]]> rc.end_time))
        )

        <if test="type!=null">
            and rc.type=#{type}
        </if>
        <if test="country!=null and country!=''">
            and (ISNULL(rc.countrys)
            or rc.countrys=''
            or rc.countrys REGEXP #{country})
        </if>
        <if test="state!=null and state!=''">
            and (ISNULL(rc.states)
            or rc.states=''
            or rc.states REGEXP #{state})
        </if>
        <if test="channelCode!=null and channelCode.size()>0">
            and rc.code in
            <foreach collection="channelCode" index="idx" item="item" separator="," open="(" close=")">
                #{item}
            </foreach>
        </if>
        <if test="cId!=null">
            and (ISNULL(rc.c_ids)
            or rc.c_ids=''
            or rc.c_ids REGEXP #{cId})
            AND
            (ISNULL(rc.exclude_c_ids)
            or rc.exclude_c_ids=''
            or rc.exclude_c_ids not REGEXP #{cId})
        </if>
        GROUP BY rc.id
        ORDER BY rc.sub_index
    </select>

</mapper>