<?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.corp.CorpUserFollowRelationMapper">

    <select id="selectCorpCustomers" resultType="java.lang.String">
        select group_concat(cf.name)
        from (select id
              from corp_user
              where unionid = #{unionid} limit 1) cu
                 inner join corp_user_auth ca on ca.corp_user_id = cu.id and wx_corp_id = #{wxCorpId}
                 inner join corp_user_follow_relation cfr on cfr.corp_user_id = ca.external_userid
                 inner join corp_follow cf on cf.userid = cfr.follow_id and cf.wx_corp_id = #{wxCorpId}
    </select>

    <select id="selectCorpUserFollowRelationByTagIds" resultType="com.cyksj.model.dto.CorpUserFollowRelationDto">
        select cufr.corp_user_id,
        any_value(cufr.follow_id) as follow_id,
        any_value(cu.user_id) as user_id
        from (select
        relation_id from `corp_user_tag`
        where tag_id in
        <foreach collection="list" item="item" open="(" separator="," close=")">
            #{item}
        </foreach>) cut left join `corp_user_follow_relation` cufr on cufr.id = cut.relation_id
        left join `corp_user_auth` ca on ca.external_userid = cufr.corp_user_id
        left join `corp_user` cu on cu.id = ca.corp_user_id
        where cufr.wx_corp_id = #{wxCorpId}
        <if test="joinStartTime != null">
            and cu.created_time >= #{joinStartTime}
        </if>
        <if test="joinEndTime != null">
            and cu.created_time &lt; #{joinEndTime}
        </if>
        <choose>
            <when test="userScope == 2">
                and cu.user_id != 0
            </when>
            <when test="userScope == 3">
                and cu.user_id = 0
            </when>
        </choose>
        <choose>
            <when test="consumerScope == 2">
                and exists (select id from order_don o where o.user_id != 0 and o.user_id = cu.user_id and status not in
                ('close','noPayment','refund') limit 1)
            </when>
            <when test="consumerScope == 3">
                and not exists (select id from order_don o where o.user_id != 0 and o.user_id = cu.user_id and status
                not in
                ('close','noPayment','refund') limit 1)
            </when>
        </choose>
        and ca.wx_corp_id = #{wxCorpId}
        <if test="followIds != null and followIds.size() > 0">
            and cufr.follow_id in
            <foreach collection="followIds" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        group by cufr.corp_user_id
    </select>

    <select id="selectCorpUserFollowRelation" resultType="com.cyksj.model.dto.CorpUserFollowRelationDto">
        select cufr.corp_user_id,
        any_value(cufr.follow_id) as follow_id,
        any_value(cu.user_id) as user_id
        from `corp_user_follow_relation` cufr left join `corp_user_auth` ca on ca.external_userid = cufr.corp_user_id
        left join `corp_user` cu on cu.id = ca.corp_user_id
        where cufr.wx_corp_id = #{wxCorpId}
        and ca.wx_corp_id = #{wxCorpId}
        <if test="followIds != null and followIds.size() > 0">
            and cufr.follow_id in
            <foreach collection="followIds" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        group by cufr.corp_user_id
    </select>

    <select id="selectCorpUserNotUnderTagIds" resultType="com.cyksj.model.dto.CorpUserFollowRelationDto">
        select cufr.corp_user_id,
        any_value(cufr.follow_id) as follow_id,
        any_value(cu.user_id) as user_id
        from corp_user cu inner join corp_user_auth ca on ca.corp_user_id = cu.id
        and ca.wx_corp_id = #{wxCorpId}
        <if test="joinStartTime != null">
            and cu.created_time >= #{joinStartTime}
        </if>
        <if test="joinEndTime != null">
            and cu.created_time &lt; #{joinEndTime}
        </if>
        <choose>
            <when test="userScope == 2">
                and cu.user_id != 0
            </when>
            <when test="userScope == 3">
                and cu.user_id = 0
            </when>
        </choose>
        <choose>
            <when test="consumerScope == 2">
                and exists (select id from order_don o where o.user_id != 0 and o.user_id = cu.user_id and status not in
                ('close','noPayment','refund') limit 1)
            </when>
            <when test="consumerScope == 3">
                and not exists (select id from order_don o where o.user_id != 0 and o.user_id = cu.user_id and status
                not in
                ('close','noPayment','refund') limit 1)
            </when>
        </choose>
        inner join corp_user_follow_relation cufr on cufr.corp_user_id = ca.external_userid
        and cufr.wx_corp_id = #{wxCorpId}
        <if test="followIds != null and followIds.size() > 0">
            and cufr.follow_id in
            <foreach collection="followIds" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="list != null and list.size() > 0">
            and not exists (select s.corp_user_id from (select corp_user_id from corp_user_follow_relation cufr inner
            join `corp_user_tag` ct on ct.relation_id = cufr.id and ct.tag_id in
            <foreach collection="list" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
            group by cufr.corp_user_id) s where s.corp_user_id = cufr.corp_user_id limit 1)
        </if>
        group by cufr.corp_user_id
    </select>

    <select id="getCorpUserFollowRelation" resultType="com.cyksj.model.entity.CorpUserFollowRelation">
        select cufr.*
        from (select id
              from corp_user
              where unionid = #{unionId} limit 1) cu
                 inner join corp_user_auth ca on ca.corp_user_id = cu.id
                 inner join corp_user_follow_relation cufr on cufr.corp_user_id = ca.external_userid
        where cufr.follow_id = #{followId} limit 1
    </select>
</mapper>