<?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.yami.shop.distribution.comment.dao.DistributionProdBindMapper">
    <resultMap id="distributionProdBindMap" type="com.yami.shop.distribution.comment.model.DistributionProdBind">
         <id property="id" column="id"/>
        <result property="distributionUserId" column="distribution_user_id"/>
        <result property="userId" column="user_id"/>
        <result property="bindTime" column="bind_time"/>
        <result property="prodId" column="prod_id"/>
        <result property="state" column="state"/>
    </resultMap>

    <select id="getDistributionUserIdByUserIdAndProdId" resultType="long">
    SELECT
        distribution_user_id
    FROM
        tz_distribution_prod_bind
    WHERE
	 user_id = #{userId}
	AND prod_id = #{prodId}
	AND state = #{state}
    </select>

    <update id="updateStateByUserIdByUserIdAndProdId">
        update tz_distribution_prod_bind set state = #{state}
        where user_id = #{userId}
        and prod_id = #{prodId}
    </update>


    <!--根据订单项列表查出最新的的分享人-->
    <select id="getNewDistributionUserIdByOrderItems" resultType="long">
        select distribution_user_id from
        tz_distribution_user where distribution_user_id in
        (
        select distribution_user_id from tz_distribution_prod_bind
        where prod_id in (
        <foreach collection="orderItems" item="orderItem" separator=",">
            #{orderItem.prodId}
        </foreach>
        )
        and
        state = 1
        order by bind_time desc
        )
        and
        state = 1
    </select>

    <update id="updateBindTimeByUserIdAndProdId">
        update tz_distribution_prod_bind
        set bind_time = #{bindTime}
        where user_id = #{userId}
        and prod_id = #{prodId}
        and distribution_user_id = #{distributionUserId}
    </update>

    <!--根据分销员id修改状态-->
    <update id="updateStateByDistributionUserId">
        update tz_distribution_prod_bind
        set state = #{state}
        where
        distribution_user_id = #{distributionUserId}
    </update>




</mapper>
