<?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.cyksj.mapper.manage.distribute.UserDistributeBusinessRateMapper">

    <insert id="batchInsertDistributePlatRate">
        insert into
        user_plat_distribute_rate(`distribute_id`,`goods_id`,`rate`,`second_rate`,`created_time`,`update_time`)
        values
        <foreach collection="list" item="item" separator=",">
            (#{item.distributeId},#{item.goodsId},#{item.rate},#{item.secondRate},now(),now())
        </foreach>
        ON DUPLICATE KEY UPDATE rate = VALUES(rate)
    </insert>

    <insert id="insertOrUpdateBusinessGoodsSkuRate">
        insert into user_distribute_business_rate
        <trim prefix="(" suffix=")" suffixOverrides=",">
            `business_id`,`goods_id`,`sku_id`,
            <if test="firstOrderRate != null">
                `first_order_rate`,
            </if>
            <if test="renewOrderRate != null">
                `renew_order_rate`,
            </if>
            `created_time`,`update_time`
        </trim>
        values
        <foreach collection="list" item="item" separator=",">
            (#{item},#{goodsId},#{skuId},
            <if test="firstOrderRate != null">
                #{firstOrderRate},
            </if>
            <if test="renewOrderRate != null">
                #{renewOrderRate},
            </if>
            now(),now())
        </foreach>
        ON DUPLICATE KEY UPDATE
        <trim suffixOverrides=",">
            <if test="firstOrderRate != null">
                first_order_rate = VALUES(first_order_rate),
            </if>
            <if test="renewOrderRate != null">
                renew_order_rate = VALUES(renew_order_rate)
            </if>
        </trim>
    </insert>

    <select id="selectNoThisGoodsDistribute" resultType="java.lang.Long">
        select distinct ud.id from user_distribute ud left join user_plat_distribute_rate ur
        on ur.distribute_id = ud.id and ur.goods_id = #{goodsId}
        where (ur.id is null or ur.rate = 0) and ud.deleted is true
    </select>
</mapper>