<?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.DistributionUserBindMapper">
  <resultMap id="BaseResultMap" type="com.yami.shop.distribution.comment.model.DistributionUserBind">
    <result column="bind_id" jdbcType="BIGINT" property="bindId" />
    <result column="distribution_user_id" jdbcType="BIGINT" property="distributionUserId" />
    <result column="shop_id" jdbcType="BIGINT" property="shopId" />
    <result column="user_id" jdbcType="VARCHAR" property="userId" />
    <result column="state" jdbcType="TINYINT" property="state" />
    <result column="invalid_reason" jdbcType="TINYINT" property="invalidReason" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="bind_time" jdbcType="TIMESTAMP" property="bindTime" />
    <result column="invalid_time" jdbcType="TIMESTAMP" property="invalidTime" />

  </resultMap>

  <resultMap id="DistributionUserBind_User_User_DistributionUser"  type="com.yami.shop.distribution.comment.model.DistributionUserBind">
    <result column="bind_id" jdbcType="BIGINT" property="bindId" />
    <result column="distribution_user_id" jdbcType="BIGINT" property="distributionUserId" />
    <result column="shop_id" jdbcType="BIGINT" property="shopId" />
    <result column="user_id" jdbcType="VARCHAR" property="userId" />
    <result column="state" jdbcType="TINYINT" property="state" />
    <result column="invalid_reason" jdbcType="TINYINT" property="invalidReason" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="bind_time" jdbcType="TIMESTAMP" property="bindTime" />
    <result column="invalid_time" jdbcType="TIMESTAMP" property="invalidTime" />
    <association property="user" javaType="com.yami.shop.bean.vo.UserVO">
      <id column="user_id" jdbcType="VARCHAR" property="userId" />
      <result column="nick_name" jdbcType="VARCHAR" property="nickName" />
    </association>
    <association property="distributionUser" javaType="com.yami.shop.distribution.comment.vo.DistributionUserVO">
      <result column="distribution_user_id" jdbcType="BIGINT" property="distributionUserId" />
        <result column="parent_nick_name" jdbcType="VARCHAR" property="nickName" />
      <result column="parent_user_mobile" jdbcType="VARCHAR" property="userMobile" />
    </association>
  </resultMap>

  <select id="distributionMsgsAndUserPage" resultMap="DistributionUserBind_User_User_DistributionUser">
      select dub.*,
      du.nick_name as parent_nick_name,
      du.user_mobile as parent_user_mobile,
      u.nick_name,
      u.user_mobile as u_user_mobile
      from
      tz_distribution_user_bind dub
      left join tz_distribution_user du on du.distribution_user_id=dub.distribution_user_id
      left join tz_user u on dub.user_id=u.user_id
     <where>
       <if test="userName!= null and userName!='' ">
         and u.nick_name LIKE CONCAT('%',#{userName},'%')
       </if>

       <if test="parentName!= null and parentName!='' ">
         and du.nick_name LIKE CONCAT('%',#{parentName},'%')
       </if>

       <if test="distributionUserBind.state!= null ">
          and dub.state=#{distributionUserBind.state}
       </if>

       <if test="bindTime.startTime!= null ">
         and dub.bind_time &gt;= #{bindTime.startTime}
       </if>
       <if test="bindTime.endTime!= null ">
         and dub.bind_time &lt;= #{bindTime.endTime}
       </if>

       <if test="invalidTime.startTime!= null ">
         and dub.invalid_time &gt;= #{invalidTime.startTime}
       </if>
       <if test="invalidTime.endTime!= null ">
         and dub.invalid_time &lt;= #{invalidTime.endTime}
       </if>
     </where>
  </select>

  <!--根据用户id和店铺Id获取绑定人id-->
  <select id="getParentDistributionIdByShopIdAndUserId" resultType="long">
    select distribution_user_id from
    tz_distribution_user_bind
    where shop_id=#{shopId}
    and user_id=#{userId}
    and state=#{state}
  </select>

  <!--根据客户id 店铺id 状态获取最新的一条记录-->
  <select id="getNewDistributionUserBindByUserIdAndShopIdAndState" resultMap="BaseResultMap">
        select * from tz_distribution_user_bind
        where shop_id = #{shopId}
        and user_id = #{userId}
        and state = #{state}
        ORDER BY bind_time desc
        limit 0,1
    </select>

    <!--根据分销员id修改绑定状态-->
    <update id="updateStateByDistributionUserId">
        update tz_distribution_user_bind
        set state = #{state}
        where
        distribution_user_id = #{distributionUserId}
    </update>
    <select id="bindUserList" resultType="com.yami.shop.distribution.comment.dto.BindUserInfoDto">
        select dub.*,
        u.nick_name,
        u.pic
        from
        tz_distribution_user_bind dub
        left join tz_distribution_user du on du.distribution_user_id=dub.distribution_user_id
        left join tz_user u on dub.user_id=u.user_id
        where du.user_id = #{userId}  and du.shop_id = #{shopId} order by dub.bind_time desc
    </select>

</mapper>