<?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="cn.ezhizao.project.business.payment.mapper.BizPaymentDetailMapper">
    <!-- 基础的返回map 多表复杂的 自行组合 -->
    <resultMap id="BaseResultMap" type="cn.ezhizao.project.business.payment.domain.BizPaymentDetail"/>

    <!--根据主表id，删除 为的是表单保存时，明细统一物理删除后，再统一插入-->
    <delete id="physicallyDelete">
        DELETE FROM
        biz_payment_detail
        WHERE id > 0
        <if test="id != null">
            AND id = #{id}
        </if>
        <if test="paymentId != null">
            AND payment_id = #{paymentId}
        </if>
    </delete>

    <!--带条件的List查询-->
    <select id="getList" resultMap="BaseResultMap">
        <include refid="getListSql"/>
    </select>

    <!--带条件的List查询 数据总条数-->
    <select id="getTotal" resultType="java.lang.Integer">
        SELECT COUNT(1) FROM (<include refid="getListSql"/>) AS C
    </select>

    <sql id="getListSql">
        SELECT t1.*, t2.name as task_type_name,
        (select sum(cd.arrive_amount) from biz_collection_detail cd left join biz_collection c on c.id =
        cd.collection_id where cd.deleted = 0 and c.status = 1 and cd.contract_detail_id = t1.contract_detail_id)
        arrived
--         (select sum(payment_amount) from biz_payment_detail pd where pd.deleted = 0
--         and pd.contract_detail_id = t1.contract_detail_id) payment
        FROM biz_payment_detail t1
        left join biz_task_type t2 on t2.id = t1.task_type_id
        WHERE t1.deleted = 0
        <if test="id != null">
            AND t1.id = #{id}
        </if>
        <if test="tenantId != null">
            AND t1.tenant_id = #{tenantId}
        </if>
        <if test="paymentId != null">
            AND t1.payment_id = #{paymentId}
        </if>
        <if test="contractId != null">
            AND t1.contract_id = #{contractId}
        </if>
        <if test="companyId != null">
            AND t1.company_id = #{companyId}
        </if>
        <if test="buyNum != null">
            AND t1.buy_num = #{buyNum}
        </if>
        <if test="freeNum != null">
            AND t1.free_num = #{freeNum}
        </if>
        <if test="unit != null and unit != ''">
            AND t1.unit = #{unit}
        </if>
        <if test="amount != null">
            AND t1.amount = #{amount}
        </if>
        <if test="discountAmount != null">
            AND t1.discount_amount = #{discountAmount}
        </if>
        <if test="dealPrice != null">
            AND t1.deal_price = #{dealPrice}
        </if>
        <if test="estimateCost != null">
            AND t1.estimate_cost = #{estimateCost}
        </if>
        <if test="arriveAmount != null">
            AND t1.arrive_amount = #{arriveAmount}
        </if>
        <if test="remark != null and remark != ''">
            AND t1.remark = #{remark}
        </if>
        <if test="createTime != null">
            AND t1.create_time = #{createTime}
        </if>
        <if test="creatorId != null">
            AND t1.creator_id = #{creatorId}
        </if>
        <if test="updateTime != null">
            AND t1.update_time = #{updateTime}
        </if>
        <if test="updaterId != null">
            AND t1.updater_id = #{updaterId}
        </if>
        <if test="deleted != null">
            AND t1.deleted = #{deleted}
        </if>
        <if test="version != null">
            AND t1.version = #{version}
        </if>
    </sql>
</mapper>
