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


    <!-- 字段转换这个地方也不知道是为何, 默认接收的类型是byte -->
    <select id="selectRyxTrans" resultType="java.util.Map">
        SELECT
          mcs.merchantCode, mcs.merchantId,
          cast(mcs.extra ->> "$.merInId" as char) merInId,
          IFNULL(mm.lastSettleDate, mo.earliestDate) lastSettleDate
        FROM tb_pluss_merchant_channel_status mcs
        LEFT JOIN (
            SELECT merchantId, DATE_FORMAT(MAX( settDate ), '%Y%m%d' ) lastSettleDate
            FROM tb_pluss_make_money
            GROUP BY merchantId
        ) mm ON mcs.merchantId = mm.merchantId
        LEFT JOIN (
            SELECT
                   mo.merchantCode,
                   DATE_FORMAT(MIN(mo.createDt), '%Y%m%d') earliestDate,
                   DATE_FORMAT( MAX( mo.createDt ), '%Y%m%d' ) lastDate
            FROM tb_pluss_merchant_order mo
            LEFT JOIN tb_pluss_merchant_channel_status mcs ON mo.merchantCode = mcs.merchantCode
            WHERE mo.`status` = '1' AND mcs.channel = '3' AND mo.`aisleSwitch` = '3'
            GROUP BY mo.merchantCode
        ) mo ON mo.merchantCode = mcs.merchantCode
        WHERE mcs.channel = '3' AND mcs.`status` = '3'
        AND IFNULL( mm.lastSettleDate, mo.earliestDate ) &lt; #{lastDate}
        AND (mo.lastDate >= mm.lastSettleDate OR mm.lastSettleDate IS NULL)
        LIMIT ${(pageNo - 1) * pageSize}, ${pageSize}
    </select>

    <select id="selectRyxInfoYesterday" resultType="java.util.Map">
        SELECT DISTINCT mcs.merchantId, cast(mcs.extra ->> "$.merInId" as char) merInId
        FROM
            (
                SELECT merchantCode, count(1) count FROM tb_pluss_merchant_order
                WHERE `status` IN (1, 2) AND createDate = DATE_SUB(CURRENT_DATE,INTERVAL 1 DAY) AND aisleSwitch = 3
            GROUP BY orderNumber, merchantCode
            ) mo
        LEFT JOIN tb_pluss_merchant_channel_status mcs ON mo.merchantCode = mcs.merchantCode
        LEFT JOIN (SELECT merchantId, MAX(settDate) settDate FROM tb_pluss_make_money GROUP BY merchantId ) t ON t.merchantId = mcs.merchantId
        WHERE mcs.channel = 3 AND mo.count = 1 AND (t.settDate &lt; CURRENT_DATE OR t.settDate IS NULL)
        LIMIT ${(pageNo - 1) * pageSize}, ${pageSize}
    </select>

</mapper>
