<?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="cn.pluss.platform.mapper.MerchantRateNewRecordMapper">

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="cn.pluss.platform.entity.MerchantRateNewRecord">
        <id column="id" property="id" />
        <result column="channel" property="channel" />
        <result column="merchantId" property="merchantId" />
        <result column="wxRate" property="wxRate" />
        <result column="aliRate" property="aliRate" />
        <result column="bankRate" property="bankRate" />
        <result column="bankRateLarge" property="bankRateLarge" />
        <result column="status" property="status" />
        <result column="createTime" property="createTime" />
        <result column="updateTime" property="updateTime" />
    </resultMap>

    <select id="selectLastExaminedDataByUserId" resultType="cn.pluss.platform.entity.MerchantRateNewRecord">
        SELECT
            mrnr.id, mrnr.channel, mrnr.merchantId, mrnr.wxRate, mrnr.aliRate,
               mrnr.bankRate, mrnr.bankRateLarge, mrnr.createTime, mrnr.updateTime, mrnr.`status`
        FROM tb_pluss_merchant_rate_new_record mrnr
                 LEFT JOIN tb_pluss_merchant_channel_status mcs ON mrnr.merchantId = mcs.merchantId AND mrnr.channel = mcs.channel
                 LEFT JOIN tb_pluss_merchant_base_info mbi ON mcs.merchantCode = mbi.merchantCode
        WHERE mbi.userId = #{userId} AND mrnr.status IN (0, 3) AND mrnr.channel = #{channel}
        ORDER BY mrnr.createTime DESC LIMIT 1
    </select>

    <select id="selectLastExaminedDataByMerchantCode"
            resultType="cn.pluss.platform.entity.MerchantRateNewRecord">
        SELECT
            mrnr.id, mrnr.channel, mrnr.merchantId, mrnr.wxRate, mrnr.aliRate,
               mrnr.bankRate, mrnr.bankRateLarge, mrnr.createTime, mrnr.updateTime, mrnr.`status`
        FROM tb_pluss_merchant_rate_new_record mrnr
                 LEFT JOIN tb_pluss_merchant_channel_status mcs ON mrnr.merchantId = mcs.merchantId AND mrnr.channel = mcs.channel
        WHERE mcs.merchantCode = #{merchantCode} AND mrnr.status IN (0, 3) AND mrnr.channel = #{channel}
        ORDER BY mrnr.createTime DESC LIMIT 1
    </select>

    <select id="selectValidDataByMerchantCode"
            resultType="cn.pluss.platform.entity.MerchantRateNewRecord">
        SELECT
            mrnr.id, mrnr.channel, mrnr.merchantId, mrnr.wxRate, mrnr.aliRate,
            mrnr.bankRate, mrnr.bankRateLarge, mrnr.createTime, mrnr.updateTime, mrnr.`status`
        FROM tb_pluss_merchant_rate_new_record mrnr
                 LEFT JOIN tb_pluss_merchant_channel_status mcs ON mrnr.merchantId = mcs.merchantId AND mrnr.channel = mcs.channel
        WHERE mcs.merchantCode = #{merchantCode} AND mrnr.status IN (0, 1, 3) AND mrnr.channel = #{channel}
        ORDER BY mrnr.createTime DESC LIMIT 1
    </select>

    <select id="selectRyxRateChangeList" resultType="cn.pluss.platform.entity.MerchantRateNewRecord">
        SELECT * FROM (
            SELECT mrn.*, DATE_FORMAT(mrn.updateTime, "%Y-%m-%d") updateDate, DATE_SUB(CURRENT_DATE, INTERVAL 1 DAY) yesterdayDate
            FROM tb_pluss_merchant_rate_new_record mrn
               LEFT JOIN tb_pluss_merchant_channel_status mcs ON mcs.merchantId = mrn.merchantId
               LEFT JOIN tb_pluss_user_app ua ON ua.merchantCode = mcs.merchantCode
               LEFT JOIN tb_pluss_merchant_channel mc ON mc.id = mcs.channel
            WHERE mcs.channel = '3' AND mc.`status` = '1' AND mrn.`status` = 0 AND ua.userType = 'promoter'
        ) t WHERE t.updateDate &lt;= t.yesterdayDate;
    </select>

    <select id="selectSxfRateChangeList" resultType="cn.pluss.platform.entity.MerchantRateNewRecord">
        SELECT * FROM (
              SELECT mrn.*, DATE_FORMAT(mrn.updateTime, "%Y-%m-%d") updateDate, DATE_SUB(CURRENT_DATE, INTERVAL 1 DAY) yesterdayDate
              FROM tb_pluss_merchant_rate_new_record mrn
                       LEFT JOIN tb_pluss_merchant_channel_status mcs ON mcs.merchantId = mrn.merchantId
                       LEFT JOIN tb_pluss_user_app ua ON ua.merchantCode = mcs.merchantCode
                       LEFT JOIN tb_pluss_merchant_channel mc ON mc.id = mcs.channel
              WHERE mcs.channel = '1' AND mc.`status` = '1' AND mrn.`status` = 3 AND ua.userType = 'promoter'
          ) t WHERE t.updateDate &lt;= t.yesterdayDate;
    </select>
</mapper>
