<?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.huiju.estateagents.mapper.TaBuildingMapper">
    <select id="buildingList" resultType="com.huiju.estateagents.entity.TaBuilding">
        SELECT * FROM ta_building
        <where>
            status > -1
            <if test="Name != null and Name != ''">
                and  (ta_building.name like concat('%',#{Name,jdbcType=VARCHAR},'%') or ta_building.building_name like concat('%',#{Name,jdbcType=VARCHAR},'%'))
            </if>
            <if test="Code != null and Code != ''">
                and  ta_building.code like concat('%',#{Code,jdbcType=VARCHAR},'%')
            </if>
            <if test="startDate != null">
                and  TO_DAYS(ta_building.opening_date ) = TO_DAYS(#{startDate})
            </if>
            <if test="buildingStatus != null and buildingStatus != ''">
                and  ta_building.status = #{buildingStatus}
            </if>
            <if test="marketStatus != null and marketStatus != ''">
                and  ta_building.market_status =#{marketStatus}
            </if>
            <if test="cityId != null and cityId != ''">
                and  ta_building.city_id =#{cityId}
            </if>
            <if test="isMain != null">
                and  ta_building.is_main = #{isMain}
            </if>
            <if test="orgId != null">
                and  ta_building.org_id = #{orgId}
            </if>
            <if test="personBuildingList != null and personBuildingList.size > 0">
                AND ta_building.building_id in
                <foreach collection="personBuildingList" item="personBuilding" open="(" close=")" separator=",">
                    #{personBuilding.buildingId}
                </foreach>
            </if>
        </where>
        ORDER BY status ASC, create_date DESC
    </select>

    <select id="buildingListSelect" resultType="com.huiju.estateagents.entity.TaBuilding">
        SELECT * FROM ta_building
        <where>
            1 =1
            and status = 1
            <if test="Name != null and Name != ''">
                and  (ta_building.name like concat('%',#{Name,jdbcType=VARCHAR},'%') or ta_building.building_name like concat('%',#{Name,jdbcType=VARCHAR},'%'))
            </if>
            <if test="Code != null and Code != ''">
                and  ta_building.code like concat('%',#{Code,jdbcType=VARCHAR},'%')
            </if>
            <if test="startDate != null">
                and  TO_DAYS(ta_building.opening_date ) = TO_DAYS(#{startDate})
            </if>
            <if test="marketStatus != null and marketStatus != ''">
                and  ta_building.market_status =#{marketStatus}
            </if>
            <if test="cityId != null and cityId != ''">
                and  ta_building.city_id =#{cityId}
            </if>
            <if test="isMain != null and isMain != ''">
                and  ta_building.is_main = #{isMain}
            </if>
            <if test="orgId != null and orgId != ''">
                and  ta_building.org_id = #{orgId}
            </if>
            <if test="personBuildingList != null and personBuildingList.size > 0">
                AND ta_building.building_id in
                <foreach collection="personBuildingList" item="personBuilding" open="(" close=")" separator=",">
                    #{personBuilding.buildingId}
                </foreach>
            </if>
        </where>
        ORDER BY create_date DESC,status asc
    </select>

    <select id="buildingAll" resultType="com.huiju.estateagents.entity.TaBuilding">
         SELECT * FROM ta_building
         where status > -1
         and org_id = #{orgId}
    </select>

    <select id="selectBuilding" resultType="com.huiju.estateagents.entity.TaBuilding">
         SELECT * FROM ta_building
         where building_id = #{buildingId}
    </select>

    <select id="selectBuildingStatistical" resultType="map">
        select
        IFNULL(sum(pv_num), 0) as pvNum,
          <if test="saveOrShare == 'save'">
              IFNULL(sum(save_num),0) as saveNum
          </if>
          <if test="saveOrShare == 'share'">
              IFNULL(sum(share_num),0) as shareNum
          </if>
        from ta_building
        where org_id = ${orgId}
    </select>

    <select id="selectBuildingPage" resultType="com.huiju.estateagents.entity.TaBuilding">
      SELECT
            tb.*
        FROM
            (
             select * from  ta_building
                WHERE
                `status` = 1
                and org_id = #{orgId}
                <if test="cityId != null">
                    AND city_id = #{cityId}
                </if>
                <if test="targets != null and targets.size > 0">
                    AND building_id IN
                    <foreach collection="targets" item="buildingId" open="(" close=")" separator=",">
                        #{buildingId}
                    </foreach>
                </if>
                <if test="name != null and name !=''">
                    AND (`name` LIKE CONCAT('%',#{name},'%')
                    OR `building_name` LIKE CONCAT('%',#{name},'%'))
                </if>
            ) as tb

            <if test="(priceList != null and priceList.size > 0) or (buildingTypeList != null and buildingTypeList.length > 0)">
                INNER JOIN
                (
                SELECT * FROM ta_building_project_type
                <trim prefix="where" prefixOverrides="and | or">

                    <if test="priceList != null">
                        <foreach collection="priceList" item="priceArr" open="(" close=")" separator=" OR ">
                            (
                                <if test='priceArr[0] != null and priceArr[0] != ""'>
                                    <![CDATA[ start_price >= ${priceArr[0]}  ]]>
                                </if>
                                <if test='priceArr.length > 1 and priceArr[0] != null and priceArr[0] != ""'>
                                    AND
                                </if>
                                <if test='priceArr.length > 1 and priceArr[1] != null and priceArr[1] != ""'>
                                    <![CDATA[ start_price <= ${priceArr[1]}  ]]>
                                </if>
                            )  or
                            (
                            <if test='priceArr[0] != null and priceArr[0] != ""'>
                                <![CDATA[ end_price >= ${priceArr[0]}  ]]>
                            </if>
                            <if test='priceArr.length > 1 and priceArr[0] != null and priceArr[0] != ""'>
                                AND
                            </if>
                            <if test='priceArr.length > 1 and priceArr[1] != null and priceArr[1] != ""'>
                                <![CDATA[ end_price <= ${priceArr[1]}  ]]>
                            </if>
                            )
                        </foreach>
                    </if>

                    <if test="priceType != null and priceType != ''">
                        and price_type = #{priceType}
                    </if>

                    <if test="buildingTypeList != null and buildingTypeList.length > 0">
                        AND building_type_id in
                        <foreach collection="buildingTypeList" item="buildingType" open="(" close=")" separator=",">
                            #{buildingType}
                        </foreach>
                    </if>
                </trim>
                GROUP BY building_id
                )
                AS tbpt ON tb.building_id = tbpt.building_id
            </if>

            <if test="(areaList != null and areaList.size > 0) or (houseTypeList != null and houseTypeList.length > 0)">
                INNER JOIN
                (
                SELECT * FROM ta_building_apartment
                <trim prefix="where" prefixOverrides="and | or">
                    <if test="areaList != null">
                        <foreach collection="areaList" item="areaArr" open="(" close=")" separator=" OR ">
                            (
                                <if test='areaArr[0] != null and areaArr[0] != ""'>
                                    <![CDATA[ building_area >= ${areaArr[0]}  ]]>
                                </if>
                                <if test='areaArr.length > 1 and areaArr[0] != null and areaArr[0] != ""'>
                                    AND
                                </if>
                                <if test='areaArr.length > 1 and areaArr[1] != null and areaArr[1] != ""'>
                                    <![CDATA[ building_area <= ${areaArr[1]}  ]]>
                                </if>
                            )
                        </foreach>
                    </if>

                    <if test="houseTypeList != null and houseTypeList.length > 0">
                        AND house_type in
                        <foreach collection="houseTypeList" item="houseType" open="(" close=")" separator=",">
                            #{houseType}
                        </foreach>
                    </if>
                </trim>
                GROUP BY building_id
                ) as tba ON tb.building_id = tba.building_id
            </if>
            WHERE tb.org_id = #{orgId}
            ORDER BY tb.order_no DESC
    </select>

    <select id="selectBuildingByBuildingTypeId" resultType="com.huiju.estateagents.entity.TaBuilding">

        SELECT
            tab.*
        FROM
            ta_building_project_type tbpt
            LEFT JOIN td_building_type tbt ON tbpt.building_type_id = tbt.building_type_id
            LEFT JOIN ta_building tab ON tbpt.building_id = tab.building_id
        WHERE
          tab.status <![CDATA[ > ]]> -1
        and
          tbpt.building_type_id = #{buildingTypeId}

    </select>

    <select id="getBuildingListById" resultType="com.huiju.estateagents.entity.TaBuilding">
      select * from ta_building t where t.building_id in (
	    select t.building_id from ta_goods t where t.org_id = #{orgId} and t.city_id = #{cityId} and `status`=1 group by t.building_id
      )
    </select>

    <select id="countNumByHeavy" resultType="java.lang.Integer">
      select COUNT(1) from ta_building_dynamic t where t.heavy = #{heavy}
      <if test="dynamicId != null and dynamicId != ''">
          and t.dynamic_id != #{dynamicId}
      </if>
    </select>

    <select id="selectMainBuildings" resultType="com.huiju.estateagents.entity.TaBuilding">
        SELECT
            CONCAT( c.`name`, t.building_area  ) AS building_area,
            t.*,
            s.url AS list_poster,
            GROUP_CONCAT( m.tag_name ) AS tags
        FROM
            ta_building t
        LEFT JOIN ta_building_img s ON t.building_id = s.building_id
        AND s.img_type = 'list'
        LEFT JOIN ta_building_tag m ON t.building_id = m.building_id
        LEFT JOIN td_city c ON t.city_id = c.id
        WHERE
            t.org_id = #{orgId}
        <if test="cityId != null and cityId != ''">
            AND t.city_id = #{cityId}
        </if>
            AND t.status = 1
            AND t.is_main = 1
        GROUP BY
            t.building_id
        ORDER BY t.order_no desc
    </select>

    <select id="getGkBarData" resultType="java.util.Map">
        SELECT
            count( * ) AS gk_num,
            bb.building_id,
            bb.create_date,
            bb.building_name
        FROM
        (
            SELECT
                b.*,
                d.building_name
            FROM
                ta_recommend_customer b
            LEFT JOIN ta_building d ON b.building_id = d.building_id
            WHERE
                b.STATUS > 0
                AND b.org_id = #{orgId}
                AND b.verify_status = 1
                AND ( b.realty_consultant IS NULL OR b.realty_consultant = '' )
                <if test="personBuildingList != null and personBuildingList.size > 0">
                    AND b.building_id in
                    <foreach collection="personBuildingList" item="personBuilding" open="(" close=")" separator=",">
                        #{personBuilding.buildingId}
                    </foreach>
                </if>
                <if test="startDate != null">
                    and  TO_DAYS(b.create_date) >= TO_DAYS(#{startDate})
                </if>
                <if test="endDate != null">
                    and  TO_DAYS(b.create_date) &lt;= TO_DAYS(#{endDate})
                </if>
                and b.building_id is not null
            GROUP BY
            b.person_id
        ) bb
        GROUP BY
            bb.building_id
        order by gk_num desc
        limit 6
    </select>

    <select id="getSkBarData" resultType="java.util.Map">
        SELECT
            count( * ) AS sk_num,
            a.building_id,
            d.building_name
        FROM
            ta_recommend_customer a
        LEFT JOIN ta_building d ON a.building_id = d.building_id
        WHERE
            a.STATUS > 0
            AND a.verify_status = 1
            AND a.realty_consultant IS NOT NULL
            AND a.realty_consultant != ''
            AND a.org_id = #{orgId}
            <if test="personBuildingList != null and personBuildingList.size > 0">
                AND a.building_id in
                <foreach collection="personBuildingList" item="personBuilding" open="(" close=")" separator=",">
                    #{personBuilding.buildingId}
                </foreach>
            </if>
            <if test="startDate != null">
                and  TO_DAYS(a.create_date) >= TO_DAYS(#{startDate})
            </if>
            <if test="endDate != null">
                and  TO_DAYS(a.create_date) &lt;= TO_DAYS(#{endDate})
            </if>
        GROUP BY
        a.building_id
        ORDER BY
        sk_num DESC
        LIMIT 6
    </select>

    <select id="getWgBarData" resultType="java.util.Map">
        SELECT
            count( * ) AS wg_num,
            a.be_uv AS building_id,
            a.create_date,
            d.building_name
        FROM
            ta_uv a
        LEFT JOIN ta_building d ON a.be_uv = d.building_id
        LEFT JOIN ta_person p ON a.person_id = p.person_id
        WHERE
            a.tagert_type = 'project'
            and p.org_id = #{orgId}
            and a.be_uv != "undefined"
            <if test="personBuildingList != null and personBuildingList.size > 0">
                AND a.be_uv in
                <foreach collection="personBuildingList" item="personBuilding" open="(" close=")" separator=",">
                    #{personBuilding.buildingId}
                </foreach>
            </if>
            <if test="startDate != null">
                and  TO_DAYS(a.create_date) >= TO_DAYS(#{startDate})
            </if>
            <if test="endDate != null">
                and  TO_DAYS(a.create_date) &lt;= TO_DAYS(#{endDate})
            </if>
        GROUP BY
            a.be_uv
        ORDER BY
            wg_num DESC
        LIMIT 6
    </select>

    <select id="getFwBarData" resultType="java.util.Map">
        SELECT
            t.pv_num,
            t.building_id,
            t.building_name
        FROM
            ta_building t
        WHERE
            t.org_id = #{orgId}
        <if test="personBuildingList != null and personBuildingList.size > 0">
            AND t.building_id in
            <foreach collection="personBuildingList" item="personBuilding" open="(" close=")" separator=",">
                #{personBuilding.buildingId}
            </foreach>
        </if>
        ORDER BY
        pv_num DESC
        LIMIT 6
    </select>

    <select id="getStatsTableList" resultType="com.huiju.estateagents.excel.StatsBuilding">
        SELECT
            t.building_id,
            t.building_name,
            IFNULL( t.pv_num, 0 ) AS pv_num,
            IFNULL( uu.uv_num, 0 ) AS uv_num,
            IFNULL( aa.sk_num, 0 ) AS sk_num,
            IFNULL( bbb.gk_num, 0 ) AS gk_num,
            IFNULL( aa.sk_num, 0 ) + IFNULL( bbb.gk_num, 0 ) AS kh_num
        FROM
            ta_building t
        LEFT JOIN (
            SELECT
                count( * ) AS sk_num,
                a.building_id
            FROM
                ta_recommend_customer a
            WHERE
                a.STATUS > 0
                AND a.verify_status = 1
                AND a.realty_consultant IS NOT NULL
                AND a.realty_consultant != ''
                AND a.org_id = #{orgId}
                <if test="startDate != null">
                    and  TO_DAYS(a.create_date) >= TO_DAYS(#{startDate})
                </if>
                <if test="endDate != null">
                    and  TO_DAYS(a.create_date) &lt;= TO_DAYS(#{endDate})
                </if>
            GROUP BY
                a.building_id
        ) aa ON aa.building_id = t.building_id
        LEFT JOIN (
            SELECT
                count( * ) AS gk_num,
                bb.building_id
            FROM
            (
                SELECT
                    b.*
                FROM
                    ta_recommend_customer b
                WHERE
                    b.STATUS > 0
                    AND b.org_id = #{orgId}
                    AND b.verify_status = 1
                    AND ( b.realty_consultant IS NULL OR b.realty_consultant = '' )
                    <if test="startDate != null">
                        and  TO_DAYS(b.create_date) >= TO_DAYS(#{startDate})
                    </if>
                    <if test="endDate != null">
                        and  TO_DAYS(b.create_date) &lt;= TO_DAYS(#{endDate})
                    </if>
                GROUP BY
                    b.person_id
            ) bb
            GROUP BY
                bb.building_id
        ) bbb ON bbb.building_id = t.building_id
        LEFT JOIN (
            SELECT
                count( * ) AS uv_num,
                u.be_uv AS building_id
            FROM
                ta_uv u
            LEFT JOIN ta_person p ON u.person_id = p.person_id
        WHERE
                u.tagert_type = 'project'
                AND p.org_id = #{orgId}
            <if test="startDate != null">
                and  TO_DAYS(u.create_date) >= TO_DAYS(#{startDate})
            </if>
            <if test="endDate != null">
                and  TO_DAYS(u.create_date) &lt;= TO_DAYS(#{endDate})
            </if>
            GROUP BY
                u.be_uv
        ) uu ON uu.building_id = t.building_id
        WHERE
        t.org_id = #{orgId}
        <if test="personBuildingList != null and personBuildingList.size > 0">
            AND t.building_id in
            <foreach collection="personBuildingList" item="personBuilding" open="(" close=")" separator=",">
                #{personBuilding.buildingId}
            </foreach>
        </if>
        <if test="buildingId != null and buildingId != '' ">
            AND t.building_id = #{buildingId}
        </if>
        <if test="sortField != null and sortField != ''">
            ORDER BY ${sortField} ${orderType}
        </if>
    </select>
    <select id="getExportTableList" resultType="com.huiju.estateagents.excel.StatsBuilding">
        SELECT
        t.building_id,
        t.building_name,
        IFNULL( t.pv_num, 0 ) AS pv_num,
        IFNULL( uu.uv_num, 0 ) AS uv_num,
        IFNULL( aa.sk_num, 0 ) AS sk_num,
        IFNULL( bbb.gk_num, 0 ) AS gk_num,
        IFNULL( aa.sk_num, 0 ) + IFNULL( bbb.gk_num, 0 ) AS kh_num
        FROM
        ta_building t
        LEFT JOIN (
        SELECT
        count( * ) AS sk_num,
        a.building_id
        FROM
        ta_recommend_customer a
        WHERE
        a.STATUS > 0
        AND a.verify_status = 1
        AND a.realty_consultant IS NOT NULL
        AND a.realty_consultant != ''
        AND a.org_id = #{orgId}
        <if test="startDate != null">
            and  TO_DAYS(a.create_date) >= TO_DAYS(#{startDate})
        </if>
        <if test="endDate != null">
            and  TO_DAYS(a.create_date) &lt;= TO_DAYS(#{endDate})
        </if>
        GROUP BY
        a.building_id
        ) aa ON aa.building_id = t.building_id
        LEFT JOIN (
        SELECT
        count( * ) AS gk_num,
        bb.building_id
        FROM
        (
        SELECT
        b.*
        FROM
        ta_person b
        WHERE
        b.STATUS > 0
        AND b.org_id = #{orgId}
        AND IFNULL( b.person_type, '' ) IN ( 'estate agent', 'customer' )
        AND b.person_id NOT IN ( SELECT c.person_id FROM ta_recommend_customer c WHERE c.org_id = #{orgId} AND person_id IS NOT NULL AND person_id != '' )
        <if test="startDate != null">
            and  TO_DAYS(b.create_date) >= TO_DAYS(#{startDate})
        </if>
        <if test="endDate != null">
            and  TO_DAYS(b.create_date) &lt;= TO_DAYS(#{endDate})
        </if>
        GROUP BY
        b.person_id
        ) bb
        GROUP BY
        bb.building_id
        ) bbb ON bbb.building_id = t.building_id
        LEFT JOIN (
        SELECT
        count( * ) AS uv_num,
        u.be_uv AS building_id
        FROM
        ta_uv u
        LEFT JOIN ta_person p ON u.person_id = p.person_id
        WHERE
        u.tagert_type = 'project'
        AND p.org_id = #{orgId}
        <if test="startDate != null">
            and  TO_DAYS(u.create_date) >= TO_DAYS(#{startDate})
        </if>
        <if test="endDate != null">
            and  TO_DAYS(u.create_date) &lt;= TO_DAYS(#{endDate})
        </if>
        GROUP BY
        u.be_uv
        ) uu ON uu.building_id = t.building_id
        WHERE
        t.org_id = #{orgId}
        <if test="personBuildingList != null and personBuildingList.size > 0">
            AND t.building_id in
            <foreach collection="personBuildingList" item="personBuilding" open="(" close=")" separator=",">
                #{personBuilding.buildingId}
            </foreach>
        </if>
        <if test="buildingId != null and buildingId != '' ">
            AND t.building_id = #{buildingId}
        </if>
    </select>
    <select id="getStatsTimeBarList" resultType="com.huiju.estateagents.excel.StatsTimeBuilding">
        SELECT
            #{buildingName} as building_name,
            t.date as create_date,
            IFNULL( s.sk_num, 0 ) AS sk_num,
            IFNULL( g.gk_num, 0 ) AS gk_num,
            IFNULL( u.uv_num, 0 ) AS uv_num,
            IFNULL( s.sk_num, 0 ) + IFNULL( g.gk_num, 0 ) AS kh_num
        FROM
        (
            SELECT
            DATE_FORMAT( DATE_SUB( #{endDate}, INTERVAL a.rownum DAY ), '%Y-%m-%d' ) AS date
            FROM
            sequence a
            WHERE
            a.rownum &lt;= datediff( #{endDate}, #{startDate} )
        ) t
        LEFT JOIN (
            SELECT
                count( * ) AS sk_num,
                a.building_id,
                DATE_FORMAT( a.create_date, '%Y-%m-%d' ) AS create_date
            FROM
                ta_recommend_customer a
            WHERE
                a.STATUS > 0
                AND a.verify_status = 1
                AND a.realty_consultant IS NOT NULL
                AND a.realty_consultant != ''
                AND a.org_id = #{orgId}
                AND a.building_id = #{buildingId}
                <if test="startDate != null">
                    and  TO_DAYS(a.create_date) >= TO_DAYS(#{startDate})
                </if>
                <if test="endDate != null">
                    and  TO_DAYS(a.create_date) &lt;= TO_DAYS(#{endDate})
                </if>
            GROUP BY
                DATE_FORMAT( a.create_date, '%Y-%m-%d' )
        ) s ON t.date = s.create_date
        LEFT JOIN (
            SELECT
                count( * ) AS gk_num,
                b.building_id,
                DATE_FORMAT( b.create_date, '%Y-%m-%d' ) AS create_date
            FROM
                ta_person b
            WHERE
                b.STATUS > 0
                AND b.org_id = #{orgId}
                AND IFNULL( b.person_type, '' ) IN ( 'estate agent', 'customer' )
                AND b.person_id NOT IN ( SELECT c.person_id FROM ta_recommend_customer c WHERE c.org_id = 84 AND person_id IS NOT NULL AND person_id != '' )
                AND b.building_id = #{buildingId}
                <if test="startDate != null">
                    and  TO_DAYS(b.create_date) >= TO_DAYS(#{startDate})
                </if>
                <if test="endDate != null">
                    and  TO_DAYS(b.create_date) &lt;= TO_DAYS(#{endDate})
                </if>
            GROUP BY
                b.person_id,
                DATE_FORMAT( b.create_date, '%Y-%m-%d' )
        ) g ON t.date = g.create_date
        LEFT JOIN (
            SELECT
                count( * ) AS uv_num,
                u.be_uv AS building_id,
                DATE_FORMAT( u.create_date, '%Y-%m-%d' ) AS create_date
            FROM
                ta_uv u
                LEFT JOIN ta_person p ON u.person_id = p.person_id
            WHERE
                tagert_type = 'project'
                AND p.org_id = #{orgId}
                AND u.be_uv = #{buildingId}
                <if test="startDate != null">
                    and  TO_DAYS(u.create_date) >= TO_DAYS(#{startDate})
                </if>
                <if test="endDate != null">
                    and  TO_DAYS(u.create_date) &lt;= TO_DAYS(#{endDate})
                </if>
            GROUP BY
                DATE_FORMAT( u.create_date, '%Y-%m-%d' )
        ) u ON t.date = u.create_date
        ORDER BY create_date
    </select>
    <select id="getStatsTimeTableList" resultType="com.huiju.estateagents.excel.StatsTimeBuilding">
        SELECT
        t.date as create_date,
        IFNULL( s.sk_num, 0 ) AS sk_num,
        IFNULL( g.gk_num, 0 ) AS gk_num,
        IFNULL( u.uv_num, 0 ) AS uv_num,
        IFNULL( s.sk_num, 0 ) + IFNULL( g.gk_num, 0 ) AS kh_num
        FROM
        (
        SELECT
        DATE_FORMAT( DATE_SUB( #{endDate}, INTERVAL a.rownum DAY ), '%Y-%m-%d' ) AS date
        FROM
        sequence a
        WHERE
        a.rownum &lt;= datediff( #{endDate}, #{startDate} )
        ) t
        LEFT JOIN (
        SELECT
        count( * ) AS sk_num,
        a.building_id,
        DATE_FORMAT( a.create_date, '%Y-%m-%d' ) AS create_date
        FROM
        ta_recommend_customer a
        WHERE
        a.STATUS > 0
        AND a.verify_status = 1
        AND a.realty_consultant IS NOT NULL
        AND a.realty_consultant != ''
        AND a.org_id = #{orgId}
        AND a.building_id = #{buildingId}
        <if test="startDate != null">
            and  TO_DAYS(a.create_date) >= TO_DAYS(#{startDate})
        </if>
        <if test="endDate != null">
            and  TO_DAYS(a.create_date) &lt;= TO_DAYS(#{endDate})
        </if>
        GROUP BY
        DATE_FORMAT( a.create_date, '%Y-%m-%d' )
        ) s ON t.date = s.create_date
        LEFT JOIN (
        SELECT
        count( * ) AS gk_num,
        b.building_id,
        DATE_FORMAT( b.create_date, '%Y-%m-%d' ) AS create_date
        FROM
        ta_person b
        WHERE
        b.STATUS > 0
        AND b.org_id = #{orgId}
        AND IFNULL( b.person_type, '' ) IN ( 'estate agent', 'customer' )
        AND b.person_id NOT IN ( SELECT c.person_id FROM ta_recommend_customer c WHERE c.org_id = 84 AND person_id IS NOT NULL AND person_id != '' )
        AND b.building_id = #{buildingId}
        <if test="startDate != null">
            and  TO_DAYS(b.create_date) >= TO_DAYS(#{startDate})
        </if>
        <if test="endDate != null">
            and  TO_DAYS(b.create_date) &lt;= TO_DAYS(#{endDate})
        </if>
        GROUP BY
        b.person_id,
        DATE_FORMAT( b.create_date, '%Y-%m-%d' )
        ) g ON t.date = g.create_date
        LEFT JOIN (
        SELECT
        count( * ) AS uv_num,
        u.be_uv AS building_id,
        DATE_FORMAT( u.create_date, '%Y-%m-%d' ) AS create_date
        FROM
        ta_uv u
        LEFT JOIN ta_person p ON u.person_id = p.person_id
        WHERE
        u.tagert_type = 'project'
        AND p.org_id = #{orgId}
        AND u.be_uv = #{buildingId}
        <if test="startDate != null">
            and  TO_DAYS(u.create_date) >= TO_DAYS(#{startDate})
        </if>
        <if test="endDate != null">
            and  TO_DAYS(u.create_date) &lt;= TO_DAYS(#{endDate})
        </if>
        GROUP BY
        DATE_FORMAT( u.create_date, '%Y-%m-%d' )
        ) u ON t.date = u.create_date
        ORDER BY
        <if test="sortField != null and sortField != ''">
             ${sortField} ${orderType},
        </if>
        create_date desc
    </select>

</mapper>
