<?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>
    <resultMap id="DetailResultMap" type="cn.ezhizao.project.business.collection.domain.BizCollectionDetail">
        <id column="id" property="id"/>
        <result column="collection_id" property="collectionId"/>
        <result column="contract_id" property="contractId"/>
        <result column="task_type_id" property="taskTypeId"/>
        <result column="arrive_amount" property="arriveAmount"/>
        <result column="arrived" property="arrived"/>
        <result column="remark" property="remark"/>
        <association property="bizCollection" javaType="cn.ezhizao.project.business.collection.domain.BizCollection" >
            <id column="colId" property="id"/>
            <result column="colCompanyName" property="companyName"/>
            <result column="colIsProject" property="isProject"/>
            <result column="colActuallyDate" property="actuallyDate"/>
            <result column="colSubsidiaryName" property="subsidiaryName"/>
            <result column="colApplierName" property="applierName"/>
            <result column="colVerifyName" property="verifyName"/>
            <result column="colVerifyDate" property="verifyDate"/>
            <result column="colProjectName" property="projectName"/>
            <result column="colProjectMainName" property="projectMainName"/>
            <result column="colArriveDate" property="arriveDate"/>
        </association>
        <association property="bizArchiveInput" javaType="cn.ezhizao.project.business.collection.domain.vo.ArchiveVo" >
            <id column="inputId" property="id"/>
            <result column="inputParty" property="party"/>
            <result column="inputAmount" property="amount"/>
            <result column="inputContractType" property="contractType"/>
            <result column="inputContractNo" property="contractNo"/>
        </association>
    </resultMap>
    <select id="getCollection" resultType="cn.ezhizao.project.business.collection.domain.BizCollection">
        select *,user1.nick_name as applierName,user2.nick_name as verifyName,com.name as companyName from biz_collection
        left join sys_user user1 on user1.user_id = biz_collection.applier_id
        left join sys_user user2 on user2.user_id = biz_collection.verify_id
        left join biz_company com on com.id = biz_collection.company_id
        where biz_collection.id = #{collection_id}
    </select>
    <select id="getArchiveInput" resultType="cn.ezhizao.project.business.collection.domain.vo.ArchiveVo">
        select * from biz_archive_input where id = #{contract_id}
    </select>
    <!--带条件的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,
        t4.address_style,
        t4.fiction_address_id,
        t4.province,
        t4.city,
        t4.district,
        t4.belong_region,
        t4.remark as inputRemark,
        t1.remark,
        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>
    </delete>
    <select id="getDetailList" resultMap="DetailResultMap">
        select detail.*,company.name as cname,
        input.id as inputId,
        input.party as inputParty,
        input.amount as inputAmount,
        input.contract_type as inputContractType,
        input.contract_no as inputContractNo,
        collection.id as colId,
        collection.company_name as colCompanyName,
        collection.is_project as colIsProject,
        collection.actually_date as colActuallyDate,
        collection.subsidiary_name as colSubsidiaryName,
        user2.nick_name as colApplierName,
        user1.nick_name as colVerifyName,
        collection.verify_date as colVerifyDate,
        collection.project_name as colProjectName,
        collection.project_main_name as colProjectMainName,
        collection.arrive_date as colArriveDate,
        (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 = detail.contract_detail_id
        and cd.deleted = 0 and detail.contract_id = cd.contract_id and c.status = 1) arrived from biz_collection_detail
        detail
        LEFT JOIN biz_collection collection on collection.id = detail.collection_id
        LEFT JOIN biz_task_type t2 on t2.id = detail.task_type_id
        LEFT JOIN biz_archive_input_detail inputDetail on inputDetail.id = detail.contract_detail_id
        LEFT JOIN biz_archive_input input on input.id = inputDetail.contract_id
        LEFT JOIN biz_company company on detail.company_id = company.id
        left join sys_user user1 on user1.user_id = collection.verify_id
        left join sys_user user2 on user2.user_id = collection.applier_id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            detail.deleted = 0 AND collection.status = 1
            <if test="id != null  ">
                AND   detail.id = #{id}
            </if>
            <if test="collectionId != null  ">
                AND  detail.collection_id = #{collectionId}
            </if>
            <if test="tenantId != null">
                AND  detail.tenant_id = #{tenantId}
            </if>
            <if test="collectionIds != null and collectionIds.size > 0 ">
                AND   collection_id in
                <foreach collection="collectionIds" index="index" item="item" separator=","  open="(" close=")">
                    #{item,jdbcType=BIGINT}
                </foreach>

            </if>
            <if test="contractDetailId != null">
                AND detail.contract_detail_id = #{contractDetailId}
            </if>

            <if test="bizArchiveInput != null and bizArchiveInput.contractNo != null and bizArchiveInput.contractNo != ''">
                AND input.contract_no LIKE CONCAT('%', #{bizArchiveInput.contractNo},'%')
            </if>
            <if test="bizArchiveInput != null and bizArchiveInput.boss != null and bizArchiveInput.boss != ''">
                AND input.boss LIKE CONCAT('%', #{bizArchiveInput.boss},'%')
            </if>
            <if test="bizArchiveInput != null and bizArchiveInput.party != null and bizArchiveInput.party != ''">
                AND input.party LIKE CONCAT('%', #{bizArchiveInput.party},'%')
            </if>
            <if test="bizArchiveInput != null and bizArchiveInput.contractType != null">
                AND input.contract_type = #{bizArchiveInput.contractType}
            </if>
            <if test="bizCollection != null and bizCollection.verifyName != null and bizCollection.verifyName != ''">
                AND user1.nick_name LIKE CONCAT('%', #{bizCollection.verifyName},'%')
            </if>
            <if test="bizCollection != null and bizCollection.applierName != null and bizCollection.applierName != ''">
                AND user2.nick_name LIKE CONCAT('%', #{bizCollection.applierName},'%')
            </if>
            <if test="bizCollection != null and bizCollection.isProject != null">
                AND collection.is_project = #{bizCollection.isProject}
            </if>
            <if test="bizCollection != null and bizCollection.projectMainName != null and bizCollection.projectMainName != ''">
                AND collection.project_main_name LIKE CONCAT('%', #{bizCollection.projectMainName},'%')
            </if>
            <if test="bizCollection != null and bizCollection.projectName != null and bizCollection.projectName != ''">
                AND collection.project_name LIKE CONCAT('%', #{bizCollection.projectName},'%')
            </if>
            <if test="verifyStatus != null">
                AND  collection.status = #{verifyStatus}
            </if>
            <if test="contractId != null  ">
                AND  detail.contract_id = #{contractId}
            </if>
            <if test="bizCollection != null and bizCollection.companyName != null and bizCollection.companyName != '' ">
                AND  company.name LIKE CONCAT('%', #{bizCollection.companyName},'%')
            </if>
            <if test=" bizArchiveInput != null and bizArchiveInput.map != null and bizArchiveInput.map.receiptDateFilter != null">
                AND (
                DATE_FORMAT(collection.actually_date,'%Y-%m-%d') &gt;= #{bizArchiveInput.map.receiptDateFilter[0]}
                AND
                DATE_FORMAT(collection.actually_date,'%Y-%m-%d') &lt;= #{bizArchiveInput.map.receiptDateFilter[1]}
                )
            </if>
            <if test=" bizArchiveInput != null and bizArchiveInput.map != null and bizArchiveInput.map.actuallyDateFilter != null">
            AND(
                DATE_FORMAT(collection.arrive_date,'%Y-%m-%d') &gt;= #{bizArchiveInput.map.actuallyDateFilter[0]}
                AND
                DATE_FORMAT(collection.arrive_date,'%Y-%m-%d') &lt;= #{bizArchiveInput.map.actuallyDateFilter[1]}
                )
            </if>
            <if test="taskTypeId != null  ">
                and detail.task_type_id = #{taskTypeId} AND
            </if>
            <if test="remark != null  and remark != '' ">
                detail.remark = #{remark} AND
            </if>
            <if test="ids != null  ">
                detail.contract_id IN
                <foreach collection="ids" index="index" item="item" separator="," open="(" close=")">
                    #{item,jdbcType=BIGINT}
                </foreach>
                AND
            </if>
        </trim>
        order by detail.create_time desc
    </select>
</mapper>
