<?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.supplier.mapper.HomePageMapper">
    <!-- 查询首页报价单数量 -->
    <select id="getHeadData" resultType="HomePageIndex" parameterType="HomePageIndex">
        SELECT
            (SELECT COUNT(*) FROM bus_supplier_release WHERE DATEDIFF(day, jdrq, SYSDATETIME()) = 0 AND code = #{code}) AS todayNum,
            (SELECT COUNT(*) FROM bus_supplier_release WHERE DATEDIFF(day, jdrq, SYSDATETIME()) = 1 AND code = #{code}) AS yesterdayNum,
            (SELECT COUNT(*) FROM bus_supplier_release WHERE datediff(month, jdrq, SYSDATETIME()) = 0 AND code = #{code}) AS thisMonthNum,
            (SELECT COUNT(*) FROM bus_supplier_release WHERE datediff(month, jdrq, SYSDATETIME()) = 1 AND code = #{code}) AS lastMonthNum,
            (SELECT COUNT(*) FROM bus_supplier_release WHERE datediff(year, jdrq, SYSDATETIME()) = 0 AND code = #{code}) AS thisYearNum,
            (SELECT COUNT(*) FROM bus_supplier_release WHERE datediff(year, jdrq, SYSDATETIME()) = 1 AND code = #{code}) AS lastYearNum,
            (SELECT COUNT(*) FROM bus_pay a JOIN(SELECT uuid FROM bus_supplier_release WHERE code = #{code}) b ON a.release_uuid = b.uuid WHERE DATEDIFF(day, jdrq, SYSDATETIME()) = 0) AS todayClinch,
            (SELECT COUNT(*) FROM bus_pay a JOIN(SELECT uuid FROM bus_supplier_release WHERE code = #{code}) b ON a.release_uuid = b.uuid WHERE DATEDIFF(day, jdrq, SYSDATETIME()) = 1) AS yesterdayClinch
    </select>
    <!-- 收入 -->
    <select id="getHeadIncome" resultType="HomePageIndex" parameterType="HomePageIndex">
        SELECT
            (
                SELECT SUM( a.total_fee )
                FROM
                    bus_pay a
                    JOIN bus_supplier_release b ON a.release_uuid = b.uuid
                WHERE
                    b.code = #{code}
                  AND a.time_end IS NOT NULL
                  AND DATEDIFF( MONTH, a.jdrq, SYSDATETIME ( ) ) = 0
            ) AS thisMonthTotalFee,
            (
                SELECT SUM( a.total_fee )
                FROM
                    bus_pay a
                    JOIN bus_supplier_release b ON a.release_uuid = b.uuid
                WHERE
                    b.code = #{code}
                  AND a.time_end IS NOT NULL
                  AND DATEDIFF( MONTH, a.jdrq, SYSDATETIME ( ) ) = 1
            ) AS lastMonthTotalFee,
            (
                SELECT SUM( a.total_fee )
                FROM
                    bus_pay a
                        JOIN bus_supplier_release b ON a.release_uuid = b.uuid
                WHERE
                    b.code = #{code}
                  AND a.time_end IS NOT NULL
                  AND DATEDIFF( YEAR, a.jdrq, SYSDATETIME ( ) ) = 0
            ) AS thisYearTotalFee,
            (
                SELECT SUM( a.total_fee )
                FROM
                    bus_pay a
                    JOIN bus_supplier_release b ON a.release_uuid = b.uuid
                WHERE
                    b.code = #{code}
                  AND a.time_end IS NOT NULL
                  AND DATEDIFF( YEAR, a.jdrq, SYSDATETIME ( ) ) = 1
            ) AS lastYearTotalFee,
            (
                SELECT SUM( CAST ( a.refund_fee AS DECIMAL ( 18, 2 ) ) )
                FROM
                    bus_refund a
                    JOIN bus_supplier_release b ON a.release_uuid = b.uuid
                WHERE
                    b.code = #{code}
                  AND a.refund_state = '1'
                  AND DATEDIFF( MONTH, a.jdrq, SYSDATETIME ( ) ) = 0
            ) AS thisMonthRefundFee,
            (
                SELECT SUM( CAST ( a.refund_fee AS DECIMAL ( 18, 2 ) ) )
                FROM
                    bus_refund a
                    JOIN bus_supplier_release b ON a.release_uuid = b.uuid
                WHERE
                    b.code = #{code}
                  AND a.refund_state = '1'
                  AND DATEDIFF( MONTH, a.jdrq, SYSDATETIME ( ) ) = 1
            ) AS lastMonthRefundFee,
            (
                SELECT SUM( CAST ( a.refund_fee AS DECIMAL ( 18, 2 ) ) )
                FROM
                    bus_refund a
                    JOIN bus_supplier_release b ON a.release_uuid = b.uuid
                WHERE
                    b.code = #{code}
                  AND a.refund_state = '1'
                  AND DATEDIFF( YEAR, a.jdrq, SYSDATETIME ( ) ) = 0
            ) AS thisYearRefundFee,
            (
                SELECT SUM( CAST ( a.refund_fee AS DECIMAL ( 18, 2 ) ) )
                FROM
                    bus_refund a
                    JOIN bus_supplier_release b ON a.release_uuid = b.uuid
                WHERE
                    b.code = #{code}
                  AND a.refund_state = '1'
                  AND DATEDIFF( YEAR, a.jdrq, SYSDATETIME ( ) ) = 1
            ) AS lastYearRefundFee
    </select>
    <!-- 本年报价单每月数量 -->
    <select id="getThisYearOffer" resultType="Map" parameterType="HomePageIndex">
        SELECT
            datepart( mm, jdrq )  AS thisYearMonth,COUNT ( * ) AS releaseNum
        FROM
            bus_supplier_release
        WHERE
            DATEDIFF( YEAR, jdrq, SYSDATETIME ( ) ) = 0 AND code = #{code}
        GROUP BY
            datepart( mm, jdrq )
        ORDER BY datepart( mm, jdrq )
    </select>
    <!-- 本年成交单每月数量 -->
    <select id="getThisYearClinch" resultType="Map" parameterType="HomePageIndex">
        SELECT
            datepart( mm, a.jdrq ) AS thisYearMonth,COUNT ( * ) AS releaseNum
        FROM
            bus_supplier_release a
            JOIN ( SELECT release_uuid FROM bus_pay ) b ON a.uuid = b.release_uuid
        WHERE
            DATEDIFF( YEAR, a.jdrq, SYSDATETIME ( ) ) = 0 AND a.code = #{code}
        GROUP BY
            datepart( mm, a.jdrq )
    </select>
    <!-- 查询全国热门目的地 -->
    <select id="getHeadMap" resultType="Map">
        SELECT
            destination as name,
            COUNT ( * ) AS value
        FROM
            bus_private_ordering
        WHERE
            DATEDIFF( YEAR, jdrq, SYSDATETIME ( ) ) = 0
        GROUP BY
            destination
        ORDER BY value desc
    </select>
    <!-- 查询消息提醒 -->
    <select id="getMessageRemid" resultType="Map">
        SELECT
            (
                SELECT COUNT ( * ) AS COUNT
        FROM
            bus_private_ordering AS b
            JOIN ( SELECT code, uuid FROM bus_private_ordering WHERE uuid NOT IN ( SELECT demand_uuid FROM bus_private_ordering_type WHERE code = #{code} AND order_type != 0 ) ) AS c ON b.uuid = c.uuid
            JOIN ( SELECT code, uuid FROM bus_private_ordering WHERE uuid NOT IN ( SELECT demand_uuid FROM bus_private_ordering_type WHERE order_type = 9 ) ) AS a ON b.uuid = a.uuid
            JOIN ( SELECT code, uuid FROM bus_private_ordering WHERE uuid NOT IN ( SELECT demand_uuid FROM bus_private_ordering_type WHERE order_type = 10 ) ) AS d ON b.uuid = d.uuid
        WHERE
            b.uuid NOT IN ( SELECT ordering_uuid FROM bus_ignore_supplier WHERE code = #{code} )
          AND b.supplier_code = '000'
           OR b.supplier_code = #{code}
            ) AS demandCount,
            (
        SELECT COUNT
            ( * ) AS COUNT
        FROM
            bus_private_ordering AS b
            JOIN ( SELECT code, demand_uuid FROM bus_private_ordering_type WHERE code = #{code} AND order_type = '1' ) AS c ON b.uuid = c.demand_uuid
        WHERE
            uuid NOT IN ( SELECT ordering_uuid FROM bus_ignore_supplier WHERE code = #{code} )
            ) AS alreadyOffer,
            (
        SELECT COUNT
            ( * ) AS COUNT
        FROM
            bus_private_ordering AS b
            JOIN (
            SELECT
            code,
            demand_uuid
            FROM
            bus_private_ordering_type
            WHERE
            code = #{code}
            AND ( order_type = '2' OR order_type = '3' OR order_type = '4' OR order_type = '5' OR order_type = '12' )
            ) AS c ON b.uuid = c.demand_uuid
        WHERE
            uuid NOT IN ( SELECT ordering_uuid FROM bus_ignore_supplier WHERE code = #{code} )
            ) AS hasBeenPaid,
            (
        SELECT COUNT
            ( * ) AS COUNT
        FROM
            bus_private_ordering AS b
            JOIN ( SELECT code, demand_uuid FROM bus_private_ordering_type WHERE code = #{code} AND order_type = '5' ) AS c ON b.uuid = c.demand_uuid
        WHERE
            uuid NOT IN ( SELECT ordering_uuid FROM bus_ignore_supplier WHERE code = #{code} )
            ) AS clinch,
            (
        SELECT COUNT
            ( * )
        FROM
            bus_refund a
            JOIN ( SELECT code, uuid FROM bus_supplier_release WHERE code = #{code} ) b ON a.release_uuid = b.uuid
        WHERE
            a.refund_state = '0'
            ) AS refund
    </select>
    <!-- 小程序首页信息 -->
    <select id="appletDate" resultType="Map">
        SELECT
            (
                SELECT COUNT( * ) AS COUNT
                FROM
                    bus_private_ordering AS b
                    JOIN ( SELECT code, demand_uuid FROM bus_private_ordering_type WHERE code = #{code} AND order_type = '1' ) AS c ON b.uuid = c.demand_uuid
                WHERE
                    uuid NOT IN ( SELECT ordering_uuid FROM bus_ignore_supplier WHERE code = #{code} )
            ) AS alreadyOffer,
            (SELECT COUNT(*) FROM bus_pay a JOIN(SELECT uuid FROM bus_supplier_release WHERE code = #{code}) b ON a.release_uuid = b.uuid WHERE DATEDIFF(day, jdrq, SYSDATETIME()) = 0) AS todayClinch,
            (SELECT COUNT(*) FROM bus_pay a JOIN(SELECT uuid FROM bus_supplier_release WHERE code = #{code}) b ON a.release_uuid = b.uuid WHERE DATEDIFF(day, jdrq, SYSDATETIME()) = 1) AS yesterdayClinch,
            (SELECT offer_num FROM sys_supplier WHERE code = #{code}) as offerNum,
            (SELECT balance_of FROM sys_supplier WHERE code = #{code}) as balanceOf,
            (SELECT name FROM sys_supplier WHERE code = #{code}) as name,
            (SELECT headpir FROM sys_supplier WHERE code = #{code}) as headpir,
            (SELECT id_number FROM sys_supplier WHERE code = #{code}) as idNumber
    </select>
</mapper>