<?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>

    <sql id="getListSql">
        SELECT 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="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 = #{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="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>
        </trim>
    </sql>
</mapper>
