<?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.fjhx.sale.mapper.contract.ContractDocumentaryMapper">
    <select id="getPage" resultType="com.fjhx.sale.entity.contract.po.ContractDocumentary">
        select
            *
        from ehsd_documentary cd
            ${ew.customSqlSegment}
    </select>

    <select id="getSumCount" resultType="com.fjhx.sale.entity.contract.po.ContractDocumentary">
        SELECT t1.documentaryType,
               count(1)AS count
        FROM
            (
            SELECT
            t3.documentary_type AS documentaryType
            FROM
            (
            select t1.*, CASE
            WHEN t1.purchaseAmountCNY &lt; t1.amountCNY AND t1.sumClaimMoney &lt; t1.amountCNY THEN 10
            WHEN t1.purchaseAmountCNY &lt; t1.amountCNY AND t1.sumClaimMoney >= t1.amountCNY THEN 20
            WHEN t1.purchaseAmountCNY >= t1.amountCNY AND t1.sumClaimMoney &lt; t1.amountCNY THEN 30
            WHEN t1.purchaseAmountCNY >= t1.amountCNY AND t1.sumClaimMoney >= t1.amountCNY THEN 40
            ELSE 88
            END AS orderStatus
            from (
            SELECT
            (
            SELECT
            count ( 1 )
            FROM
            ehsd_purchase p
            LEFT JOIN ehsd_documentary pd ON p.id = pd.business_id
            WHERE
            p.data_resource_id = t1.id
            AND pd.documentary_type = #{documentaryType}
            ) AS documentaryCount, t1.*, ( SELECT IFNULL(SUM (amount*rate), 0) FROM ehsd_purchase WHERE `status` = 30 AND data_resource_id = t1.id) AS purchaseAmountCNY, IFNULL( t1.amount * t1.rate, 0 ) AS amountCNY, (
            SELECT
            IFNULL( SUM ( IFNULL( cc.money, 0 ) * IFNULL( arw.rate, 1 )), 0 )
            FROM
            claim_contract cc
            LEFT JOIN claim c ON cc.claim_id = c.id
            LEFT JOIN bytesailing_account.account_running_water arw ON c.business_id = arw.id
            WHERE
            cc.contract_id = t1.id
            ) AS sumClaimMoney, cus.nick_name as salesmanName
            FROM
            contract t1
            left join bytesailing_customer.customer cu on t1.buy_corporation_id = cu.id
            left join bytesailing_base.sys_user cus on cu.user_id = cus.user_id
            WHERE
            t1.`status` = 30
            ) t1
            ) t1
            RIGHT JOIN ehsd_purchase t2 ON t1.id = t2.data_resource_id
            RIGHT JOIN ehsd_documentary t3 ON t2.id = t3.business_id
            ${ew.customSqlSegment}
            ) t1
        GROUP BY t1.documentaryType
    </select>
</mapper>
