<?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.cool.store.mapper.HyPartnerUserInfoMapper">
  <resultMap id="BaseResultMap" type="com.cool.store.entity.HyPartnerUserInfoDO">
    <id column="id" jdbcType="BIGINT" property="id" />
    <result column="partner_id" jdbcType="VARCHAR" property="partnerId" />
    <result column="mobile" jdbcType="VARCHAR" property="mobile" />
    <result column="username" jdbcType="VARCHAR" property="username" />
    <result column="live_area" jdbcType="VARCHAR" property="liveArea" />
    <result column="want_shop_area" jdbcType="VARCHAR" property="wantShopArea" />
    <result column="accept_adjust_type" jdbcType="TINYINT" property="acceptAdjustType" />
    <result column="invite_code" jdbcType="VARCHAR" property="inviteCode" />
    <result column="is_write_partner_know" jdbcType="TINYINT" property="isWritePartnerKnow" />
    <result column="create_time" jdbcType="TIMESTAMP" property="createTime" />
    <result column="update_time" jdbcType="TIMESTAMP" property="updateTime" />
    <result column="shop_code" jdbcType="VARCHAR" property="shopCode" />
    <result column="shop_name" jdbcType="VARCHAR" property="shopName" />
    <result column="shop_id" jdbcType="VARCHAR" property="shopId" />
    <result column="recommend_partner_id" jdbcType="VARCHAR" property="recommendPartnerId" />
    <result column="recommend_partner_name" jdbcType="VARCHAR" property="recommendPartnerName" />
    <result column="recommend_partner_mobile" jdbcType="VARCHAR" property="recommendPartnerMobile" />
    <result column="user_channel_id" jdbcType="BIGINT" property="userChannelId" />
  </resultMap>
  <sql id="Base_Column_List">
    id, partner_id, mobile, username, live_area, want_shop_area, accept_adjust_type, 
    invite_code, is_write_partner_know, create_time, update_time, shop_code, shop_name, shop_id,
    recommend_partner_id, recommend_partner_name, recommend_partner_mobile,user_channel_id
  </sql>
  <select id="selectByPartnerId" resultMap="BaseResultMap" >
    select
    <include refid="Base_Column_List"></include>
    from hy_partner_user_info
    where partner_id = #{partnerId}
  </select>

  <select id="selectByMobile" resultMap="BaseResultMap" >
    select
    <include refid="Base_Column_List"></include>
    from hy_partner_user_info
    where mobile = #{mobile}
  </select>


  <select id="selectByPartnerIds" resultMap="BaseResultMap" >
    select
    <include refid="Base_Column_List"></include>
    from hy_partner_user_info
    <where>
      <if test="partnerIdList!=null and partnerIdList.size>0">
        <foreach collection="partnerIdList" open="and partner_id in (" close=")" separator="," item="partnerId">
          #{partnerId}
        </foreach>
      </if>
    </where>
  </select>
    <select id="selectByHourDate" resultType="com.cool.store.entity.SyncEcCustomerDO" >
      SELECT
        a.id AS id,b.partner_id,
        a.username AS customername,
        a.mobile AS customermobile,
        d.`name` AS followname,
        d.mobile AS followmobile,b.line_status as lineStatus,
        c.channel_id AS channelId ,ifnull(tl_l.followCount,0)  as followCount
      FROM
        hy_partner_user_info a
          LEFT JOIN hy_partner_line_info b ON a.partner_id = b.partner_id   and b.deleted=0
          LEFT JOIN hy_partner_user_channel c ON a.user_channel_id = c.channel_id
          LEFT JOIN enterprise_user d ON b.investment_manager = d.user_id and d.deleted=0
          LEFT JOIN	(
          SELECT
            partner_id,
            IFNULL( COUNT( 1 ), 0 ) AS followCount
          FROM
            hy_partner_line_info
          WHERE
            ( deleted = 1 OR ( deleted = 0 AND line_status IN ( 0, 3 ) AND close_time IS NOT NULL ) )
            AND investment_manager IS NOT NULL
          GROUP BY
            partner_id )  tl_l  on  b.partner_id=tl_l.partner_id
      WHERE ( a.create_time BETWEEN #{selectTime}  and   #{now}  or
        a.update_time BETWEEN #{selectTime}  and   #{now}  ) and  a.username is not null  order by  a.id  Limit  #{limit1},#{limit2}
    </select>
  <select id="selectByHourDateCount" resultType="java.lang.Integer">
    SELECT
      count(*)
    FROM
      hy_partner_user_info a
        LEFT JOIN hy_partner_line_info b ON a.partner_id = b.partner_id   and b.deleted=0
        LEFT JOIN hy_partner_user_channel c ON a.user_channel_id = c.channel_id
        LEFT JOIN enterprise_user d ON b.investment_manager = d.user_id and d.deleted=0
        LEFT JOIN	(
        SELECT
          partner_id,
          IFNULL( COUNT( 1 ), 0 ) AS followCount
        FROM
          hy_partner_line_info
        WHERE
          ( deleted = 1 OR ( deleted = 0 AND line_status IN ( 0, 3 ) AND close_time IS NOT NULL ) )
          AND investment_manager IS NOT NULL
        GROUP BY
          partner_id )  tl_l  on  b.partner_id=tl_l.partner_id
    WHERE (a.create_time BETWEEN #{selectTime}  and   #{now}  or
      a.update_time BETWEEN #{selectTime}  and   #{now} )  and  a.username is not null
  </select>

  <insert id="insertSelective" keyColumn="id" keyProperty="record.id" useGeneratedKeys="true">
    insert into hy_partner_user_info
    <trim prefix="(" suffix=")" suffixOverrides=",">
      <if test="record.partnerId != null">
        partner_id,
      </if>
      <if test="record.mobile != null">
        mobile,
      </if>
      <if test="record.username != null">
        username,
      </if>
      <if test="record.liveArea != null">
        live_area,
      </if>
      <if test="record.wantShopArea != null">
        want_shop_area,
      </if>
      <if test="record.acceptAdjustType != null">
        accept_adjust_type,
      </if>
      <if test="record.inviteCode != null">
        invite_code,
      </if>
      <if test="record.isWritePartnerKnow != null">
        is_write_partner_know,
      </if>
      <if test="record.createTime != null">
        create_time,
      </if>
      <if test="record.updateTime != null">
        update_time,
      </if>
      <if test="record.shopCode != null">
        shop_code,
      </if>
      <if test="record.shopName != null">
        shop_name,
      </if>
      <if test="record.shopId != null">
        shop_id,
      </if>
      <if test="record.recommendPartnerId != null">
        recommend_partner_id,
      </if>
      <if test="record.recommendPartnerName != null">
        recommend_partner_name,
      </if>
      <if test="record.recommendPartnerMobile != null">
        recommend_partner_mobile,
        </if>
      <if test="record.userChannelId!=null">
        user_channel_id,
      </if>
    </trim>
    <trim prefix="values (" suffix=")" suffixOverrides=",">
      <if test="record.partnerId != null">
        #{record.partnerId},
      </if>
      <if test="record.mobile != null">
        #{record.mobile},
      </if>
      <if test="record.username != null">
        #{record.username},
      </if>
      <if test="record.liveArea != null">
        #{record.liveArea},
      </if>
      <if test="record.wantShopArea != null">
        #{record.wantShopArea},
      </if>
      <if test="record.acceptAdjustType != null">
        #{record.acceptAdjustType},
      </if>
      <if test="record.inviteCode != null">
        #{record.inviteCode},
      </if>
      <if test="record.isWritePartnerKnow != null">
        #{record.isWritePartnerKnow},
      </if>
      <if test="record.createTime != null">
        #{record.createTime},
      </if>
      <if test="record.updateTime != null">
        #{record.updateTime},
      </if>
      <if test="record.shopCode != null">
        #{record.shopCode},
      </if>
      <if test="record.shopName != null">
        #{record.shopName},
      </if>
      <if test="record.shopId != null">
        #{record.shopId},
      </if>
      <if test="record.recommendPartnerId != null">
        #{record.recommendPartnerId},
      </if>
      <if test="record.recommendPartnerName != null">
        #{record.recommendPartnerName},
      </if>
      <if test="record.recommendPartnerMobile != null">
        #{record.recommendPartnerMobile},
      </if>
      <if test="record.userChannelId != null">
        #{record.userChannelId},
      </if>
    </trim>
  </insert>
  <update id="updateByPrimaryKeySelective">
    update hy_partner_user_info
    <set>
      <if test="record.partnerId != null">
        partner_id = #{record.partnerId},
      </if>
      <if test="record.mobile != null">
        mobile = #{record.mobile},
      </if>
      <if test="record.username != null">
        username = #{record.username},
      </if>
      <if test="record.liveArea != null">
        live_area = #{record.liveArea},
      </if>
      <if test="record.wantShopArea != null">
        want_shop_area = #{record.wantShopArea},
      </if>
      <if test="record.acceptAdjustType != null">
        accept_adjust_type = #{record.acceptAdjustType},
      </if>
      <if test="record.inviteCode != null">
        invite_code = #{record.inviteCode},
      </if>
      <if test="record.isWritePartnerKnow != null">
        is_write_partner_know = #{record.isWritePartnerKnow},
      </if>
      <if test="record.createTime != null">
        create_time = #{record.createTime},
      </if>
      <if test="record.updateTime != null">
        update_time = #{record.updateTime},
      </if>
      <if test="record.shopCode != null">
        shop_code = #{record.shopCode},
      </if>
      <if test="record.shopName != null">
        shop_name = #{record.shopName},
      </if>
      <if test="record.shopId != null">
        shop_id = #{record.shopId},
      </if>
      <if test="record.recommendPartnerId != null">
        recommend_partner_id = #{record.recommendPartnerId},
      </if>
      <if test="record.recommendPartnerName != null">
        recommend_partner_name = #{record.recommendPartnerName},
      </if>
      <if test="record.recommendPartnerMobile != null">
        recommend_partner_mobile = #{record.recommendPartnerMobile},
      </if>
    </set>
    where id = #{record.id}
  </update>

  <update id="updateJoinKnowById">
    update hy_partner_user_info
    set `is_write_partner_know`=#{isWritePartnerKnow,jdbcType=INTEGER}
    where id=#{id,jdbcType=BIGINT}
  </update>

</mapper>