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

    <select id="listHousingResources" resultType="com.huiju.estateagents.po.TaHousingResourcesPO">
        SELECT
        *
        FROM
        (
        SELECT
        t.*,
        (
        CASE
        WHEN ( SELECT num_char_extract ( t.term_name, 2 ) + 0 ) = 0 THEN
        ( SELECT num_char_extract ( t.term_name, 2 ) ) ELSE ( SELECT num_char_extract ( t.term_name, 2 ) + 0 )
        END
        ) termNamePre,
        (
        CASE
        WHEN ( SELECT num_char_extract ( t.block_name, 2 ) + 0 ) = 0 THEN
        ( SELECT num_char_extract ( t.block_name, 2 ) ) ELSE ( SELECT num_char_extract ( t.block_name, 2 ) + 0 )
        END
        ) blockNamePre,
        (
        CASE
        WHEN ( SELECT num_char_extract ( t.unit_name, 2 ) + 0 ) = 0 THEN
        ( SELECT num_char_extract ( t.unit_name, 2 ) ) ELSE ( SELECT num_char_extract ( t.unit_name, 2 ) + 0 )
        END
        ) unitNamePre,
        t2.inside_area,
        t2.building_area,
        t2.apartment_name
        FROM
        ta_housing_resources t
        LEFT JOIN ta_building_apartment t2 ON t.apartment_id = t2.apartment_id
        LEFT JOIN ta_sales_batch t4 ON t.sales_batch_id = t4.sales_batch_id
        <if test="source == 'raiseChoose'">
            LEFT JOIN ta_raise_house t5 ON t.house_id = t5.house_id
            LEFT JOIN ta_raise_record t6 ON t6.raise_record_id = t5.raise_record_id
            LEFT JOIN ta_raise t7 ON t.sales_batch_id = t7.sales_batch_id
        </if>
        WHERE
            t.sales_batch_id = #{salesBatchId}
            AND t.org_id = #{orgId}
            AND t.`status` > 0
            AND t4.`status` = 1
        <choose>
            <when test=" endPrice != null and startPrice == null">
                AND t.price &lt;= #{endPrice}
            </when>
            <when test=" startPrice != null and endPrice == null">
                AND t.price &gt;= #{startPrice}
            </when>
            <when test="startPrice != null and endPrice != null ">
                AND ( t.price BETWEEN #{startPrice} AND #{endPrice} )
            </when>
            <otherwise>
            </otherwise>
        </choose>
        <if test="apartmentId != null and apartmentId != ''">
            AND t.apartment_id = #{apartmentId}
        </if>
        ) t5
        GROUP BY
        t5.house_id
        ORDER BY
        t5.termNamePre,
        t5.blockNamePre,
        t5.unitNamePre,
        ( SELECT num_char_extract ( t5.floor_name, 2 ) + 0 ) DESC,
        ( SELECT num_char_extract ( t5.room_name, 2 ) + 0)
    </select>

    <select id="listHousingResourcesForCms" resultType="com.huiju.estateagents.po.TaHousingResourcesPO">
        SELECT
        *
        FROM
        (
        SELECT
        t.house_id,
        (
        CASE
        WHEN ( SELECT num_char_extract ( t.term_name, 2 ) + 0 ) = 0 THEN
        ( SELECT num_char_extract ( t.term_name, 2 ) ) ELSE ( SELECT num_char_extract ( t.term_name, 2 ) + 0 )
        END
        ) termNamePre,
        (
        CASE
        WHEN ( SELECT num_char_extract ( t.block_name, 2 ) + 0 ) = 0 THEN
        ( SELECT num_char_extract ( t.block_name, 2 ) ) ELSE ( SELECT num_char_extract ( t.block_name, 2 ) + 0 )
        END
        ) blockNamePre,
        (
        CASE
        WHEN ( SELECT num_char_extract ( t.unit_name, 2 ) + 0 ) = 0 THEN
        ( SELECT num_char_extract ( t.unit_name, 2 ) ) ELSE ( SELECT num_char_extract ( t.unit_name, 2 ) + 0 )
        END
        ) unitNamePre,
        t.building_id,
        t.block_id,
        t.block_name,
        t.sales_batch_id,
        t.unit_id,
        t.unit_name,
        t.floor_id,
        t.floor_name,
        t.room_id,
        t.room_name,
        t.price,
        t.heat,
        t.real_heat,
        t.apartment_id,
        t.`status`,
        t.org_id,
        t2.inside_area,
        t2.building_area,
        t2.apartment_name,
        t3.term_id,
        t.term_name,
        t.raise_heat,
        t.raise_real_heat,
        t.house_locking_status
        FROM
        ta_housing_resources t
        LEFT JOIN ta_building_apartment t2 ON t.apartment_id = t2.apartment_id
        LEFT JOIN ta_building_block t3 ON t.block_id = t3.block_id
        WHERE
        t.sales_batch_id = #{salesBatchId}
        AND t.org_id = #{orgId}
        <choose>
            <when test=" endPrice != null and startPrice == null">
                AND t.price &lt;= #{endPrice}
            </when>
            <when test=" startPrice != null and endPrice == null">
                AND t.price &gt;= #{startPrice}
            </when>
            <when test="startPrice != null and endPrice != null ">
                AND ( t.price BETWEEN #{startPrice} AND #{endPrice} )
            </when>
            <otherwise>
            </otherwise>
        </choose>
        <if test="apartmentId != null and apartmentId != ''">
            AND t.apartment_id = #{apartmentId}
        </if>
        <choose>
            <when test="type == 1">
                AND t.`status` > 0
            </when>
            <otherwise>
                AND t.`status` > -1
            </otherwise>
        </choose>

        ) t5
        ORDER BY
        t5.termNamePre,
        t5.blockNamePre,
        t5.unitNamePre,
        ( SELECT num_char_extract ( t5.floor_name, 2 ) + 0 ) DESC,
        ( SELECT num_char_extract ( t5.room_name, 2 ) + 0)
    </select>

    <select id="listBuildApartmentBySalesBatchId" resultType="com.huiju.estateagents.entity.TaBuildingApartment">
	    SELECT
            t3.apartment_id,
            t3.apartment_name,
            t3.apartment_type,
            t3.inside_area,
            t3.building_area,
            t2.apartment_id
        FROM
            (
        SELECT
            t.sales_batch_id,
            t.apartment_id
        FROM
            ta_housing_resources t
        WHERE
            t.sales_batch_id = #{salesBatchId}
            AND t.`status` > 0
            AND t.apartment_id IS NOT NULL
        GROUP BY
            t.apartment_id
            ) t2
            LEFT JOIN ta_building_apartment t3 ON t2.apartment_id = t3.apartment_id
        ORDER BY
            t3.apartment_name
    </select>

    <update id="updateForAddHeat" parameterType="java.lang.Integer">
        UPDATE ta_housing_resources t
        SET t.real_heat = IFNULL(t.real_heat, 0) + 1,
            t.update_date = now()
        WHERE
            t.house_id = #{houseId}
    </update>

    <update id="updateForSubtractHeat" parameterType="java.lang.Integer">
        UPDATE ta_housing_resources t
        SET t.real_heat = IFNULL(t.real_heat, 0) - 1,
            t.update_date = now()
        WHERE
            t.house_id = #{houseId}
            AND t.real_heat > 0
    </update>

    <update id="updateForSubtractRaiseHeat" parameterType="java.lang.Integer">
        UPDATE ta_housing_resources t
        SET t.raise_real_heat = IFNULL( t.raise_real_heat, 0 ) - 1,
            t.update_date = now()
        WHERE
            t.house_id IN ( SELECT tr.house_id FROM ta_raise_house tr WHERE tr.raise_record_id = #{raiseRecordId})
            AND t.raise_real_heat > 0
    </update>

    <update id="updateHouseLockingStatus">
        UPDATE ta_housing_resources t
        SET t.house_locking_status = #{houseLockingStatus},
            t.update_date = now()
        WHERE
            t.house_id IN ( SELECT tr.house_id FROM ta_raise_house tr WHERE tr.raise_record_id = #{raiseRecordId})
            AND t.house_locking_person = #{personId}
    </update>

    <select id="getHousingDetailById" resultType="com.huiju.estateagents.po.TaHousingResourcesPO">
        SELECT
        t.house_id,
        t.building_id,
        t.block_id,
        t.block_name,
        t.sales_batch_id,
        t.unit_id,
        t.unit_name,
        t.floor_id,
        t.floor_name,
        t.room_id,
        t.room_name,
        t.price,
        t.heat,
        t.real_heat,
        t.apartment_id,
        t.`status`,
        t.org_id,
        t.term_id,
        t.term_name,
        t2.apartment_name,
        t2.inside_area,
        t2.building_area,
        t3.preselection_start_time,
        t3.preselection_end_time,
        t3.`status` saleBatchStatus,
        t3.display_house_price,
        t4.building_name buildingName
        FROM
        ta_housing_resources t
        LEFT JOIN ta_building_apartment t2 ON t.apartment_id = t2.apartment_id
        LEFT JOIN ta_sales_batch t3 ON t.sales_batch_id = t3.sales_batch_id
        LEFT JOIN ta_building t4 ON t.building_id = t4.building_id
        WHERE
        t.house_id = #{houseId}
        <if test="type != null and type == '1' ">
            AND t.`status` = 1
            AND t3.`status` = 1
        </if>
    </select>

    <select id="getHouseResourceList" resultType="com.huiju.estateagents.entity.TaHousingResources">
        SELECT
        t.* ,
        a.apartment_name,
        u.user_name as update_name
        FROM
        ta_housing_resources t
        LEFT JOIN ta_building_apartment a ON t.apartment_id = a.apartment_id
        LEFT JOIN ta_user u on t.update_user = u.user_id
        WHERE t.`status` > -1
        AND t.org_id = #{orgId}
        and t.building_id = #{buildingId}
        and t.sales_batch_id = #{salesBatchId}
        <if test="apartmentId != null and apartmentId != ''">
            AND t.apartment_id = #{apartmentId}
        </if>
        <if test="status != null">
            AND t.status = #{status}
        </if>
        <if test="houseId != null and houseId != ''">
            AND t.house_id like CONCAT('%',#{houseId}, '%')
        </if>
        <if test="termName != null and termName != ''">
            AND t.term_name like CONCAT('%',#{termName}, '%')
        </if>
        <if test="blockName != null and blockName != ''">
            AND t.block_name like CONCAT('%',#{blockName}, '%')
        </if>
        <if test="unitName != null and unitName != ''">
            AND t.unit_name like CONCAT('%',#{unitName}, '%')
        </if>
        <if test="floorName != null and floorName != ''">
            AND t.floor_name like CONCAT('%',#{floorName}, '%')
        </if>
        <if test="roomName != null and roomName != ''">
            AND t.room_name like CONCAT('%',#{roomName}, '%')
        </if>
        <choose>
            <when test="lockingStatus == 'unlocked'">
                AND (t.house_locking_status = #{lockingStatus} OR t.house_locking_status IS NULL)
            </when>
            <when test="lockingStatus != null and lockingStatus != ''">
                AND t.house_locking_status = #{lockingStatus}
            </when>
        </choose>
        <if test="actulStartHot != '' and actulStartHot != null">
            and t.real_heat &gt;= #{actulStartHot}
        </if>
        <if test="actulEndHot != '' and actulEndHot != null">
            and t.real_heat &lt;= #{actulEndHot}
        </if>
        ORDER BY
            t.create_date DESC
    </select>

    <select id="selectHousingList" resultType="com.huiju.estateagents.entity.TaHousingResources">
        select * from ta_housing_resources where org_id = #{orgId} and sales_batch_id in
        <foreach collection="taSalesBatchList" item="taSalesBatch" open="(" close=")" separator=",">
            #{taSalesBatch.salesBatchId}
        </foreach>
    </select>

    <update id="batchUpdateRaiseHeat">
        UPDATE ta_housing_resources t
        SET t.raise_heat = #{raiseHeat},
        t.update_user = #{updateUser},
        t.update_date =now()
        WHERE
        t.org_id = #{orgId}
        AND t.sales_batch_id = #{salesBatchId}
        AND t.house_id IN
        <foreach collection="housingResourcesList" item="record" open="(" close=")" separator=",">
            #{record.houseId}
        </foreach>
    </update>

    <update id="batchUpdateHeat">
        UPDATE ta_housing_resources t
        SET t.heat = #{heat},
        t.update_user = #{updateUser},
        t.update_date =now()
        WHERE
        t.org_id = #{orgId}
        AND t.sales_batch_id = #{salesBatchId}
        AND t.house_id IN
        <foreach collection="housingResourcesList" item="record" open="(" close=")" separator=",">
            #{record.houseId}
        </foreach>
    </update>

    <select id="listHouseByRaiseRecordId" resultType="com.huiju.estateagents.po.TaHousingResourcesPO">
        SELECT
            t.*,
            t2.raise_house_id,
            t3.person_id lockingPersonId,
            ifnull(t3.name, t3.nickname ) lockingPersonName
        FROM
            ta_housing_resources t
            LEFT JOIN ta_raise_house t2 ON t.house_id = t2.house_id
            left join ta_person t3 on t.house_locking_person = t3.person_id
        WHERE
            t.org_id = #{orgId}
            AND t.`status` != - 1
            AND t2.raise_record_id = #{raiseRecordId}
        ORDER BY
            t.create_date DESC
    </select>

    <select id="getHouseByRaiseHouseId" resultType="com.huiju.estateagents.po.TaHousingResourcesPO">
        SELECT
            t.*,
            t3.person_id lockingPersonId,
            t3.name lockingPersonName
        FROM
            ta_housing_resources t
            LEFT JOIN ta_raise_house t2 ON t.house_id = t2.house_id
            left join ta_person t3 on t.house_locking_person = t3.person_id
        WHERE
            t2.raise_house_id = #{raiseHouseId}
            AND t2.org_id = #{orgId}
            AND t.`status` != -1
    </select>

</mapper>
