<?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.property.dao.BillInvoiceMapper">
    <sql id="Base_Column_List" >
    id, org_id, bill_id, building_owner_info_id, ta_user_id, bill_invoice_explain,
    pay_price, bill_statement_id, bill_status, pay_name, pay_date, create_user, create_date,
    update_user, update_date, pay_type
  </sql>
    <select id="selectBillInvoice" resultType="com.huiju.estateagents.property.model.BillInvoice">

        SELECT
            tpi.*,
            tboi.owner_name as ownerName,
            tpro.phase_name as phase,
            tpro.building_name as building,
            tpro.unit_name as unit,
            tpro.name as roomNo,
            tpbo.order_bumber as billStatement,
            (SELECT user_name from ta_user tu WHERE tu.user_id = tpi.create_user) as createUserName,
            (SELECT user_name from ta_user tu WHERE tu.user_id = tpi.update_user) as updateUserName
        FROM
          tp_bill_invoice tpi
        LEFT JOIN tp_room_no tpro ON tpi.room_no_id = tpro.id
        LEFT JOIN tp_bill_order tpbo ON tpi.id = tpbo.tp_bill_invoice_id
        LEFT JOIN tp_building_owner_info tboi ON tpi.building_owner_info_id = tboi.id
        <where>
            <trim prefixOverrides="and | or">
                <if test="map.phase != null and map.phase != ''">
                    AND tpro.phase_id = #{map.phase}
                </if>
                <if test="map.building != null and map.building != ''">
                    AND tpro.building_id = #{map.building}
                </if>
                <if test="map.unit != null and map.unit != ''">
                    AND tpro.unit_id = #{map.unit}
                </if>
                <if test="map.roomNo != null and map.roomNo != ''">
                    AND tpro.room_no_id = #{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.orgId != null">
                    AND tpi.org_id = #{map.orgId}
                </if>
                AND tpi.status = 1
            </trim>
        </where>
        order by tpi.create_date DESC

    </select>

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

        SELECT
        tpi.*,
        tboi.owner_name as ownerName,
        tprn.phase_name as phase,
        tprn.building_name as building,
        tprn.unit_name as unit,
        tprn.name as roomNo
        FROM
        tp_bill_invoice tpi
        LEFT JOIN tp_room_no tprn ON tpi.room_no_id = tprn.id
        LEFT JOIN tp_building_owner_info tboi ON tpi.bill_statement_id = tboi.id
        <where>
            <trim prefixOverrides="and | or">
                <if test="map.billId != null">
                    AND tpi.bill_id = #{map.billId}
                </if>
                <if test="map.orgId != null">
                    AND tpi.org_id = #{map.orgId}
                </if>
                AND tpi.status = 0
            </trim>
        </where>
        order by tpi.create_date DESC
    </select>

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

        SELECT
        tpi.*,
        tboi.owner_name as ownerName,
        tpro.phase_name as phase,
        tpro.building_name as building,
        tpro.unit_name as unit,
        tpro.name as roomNo,
        tpbo.order_bumber as billStatement,
        (SELECT user_name from ta_user tu WHERE tu.user_id = tpi.create_user) as createUserName,
        (SELECT user_name from ta_user tu WHERE tu.user_id = tpi.update_user) as updateUserName
        FROM
        tp_bill_invoice tpi
        LEFT JOIN tp_room_no tpro ON tpi.room_no_id = tpro.id
        LEFT JOIN tp_bill_order tpbo ON tpi.id = tpbo.tp_bill_invoice_id
        LEFT JOIN tp_building_owner_info tboi ON tpi.building_owner_info_id = tboi.id
        <where>
            <trim prefixOverrides="and | or">
                <if test="map.billId != null">
                    AND tpi.bill_id = #{map.billId}
                </if>
                <if test="map.orgId != null">
                    AND tpi.org_id = #{map.orgId}
                </if>
                AND tpi.status = 1
            </trim>
        </where>
        order by tpi.create_date DESC
    </select>

    <select id="getPriceCount" resultType="map">
        SELECT
            sum( pay_price ) AS totalPrice,
            org_id AS orgId
        FROM
            tp_bill_invoice
        WHERE
            `status` = 1
        AND bill_status = 1
        GROUP BY
            org_id
    </select>

    <select id="getBillsList" resultType="map">
        SELECT
        i.id,
        i.bill_invoice_explain as billInvoiceExplain,
        i.pay_price AS payPrice,
        i.ta_user_id AS taUserId,
        i.bill_status AS billStatus,
        b.bill_name AS billName,
        b.bill_explain AS billExplain,
        b.end_date AS endDate,
        b.create_date AS createDate,
        i.out_trade_no AS outTradeNo
        FROM
        tp_bill_invoice i
        LEFT JOIN tp_bill b ON b.id = i.bill_id
        AND b.org_id = #{orgId,jdbcType=INTEGER}
        WHERE
        i.org_id = #{orgId,jdbcType=INTEGER}
        <if test="payType == 0">
            AND i.bill_status = 0
        </if>
        <if test="payType == 1">
            AND (i.bill_status = 1 or i.bill_status = 2)
        </if>
        and i.status=1
        AND i.room_no_id in (SELECT room_no_id
        from ta_user_verify WHERE person_id = #{personId}
        )
    </select>

    <select id="getBillInvoiceDetail" resultType="map">
    SELECT
        i.id,
        i.bill_invoice_explain AS billInvoiceExplain,
        i.pay_price AS payPrice,
        i.ta_user_id AS taUserId,
        i.bill_status AS billStatus,
        b.bill_name AS billName,
        b.bill_explain AS billExplain,
        b.end_date AS endDate,
        s.id AS billStatementId,
        s.pay_name AS payName,
        s.pay_remark AS payRemark,
        s.pay_type AS payType,
        s.create_time AS createTime,
        i.out_trade_no AS outTradeNo
    FROM
        tp_bill_invoice i
        LEFT JOIN tp_bill b ON b.id = i.bill_id
        AND b.org_id = #{orgId,jdbcType=INTEGER}
        LEFT JOIN tp_bill_statement s ON i.bill_statement_id = s.id
        AND s.org_id = #{orgId,jdbcType=INTEGER}
    WHERE
        i.id = #{billInvoiceId,jdbcType=INTEGER}
    AND i.org_id = #{orgId,jdbcType=INTEGER}
  </select>

    <select id="selectByIdAndroomNoId" resultType="com.huiju.estateagents.property.model.BillInvoice" >
    SELECT
        t.id,
        t.org_id AS orgId,
        t.bill_id AS billId,
        t.building_owner_info_id AS buildingOwnerInfoId,
        t.ta_user_id AS taUserId,
        t.bill_invoice_explain AS billInvoiceExplain,
        t.pay_price AS payPrice,
        t.bill_statement_id AS billStatementId,
        t.bill_status AS billStatus,
        t.pay_name AS payName,
        t.out_trade_no AS outTradeNo,
        b.bill_name as billName
    FROM
	    tp_bill_invoice t
	LEFT JOIN tp_bill b ON t.bill_id = b.id and b.org_id = #{orgId,jdbcType=INTEGER}
    where t.id = #{id,jdbcType=INTEGER}
    and t.org_id = #{orgId,jdbcType=INTEGER}
    and t.status = 1
    and t.room_no_id = #{roomNoId,jdbcType=INTEGER}
  </select>

    <select id="selectByOutTradeNo" resultType="com.huiju.estateagents.property.model.BillInvoice" >
  SELECT
        t.id,
        t.org_id AS orgId,
        t.bill_id AS billId,
        t.building_owner_info_id AS buildingOwnerInfoId,
        t.ta_user_id AS taUserId,
        t.bill_invoice_explain AS billInvoiceExplain,
        t.pay_price AS payPrice,
        t.bill_statement_id AS billStatementId,
        t.bill_status AS billStatus,
        t.pay_name AS payName,
        t.out_trade_no AS outTradeNo,
        b.bill_name as billName
    FROM
	    tp_bill_invoice t
	LEFT JOIN tp_bill b ON t.bill_id = b.id
	where t.out_trade_no = #{outTradeNo,jdbcType=VARCHAR}
	and t.status = 1
  </select>

    <select id="selectByBuildingOwnerInfoId" parameterType="integer" resultType="com.huiju.estateagents.property.model.BillInvoice" >
        select
        <include refid="Base_Column_List"/>
        from tp_bill_invoice
        where bill_statement_id = #{buildingOwnerInfoId,jdbcType=INTEGER}
    </select>

    <update id="updateBeach" parameterType="map" >
        update tp_bill_invoice
        set ta_user_id = #{map.taUserId}
        where id in
        <foreach collection="map.list" item="item" index="index" separator="," open="(" close=")">
            #{item.id}
        </foreach>
    </update>
</mapper>
