<?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="com.sunxung.factoring.mapper.settlement.SettlementBasicInfoMapper">


    <resultMap type="SettlementBasicInfo" id="listVoResult">
        <result property="id" column="id" />
        <result property="moduleNumber" column="settlement_number" />
        <result property="businessNumber" column="business_number" />
        <result property="customerName" column="customerName" />
        <result property="businessName" column="business_name" />
<!--        <result property="businessName" column="project_name" />-->
        <result property="supplierName" column="supplierName" />
        <result property="contractId" column="contract_id"/>
        <result property="settlementDate" column="settlement_date" />
        <result property="settlementAmount" column="settlement_amount" />
        <result property="gmtModified" column="gmt_modified" />
        <result property="tradingPlatformId" column="trading_platform_id" />
        <result property="tradingPlatformName" column="platform_name" />
        <association property="status" column="settlement_status" resultMap="dictionaryResult"/>
    </resultMap>

    <resultMap type="com.sunxung.factoring.entity.project.vo.BusinessProcessingTaskVo" id="baseResultMap">
        <result property="id" column="id" />
        <result property="moduleNumber" column="settlement_number" />
        <result property="businessNumber" column="business_number" />
        <result property="customerName" column="customerName" />
        <result property="businessName" column="business_name" />
        <result property="supplierName" column="supplierName" />
        <result property="settlementDate" column="settlement_date" />
        <result property="settlementAmount" column="settlement_amount" />
        <result property="gmtModified" column="gmt_modified" />
        <result property="platformName" column="platform_name" />
        <association property="status" column="settlement_status" resultMap="dictionaryResult" />
    </resultMap>

    <resultMap id="dictionaryResult" type="Dictionary">
        <result property="id" column="did" />
        <result property="name" column="dname" />
        <result property="code" column="dcode" />
    </resultMap>

    <select id="getListPage" parameterType="java.lang.Long" resultMap="listVoResult">
        select DISTINCT
            t1.id,
            t1.settlement_number,
            t1.business_number,
            t1.contract_id,
            t4.corporate_name customerName,
            t3.project_name business_name,
            t6.corporate_name supplierName,
            t1.settlement_date,
            t1.settlement_amount,
            t1.settlement_status,
            t1.gmt_modified,
            t2.id did,
            t2.code dcode,
            t2.name dname,
            t1.trading_platform_id,
            t7.platform_name
        from c_settlement_basic_info t1
                LEFT JOIN t_dictionary t2 ON t1.settlement_status = t2.code
                LEFT JOIN c_project_information t3 ON t1.business_number = t3.business_number
                LEFT JOIN c_enterprise t4 ON t1.enterprise_customer_info_id = t4.id
                LEFT JOIN c_supplier_info t5 ON t1.supplier_id = t5.id
                LEFT JOIN c_enterprise t6 ON t5.c_enterprise_id = t6.id
            left join trading_platform t7 on t1.trading_platform_id = t7.id
        where t1.id = #{id}
        ORDER BY t1.gmt_create ASC
    </select>

    <select id="findMyCompletedList" parameterType="com.sunxung.factoring.entity.project.vo.SearchBusinessProcessingTask" resultMap="baseResultMap">
        SELECT DISTINCT
        t2.id,
        t2.settlement_number ,
        t2.business_number,
        t5.corporate_name customerName,
        t3.business_name ,
        t4.corporate_name supplierName,
        t2.settlement_date,
        t2.settlement_amount,
        t2.settlement_status ,
        t2.gmt_modified ,
        t6.id did,
        t6.code dcode,
        t6.name dname
        FROM business_processing_log t1
        LEFT JOIN settlement_basic_info t2 ON t1.busness_key = t2.id
        LEFT JOIN business_basic_info t3 ON t2.business_number = t3.business_number
        LEFT JOIN c_enterprise t4 ON t2.supplier_id = t4.id
        LEFT JOIN c_enterprise t5 ON t2.enterprise_customer_info_id = t5.id
        LEFT JOIN t_dictionary t6 ON t2.settlement_status = t6.id
        <where>
            t1.business_type = "BillBasicInformation"
            AND t1.end_time IS NOT NULL
            <if test="userId != null">
                AND t1.assignee_id = #{userId}
            </if>
            <if test="fastSearch != null">
                AND (t2.settlement_number LIKE concat('%',#{fastSearch},'%') or t3.business_number LIKE concat('%',#{fastSearch},'%') or t5.corporate_name LIKE concat('%',#{fastSearch},'%') or t3.business_name LIKE concat('%',#{fastSearch},'%') or t4.corporate_name LIKE concat('%',#{fastSearch},'%'))
            </if>
        </where>
        GROUP BY t1.busness_key,t2.settlement_date
        ORDER BY t2.gmt_modified DESC
    </select>

    <select id="findAllList" parameterType="com.sunxung.factoring.entity.project.vo.SearchBusinessProcessingTask" resultMap="baseResultMap">
        SELECT DISTINCT
        t2.id,
        t2.settlement_number ,
        t2.business_number,
        t5.corporate_name customerName,
        t3.project_name business_name ,
<!--        t4.corporate_name supplierName,-->
        t2.settlement_date,
        t2.settlement_amount,
        t2.settlement_status ,
        t2.gmt_modified ,
        t6.id did,
        t6.code dcode,
        t6.name dname,
        t8.corporate_name supplierName,
        t9.platform_name
        FROM business_processing_log t1
        LEFT JOIN c_settlement_basic_info t2 ON t1.busness_key = t2.id
        LEFT JOIN c_project_information t3 ON t2.business_number = t3.business_number
        LEFT JOIN c_enterprise t5 ON t2.enterprise_customer_info_id = t5.id
        LEFT JOIN t_dictionary t6 ON t2.settlement_status = t6.code
        LEFT JOIN c_supplier_info t7 ON t3.c_supplier_info_id = t7.id
        LEFT JOIN c_enterprise t8 ON t7.c_enterprise_id = t8.id
        left join trading_platform t9 on t2.trading_platform_id = t9.id
        LEFT JOIN c_sys_user t10 ON t10.id = t7.sys_user_id
        <where>
            t1.business_type = "BillBasicInformation"
            <if test="userIds != null and userIds.size()>0">
                AND t10.project_manager in
                <foreach collection="userIds" index="index" item="item"  open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="businessNo != null and businessNo != ''">
                AND t2.business_number = #{businessNo}
            </if>
            <if test="startDate != null">
                <![CDATA[ AND DATE_FORMAT(t2.settlement_date, '%Y-%m-%d')>=  DATE_FORMAT(#{startDate}, '%Y-%m-%d') ]]>
            </if>
            <if test="endDate != null">
                <![CDATA[ AND DATE_FORMAT(t2.settlement_date, '%Y-%m-%d')<=  DATE_FORMAT(#{endDate}, '%Y-%m-%d') ]]>
            </if>
            <if test="fastSearch != null and fastSearch!=''">
                AND (t5.corporate_name LIKE concat('%',#{fastSearch},'%') or t3.project_name LIKE concat('%',#{fastSearch},'%') or t8.corporate_name  LIKE concat('%',#{fastSearch},'%'))
            </if>
            <if test="status != null and status !=  ''">
                AND t2.settlement_status = #{status}
            </if>
        </where>
        GROUP BY t1.busness_key,t2.settlement_date
        ORDER BY t2.gmt_modified DESC
    </select>

    <select id="findAllListSupplier"
            parameterType="com.sunxung.factoring.entity.project.vo.SearchBusinessProcessingTask" resultMap="baseResultMap">
        SELECT DISTINCT
        t2.id,
        t2.settlement_number ,
        t2.business_number,
        t5.corporate_name customerName,
        t3.business_name ,
        t4.corporate_name supplierName,
        t2.settlement_date,
        t2.settlement_amount,
        t2.settlement_status ,
        t2.gmt_modified ,
        t6.id did,
        t6.code dcode,
        t6.name dname
        FROM business_processing_log t1
        LEFT JOIN settlement_basic_info t2 ON t1.busness_key = t2.id
        LEFT JOIN business_basic_info t3 ON t2.business_number = t3.business_number
        LEFT JOIN c_enterprise t4 ON t2.supplier_id = t4.id
        LEFT JOIN c_enterprise t5 ON t2.enterprise_customer_info_id = t5.id
        LEFT JOIN t_dictionary t6 ON t2.settlement_status = t6.id
        <where>
            t1.business_type = "BillBasicInformation"
            and t2.supplier_id = #{supplierId}
            <if test="startDate != null">
                <![CDATA[ AND DATE_FORMAT(t2.settlement_date, '%Y-%m-%d')>=  DATE_FORMAT(#{startDate}, '%Y-%m-%d') ]]>
            </if>
            <if test="endDate != null">
                <![CDATA[ AND DATE_FORMAT(t2.settlement_date, '%Y-%m-%d')<=  DATE_FORMAT(#{endDate}, '%Y-%m-%d') ]]>
            </if>
            <if test="fastSearch != null">
                AND (t2.settlement_number LIKE concat('%',#{fastSearch},'%') or t3.business_number LIKE concat('%',#{fastSearch},'%') or t5.corporate_name LIKE concat('%',#{fastSearch},'%') or t3.business_name LIKE concat('%',#{fastSearch},'%') or t4.corporate_name LIKE concat('%',#{fastSearch},'%'))
            </if>
            <if test="statusId != null">
                AND t2.settlement_status = #{statusId}
            </if>
        </where>
        GROUP BY t1.busness_key,t2.settlement_date
        ORDER BY t2.gmt_modified DESC
    </select>

    <select id="findRelationLedgerInfo" resultType="com.sunxung.factoring.entity.cashflowmanage.vo.BillGoodsInfoVo">
        SELECT
            t1.bill_information_number,
            t1.settlement_amount bill_amount_total,
            SUM( t3.assign_amount ) current_assign_amount,
            SUM( t3.financing_principal ) + SUM( t3.financing_interest ) principal_interest,
            SUM( t3.platform_profit ) platform_profit,
            SUM( t3.discount_amount ) discount_amount
        FROM
            bill_basic_information t1
                LEFT JOIN bill_goods_information t2 ON t1.id = t2.bill_basic_information_id
                LEFT JOIN ledger_fund_goods_assign t3 ON t2.id = t3.bill_goods_information_id
        WHERE
            t3.ledger_account_basic_info_id = #{ledgerAccountBasicInfoId}
        GROUP BY t1.bill_information_number
    </select>

</mapper>
