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

    <!--带条件的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.id,
        t1.contract_detail_id,
        t1.collection_id,
        t1.collection_contract_id,
        t1.task_type_id,
        t1.company_id,
        t1.contract_id,
        t1.arrive_amount,
        t4.amount as amount,
        t3.name AS company_name,
        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.contract_detail_id = t1.contract_detail_id
            and cd.deleted = 0 and t1.contract_id = cd.contract_id and c.status = 1) arrived
        FROM
        biz_collection_detail t1
        left join biz_collection c on c.id = t1.collection_id
        left join biz_task_type t2 on t2.id = t1.task_type_id
        LEFT JOIN
        biz_company t3 on t1.company_id = t3.id
        left join biz_archive_input_detail t4 on t4.id = t1.contract_detail_id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            t1.deleted = 0 AND
            <if test="id != null  ">
                t1.id = #{id} AND
            </if>
            <if test="collectionId != null  ">
                t1.collection_id = #{collectionId} AND
            </if>
            <if test="tenantId != null">
                t1.tenant_id = #{tenantId} AND
            </if>
            <if test="collectionIds != null and collectionIds.size > 0 ">
                collection_id in
                <foreach collection="collectionIds" index="index" item="item" separator=","  open="(" close=")">
                    #{item,jdbcType=BIGINT}
                </foreach>
                AND
            </if>
            <if test="contractDetailId != null">
                t1.contract_detail_id = #{contractDetailId} AND
            </if>
            <if test="verifyStatus != null">
                c.status = #{verifyStatus} AND
            </if>
            <if test="contractId != null  ">
                t1.contract_id = #{contractId} AND
            </if>
            <if test="companyId != null  ">
                t1.company_id = #{companyId} AND
            </if>
            <if test="taskTypeId != null  ">
                t1.task_type_id = #{taskTypeId} AND
            </if>
            <if test="remark != null  and remark != '' ">
                t1.remark = #{remark} AND
            </if>
            <if test="ids != null  ">
                t1.contract_id IN
                <foreach collection="ids" index="index" item="item" separator="," open="(" close=")">
                    #{item,jdbcType=BIGINT}
                </foreach>
                AND
            </if>
        </trim>
    </sql>
    <!--根据主表id，删除 为的是表单保存时，明细统一物理删除后，再统一插入-->
    <update id="remove">
        UPDATE biz_collection_detail
        SET deleted=1
        WHERE collection_id in
        <foreach collection="ids" item="id" index="index" open="(" separator="," close=")">
            #{id}
        </foreach>
    </update>
    <!--根据主表id，删除 为的是表单保存时，明细统一物理删除后，再统一插入-->
    <delete id="physicallyDelete">
        DELETE FROM
        biz_collection_detail
        WHERE id > 0
        <if test="id != null">
            AND id = #{id}
        </if>
        <if test="collectionId != null">
            AND collection_id = #{collectionId}
        </if>
        <if test="tenantId != null">
            AND tenant_id = #{tenantId}
        </if>
    </delete>
</mapper>
