<?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.community.huiju.dao.BillInvoiceMapper">

    <select id="selectBillInvoice" resultType="com.community.huiju.model.BillInvoice">

        SELECT
            tpi.*,
            tboi.owner_name as ownerName,
            tboi.phase as phase,
            tboi.building as building,
            tboi.unit as unit,
            tboi.room_no as roomNo,
            tpi.out_trade_no as billStatement,
            (SELECT user_name from tp_user tu WHERE tu.id = tpi.create_user) as createUserName,
            (SELECT user_name from tp_user tu WHERE tu.id = tpi.update_user) as updateUserName
        FROM
          tp_bill_invoice tpi
        LEFT JOIN tp_building_owner_info tboi ON tpi.building_owner_info_id = tboi.id
        LEFT JOIN tp_bill_statement tbs ON tpi.bill_statement_id = tbs.id
        <where>
            <trim prefixOverrides="and | or">
                <if test="map.phase != null and map.phase != ''">
                    AND tboi.phase = #{map.phase}
                </if>
                <if test="map.building != null and map.building != ''">
                    AND tboi.building = #{map.building}
                </if>
                <if test="map.unit != null and map.unit != ''">
                    AND tboi.unit = #{map.unit}
                </if>
                <if test="map.roomNo != null and map.roomNo != ''">
                    AND tboi.room_no = #{map.roomNo}
                </if>
                <if test="map.ownerName != null and map.ownerName != ''">
                    AND tboi.owner_name like CONCAT('%',#{map.ownerName},'%')
                </if>
                <if test="map.billStatus != null">
                    AND tpi.bill_status = #{map.billStatus}
                </if>
                <if test="map.payName != null and map.payName != ''">
                    AND tpi.pay_name like CONCAT('%',#{map.payName}, '%')
                </if>
                <if test="map.billId != null">
                    AND tpi.bill_id = #{map.billId}
                </if>
                <if test="map.communityId != null">
                    AND tpi.community_Id = #{map.communityId}
                </if>
                AND tpi.status = 1
            </trim>
        </where>
        order by tpi.create_date DESC

    </select>

    <select id="selectBillInvoiceTemp" resultType="com.community.huiju.model.BillInvoice">

        SELECT
        tpi.*,
        tboi.owner_name as ownerName,
        tboi.phase as phase,
        tboi.building as building,
        tboi.unit as unit,
        tboi.room_no as roomNo
        FROM
        tp_bill_invoice tpi
        LEFT JOIN tp_building_owner_info tboi ON tpi.building_owner_info_id = tboi.id
        LEFT JOIN tp_bill_statement tbs ON tpi.bill_statement_id = tbs.id
        <where>
            <trim prefixOverrides="and | or">
                <if test="map.billId != null">
                    AND tpi.bill_id = #{map.billId}
                </if>
                <if test="map.communityId != null">
                    AND tpi.community_Id = #{map.communityId}
                </if>
                AND tpi.status = 0
            </trim>
        </where>
        order by tpi.create_date DESC

    </select>

    <select id="selectBillInvoiceEffective" resultType="com.community.huiju.model.BillInvoice">

        SELECT
        tpi.*,
        tboi.owner_name as ownerName,
        tboi.phase as phase,
        tboi.building as building,
        tboi.unit as unit,
        tboi.room_no as roomNo,
        tbs.id as billStatement,
        (SELECT user_name from tp_user tu WHERE tu.id = tpi.create_user) as createUserName,
        (SELECT user_name from tp_user tu WHERE tu.id = tpi.update_user) as updateUserName
        FROM
        tp_bill_invoice tpi
        LEFT JOIN tp_building_owner_info tboi ON tpi.building_owner_info_id = tboi.id
        LEFT JOIN tp_bill_statement tbs ON tpi.bill_statement_id = tbs.id
        <where>
            <trim prefixOverrides="and | or">
                <if test="map.billId != null">
                    AND tpi.bill_id = #{map.billId}
                </if>
                <if test="map.communityId != null">
                    AND tpi.community_Id = #{map.communityId}
                </if>
                AND tpi.status = 1
            </trim>
        </where>
        order by tpi.create_date DESC
    </select>

</mapper>
