<?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.ContractMapper">
    <select id="getPage" resultType="com.fjhx.sale.entity.contract.vo.ContractVo">
        SELECT *
        FROM
             (SELECT t1.*,
                     IFNULL(t1.amount, 0) * IFNULL(t1.rate, 0) AS amountCNY,
                     CASE
                         WHEN ( IFNULL( t1.amount, 0 ) * IFNULL( t1.rate, 0 ) - t1.sumClaimMoney ) &lt;=  0 THEN
                             20
                         WHEN t1.sumClaimMoney = 0 THEN
                             0
                         ELSE 10
                         END                                   AS refundStatusNew
              FROM (SELECT t1.*,
                           (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
                    FROM contract t1) t1) t1
            ${ew.customSqlSegment}
    </select>

    <select id="getProfitClearingPage" resultType="com.fjhx.sale.entity.contract.vo.ContractBudgetVo">
        SELECT t1.id AS contractId,
               t1.*
        FROM contract t1
            ${ew.customSqlSegment}
    </select>

    <select id="getProfitBudgetPage" resultType="com.fjhx.sale.entity.contract.vo.ContractBudgetVo">
        SELECT
            (select GROUP_CONCAT(id) from contract_product WHERE contract_id = t1.id) AS contractProductIds,
            t1.id                                                                     AS contractId,
            t1.`code`,
            t1.buy_corporation_id ,
            t1.user_name,
            t1.amount,
            t1.currency,
            t1.currency_rate_json,
            t1.rate AS rate,
            t2.*,
            IFNULL(t1.budget_money,0)AS budgetMoney
        FROM
            contract t1
        LEFT JOIN contract_budget t2 ON t1.id = t2.contract_id
        ${ew.customSqlSegment}
    </select>

    <select id="getNoPackContractByCustomerId" resultType="com.fjhx.sale.entity.contract.vo.ContractVo">
        SELECT
            *
        FROM
            (
                SELECT
                    t1.*,
                    t2.quantity AS cpQuantity,
                    ( SELECT IFNULL( SUM( quantity ), 0 ) FROM pack_detail_product WHERE contract_product_id = t2.id ) AS sumPackQuantity
                FROM
                    contract t1
                LEFT JOIN contract_product t2 ON t1.id = t2.contract_id
        <where>
            <if test="customerId neq null and customerId neq '' ">
                buy_corporation_id = #{customerId}
            </if>
        </where>
        ) t1
        WHERE t1.sumPackQuantity &lt; t1.cpQuantity
        AND t1.`status`=30
        GROUP BY t1.id
    </select>

    <select id="getSalesTotal" resultType="com.fjhx.sale.entity.contract.vo.ContractVo">
        SELECT
            SUM(amount) amount,
            currency,
            count(*) contractCount
        FROM
            contract
        WHERE
            buy_corporation_id = #{id} and status = 30 and del_flag  = 0
        GROUP BY
            buy_corporation_id
    </select>

    <select id="getAmount" resultType="com.fjhx.sale.entity.contract.vo.ContractVo">
        SELECT
            SUM(amount) amount,
            DATE_FORMAT(create_time,'%Y-%m') time
        FROM
            contract
        WHERE
              buy_corporation_id = #{id}
                and (DATE_FORMAT(create_time,'%Y-%m') >= #{beginTime} and #{endTime} >= DATE_FORMAT(create_time,'%Y-%m'))
        GROUP BY
            TIME
    </select>

    <select id="contractStatistics" resultType="com.fjhx.sale.entity.sale.vo.SaleQuotationVo">
        SELECT
            COUNT(*) count,
            ifnull(c.`status`,-1) status,
            ifnull(DATE_FORMAT(sq.create_time,'%Y-%m'),-1) time
        FROM
            bytesailing_customer.customer c
              left  JOIN  contract sq ON sq.buy_corporation_id = c.id
            ${ew.customSqlSegment}
    </select>
    <select id="countrySalesStatistics" resultType="com.fjhx.sale.entity.contract.dto.ContractDto">
        SELECT
            sum(cc.amount) amount,
            COUNT(cc.buy_country_id) count,
            ai.name countryName

        FROM
            contract cc
                LEFT JOIN bytesailing_base.customize_area ai ON cc.buy_country_id = ai.id
            ${ew.customSqlSegment}
    </select>

    <select id="customSalesStatistics" resultType="com.fjhx.sale.entity.contract.dto.ContractDto">
        SELECT
            sum(cc.amount) amount,
            bc.name corporationName
        FROM
            contract cc
                LEFT JOIN bytesailing_customer.customer bc ON cc.buy_corporation_id = bc.id
            ${ew.customSqlSegment}
    </select>

    <select id="salesmanSalesStatistics" resultType="com.fjhx.sale.entity.contract.dto.ContractDto">
        SELECT
            SUM(cc.amount) amount,
            su.nick_name userName
        FROM
            contract cc
                LEFT JOIN bytesailing_base.sys_user su ON cc.create_user = su.user_id
            ${ew.customSqlSegment}
    </select>

    <select id="productSalesStatistics" resultType="com.fjhx.sale.entity.contract.dto.ContractDto">
        SELECT
            SUM(cp.amount) amount,
            pi.name productName
        FROM
            contract_product cp
                LEFT JOIN bytesailing_item.product_info pi ON cp.product_id = pi.id
            ${ew.customSqlSegment}
    </select>
    <select id="salesVolumeStatistics" resultType="com.fjhx.sale.entity.contract.dto.ContractDto">

        SELECT SUM(cp.quantity) quantity,
               pi.name          productName
        FROM contract_product cp
                 LEFT JOIN bytesailing_item.product_info pi ON cp.product_id = pi.id
            ${ew.customSqlSegment}
    </select>
    <select id="getAccountRunningWaterByContractId"
            resultType="com.fjhx.account.entity.account.vo.AccountRunningWaterVo">
        SELECT arw.id,
               arw.create_time,
               ap.type,
               arw.currency,
               arw.amount,
               arw.remarks,
               am.`name`          accountManagementName,
               am.account_opening accountManagementOpening,
               arw.`name`
        FROM contract c
                 JOIN bytesailing_purchase.purchase p ON p.data_resource_id = c.id
                 JOIN bytesailing_purchase.pay_detail pad
        ON pad.purchase_id = p.id
            JOIN bytesailing_purchase.pay pa ON pad.pay_id = pa.id
            JOIN bytesailing_account.account_payment ap ON ap.business_id = pa.id
            JOIN bytesailing_account.account_running_water arw ON arw.business_id = ap.id
            LEFT JOIN bytesailing_account.account_management am ON arw.account_management_id = am.id
        WHERE
            c.id = #{contractId}
        UNION ALL
        SELECT arw.id,
               arw.create_time,
               ap.type,
               arw.currency,
               arw.amount,
               arw.remarks,
               am.`name`          accountManagementName,
               am.account_opening accountManagementOpening,
               arw.`name`
        FROM contract c
                 JOIN bytesailing_account.account_request_funds_detail arfd ON arfd.contract_id = c.id
                 JOIN bytesailing_account.account_payment ap ON ap.business_id = arfd.account_request_funds_id
                 JOIN bytesailing_account.account_running_water arw ON arw.business_id = ap.id
                 LEFT JOIN bytesailing_account.account_management am ON arw.account_management_id = am.id
        WHERE c.id = #{contractId}
        UNION ALL
        SELECT arw.id,
               arw.create_time,
               30                 type,
               arw.currency,
               clc.money,
               arw.remarks,
               am.`name`          accountManagementName,
               am.account_opening accountManagementOpening,
               arw.`name`
        FROM bytesailing_account.account_running_water arw
                 JOIN claim cl ON cl.business_id = arw.id
                 JOIN claim_contract clc ON clc.claim_id = cl.id
                 JOIN contract c ON clc.contract_id = c.id
                 LEFT JOIN bytesailing_account.account_management am ON arw.account_management_id = am.id
        WHERE c.id = #{contractId}
    </select>
    <select id="getContractAndSamplePagez" resultType="com.fjhx.sale.entity.contract.vo.ContractVo">
        SELECT
        *
        FROM
        (
        SELECT
        t1.id,
        t1.sell_corporation_id,
        t1.contract_type,
        1 AS dataType,
        t1.`code`,
        t1.buy_corporation_id,
        t1.version,
        t1.amount,
        t1.create_user,
        t1.create_time,
        t1.refund_status,
        t1.`status`,
        (
        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
        FROM
        contract t1
        WHERE
        t1.`status` >= 30
        AND t1.`status` &lt; 88 UNION
        SELECT
        t1.id,
        t1.sell_corporation_id,
        3 AS contract_type,
        2 AS dataType,
        t1.`code`,
        t1.buy_corporation_id,
        t1.versions AS version,
        t1.amount,
        t1.create_user,
        t1.create_time,
        IF
        ( sumClaimMoney >= t1.amount, 20, IF ( sumClaimMoney != 0, 10, 0 ) ) AS refund_status,
        t1.`status`,
        t1.sumClaimMoney
        FROM
        (
        SELECT
        t1.*,
        (
        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
        FROM
        sample t1
        WHERE
        t1.`status` >= 30
        AND t1.`status` &lt; 88
        ) AS t1
        ) t1
        ${ew.customSqlSegment}
    </select>

</mapper>
