<?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.inkasso.factoring.settlement.dao.SettlementBasicInfoMapper">


    <resultMap type="com.inkasso.factoring.settlement.model.dto.BusinessProcessingTaskDTO" 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="platformName" column="platform_name" />
        <result property="settlementDate" column="settlement_date" />
        <result property="settlementAmount" column="settlement_amount" />
        <result property="taskId" column="task_id" />
        <result property="taskDefKey" column="task_def_key" />
        <result property="taskName" column="task_name" />
        <result property="currentStage" column="task_name" />
        <result property="businessName" column="project_name" />
        <result property="gmtModified" column="gmt_modified" />
        <association property="status" column="settlement_status" resultMap="dictionaryResult" />
    </resultMap>

    <resultMap id="dictionaryResult" type="com.inkasso.factoring.dict.entity.Dictionary">
        <result property="id" column="did" />
        <result property="name" column="dname" />
        <result property="code" column="dcode" />
    </resultMap>

    <select id="findAllList" parameterType="com.inkasso.factoring.settlement.entity.SearchBusiness" resultMap="baseResultMap">
        select * from
        (SELECT DISTINCT
        t1.historic_task_id task_id,
        if(t2.settlement_status = 'project_init_completed', "",t1.task_name) as task_name,
        t1.task_def_key,
        t2.id,
        t2.settlement_number ,
        t2.business_number,
        d.project_name,
        e.corporate_name customerName,
        t4.supplier_name supplierName,
        g.platform_name,
        t2.settlement_date,
        t2.settlement_amount,
        t2.settlement_status ,
        t2.gmt_modified ,
        t6.id did,
        t6.code dcode,
        t6.name as dname
        FROM c_settlement_basic_info t2
        LEFT JOIN business_processing_log t1 ON t1.busness_key = t2.id and (t1.business_type = "BillBasicInformation" or t1.business_type = "BillUnitPriceAdjust")
        left join c_project_information d on t2.business_number = d.business_number
        left join c_enterprise e on e.id = t2.enterprise_customer_info_id
        left join c_acceptance_basic_info f on f.trading_platform_id = e.id
        left join trading_platform g on t2.trading_platform_id = g.id
        left join (
                   select  a.id,
                           b.corporate_name supplier_name,
                          a.sys_user_id
                   from  c_supplier_info a
                left join c_enterprise b on a.c_enterprise_id = b.id ) t4 on t4.id = t2.supplier_id
        LEFT JOIN t_dictionary t6 ON t2.settlement_status = t6.code
        <where>
<!--            t1.business_type = "BillBasicInformation"-->
            <if test="userIds != null and userIds.size()>0">
                t4.sys_user_id in <foreach collection="userIds" index="index" item="item"  open="(" separator="," close=")">
                #{item}
            </foreach>
            </if>
            <if test="businessNo != null and businessNo!=''">
                AND (t2.settlement_number LIKE concat('%',#{businessNo},'%') or t2.business_number LIKE concat('%',#{businessNo},'%'))
            </if>
            <if test="customerName != null and customerName!=''">
                AND (e.corporate_name LIKE concat('%',#{customerName},'%') or d.project_name LIKE concat('%',#{customerName},'%') or t4.supplier_name LIKE concat('%',#{customerName},'%'))
            </if>
            <if test="status != null and status != ''">
                AND t2.settlement_status = #{status}
            </if>
        </where>
        ORDER BY t2.gmt_modified DESC ,t1.id DESC) t
        GROUP BY t.id,t.settlement_date
        ORDER BY
        t.gmt_modified desc
    </select>
    <select id="getListPage" resultType="com.inkasso.factoring.settlement.entity.SettlementBasicInfo">

    </select>
</mapper>
