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

    <!-- 通用查询映射结果 -->
    <resultMap id="BaseResultMap" type="cn.pluss.platform.entity.MerchantRate">
        <id column="id" property="id" />
        <result column="userId" property="userId" />
        <result column="rate" property="rate" />
        <result column="updateTime" property="updateTime" />
        <result column="previousRate" property="previousRate" />
        <result column="isFixedRate" property="isFixedRate" />
        <result column="startTime" property="startTime" />
        <result column="endTime" property="endTime" />
    </resultMap>
    
    <sql id="baseCol">
        id, userId, rate, updateTime, previousRate, isFixedRate, startTime, endTime
    </sql>

    <update id="batchUpdateRate" parameterType="java.util.List">
        update tb_pluss_merchant_rate
        <trim prefix="set" suffixOverrides=",">
            <trim prefix="previousRate =case" suffix="end,">
                <foreach collection="list" item="i" index="index">
                    when userId=#{i.userId} then rate
                </foreach>
            </trim>
            <trim prefix="rate =case" suffix="end,">
                <foreach collection="list" item="i" index="index">
                    <if test="i.rate!=null">
                        when userId=#{i.userId} then #{i.rate}
                    </if>
                </foreach>
            </trim>
            <trim prefix="updateTime =case" suffix="end,">
                <foreach collection="list" item="i" index="index">
                    when userId=#{i.userId} then #{i.updateTime}
                </foreach>
            </trim>
        </trim>
        where
        <foreach collection="list" separator="or" item="i" index="index" >
            userId=#{i.userId}
        </foreach>
    </update>

    <select id="selectRyxRateChangeList" resultType="cn.pluss.platform.entity.MerchantRate">
        SELECT <include refid="baseCol" /> FROM (
            SELECT mr.id, mr.userId, mr.rate, mr.updateTime, mr.previousRate, mr.isFixedRate, mr.startTime, mr.endTime,
                   DATE_FORMAT(mr.updateTime, "%Y-%m-%d") updateDate,
                   DATE_SUB(CURRENT_DATE, INTERVAL 1 DAY) yesterdayDate
            FROM tb_pluss_merchant_rate mr LEFT JOIN tb_pluss_user_app ua ON mr.userId = ua.userId
            LEFT JOIN tb_pluss_merchant_channel_status mcs ON mcs.merchantCode = ua.merchantCode
            LEFT JOIN tb_pluss_merchant_channel mc ON mc.id = mcs.channel
            WHERE mcs.channel = '3' AND mc.`status` = '1'
        )	t WHERE t.updateDate = t.yesterdayDate;
    </select>
</mapper>
