<?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.TaSalesBatchMapper">
    <update id="updateSalesBatchStatus">
        update ta_sales_batch t set t.status = -1
        where t.org_id = #{orgId}
        and t.sales_batch_id in
        <foreach collection="taSalesBatchList" item="taSalesBatch" open="(" close=")" separator=",">
            #{taSalesBatch.salesBatchId}
        </foreach>
    </update>

    <select id="selectByCondition" resultType="com.huiju.estateagents.entity.TaSalesBatch">
        select t.*, a.building_name from ta_sales_batch t
        left join ta_building a on t.building_id = a.building_id
        where t.org_id = #{orgId} and t.status != -1
        <if test="buildingId != null and buildingId != ''">
            and t.building_id = #{buildingId}
        </if>
        <if test="salesBatchName != null and salesBatchName != ''">
            and t.sales_batch_name like CONCAT('%',#{salesBatchName}, '%')
        </if>
        <if test="status != null">
            and t.status = #{status}
        </if>
        <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
          t.create_date DESC
    </select>

    <select id="countPreSelectRecord" resultType="java.lang.Integer">
        select count(*) from ta_preselection_record t
        where t.org_id = #{orgId}
        and t.status != -1
        and t.sales_batch_id in
        <foreach collection="taSalesBatchList" item="taSalesBatch" open="(" close=")" separator=",">
            #{taSalesBatch.salesBatchId}
        </foreach>
    </select>

    <select id="getTotalHousesAndHeat" resultType="java.util.Map">
        SELECT
            count( * ) totalHouses,
            sum( t.real_heat ) totalRealHeat,
            sum( t.heat) totalHeat,
            sum( t.raise_real_heat ) totalRaiseRealHeat,
	        sum( t.raise_heat ) totalRaiseHeat
        FROM
            ta_housing_resources t
        WHERE
            t.STATUS = 1 and t.sales_batch_id = #{salesBatchId}
    </select>

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

        SELECT
            t2.*
        FROM
            ta_sales_batch t,
            ta_building t2
        WHERE
            t.building_id = t2.building_id
            AND t.sales_batch_id = #{salesBatchId}
            AND t.org_id = #{orgId}
            AND t.`status` != -1

    </select>

    <update id="addSalesNumberById">
        UPDATE ta_sales_batch t
        SET t.sales_number = IFNULL( t.sales_number, 0 ) + 1
        WHERE
            t.org_id = #{orgId}
            AND t.sales_batch_id = #{salesBatchId}
    </update>
</mapper>
