<?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.refundsManagement.RefundManagementMapper">

    <resultMap type="com.sunxung.factoring.entity.project.vo.BusinessProcessingTaskVo" id="baseResultMap">
        <result property="id" column="id"/>
        <result property="businessNumber" column="business_number"/>
        <result property="refundNumber" column="refund_number"/>
        <result property="projectName" column="project_name"/>
        <result property="customerName" column="customer_name"/>
        <result property="contractNumber" column="contract_no"/>
        <result property="customerName" column="customer_name"/>
        <result property="supplierName" column="supplier_name"/>
        <result property="platformName" column="platform_name"/>
        <result property="taskName" column="stage" />
        <result property="quotaDisburseBatch" column="quota_disburse_batch"/>
        <result property="gmtModified" column="gmt_modified"/>
        <association property="status" column="status" resultMap="dictionaryResult"/>
        <association property="assignee" column="manager_id" resultMap="userResult"/>
    </resultMap>

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

    <resultMap id="userResult" type="User">
        <result property="id" column="uid"/>
        <result property="name" column="uname"/>
    </resultMap>

    <!-- 通用查询结果列 -->
    <sql id="Base_Column_List">
        id
        , ledger_fund_goods_assign_id, business_number, contract_no, quota_disburse_batch, trading_platform_id, refund_status, user_create, user_modified, gmt_create, gmt_modified
    </sql>
    <select id="findBySearch" resultMap="baseResultMap">
        SELECT
        t.id,
        t.business_number,
        t.refund_number,
        t.contract_no,
        t.quota_disburse_batch,
        t.refund_status status,
        t.stage,
        t5.platform_name,
        t1.project_name,
        t3.corporate_name supplier_name,
        t7.corporate_name customer_name,
        t6.NAME dname,
        t6.CODE dcode,
        t6.id did,
        t.gmt_create,
        t.gmt_modified
        FROM refund_management t
        LEFT JOIN c_project_information t1 ON t1.business_number = t.business_number
        LEFT JOIN c_supplier_info t2 ON t1.c_supplier_info_id = t2.id
        LEFT JOIN c_enterprise t3 ON t3.id = t2.c_enterprise_id
        LEFT JOIN c_project_core_enterprise t4 ON t4.c_project_information_id = t1.id
        LEFT JOIN c_enterprise t7 ON t7.id = t4.c_enterprise_id
        LEFT JOIN trading_platform t5 ON t5.id = t.trading_platform_id
        LEFT JOIN t_dictionary t6 ON t6.`code` = t.refund_status
        LEFT JOIN c_sys_user t8 on t8.id = t2.sys_user_id
        <where>
            <if test="null != userIds and userIds.size()>0">
                AND t8.project_manager IN
                <foreach collection="userIds" item="item" index="index" open="(" separator="," close=")">
                    #{item}
                </foreach>
            </if>
            <if test="riskManagerId != null ">
                AND t8.risk_manager=#{riskManagerId}
            </if>
            <if test="status != null and status !='' ">
                AND t.refund_status=#{status}
            </if>
            <if test="fastSearch !=null and fastSearch !=''">
                AND (
                t1.project_name LIKE CONCAT( '%',#{fastSearch},'%' )
                OR t3.corporate_name LIKE CONCAT( '%',#{fastSearch},'%' )
                OR t7.corporate_name LIKE CONCAT( '%',#{fastSearch},'%' )
                )
            </if>
            <if test="searchNumber !=null and searchNumber !=''">
                AND (
                t.contract_no LIKE CONCAT( '%',#{searchNumber},'%' )
                OR t.business_number LIKE CONCAT( '%',#{searchNumber},'%' )
                )
            </if>
        </where>
        ORDER BY
        t.gmt_modified DESC
    </select>
    <select id="getById" resultType="com.sunxung.factoring.entity.refundsManagement.RefundManagement">
        SELECT t.id,
               t.business_number,
               t.refund_number,
               t.contract_no contract_number,
               t.quota_disburse_batch,
               t.refund_status,
               t5.platform_name trading_platform_name,
               t1.project_name,
               t3.corporate_name supplier_name,
               t7.corporate_name customer_name,
               t6.id             did,
               t.gmt_create,
               t.gmt_modified
        FROM refund_management t
                 LEFT JOIN c_project_information t1 ON t1.business_number = t.business_number
                 LEFT JOIN c_supplier_info t2 ON t1.c_supplier_info_id = t2.id
                 LEFT JOIN c_enterprise t3 ON t3.id = t2.c_enterprise_id
                 LEFT JOIN c_project_core_enterprise t4 ON t4.c_project_information_id = t1.id
                 LEFT JOIN c_enterprise t7 ON t7.id = t4.c_enterprise_id
                 LEFT JOIN trading_platform t5 ON t5.id = t.trading_platform_id
                 LEFT JOIN t_dictionary t6 ON t6.`code` = t.refund_status
                 LEFT JOIN c_sys_user t8 on t8.id = t2.sys_user_id
        where t.id = #{value}
    </select>
    <select id="listByRefundId" resultType="com.sunxung.factoring.entity.refundsManagement.vo.LedgerDetailsVo">
        SELECT tt.ledger_account_basic_info_id,
               tt.ledger_number,
               tt.ledger_date,
               tt.label,
               tt.amount_type,
               tt.amount,
               b.selected,
               tt.ledger_fund_goods_assign_id
        FROM (
                 SELECT DISTINCT t3.ledger_number,
                                 t3.ledger_date,
                                 t.contract_no,
                                 t.quota_disburse_batch,
                                 t2.cash_type amount_type,
                                 t.id         ledger_fund_goods_assign_id,
                                 t3.id        ledger_account_basic_info_id,
                                 t3.label,
                                 amount
                 FROM ledger_fund_goods_assign t,
                      cash_flow_manage_basic_info t2,
                      ledger_account_basic_info t3
                 WHERE t.id = t2.ledger_fund_goods_assign_id
                   AND t.ledger_account_basic_info_id = t3.id
                   AND t2.cash_type IN ("outType_discount","performanceBond","retentionMoney")
             ) tt
                 LEFT JOIN refund_management a
                           ON a.contract_no = tt.contract_no AND a.quota_disburse_batch = tt.quota_disburse_batch
                 INNER JOIN refund_details b
                            ON a.id = b.refund_management_id AND tt.amount_type = b.amount_type AND b.STATUS = 2
        WHERE a.id = #{value}
        UNION ALL
        (
            SELECT tt.ledger_account_basic_info_id,
                   tt.ledger_number,
                   tt.ledger_date,
                   tt.label,
                   tt.amount_type,
                   tt.amount,
                   0 selected,
                   tt.ledger_fund_goods_assign_id
            FROM (
                     SELECT DISTINCT t3.ledger_number,
                                     t3.ledger_date,
                                     t.contract_no,
                                     t.quota_disburse_batch,
                                     t2.cash_type amount_type,
                                     t.id         ledger_fund_goods_assign_id,
                                     t3.id        ledger_account_basic_info_id,
                                     t3.label,
                                     amount
                     FROM ledger_fund_goods_assign t,
                          cash_flow_manage_basic_info t2,
                          ledger_account_basic_info t3
                     WHERE t.id = t2.ledger_fund_goods_assign_id
                       AND t.ledger_account_basic_info_id = t3.id
                       AND t2.cash_type IN ("outType_discount","performanceBond","retentionMoney")
                 ) tt
                     LEFT JOIN refund_management a ON a.contract_no = tt.contract_no
                AND a.quota_disburse_batch = tt.quota_disburse_batch
            WHERE a.id = #{value}
              AND tt.amount_type NOT IN (SELECT mm.amount_type FROM refund_details mm)
        )
    </select>

</mapper>
