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

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

    <!--带条件的List查询 数据总条数-->
    <select id="getTotal" resultType="java.lang.Integer">
        SELECT COUNT(1) FROM (<include refid="getListSql"/>) AS C
    </select>
    <select id="statistics" resultType="resStatisticsVo">
        SELECT
        tt1.actually_date,
        tt1.company_id as companyId,
        tt1.name as companyName,
        tt1.task_type_id as taskTypeId,
        tt1.typeName,
        tt1.contract_id as contractId,
        tt1.contract_no as contractNo,
        date_format(tt1.daozhangriqi, '%Y-%m-%d') as receiptDate,
        tt1.tishoukuanjine as withdrawalAmount,
        tt1.hetongleixing as contractType,
        tt1.shenqingren as applierName,
        tt1.shifouweixiangmu as isProject,
        tt1.fuwujine as detailAllAmount,
        tt1.shishoujine as reallyDetailAmount,
        tt1.daishoukuanjine as waitDetailAmount,
        tt1.shenheshijian as examineTime,
        tt1.verifyName,
        tt1.actually_date,
        tt2.zhichu as outAmount,
        tt1.input_start_month as inputStartMonth,
        tt1.input_end_month as inputEndMonth,
        tt1.service_num + tt1.free_num            as serviceNum,
        tt1.projectName                          as projectName
        FROM
        (
        SELECT
        inputdetail.input_start_month,
        inputdetail.input_end_month,
        inputdetail.free_num,
        inputdetail.service_num,
        GROUP_CONCAT(DISTINCT c.project_name) as projectName,
        c.company_id,
        GROUP_CONCAT( DISTINCT date_format( c.actually_date, '%Y-%m-%d' )) AS actually_date,
        com.NAME,
        detail.task_type_id,
        GROUP_CONCAT( DISTINCT u2.nick_name ) AS verifyName,
        type.`name` as typeName,
        c.contract_id,
        input.contract_no,
        GROUP_CONCAT( DISTINCT arrive_date ) AS daozhangriqi,
        SUM( detail.arrive_amount ) AS tishoukuanjine,
        GROUP_CONCAT( DISTINCT u.nick_name ) AS shenqingren,
        GROUP_CONCAT( DISTINCT c.project_name ) AS project_name,
        CASE
        WHEN input.contract_type = 1 THEN
        "续签" ELSE "新签"
        END AS hetongleixing,
        CASE
        WHEN c.is_project = 0 THEN '项目'
        WHEN c.is_project = 1 THEN '非项目'
        ELSE ''
        END AS shifouweixiangmu,
        detail.amount AS fuwujine,
        SUM( CASE WHEN c.STATUS = 1 THEN detail.arrive_amount ELSE 0 END ) AS shishoujine,
        (
        detail.amount - SUM( CASE WHEN c.STATUS = 1 THEN detail.arrive_amount ELSE 0 END )) AS
        daishoukuanjine,
        GROUP_CONCAT(
        IF
        ( c.STATUS = 1, c.verify_date, NULL )) AS shenheshijian
        FROM
        biz_collection c
        LEFT JOIN biz_collection_detail detail ON c.id = detail.collection_id
        LEFT JOIN biz_company com ON com.id = c.company_id
        LEFT JOIN biz_archive_input input ON input.id = c.contract_id
        LEFT JOIN sys_user u ON u.user_id = c.applier_id
        LEFT JOIN biz_task_type type on type.id = detail.task_type_id
        LEFT JOIN sys_user u2 on u2.user_id = c.verify_id
        left join biz_archive_input_detail inputdetail on c.contract_id = inputdetail.contract_id and detail.task_type_id = inputdetail.task_type_id
        WHERE
        c.deleted = 0
        AND c.STATUS = 1
        AND com.tenant_id = #{conditions.tenantId}
        <if test="conditions.companyName != null and conditions.companyName != ''">
            AND com.NAME LIKE CONCAT('%',#{conditions.companyName},'%')
        </if>
        <if test="conditions.projectName != null and conditions.projectName != ''">
            AND c.project_name LIKE CONCAT('%',#{conditions.projectName},'%')
        </if>
        <if test="conditions.examineName != null and conditions.examineName != ''">
            AND u2.nick_name LIKE CONCAT('%',#{conditions.examineName},'%')
        </if>
        <if test="conditions.contractNo != null and conditions.contractNo != ''">
            AND input.contract_no LIKE CONCAT('%',#{conditions.contractNo},'%')
        </if>
        <if test="conditions.taskTypeIds != null and conditions.taskTypeIds.size() &gt; 0">
            AND detail.task_type_id IN
            <foreach collection="conditions.taskTypeIds" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="conditions.receiptDateFilter != null and conditions.receiptDateFilter.size() ">
            AND (
            DATE_FORMAT(c.arrive_date,'%Y-%m-%d') &gt;= #{conditions.receiptDateFilter[0]}
            AND
            DATE_FORMAT(c.arrive_date,'%Y-%m-%d') &lt;= #{conditions.receiptDateFilter[1]}
            )
        </if>
        <if test="conditions.examineTimeFilter != null and conditions.examineTimeFilter.size() ">
            AND(
            DATE_FORMAT(c.verify_date,'%Y-%m-%d') &gt;= #{conditions.examineTimeFilter[0]}
            AND
            DATE_FORMAT(c.verify_date,'%Y-%m-%d') &lt;= #{conditions.examineTimeFilter[1]}
            )
        </if>

        <if test="conditions.actuallyDateFilter != null and conditions.actuallyDateFilter.size() ">
            AND(
            DATE_FORMAT(c.actually_date,'%Y-%m-%d') &gt;= #{conditions.actuallyDateFilter[0]}
            AND
            DATE_FORMAT(c.actually_date,'%Y-%m-%d') &lt;= #{conditions.actuallyDateFilter[1]}
            )
        </if>
        <if test="conditions.isProject != null">
            AND c.is_project = #{conditions.isProject}
        </if>
        <if test="conditions.isNew != null">
            AND c.is_new = #{conditions.isNew}
        </if>
        GROUP BY
        c.company_id,
        detail.task_type_id,
        c.contract_id
        order by
        c.create_time desc
        ) tt1
        LEFT JOIN (
        SELECT
        t1.company_id,
        t3.task_type_id,
        t1.contract_id,
        SUM( CASE WHEN t1.payment_status = 1 THEN t3.payment_amount ELSE 0 END ) zhichu
        FROM
        biz_payment t1
        LEFT JOIN biz_archive_input t2 ON t1.contract_id = t2.id
        LEFT JOIN biz_payment_detail t3 ON t1.id = t3.payment_id
        left join biz_company com on com.id = t1.company_id
        left join biz_archive_input input on input.id = t1.contract_id
        WHERE
        t1.deleted = 0
        AND t1.payment_status = 1
        AND t1.verify_status = 3
        AND com.tenant_id = #{conditions.tenantId}
        <if test="conditions.companyName != null and conditions.companyName != ''">
            AND com.NAME LIKE CONCAT('%',#{conditions.companyName},'%')
        </if>
        <if test="conditions.contractNo != null and conditions.contractNo != ''">
            AND input.contract_no LIKE CONCAT('%',#{conditions.contractNo},'%')
        </if>
        <if test="conditions.taskTypeIds != null and conditions.taskTypeIds.size() &gt; 0">
            AND t3.task_type_id IN
            <foreach collection="conditions.taskTypeIds" index="index" item="item" open="(" separator="," close=")">
                #{item}
            </foreach>
        </if>
        <if test="conditions.examineTimeFilter != null and conditions.examineTimeFilter.size() &gt; 0 ">
            AND (
            DATE_FORMAT(t1.verify_date,'%Y-%m-%d') &gt;= #{conditions.examineTimeFilter[0]}
            AND
            DATE_FORMAT(t1.verify_date,'%Y-%m-%d') &lt;= #{conditions.examineTimeFilter[1]}
            )
        </if>

        GROUP BY
        t1.company_id,
        t3.task_type_id,
        t1.contract_id
        ) tt2 ON tt1.company_id = tt2.company_id
        AND tt1.task_type_id = tt2.task_type_id
        AND tt1.contract_id = tt2.contract_id
    </select>
    <select id="getByContractIn" resultType="cn.ezhizao.project.business.collection.domain.vo.ResStatisticsVo">
        SELECT
        t3.NAME AS companyName,
        t4.`name` AS taskTypeName,
        t5.contract_no AS contractNo,
        t1.arrive_date,
        t6.nick_name,
        CASE
        WHEN t5.contract_type = 1 THEN "续签" ELSE "新签"
        END AS contractType,
        CASE
        WHEN t1.is_project = 0 THEN '项目'
        WHEN t1.is_project = 1 THEN '非项目'
        ELSE ''
        END AS isProject,
        t2.amount AS detailAllAmount,
        CASE WHEN t1.STATUS = 1 THEN t2.arrive_amount ELSE 0 END AS reallyDetailAmount,
        t1.verify_date as examineTime,
        t1.applier_id,
        t6.nick_name AS applierName,
        t2.task_type_id,
        t7.name as typeName,
        t8.nick_name AS verifyName,
        t1.actually_date,
        date_format( t1.arrive_date, '%Y-%m-%d') as receiptDate
        FROM
        biz_collection t1 right join biz_collection_detail t2 ON t1.id = t2.collection_id
        LEFT JOIN biz_company t3 ON t3.id = t1.company_id
        LEFT JOIN biz_task_type t4 ON t4.id = t2.task_type_id
        LEFT JOIN biz_archive_input t5 ON t5.id = t1.contract_id
        LEFT JOIN sys_user t6 ON t6.user_id = t1.applier_id
        LEFT JOIN biz_task_type t7 on t7.id = t2.task_type_id
        left join sys_user t8 on t8.user_id = t1.verify_id
        WHERE
        t1.contract_id = #{conditions.contractId}
        AND
        t2.task_type_id = #{conditions.taskTypeIds[0]}
        <if test="conditions.receiptDateFilter != null and conditions.receiptDateFilter.size() &gt; 0 ">
            AND (
            DATE_FORMAT(c.arrive_date,'%Y-%m-%d') &gt;= #{conditions.receiptDateFilter[0]}
            AND
            DATE_FORMAT(c.arrive_date,'%Y-%m-%d') &lt;= #{conditions.receiptDateFilter[1]}
            )
        </if>
        <if test="conditions.examineTimeFilter != null and conditions.examineTimeFilter.size() &gt; 0 ">
            AND(
            DATE_FORMAT(c.verify_date,'%Y-%m-%d') &gt;= #{conditions.examineTimeFilter[0]}
            AND
            DATE_FORMAT(c.verify_date,'%Y-%m-%d') &lt;= #{conditions.examineTimeFilter[1]}
            )
        </if>
    </select>
    <select id="getByContractOut"
            resultType="cn.ezhizao.project.business.collection.domain.vo.ResStatisticsVo">
        select tt1.*, tt2.zongjine as withdrawalAmount
        from (SELECT t3.NAME AS companyName,
        t4.`name` AS taskTypeName,
        t5.contract_no AS contractNo,
        t6.nick_name,
        t2.amount AS detailAllAmount,
        t2.payment_amount AS reallyDetailAmount,
        t1.verify_date AS examineTime,
        t2.task_type_id,
        t1.company_id,
        t1.contract_id,
        t1.applier_id,
        t7.name as typeName,
        t6.nick_name AS applierName

        FROM biz_payment t1
        RIGHT JOIN biz_payment_detail t2 ON t1.id = t2.payment_id
        LEFT JOIN biz_company t3 ON t3.id = t1.company_id
        LEFT JOIN biz_task_type t4 ON t4.id = t2.task_type_id
        LEFT JOIN biz_archive_input t5 ON t5.id = t1.contract_id
        LEFT JOIN sys_user t6 ON t6.user_id = t1.applier_id
        LEFT JOIN biz_task_type t7 on t7.id = t2.task_type_id
        WHERE t1.contract_id = #{conditions.contractId}
        AND t2.task_type_id = #{conditions.taskTypeIds[0]}
        <if test="conditions.examineTimeFilter != null and conditions.examineTimeFilter.size() &gt; 0 ">
            AND (
            DATE_FORMAT(t1.verify_date,'%Y-%m-%d') &gt;= #{conditions.examineTimeFilter[0]}
            AND
            DATE_FORMAT(t1.verify_date,'%Y-%m-%d') &lt;= #{conditions.examineTimeFilter[1]}
            )
        </if>
        ORDER BY t1.verify_date DESC

        ) tt1
        LEFT JOIN
        (SELECT p.company_id,
        de.task_type_id,
        p.contract_id,
        sum(de.payment_amount) as zongjine
        FROM biz_payment p
        right join biz_payment_detail de ON p.id = de.payment_id
        WHERE p.verify_status != 4
        <if test="conditions.examineTimeFilter != null and conditions.examineTimeFilter.size() &gt; 0 ">
            AND (
            DATE_FORMAT(t1.verify_date,'%Y-%m-%d') &gt;= #{conditions.examineTimeFilter[0]}
            AND
            DATE_FORMAT(t1.verify_date,'%Y-%m-%d') &lt;= #{conditions.examineTimeFilter[1]}
            )
        </if>
        GROUP BY
        p.company_id,
        de.task_type_id,
        p.contract_id) tt2 on tt1.task_type_id = tt2.task_type_id and tt1.company_id = tt2.company_id and
        tt1.contract_id = tt2.contract_id

    </select>

    <sql id="getListSql">
        SELECT
        ai.boss,ai.party,
        a.id, a.flow_no, a.subsidiary_id, a.subsidiary, a.subsidiary_name, a.subsidiary_bank_account, a.form_date
        , a.subsidiary_bank_name, a.company_id, b.name as company_name
        , a.is_new, a.is_project, a.project_name, a.project_no, a.project_main_name
        , a.amount
        , a.verify_status
        , a.arrive_amount
        , a.arrive_date
        , a.arrive_time, a.status, a.applier_id, a.verify_date, a.verify_comment
        , a.arrive_status, a.apply_name
        , a.appy_account, a.create_time, a.creator_id, a.update_time, a.updater_id
        , a.deleted, a.version, a.remark
        , a.actually_date
        , b.name as company_name, t5.nick_name as applier_name
        , ai.contract_no
        , (select ifnull(sum(amount), 0) from biz_collection col where col.contract_id = a.contract_id and col.status =
        1) payment_amount
        FROM biz_collection a left join biz_company b on b.id = a.company_id
        left join biz_archive_input ai on ai.id = a.contract_id
        left join sys_user t5 on t5.user_id = a.applier_id
        <trim prefix=" WHERE" suffix="" suffixOverrides="AND">
            a.deleted = 0 AND
            <if test="id != null  ">
                a.id = #{id} AND
            </if>
            <if test="tenantId != null">
                a.tenant_id = #{tenantId} AND
            </if>
            <if test="contractId != null">
                a.contract_id = #{contractId} AND
            </if>
            <if test="contractNo != null and contractNo != ''">
                ai.contract_no LIKE CONCAT('%',#{contractNo},'%') AND
            </if>
            <if test="flowNo != null  and formNo != '' ">
                a.flow_no LIKE CONCAT('%',#{flowNo},'%') AND
            </if>
            <if test="subsidiaryId != null">
                a.subsidiary_id = #{subsidiaryId} AND
            </if>
            <if test="subsidiary != null  and subsidiary != '' ">
                a.subsidiary = #{subsidiary} AND
            </if>
            <if test="subsidiaryName != null  and subsidiaryName != '' ">
                a.subsidiary_name LIKE CONCAT('%',#{subsidiaryName},'%') AND
            </if>
            <if test="subsidiaryBankAccount != null  and subsidiaryBankAccount != ''">
                a.subsidiary_bank_account = #{subsidiaryBankAccount} AND
            </if>
            <if test="subsidiaryBankName != null  and subsidiaryBankName != ''">
                a.subsidiary_bank_name = #{subsidiaryBankName} AND
            </if>
            <if test="companyId != null">
                a.company_id = #{companyId} AND
            </if>
            <if test="companyName != null  and companyName != '' ">
                (LOCATE(#{companyName}, a.company_name) or LOCATE(#{companyName}, b.name)) AND
            </if>
            <if test="applierName != null  and applierName != '' ">
                LOCATE(#{applierName}, t5.nick_name) AND
            </if>
            <if test="amount != null  ">
                a.amount = #{amount} AND
            </if>
            <if test="arriveDate != null  ">
                a.arrive_date = #{arriveDate} AND
            </if>
            <if test="actuallyDate != null">
                DATE_FORMAT(a.actually_date, '%Y-%m-%d') = #{actuallyDate} AND
            </if>
            <if test="startDate != null and endDate != null">
                (
                DATE_FORMAT(a.arrive_date,'%Y-%m-%d') &gt;= #{startDate}
                AND
                DATE_FORMAT(a.arrive_date,'%Y-%m-%d') &lt;= #{endDate}
                ) AND
            </if>
            <if test="dateFilter != null">
                (
                DATE_FORMAT(a.verify_date,'%Y-%m-%d') &gt;= #{dateFilter[0]}
                AND
                DATE_FORMAT(a.verify_date,'%Y-%m-%d') &lt;= #{dateFilter[1]}
                ) AND
            </if>

            <if test="arriveTime != null  and arriveTime != '' ">
                a.arrive_time = #{arriveTime} AND
            </if>
            <if test="status != null">
                a.status = #{status} AND
            </if>
            <if test="arriveStatus != null">
                a.arrive_status = #{arriveStatus} AND
            </if>
            <if test="applyName != null  and applyName != '' ">
                a.apply_name = #{applyName} AND
            </if>
            <if test="appyAccount != null  and appyAccount != '' ">
                a.appy_account = #{appyAccount} AND
            </if>
            <if test="verifyDate != null">
                a.verify_date = #{verifyDate}
            </if>
            <if test="remark != null  and remark != '' ">
                a.remark = #{remark} AND
            </if>

            <if test="boss != null  and boss != '' ">
                ai.boss LIKE CONCAT('%', #{boss},'%') AND
            </if>

            <if test="party != null  and party != '' ">
                ai.party LIKE CONCAT('%', #{party},'%') AND
            </if>
        </trim>
    </sql>
</mapper>
