<?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.TaPersonIntentionRecordMapper">

    <select id="selectAll" resultType="com.huiju.estateagents.entity.TaPersonIntentionRecord">
        SELECT
            *,
            tp.phone as phone,
            SUM(tpir.intention) as intentionCount
        FROM
            ta_person_intention_record tpir
            LEFT JOIN ta_person tp ON tpir.person_id = tp.person_id
            GROUP BY tpir.person_id
    </select>


    <select id="selectIntentionUser" resultType="map">
        SELECT
            tpir.person_name as  personName,
            tp.phone as phone,
            tpir.building_name as buildingName,
            SUM(tpir.intention) as intention
        FROM
            ta_person_intention_record tpir
            LEFT JOIN ta_person tp on tpir.person_id = tp.person_id
            <trim prefix="where" prefixOverrides="and | or">
                <if test="buildingId != null and buildingId != ''">
                    tpir.building_id = #{buildingId}
                </if>
                <if test="orgId != null">
                    AND tpir.org_id = #{orgId}
                    AND tp.org_id = #{orgId}
                </if>
                <if test="personBuildingList != null and personBuildingList.size > 0">
                    AND tpir.building_id in
                    <foreach collection="personBuildingList" item="personBuilding" open="(" close=")" separator=",">
                        #{personBuilding.buildingId}
                    </foreach>
                </if>
            </trim>
        GROUP BY
            tpir.person_id, tpir.building_id
        ORDER BY SUM(tpir.intention) DESC
    </select>
    <select id="selectExportIntentionUser" resultType="com.huiju.estateagents.excel.IntentionUser">
        SELECT
        tpir.person_name as  person_name,
        tp.phone as phone,
        tpir.building_name as building_name,
        SUM(tpir.intention) as intention
        FROM
        ta_person_intention_record tpir
        LEFT JOIN ta_person tp on tpir.person_id = tp.person_id
        <trim prefix="where" prefixOverrides="and | or">
            <if test="buildingId != null and buildingId != ''">
                tpir.building_id = #{buildingId}
            </if>
            <if test="orgId != null">
                AND tpir.org_id = #{orgId}
                AND tp.org_id = #{orgId}
            </if>
            <if test="personBuildingList != null and personBuildingList.size > 0">
                AND tpir.building_id in
                <foreach collection="personBuildingList" item="personBuilding" open="(" close=")" separator=",">
                    #{personBuilding.buildingId}
                </foreach>
            </if>
        </trim>
        GROUP BY
        tpir.person_id, tpir.building_id
        ORDER BY SUM(tpir.intention) DESC
    </select>
</mapper>
