<?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.yunzhi.marketing.mapper.TaRaiseRecordMapper">

    <select id="listHousingRaiseByCondition" resultType="com.yunzhi.marketing.po.TaHousingResourcesPO">
        SELECT
            t.*,
            t4.apartment_name,
            t2.raise_record_id,
            t5.user_name updateName
        FROM
            ta_housing_resources t
            LEFT JOIN ta_raise_house t2 ON t.house_id = t2.house_id
            LEFT JOIN ta_raise_record t3 ON t2.raise_record_id = t3.raise_record_id
            LEFT JOIN ta_building_apartment t4 ON t.apartment_id = t4.apartment_id
            LEFT JOIN ta_user t5 ON t5.user_id = t.update_user
        WHERE
            t.org_id = #{bo.orgId}
            AND t.`status` != - 1
        <if test="bo.salesBatchId != null and bo.salesBatchId != ''">
            AND t.sales_batch_id = #{bo.salesBatchId}
        </if>
        <if test="bo.termName != null and bo.termName != ''">
            AND t.term_name like CONCAT('%',#{bo.termName}, '%')
        </if>
        <if test="bo.blockName != null and bo.blockName != ''">
            AND t.block_name like CONCAT('%',#{bo.blockName}, '%')
        </if>
        <if test="bo.unitName != null and bo.unitName != ''">
            AND t.unit_name like CONCAT('%',#{bo.unitName}, '%')
        </if>
        <if test="bo.floorName != null and bo.floorName != ''">
            AND t.floor_name like CONCAT('%',#{bo.floorName}, '%')
        </if>
        <if test="bo.roomName != null and bo.roomName != ''">
            AND t.room_name like CONCAT('%',#{bo.roomName}, '%')
        </if>
        <if test="bo.status != null">
            AND t.`status` = #{bo.status}
        </if>
        <if test="bo.lockingStatus == 'locked'">
            AND t.house_locking_status = #{bo.lockingStatus}
        </if>
        <if test="bo.lockingStatus == 'unlocked'">
            AND (t.house_locking_status = #{bo.lockingStatus} OR t.house_locking_status is null)
        </if>
        <if test="bo.apartmentId != null and bo.apartmentId != ''">
            AND t.apartment_id = #{bo.apartmentId}
        </if>
        <if test="bo.houseId != null">
            AND t.house_id = #{bo.houseId}
        </if>
        GROUP BY
          t.house_id
        ORDER BY
          t.raise_real_heat DESC
    </select>

    <select id="listRaiseRecordByCondition" resultType="com.yunzhi.marketing.entity.TaRaiseRecord">
        SELECT
            t.*,
            t3.house_id,
            t3.term_name,
            t3.block_name,
            t3.unit_name,
            t3.floor_name,
            t3.room_name,
            t3.apartment_id,
            t4.apartment_name,
            t5.tel personTel,
            t5.nickname,
            t5.avatarurl
        FROM
            ta_raise_record t
            LEFT JOIN ta_raise_house t2 ON t.raise_record_id = t2.raise_record_id
            LEFT JOIN ta_housing_resources t3 ON t2.house_id = t3.house_id
            LEFT JOIN ta_building_apartment t4 ON t3.apartment_id = t4.apartment_id
            LEFT JOIN ta_person t5 ON t.person_id = t5.person_id
        WHERE
            t.org_id = #{bo.orgId}
            AND t.`status` != - 1
            AND t3.`status` != - 1
        <if test="bo.raiseId != null and bo.raiseId != ''">
            AND t.raise_id =#{bo.raiseId}
        </if>
        <if test="bo.houseId != null and bo.houseId != ''">
            AND t3.house_id = #{bo.houseId}
        </if>
        <if test="bo.raiseRecordId != null and bo.raiseRecordId != ''">
            AND t.raise_record_id = #{bo.raiseRecordId}
        </if>
        <if test="bo.termName != null and bo.termName != ''">
            AND t3.term_name like CONCAT('%',#{bo.termName}, '%')
        </if>
        <if test="bo.blockName != null and bo.blockName != ''">
            AND t3.block_name like CONCAT('%',#{bo.blockName}, '%')
        </if>
        <if test="bo.unitName != null and bo.unitName != ''">
            AND t3.unit_name like CONCAT('%',#{bo.unitName}, '%')
        </if>
        <if test="bo.floorName != null and bo.floorName != ''">
            AND t3.floor_name like CONCAT('%',#{bo.floorName}, '%')
        </if>
        <if test="bo.roomName != null and bo.roomName != ''">
            AND t3.room_name like CONCAT('%',#{bo.roomName}, '%')
        </if>
        <if test="bo.status != null">
            AND t.status = #{bo.status}
        </if>
        <if test="bo.lockingStatus != null and bo.lockingStatus != ''">
            AND t.house_locking_status = #{bo.lockingStatus}
        </if>
        <if test="bo.tel != null and bo.tel != ''">
            AND t.tel like CONCAT('%',#{bo.tel} , '%')
        </if>
        <if test="bo.personTel != null and bo.personTel != ''">
            AND t.tel like CONCAT('%',#{bo.personTel}  , '%')
        </if>
        <if test="bo.payStatus != null and bo.payStatus != ''">
            AND t.pay_status = #{bo.payStatus}
        </if>
        <if test="bo.payType != null and bo.payType != ''">
            AND t.pay_type = #{bo.payType}
        </if>
        <if test = "bo.startTime != null and bo.startTime != ''">
            AND t.create_date &gt;= STR_TO_DATE(#{bo.startTime}, '%Y-%m-%d %H:%i:%s')
        </if>
        <if test = "bo.endTime != null and bo.endTime != ''">
            AND t.create_date &lt;= STR_TO_DATE(#{bo.endTime}, '%Y-%m-%d %H:%i:%s')
        </if>
        <if test="bo.personName != null and bo.personName != ''">
            AND t.name like CONCAT('%', #{bo.personName} , '%')
        </if>
        GROUP BY
            t2.raise_record_id
        ORDER BY
          t.create_date DESC
    </select>

    <select id ="listRaiseOrderByCondition" resultType="com.yunzhi.marketing.entity.TaRaiseRecord">
        SELECT
            t.*,
            t2.order_id,
            t2.pay_time,
            t2.trade_no,
            t2.transaction_id,
            t4.refund_no,
            t4.order_id as refunded_order_id,
            t2.total_fee
        FROM
            ta_raise_record t
            INNER JOIN ta_order t2 ON t.order_id = t2.order_id
            LEFT JOIN ta_person t3 ON t2.person_id = t3.person_id
            left join ta_order t4 on t2.order_id = t4.parent_order_id
        WHERE
            t.org_id = #{bo.orgId}
            AND t.`status` != - 1
            <if test = "bo.raiseId != null and bo.raiseId != ''">
                AND t.raise_id = #{bo.raiseId}
            </if>
            <if test = "bo.orderId != null and bo.orderId != ''">
                AND t2.order_id = #{bo.orderId}
            </if>
            <if test = "bo.refundedOrderId != null and bo.refundedOrderId != ''">
                AND t4.order_id = #{bo.refundedOrderId}
            </if>
            <if test = "bo.raiseRecordId != null and bo.raiseRecordId != ''">
                AND t.raise_record_id = #{bo.raiseRecordId}
            </if>
            <if test = "bo.tel != null and bo.tel != ''">
                AND t.tel  like CONCAT('%',#{bo.tel} , '%')
            </if>
            <if test = "bo.personTel != null and bo.personTel != ''">
                AND t3.tel like CONCAT('%',#{bo.personTel}  , '%')
            </if>
            <if test = "bo.transactionId != null and bo.transactionId != ''">
                AND t2.transaction_id = #{bo.transactionId}
            </if>
            <if test = "bo.tradeNo != null and bo.tradeNo != ''">
                AND t2.trade_no = #{bo.tradeNo}
            </if>
            <if test = "bo.refundNo != null and bo.refundNo != ''">
                AND t2.refund_no = #{bo.refundNo}
            </if>
            <if test = "bo.payStatus == 'paid'">
                AND t.pay_status IN ('refunded','paid')
            </if>
            <if test = "bo.payStatus == 'refunded'">
                AND t.pay_status IN ('refunded','change')
            </if>
            <if test = "bo.payStartTime != null and bo.payStartTime != ''">
                AND t2.pay_time &gt;= STR_TO_DATE(#{bo.payStartTime}, '%Y-%m-%d %H:%i:%s')
            </if>
            <if test = "bo.payEndTime != null and bo.payEndTime != ''">
                AND t2.pay_time &lt;= STR_TO_DATE(#{bo.payEndTime}, '%Y-%m-%d %H:%i:%s')
            </if>
        ORDER BY
          t.create_date DESC
    </select>

    <select id="getUserInfo" resultType="com.yunzhi.marketing.entity.TaRaiseRecord">
        select a.nickname as nickname, a.`name` as name, a.phone as personTel, t.tel, t.idcard from ta_raise_record t
        left join ta_person a on t.person_id = a.person_id
        where
        t.raise_record_id = #{raiseRecordId} and t.org_id = #{orgId}
    </select>

    <update id="addHouseNum">
        UPDATE ta_raise_record t
         SET t.house_num = IFNULL ( t.house_num, 0 ) + 1,
             t.update_date = now( ),
             t.update_user_name = #{updateUserName}
        WHERE
            t.raise_record_id = #{raiseRecordId}
            AND t.org_id = #{orgId}
            AND t.`status` != -1
    </update>

</mapper>
